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 +4 -4
- data/context_grep.gemspec +1 -1
- data/lib/context_grep.rb +24 -20
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 96859b0b047ac62b6a29e725518cb98c50f59e28fa0d06dfde0dd5e78a8eebfb
|
|
4
|
+
data.tar.gz: ba8f1bd226679c2686dec7c6f021e24ff6210df494a964068cfefa619a5b5c9c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f80962f5e919f9353357db5dfbae871dc55195cb859ff54ca12f133aa4e7e2930e9717f3d8f4d0ec2893fda4e9eda41c2f05144985487b11e9564b2996d6f785
|
|
7
|
+
data.tar.gz: 2646c563e45f9c8996708cfc9d40bc06ecae1fe998757f7606f8b26f51a58f164d8cc8029de57fbf86c3efb6192e68ba496235cd4d2467f4851d9453e785fd76
|
data/context_grep.gemspec
CHANGED
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
|
-
|
|
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
|
-
|
|
27
|
-
|
|
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
|
-
|
|
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
|