context_grep 0.0.1 → 0.2.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: ea3059bcd967d01416dfaa3b863e35e5769f4ba324ed8a0e2e955f5dba7af4aa
4
- data.tar.gz: 67e00462e9acea5656acbfdfaf9332a022517c46accbccc23ec73aa6dace4949
3
+ metadata.gz: f0da5e25812952e719c642974b5a7cdec355f50ce0e0d1acc954fd38cf10e93c
4
+ data.tar.gz: 451808e55032fbd2f64fd138de7427c365cd140055324260e60887e687034526
5
5
  SHA512:
6
- metadata.gz: 17f35813e93877bfab7cd0e2ecde9bca67fdbab0d76d2ae7f4bc6d348eb88f4d007aa60e43ff264ce7111cd1b77698db4c890bbb99e8c6c5ed747dbdfa555f6d
7
- data.tar.gz: 2189d4ae24540a092a3dba04f2336cd34389e24e16d6e213b58d958b38d242dc335c3bad560cca8d4fa818ccb44a52b35ed539da2c5cd944b898fb9b8450d35f
6
+ metadata.gz: 50675d9cd5800cf45270d7458493739b94a074df3b6fa76014d1d4fe8086f4d846161f32af14576130f53905d816617c2278dcdc3b8362c8a47b3659cfe9bc2d
7
+ data.tar.gz: 979ced1ef8bdfb26c1fc7c5d094d549d3cd5c688aabb6b4499a2127980e30915e9af6aab12ceab1b29a0a743bcca23f63941680cac91de147b7fd3fb4cf4aa5f
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.0.1"
3
+ spec.version = "0.2.0"
4
4
  spec.summary = "grep results with syntactic parent chains"
5
5
 
6
6
  spec.author = "Victor Maslov aka Nakilon"
data/lib/context_grep.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  require "linguist"
2
2
  require "pathname" # undefined method `Pathname' for TreeSitter:Module (NoMethodError)
3
3
  require "tree_stand"
4
- TreeStand.config.parser_path = File.expand_path "~/.context_grep"
4
+ ::TreeStand.config.parser_path = ::File.expand_path "~/.context_grep"
5
5
 
6
6
  def ContextGrep pattern
7
7
  `#{system("rg --version >/dev/null 2>&1") ? "rg -n" : "grep -nrI"} #{pattern} . 2>/dev/null`
8
8
  .scan(/^([^:]+):(\d+):/).group_by(&:first).map do |file, group|
9
- ts_name = Linguist::FileBlob.new(file).language.then do |lang|
10
- {
9
+ lang = ::Linguist::FileBlob.new(file).language
10
+ next ::STDERR.puts "unsupported grammar #{lang} at #{file}" unless ts_name = {
11
11
  "Bash" => "bash",
12
12
  "C" => "c",
13
13
  "C#" => "c_sharp",
@@ -23,36 +23,42 @@ def ContextGrep pattern
23
23
  "Python" => "python",
24
24
  "Ruby" => "ruby",
25
25
  "Rust" => "rust",
26
- }[lang.name] || abort("unknown how to pass #{lang} to TreeStand::Parser")
27
- end
28
- src = File.read file
26
+ }[lang.name]
27
+ src = ::File.read file
29
28
  stack = [
30
29
  begin
31
- TreeStand::Parser.new ts_name
32
- rescue TreeSitter::ParserNotFoundError
30
+ ::TreeStand::Parser.new ts_name
31
+ rescue ::TreeSitter::ParserNotFoundError
33
32
  # require "fileutils"
34
33
  require "open-uri"
35
34
  require "zip"
36
- FileUtils.mkdir_p TreeStand.config.parser_path
37
- zip_filename = File.join TreeStand.config.parser_path, "temp.zip"
35
+ ::FileUtils.mkdir_p ::TreeStand.config.parser_path
36
+ zip_filename = ::File.join ::TreeStand.config.parser_path, "temp.zip"
37
+ lambda do
38
38
  "https://github.com/Faveod/tree-sitter-parsers/releases/download/v5.0/tree-sitter-parsers-5.0-#{
39
39
  "Darwin" == ::Etc.uname[:sysname] ? "macos" : "linux"
40
40
  }-#{
41
- RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
41
+ ::RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
42
42
  }.zip".tap do |url|
43
- puts "downloading #{url} to #{TreeStand.config.parser_path}"
44
- File.binwrite zip_filename, URI.open(url, &:read)
43
+ ::STDERR.puts "downloading #{url} to #{::TreeStand.config.parser_path}"
44
+ ::File.binwrite zip_filename, ::URI.open(url, &:read)
45
45
  end
46
- Zip::File.open(zip_filename) do |zip|
46
+ ::Zip::File.open(zip_filename) do |zip|
47
47
  for entry in zip
48
48
  next if entry.directory?
49
- target_path = File.join TreeStand.config.parser_path, File.basename(entry.name)
50
- FileUtils.rm_f target_path
49
+ target_path = ::File.join ::TreeStand.config.parser_path, ::File.basename(entry.name)
50
+ next if ::File.exists? target_path
51
51
  entry.extract target_path
52
+ begin
53
+ return ::TreeStand::Parser.new ts_name
54
+ rescue ::TreeSitter::ParserNotFoundError
55
+ ::FileUtils.rm_f target_path
56
+ end
52
57
  end
53
58
  end
54
- FileUtils.rm zip_filename
55
- TreeStand::Parser.new ts_name
59
+ ensure
60
+ ::FileUtils.rm_f zip_filename
61
+ end.call
56
62
  end.parse_string(src).root_node
57
63
  ]
58
64
  nodes = {}
@@ -67,11 +73,16 @@ def ContextGrep pattern
67
73
  .max_by{ |_| _.ts_node.start_byte } || fail
68
74
  [].tap do |lines|
69
75
  begin
70
- lines.push node.ts_node.start_point.row unless %i{ program body_statement }.include? node.type
76
+ if ::ENV["COGREP_DEBUGTYPES"]
77
+ s = node.ts_node.start_point.row + 1
78
+ e = node.ts_node.end_point.row + 1
79
+ ::STDERR.puts "#{::File.basename file} #{node.type.inspect} #{s}-#{e}: #{src.lines[s - 1]}"
80
+ end
81
+ lines.push node.ts_node.start_point.row unless %i{ program body_statement translation_unit }.include? node.type
71
82
  end while node = nodes[node]
72
83
  end
73
84
  end.uniq.sort.map{ |_| [_ + 1, src.lines[_]] }
74
85
  ]
75
- end
86
+ end.compact
76
87
 
77
88
  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.0.1
4
+ version: 0.2.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-03-20 00:00:00.000000000 Z
12
+ date: 2026-04-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: github-linguist