context_grep 0.0.1 → 0.1.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: 96859b0b047ac62b6a29e725518cb98c50f59e28fa0d06dfde0dd5e78a8eebfb
4
+ data.tar.gz: ba8f1bd226679c2686dec7c6f021e24ff6210df494a964068cfefa619a5b5c9c
5
5
  SHA512:
6
- metadata.gz: 17f35813e93877bfab7cd0e2ecde9bca67fdbab0d76d2ae7f4bc6d348eb88f4d007aa60e43ff264ce7111cd1b77698db4c890bbb99e8c6c5ed747dbdfa555f6d
7
- data.tar.gz: 2189d4ae24540a092a3dba04f2336cd34389e24e16d6e213b58d958b38d242dc335c3bad560cca8d4fa818ccb44a52b35ed539da2c5cd944b898fb9b8450d35f
6
+ metadata.gz: f80962f5e919f9353357db5dfbae871dc55195cb859ff54ca12f133aa4e7e2930e9717f3d8f4d0ec2893fda4e9eda41c2f05144985487b11e9564b2996d6f785
7
+ data.tar.gz: 2646c563e45f9c8996708cfc9d40bc06ecae1fe998757f7606f8b26f51a58f164d8cc8029de57fbf86c3efb6192e68ba496235cd4d2467f4851d9453e785fd76
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.1.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,35 @@ 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"
38
37
  "https://github.com/Faveod/tree-sitter-parsers/releases/download/v5.0/tree-sitter-parsers-5.0-#{
39
38
  "Darwin" == ::Etc.uname[:sysname] ? "macos" : "linux"
40
39
  }-#{
41
- RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
40
+ ::RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
42
41
  }.zip".tap do |url|
43
- puts "downloading #{url} to #{TreeStand.config.parser_path}"
44
- File.binwrite zip_filename, URI.open(url, &:read)
42
+ puts "downloading #{url} to #{::TreeStand.config.parser_path}"
43
+ ::File.binwrite zip_filename, ::URI.open(url, &:read)
45
44
  end
46
- Zip::File.open(zip_filename) do |zip|
45
+ ::Zip::File.open(zip_filename) do |zip|
47
46
  for entry in zip
48
47
  next if entry.directory?
49
- target_path = File.join TreeStand.config.parser_path, File.basename(entry.name)
50
- FileUtils.rm_f target_path
48
+ target_path = ::File.join ::TreeStand.config.parser_path, ::File.basename(entry.name)
49
+ ::FileUtils.rm_f target_path
51
50
  entry.extract target_path
52
51
  end
53
52
  end
54
- FileUtils.rm zip_filename
55
- TreeStand::Parser.new ts_name
53
+ ::FileUtils.rm zip_filename
54
+ ::TreeStand::Parser.new ts_name
56
55
  end.parse_string(src).root_node
57
56
  ]
58
57
  nodes = {}
@@ -67,11 +66,16 @@ def ContextGrep pattern
67
66
  .max_by{ |_| _.ts_node.start_byte } || fail
68
67
  [].tap do |lines|
69
68
  begin
70
- lines.push node.ts_node.start_point.row unless %i{ program body_statement }.include? node.type
69
+ if ::ENV["COGREP_DEBUGTYPES"]
70
+ s = node.ts_node.start_point.row + 1
71
+ e = node.ts_node.end_point.row + 1
72
+ puts "#{::File.basename file} #{node.type.inspect} #{s}-#{e}: #{src.lines[s - 1]}"
73
+ end
74
+ lines.push node.ts_node.start_point.row unless %i{ program body_statement translation_unit }.include? node.type
71
75
  end while node = nodes[node]
72
76
  end
73
77
  end.uniq.sort.map{ |_| [_ + 1, src.lines[_]] }
74
78
  ]
75
- end
79
+ end.compact
76
80
 
77
81
  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.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon