context_grep 0.0.0 → 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: 34d9782767ed12911342d2f7ee50ab690c50dc402dc5bb08b5818496cbaba827
4
- data.tar.gz: 0715e87343a1e3c885c3d0d690f548f82e2b1fcafcb20d9c69157b8504cacb3e
3
+ metadata.gz: 96859b0b047ac62b6a29e725518cb98c50f59e28fa0d06dfde0dd5e78a8eebfb
4
+ data.tar.gz: ba8f1bd226679c2686dec7c6f021e24ff6210df494a964068cfefa619a5b5c9c
5
5
  SHA512:
6
- metadata.gz: 2d8c5e790c89a6a72fa025005490528217ed793714b04c636409aa3961f5b080e53a91fb009b49458a27de234fb08c8d280b847a315c0833af6bebc4ec4ed3c3
7
- data.tar.gz: dcea446db6fd1ca7f65b9d5670979bace1d6eaa5ec3e8a84ef70302987756cc23ff3a75174329a855268063ca3ef8b49f04424bb4cfee5b3b8d75fdc7a0da1c3
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.0"
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,12 +1,13 @@
1
1
  require "linguist"
2
+ require "pathname" # undefined method `Pathname' for TreeSitter:Module (NoMethodError)
2
3
  require "tree_stand"
3
- TreeStand.config.parser_path = File.expand_path "~/.contextgrep"
4
+ ::TreeStand.config.parser_path = ::File.expand_path "~/.context_grep"
4
5
 
5
6
  def ContextGrep pattern
6
7
  `#{system("rg --version >/dev/null 2>&1") ? "rg -n" : "grep -nrI"} #{pattern} . 2>/dev/null`
7
8
  .scan(/^([^:]+):(\d+):/).group_by(&:first).map do |file, group|
8
- ts_name = Linguist::FileBlob.new(file).language.then do |lang|
9
- {
9
+ lang = ::Linguist::FileBlob.new(file).language
10
+ next ::STDERR.puts "unsupported grammar #{lang} at #{file}" unless ts_name = {
10
11
  "Bash" => "bash",
11
12
  "C" => "c",
12
13
  "C#" => "c_sharp",
@@ -22,36 +23,35 @@ def ContextGrep pattern
22
23
  "Python" => "python",
23
24
  "Ruby" => "ruby",
24
25
  "Rust" => "rust",
25
- }[lang.name] || abort("unknown how to pass #{lang} to TreeStand::Parser")
26
- end
27
- src = File.read file
26
+ }[lang.name]
27
+ src = ::File.read file
28
28
  stack = [
29
29
  begin
30
- TreeStand::Parser.new ts_name
31
- rescue TreeSitter::ParserNotFoundError
30
+ ::TreeStand::Parser.new ts_name
31
+ rescue ::TreeSitter::ParserNotFoundError
32
32
  # require "fileutils"
33
33
  require "open-uri"
34
34
  require "zip"
35
- FileUtils.mkdir_p TreeStand.config.parser_path
36
- 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
37
  "https://github.com/Faveod/tree-sitter-parsers/releases/download/v5.0/tree-sitter-parsers-5.0-#{
38
38
  "Darwin" == ::Etc.uname[:sysname] ? "macos" : "linux"
39
39
  }-#{
40
- RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
40
+ ::RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
41
41
  }.zip".tap do |url|
42
- puts "downloading #{url} to #{TreeStand.config.parser_path}"
43
- 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)
44
44
  end
45
- Zip::File.open(zip_filename) do |zip|
45
+ ::Zip::File.open(zip_filename) do |zip|
46
46
  for entry in zip
47
47
  next if entry.directory?
48
- target_path = File.join TreeStand.config.parser_path, File.basename(entry.name)
49
- FileUtils.rm_f target_path
48
+ target_path = ::File.join ::TreeStand.config.parser_path, ::File.basename(entry.name)
49
+ ::FileUtils.rm_f target_path
50
50
  entry.extract target_path
51
51
  end
52
52
  end
53
- FileUtils.rm zip_filename
54
- TreeStand::Parser.new ts_name
53
+ ::FileUtils.rm zip_filename
54
+ ::TreeStand::Parser.new ts_name
55
55
  end.parse_string(src).root_node
56
56
  ]
57
57
  nodes = {}
@@ -66,11 +66,16 @@ def ContextGrep pattern
66
66
  .max_by{ |_| _.ts_node.start_byte } || fail
67
67
  [].tap do |lines|
68
68
  begin
69
- 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
70
75
  end while node = nodes[node]
71
76
  end
72
77
  end.uniq.sort.map{ |_| [_ + 1, src.lines[_]] }
73
78
  ]
74
- end
79
+ end.compact
75
80
 
76
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.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon