context_grep 0.2.0 → 0.4.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: f0da5e25812952e719c642974b5a7cdec355f50ce0e0d1acc954fd38cf10e93c
4
- data.tar.gz: 451808e55032fbd2f64fd138de7427c365cd140055324260e60887e687034526
3
+ metadata.gz: 17b7ea8cd656122a5dc3579b326995903dacab636b8f2d2604209e0db596c365
4
+ data.tar.gz: 3145d19a41d87394dfad101e43c9cf6c22c1ebd2e360f6af4755772da40eceb4
5
5
  SHA512:
6
- metadata.gz: 50675d9cd5800cf45270d7458493739b94a074df3b6fa76014d1d4fe8086f4d846161f32af14576130f53905d816617c2278dcdc3b8362c8a47b3659cfe9bc2d
7
- data.tar.gz: 979ced1ef8bdfb26c1fc7c5d094d549d3cd5c688aabb6b4499a2127980e30915e9af6aab12ceab1b29a0a743bcca23f63941680cac91de147b7fd3fb4cf4aa5f
6
+ metadata.gz: c2e46c65ba0811f6717eae5579930925168b4198d95c606baf171dc2c05afc1748fe1a5df30317a9c1b57a213f0c943549d5f438a88fe0cc06c86b682d422514
7
+ data.tar.gz: 9ca3c5de2a2b7dd585182b48371d037603a5b3af15b472c020047667dcd8a7c27c715450c56bbe041a6850d5743328a1f21503cc7945839c802ce4414f608ed6
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.2.0"
3
+ spec.version = "0.4.0"
4
4
  spec.summary = "grep results with syntactic parent chains"
5
5
 
6
6
  spec.author = "Victor Maslov aka Nakilon"
data/exe/cogrep CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- abort "usage:\tcogrep <pattern>" unless 1 == ARGV.size
2
+ abort "usage:\tcogrep PATTERN [PATH]" unless 1 == ARGV.size || 2 == ARGV.size
3
3
  require_relative "../lib/context_grep"
4
- ContextGrep(ARGV[0]).each do |file, lines|
4
+ ContextGrep(*ARGV).each do |file, lines|
5
5
  puts file
6
6
  for lineno, line in lines
7
7
  puts "%4s %s" % [lineno, line]
data/lib/context_grep.rb CHANGED
@@ -3,8 +3,12 @@ require "pathname" # undefined method `Pathname' for TreeSitter:Module (NoMetho
3
3
  require "tree_stand"
4
4
  ::TreeStand.config.parser_path = ::File.expand_path "~/.context_grep"
5
5
 
6
- def ContextGrep pattern
7
- `#{system("rg --version >/dev/null 2>&1") ? "rg -n" : "grep -nrI"} #{pattern} . 2>/dev/null`
6
+ require "shellwords"
7
+ require "fileutils"
8
+ ZIP_FILENAME = ::File.join ::TreeStand.config.parser_path, "temp.zip"
9
+ def ContextGrep what, where = "."
10
+
11
+ `#{system("rg --version >/dev/null 2>&1") ? "rg -n" : "grep -nrI"} #{::Shellwords.shelljoin [what, where]} 2>/dev/null`
8
12
  .scan(/^([^:]+):(\d+):/).group_by(&:first).map do |file, group|
9
13
  lang = ::Linguist::FileBlob.new(file).language
10
14
  next ::STDERR.puts "unsupported grammar #{lang} at #{file}" unless ts_name = {
@@ -23,27 +27,27 @@ def ContextGrep pattern
23
27
  "Python" => "python",
24
28
  "Ruby" => "ruby",
25
29
  "Rust" => "rust",
30
+ "XML" => "xml",
26
31
  }[lang.name]
27
32
  src = ::File.read file
28
33
  stack = [
29
34
  begin
30
35
  ::TreeStand::Parser.new ts_name
31
36
  rescue ::TreeSitter::ParserNotFoundError
32
- # require "fileutils"
33
- require "open-uri"
34
- require "zip"
35
37
  ::FileUtils.mkdir_p ::TreeStand.config.parser_path
36
- zip_filename = ::File.join ::TreeStand.config.parser_path, "temp.zip"
37
- lambda do
38
+ require "etc"
39
+ require "open-uri"
38
40
  "https://github.com/Faveod/tree-sitter-parsers/releases/download/v5.0/tree-sitter-parsers-5.0-#{
39
41
  "Darwin" == ::Etc.uname[:sysname] ? "macos" : "linux"
40
42
  }-#{
41
43
  ::RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
42
44
  }.zip".tap do |url|
43
45
  ::STDERR.puts "downloading #{url} to #{::TreeStand.config.parser_path}"
44
- ::File.binwrite zip_filename, ::URI.open(url, &:read)
45
- end
46
- ::Zip::File.open(zip_filename) do |zip|
46
+ ::File.binwrite ZIP_FILENAME, ::URI.open(url, &:read)
47
+ end unless File.exist? ZIP_FILENAME
48
+ require "zip"
49
+ lambda do
50
+ ::Zip::File.open(ZIP_FILENAME) do |zip|
47
51
  for entry in zip
48
52
  next if entry.directory?
49
53
  target_path = ::File.join ::TreeStand.config.parser_path, ::File.basename(entry.name)
@@ -55,10 +59,9 @@ def ContextGrep pattern
55
59
  ::FileUtils.rm_f target_path
56
60
  end
57
61
  end
58
- end
59
- ensure
60
- ::FileUtils.rm_f zip_filename
61
- end.call
62
+ end
63
+ nil
64
+ end.call or next ::STDERR.puts "can't find grammar library for #{lang} at #{file}"
62
65
  end.parse_string(src).root_node
63
66
  ]
64
67
  nodes = {}
@@ -85,4 +88,6 @@ def ContextGrep pattern
85
88
  ]
86
89
  end.compact
87
90
 
91
+ ensure
92
+ ::FileUtils.rm_f ZIP_FILENAME
88
93
  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.2.0
4
+ version: 0.4.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-04-02 00:00:00.000000000 Z
12
+ date: 2026-06-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: github-linguist