context_grep 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f0da5e25812952e719c642974b5a7cdec355f50ce0e0d1acc954fd38cf10e93c
4
- data.tar.gz: 451808e55032fbd2f64fd138de7427c365cd140055324260e60887e687034526
3
+ metadata.gz: 86a3e25d8dad634fca9c8be38197f010b2e5f7da79fa46b10ae69c997b125c7e
4
+ data.tar.gz: ee785480697fb08af1e14136645b4ef1c5d4bedd6f97ec3ec7430cf46545356e
5
5
  SHA512:
6
- metadata.gz: 50675d9cd5800cf45270d7458493739b94a074df3b6fa76014d1d4fe8086f4d846161f32af14576130f53905d816617c2278dcdc3b8362c8a47b3659cfe9bc2d
7
- data.tar.gz: 979ced1ef8bdfb26c1fc7c5d094d549d3cd5c688aabb6b4499a2127980e30915e9af6aab12ceab1b29a0a743bcca23f63941680cac91de147b7fd3fb4cf4aa5f
6
+ metadata.gz: fc53dfb8a429f22c2f5726f897d63e63ffa1742da7f67e32d9eab56101caf043f3526fde19a2c8d598a2ee597f9771ca9b0f405441657849cb2f79c6a6942ffe
7
+ data.tar.gz: 901039c3e85e39ca4bf62e5cca9b036f6c39883bbada7a132cf6d94262d0bbef59673c46171770f83739a95bfed687f982c0a100d84229f06c5a67bdfb684e50
data/context_grep.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "context_grep"
3
- spec.version = "0.2.0"
3
+ spec.version = "0.3.0"
4
4
  spec.summary = "grep results with syntactic parent chains"
5
5
 
6
6
  spec.author = "Victor Maslov aka Nakilon"
data/exe/cogrep CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- abort "usage:\tcogrep <pattern>" unless 1 == ARGV.size
2
+ abort "usage:\tcogrep PATTERN [PATH]" unless 1 == ARGV.size || 2 == ARGV.size
3
3
  require_relative "../lib/context_grep"
4
- ContextGrep(ARGV[0]).each do |file, lines|
4
+ ContextGrep(*ARGV).each do |file, lines|
5
5
  puts file
6
6
  for lineno, line in lines
7
7
  puts "%4s %s" % [lineno, line]
data/lib/context_grep.rb CHANGED
@@ -3,8 +3,11 @@ require "pathname" # undefined method `Pathname' for TreeSitter:Module (NoMetho
3
3
  require "tree_stand"
4
4
  ::TreeStand.config.parser_path = ::File.expand_path "~/.context_grep"
5
5
 
6
- def ContextGrep pattern
7
- `#{system("rg --version >/dev/null 2>&1") ? "rg -n" : "grep -nrI"} #{pattern} . 2>/dev/null`
6
+ require "fileutils"
7
+ ZIP_FILENAME = ::File.join ::TreeStand.config.parser_path, "temp.zip"
8
+ def ContextGrep what, where = "."
9
+
10
+ `#{system("rg --version >/dev/null 2>&1") ? "rg -n" : "grep -nrI"} #{what} #{where} 2>/dev/null`
8
11
  .scan(/^([^:]+):(\d+):/).group_by(&:first).map do |file, group|
9
12
  lang = ::Linguist::FileBlob.new(file).language
10
13
  next ::STDERR.puts "unsupported grammar #{lang} at #{file}" unless ts_name = {
@@ -23,27 +26,27 @@ def ContextGrep pattern
23
26
  "Python" => "python",
24
27
  "Ruby" => "ruby",
25
28
  "Rust" => "rust",
29
+ "XML" => "xml",
26
30
  }[lang.name]
27
31
  src = ::File.read file
28
32
  stack = [
29
33
  begin
30
34
  ::TreeStand::Parser.new ts_name
31
35
  rescue ::TreeSitter::ParserNotFoundError
32
- # require "fileutils"
33
- require "open-uri"
34
- require "zip"
35
36
  ::FileUtils.mkdir_p ::TreeStand.config.parser_path
36
- zip_filename = ::File.join ::TreeStand.config.parser_path, "temp.zip"
37
- lambda do
37
+ require "etc"
38
+ require "open-uri"
38
39
  "https://github.com/Faveod/tree-sitter-parsers/releases/download/v5.0/tree-sitter-parsers-5.0-#{
39
40
  "Darwin" == ::Etc.uname[:sysname] ? "macos" : "linux"
40
41
  }-#{
41
42
  ::RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
42
43
  }.zip".tap do |url|
43
44
  ::STDERR.puts "downloading #{url} to #{::TreeStand.config.parser_path}"
44
- ::File.binwrite zip_filename, ::URI.open(url, &:read)
45
- end
46
- ::Zip::File.open(zip_filename) do |zip|
45
+ ::File.binwrite ZIP_FILENAME, ::URI.open(url, &:read)
46
+ end unless File.exist? ZIP_FILENAME
47
+ require "zip"
48
+ lambda do
49
+ ::Zip::File.open(ZIP_FILENAME) do |zip|
47
50
  for entry in zip
48
51
  next if entry.directory?
49
52
  target_path = ::File.join ::TreeStand.config.parser_path, ::File.basename(entry.name)
@@ -55,10 +58,9 @@ def ContextGrep pattern
55
58
  ::FileUtils.rm_f target_path
56
59
  end
57
60
  end
58
- end
59
- ensure
60
- ::FileUtils.rm_f zip_filename
61
- end.call
61
+ end
62
+ nil
63
+ end.call or next ::STDERR.puts "can't find grammar library for #{lang} at #{file}"
62
64
  end.parse_string(src).root_node
63
65
  ]
64
66
  nodes = {}
@@ -85,4 +87,6 @@ def ContextGrep pattern
85
87
  ]
86
88
  end.compact
87
89
 
90
+ ensure
91
+ ::FileUtils.rm_f ZIP_FILENAME
88
92
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: context_grep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir:
10
10
  - exe
11
11
  cert_chain: []
12
- date: 2026-04-02 00:00:00.000000000 Z
12
+ date: 2026-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: github-linguist