context_grep 0.1.0 → 0.3.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: 96859b0b047ac62b6a29e725518cb98c50f59e28fa0d06dfde0dd5e78a8eebfb
4
- data.tar.gz: ba8f1bd226679c2686dec7c6f021e24ff6210df494a964068cfefa619a5b5c9c
3
+ metadata.gz: 86a3e25d8dad634fca9c8be38197f010b2e5f7da79fa46b10ae69c997b125c7e
4
+ data.tar.gz: ee785480697fb08af1e14136645b4ef1c5d4bedd6f97ec3ec7430cf46545356e
5
5
  SHA512:
6
- metadata.gz: f80962f5e919f9353357db5dfbae871dc55195cb859ff54ca12f133aa4e7e2930e9717f3d8f4d0ec2893fda4e9eda41c2f05144985487b11e9564b2996d6f785
7
- data.tar.gz: 2646c563e45f9c8996708cfc9d40bc06ecae1fe998757f7606f8b26f51a58f164d8cc8029de57fbf86c3efb6192e68ba496235cd4d2467f4851d9453e785fd76
6
+ metadata.gz: fc53dfb8a429f22c2f5726f897d63e63ffa1742da7f67e32d9eab56101caf043f3526fde19a2c8d598a2ee597f9771ca9b0f405441657849cb2f79c6a6942ffe
7
+ data.tar.gz: 901039c3e85e39ca4bf62e5cca9b036f6c39883bbada7a132cf6d94262d0bbef59673c46171770f83739a95bfed687f982c0a100d84229f06c5a67bdfb684e50
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.1.0"
3
+ spec.version = "0.3.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,11 @@ 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 "fileutils"
7
+ ZIP_FILENAME = ::File.join ::TreeStand.config.parser_path, "temp.zip"
8
+ def ContextGrep what, where = "."
9
+
10
+ `#{system("rg --version >/dev/null 2>&1") ? "rg -n" : "grep -nrI"} #{what} #{where} 2>/dev/null`
8
11
  .scan(/^([^:]+):(\d+):/).group_by(&:first).map do |file, group|
9
12
  lang = ::Linguist::FileBlob.new(file).language
10
13
  next ::STDERR.puts "unsupported grammar #{lang} at #{file}" unless ts_name = {
@@ -23,35 +26,41 @@ def ContextGrep pattern
23
26
  "Python" => "python",
24
27
  "Ruby" => "ruby",
25
28
  "Rust" => "rust",
29
+ "XML" => "xml",
26
30
  }[lang.name]
27
31
  src = ::File.read file
28
32
  stack = [
29
33
  begin
30
34
  ::TreeStand::Parser.new ts_name
31
35
  rescue ::TreeSitter::ParserNotFoundError
32
- # require "fileutils"
33
- require "open-uri"
34
- require "zip"
35
36
  ::FileUtils.mkdir_p ::TreeStand.config.parser_path
36
- zip_filename = ::File.join ::TreeStand.config.parser_path, "temp.zip"
37
+ require "etc"
38
+ require "open-uri"
37
39
  "https://github.com/Faveod/tree-sitter-parsers/releases/download/v5.0/tree-sitter-parsers-5.0-#{
38
40
  "Darwin" == ::Etc.uname[:sysname] ? "macos" : "linux"
39
41
  }-#{
40
42
  ::RbConfig::CONFIG["host_cpu"] =~ /x86_64|amd64/ ? "x64" : "arm64"
41
43
  }.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|
44
+ ::STDERR.puts "downloading #{url} to #{::TreeStand.config.parser_path}"
45
+ ::File.binwrite ZIP_FILENAME, ::URI.open(url, &:read)
46
+ end unless File.exist? ZIP_FILENAME
47
+ require "zip"
48
+ lambda do
49
+ ::Zip::File.open(ZIP_FILENAME) do |zip|
46
50
  for entry in zip
47
51
  next if entry.directory?
48
52
  target_path = ::File.join ::TreeStand.config.parser_path, ::File.basename(entry.name)
49
- ::FileUtils.rm_f target_path
53
+ next if ::File.exists? target_path
50
54
  entry.extract target_path
55
+ begin
56
+ return ::TreeStand::Parser.new ts_name
57
+ rescue ::TreeSitter::ParserNotFoundError
58
+ ::FileUtils.rm_f target_path
59
+ end
51
60
  end
52
- end
53
- ::FileUtils.rm zip_filename
54
- ::TreeStand::Parser.new ts_name
61
+ end
62
+ nil
63
+ end.call or next ::STDERR.puts "can't find grammar library for #{lang} at #{file}"
55
64
  end.parse_string(src).root_node
56
65
  ]
57
66
  nodes = {}
@@ -69,7 +78,7 @@ def ContextGrep pattern
69
78
  if ::ENV["COGREP_DEBUGTYPES"]
70
79
  s = node.ts_node.start_point.row + 1
71
80
  e = node.ts_node.end_point.row + 1
72
- puts "#{::File.basename file} #{node.type.inspect} #{s}-#{e}: #{src.lines[s - 1]}"
81
+ ::STDERR.puts "#{::File.basename file} #{node.type.inspect} #{s}-#{e}: #{src.lines[s - 1]}"
73
82
  end
74
83
  lines.push node.ts_node.start_point.row unless %i{ program body_statement translation_unit }.include? node.type
75
84
  end while node = nodes[node]
@@ -78,4 +87,6 @@ def ContextGrep pattern
78
87
  ]
79
88
  end.compact
80
89
 
90
+ ensure
91
+ ::FileUtils.rm_f ZIP_FILENAME
81
92
  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.1.0
4
+ version: 0.3.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-03-20 00:00:00.000000000 Z
12
+ date: 2026-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: github-linguist