context_grep 0.0.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 +7 -0
- data/LICENSE +21 -0
- data/context_grep.gemspec +19 -0
- data/exe/cogrep +10 -0
- data/lib/context_grep.rb +76 -0
- metadata +91 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 34d9782767ed12911342d2f7ee50ab690c50dc402dc5bb08b5818496cbaba827
|
|
4
|
+
data.tar.gz: 0715e87343a1e3c885c3d0d690f548f82e2b1fcafcb20d9c69157b8504cacb3e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2d8c5e790c89a6a72fa025005490528217ed793714b04c636409aa3961f5b080e53a91fb009b49458a27de234fb08c8d280b847a315c0833af6bebc4ec4ed3c3
|
|
7
|
+
data.tar.gz: dcea446db6fd1ca7f65b9d5670979bace1d6eaa5ec3e8a84ef70302987756cc23ff3a75174329a855268063ca3ef8b49f04424bb4cfee5b3b8d75fdc7a0da1c3
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Victor Maslov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Gem::Specification.new do |spec|
|
|
2
|
+
spec.name = "context_grep"
|
|
3
|
+
spec.version = "0.0.0"
|
|
4
|
+
spec.summary = "grep results with syntactic parent chains"
|
|
5
|
+
|
|
6
|
+
spec.author = "Victor Maslov aka Nakilon"
|
|
7
|
+
spec.email = "nakilon@gmail.com"
|
|
8
|
+
spec.license = "MIT"
|
|
9
|
+
spec.metadata = {"source_code_uri" => "https://github.com/nakilon/context_grep"}
|
|
10
|
+
|
|
11
|
+
spec.add_dependency "github-linguist"
|
|
12
|
+
spec.add_dependency "rubyzip"
|
|
13
|
+
spec.add_dependency "ruby_tree_sitter"
|
|
14
|
+
|
|
15
|
+
spec.files = %w{ LICENSE context_grep.gemspec lib/context_grep.rb
|
|
16
|
+
exe/cogrep }
|
|
17
|
+
spec.bindir = %w{ exe }
|
|
18
|
+
spec.executables = %w{ cogrep }
|
|
19
|
+
end
|
data/exe/cogrep
ADDED
data/lib/context_grep.rb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
require "linguist"
|
|
2
|
+
require "tree_stand"
|
|
3
|
+
TreeStand.config.parser_path = File.expand_path "~/.contextgrep"
|
|
4
|
+
|
|
5
|
+
def ContextGrep pattern
|
|
6
|
+
`#{system("rg --version >/dev/null 2>&1") ? "rg -n" : "grep -nrI"} #{pattern} . 2>/dev/null`
|
|
7
|
+
.scan(/^([^:]+):(\d+):/).group_by(&:first).map do |file, group|
|
|
8
|
+
ts_name = Linguist::FileBlob.new(file).language.then do |lang|
|
|
9
|
+
{
|
|
10
|
+
"Bash" => "bash",
|
|
11
|
+
"C" => "c",
|
|
12
|
+
"C#" => "c_sharp",
|
|
13
|
+
"COBOL" => "cobol",
|
|
14
|
+
"Groovy" => "groovy",
|
|
15
|
+
"Haml" => "haml",
|
|
16
|
+
"HTML" => "html",
|
|
17
|
+
"Java" => "java",
|
|
18
|
+
"JavaScript" => "javascript",
|
|
19
|
+
"JSON" => "json",
|
|
20
|
+
"Pascal" => "pascal",
|
|
21
|
+
"PHP" => "php",
|
|
22
|
+
"Python" => "python",
|
|
23
|
+
"Ruby" => "ruby",
|
|
24
|
+
"Rust" => "rust",
|
|
25
|
+
}[lang.name] || abort("unknown how to pass #{lang} to TreeStand::Parser")
|
|
26
|
+
end
|
|
27
|
+
src = File.read file
|
|
28
|
+
stack = [
|
|
29
|
+
begin
|
|
30
|
+
TreeStand::Parser.new ts_name
|
|
31
|
+
rescue TreeSitter::ParserNotFoundError
|
|
32
|
+
# require "fileutils"
|
|
33
|
+
require "open-uri"
|
|
34
|
+
require "zip"
|
|
35
|
+
FileUtils.mkdir_p TreeStand.config.parser_path
|
|
36
|
+
zip_filename = File.join TreeStand.config.parser_path, "temp.zip"
|
|
37
|
+
"https://github.com/Faveod/tree-sitter-parsers/releases/download/v5.0/tree-sitter-parsers-5.0-#{
|
|
38
|
+
"Darwin" == ::Etc.uname[:sysname] ? "macos" : "linux"
|
|
39
|
+
}-#{
|
|
40
|
+
RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
|
|
41
|
+
}.zip".tap do |url|
|
|
42
|
+
puts "downloading #{url} to #{TreeStand.config.parser_path}"
|
|
43
|
+
File.binwrite zip_filename, URI.open(url, &:read)
|
|
44
|
+
end
|
|
45
|
+
Zip::File.open(zip_filename) do |zip|
|
|
46
|
+
for entry in zip
|
|
47
|
+
next if entry.directory?
|
|
48
|
+
target_path = File.join TreeStand.config.parser_path, File.basename(entry.name)
|
|
49
|
+
FileUtils.rm_f target_path
|
|
50
|
+
entry.extract target_path
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
FileUtils.rm zip_filename
|
|
54
|
+
TreeStand::Parser.new ts_name
|
|
55
|
+
end.parse_string(src).root_node
|
|
56
|
+
]
|
|
57
|
+
nodes = {}
|
|
58
|
+
for node in stack
|
|
59
|
+
stack.concat node.children.each{ |_| nodes[_] = node }
|
|
60
|
+
end
|
|
61
|
+
[
|
|
62
|
+
file,
|
|
63
|
+
group.flat_map do |_, grep_lineno|
|
|
64
|
+
node = nodes.keys
|
|
65
|
+
.select{ |_| _.ts_node.start_point.row <= grep_lineno.to_i - 1 && grep_lineno.to_i - 1 <= _.ts_node.end_point.row }
|
|
66
|
+
.max_by{ |_| _.ts_node.start_byte } || fail
|
|
67
|
+
[].tap do |lines|
|
|
68
|
+
begin
|
|
69
|
+
lines.push node.ts_node.start_point.row unless %i{ program body_statement }.include? node.type
|
|
70
|
+
end while node = nodes[node]
|
|
71
|
+
end
|
|
72
|
+
end.uniq.sort.map{ |_| [_ + 1, src.lines[_]] }
|
|
73
|
+
]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: context_grep
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Victor Maslov aka Nakilon
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir:
|
|
10
|
+
- exe
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2026-03-20 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: github-linguist
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - ">="
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: '0'
|
|
21
|
+
type: :runtime
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: '0'
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: rubyzip
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '0'
|
|
35
|
+
type: :runtime
|
|
36
|
+
prerelease: false
|
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
- !ruby/object:Gem::Dependency
|
|
43
|
+
name: ruby_tree_sitter
|
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
type: :runtime
|
|
50
|
+
prerelease: false
|
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
description:
|
|
57
|
+
email: nakilon@gmail.com
|
|
58
|
+
executables:
|
|
59
|
+
- cogrep
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- LICENSE
|
|
64
|
+
- context_grep.gemspec
|
|
65
|
+
- exe/cogrep
|
|
66
|
+
- lib/context_grep.rb
|
|
67
|
+
homepage:
|
|
68
|
+
licenses:
|
|
69
|
+
- MIT
|
|
70
|
+
metadata:
|
|
71
|
+
source_code_uri: https://github.com/nakilon/context_grep
|
|
72
|
+
post_install_message:
|
|
73
|
+
rdoc_options: []
|
|
74
|
+
require_paths:
|
|
75
|
+
- lib
|
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - ">="
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '0'
|
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
|
+
requirements:
|
|
83
|
+
- - ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
86
|
+
requirements: []
|
|
87
|
+
rubygems_version: 3.3.27
|
|
88
|
+
signing_key:
|
|
89
|
+
specification_version: 4
|
|
90
|
+
summary: grep results with syntactic parent chains
|
|
91
|
+
test_files: []
|