ruby_tree_sitter 0.20.8.2 → 0.20.8.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8fa1192182d47ed588452cc52bdc175a046ab6640de7be352b28dc1da5196d7f
4
- data.tar.gz: 5fec5601dce2ae11735046bc889a0a08f4f4cffd9bfa42f3100345b7edc9bf32
3
+ metadata.gz: f897961a83176c564dd214e3b8c893880302aa9552cac65b9f0c2e06e396ba7c
4
+ data.tar.gz: 0ba1af8010641341f95bab2bc7182faa94181da5f862a4224ef5d87c44d1eff9
5
5
  SHA512:
6
- metadata.gz: '09bd1eb015eb14bb5e0f500d23cadd50c7aa97e61296a382379ba29cec849c5393903ee79f2de98c954777462badb2e6bc514372a66cd297c6c3044f7fed6ef4'
7
- data.tar.gz: 9d0f3635d0bb438a22878e96315521c8cb396001d1093ca1f8496bd91927b7829becae12be15f9cbd9eb88ff5018fece2fec5121f808c2b3e4314cc5376a02e4
6
+ metadata.gz: a3ff40e54f3d0b87984d14217f66a16f1c61a8c59c6a41d996e1792c513b2a8a3937f44839253d994d876bcbd6c1551b6c657b1cb6b6f3a0c84d752cf226da88
7
+ data.tar.gz: fac9fc60614e9ebd14e3181a4d2519cb4da9d170ea51326585204d3b65265349b3cdf964967c57d381bf795023c7b5e839ab406ba536ee5f971228efac9d3a5e
@@ -53,48 +53,6 @@ dir_include, dir_lib =
53
53
  repo.include_and_lib_dirs
54
54
  end
55
55
 
56
- # TREESITTER_SPEC = Bundler.load_gemspec('../../../../tree_sitter.gemspec')
57
-
58
- # # def version = TREESITTER_SPEC.version.gsub(/\A(\d+\.\d+\.\d+)(\.\d+)?\z/, '\1')
59
-
60
- # def version = '0.20.8'
61
-
62
- # LINUX_PLATFORM_REGEX = /linux/
63
- # DARWIN_PLATFORM_REGEX = /darwin/
64
-
65
- # def platform = RUBY_PLATFORM
66
-
67
- # def darwin?
68
- # !!(platform =~ DARWIN_PLATFORM_REGEX)
69
- # end
70
-
71
- # def dll_ext
72
- # darwin? ? 'dylib' : 'so'
73
- # end
74
-
75
- # def staging_path
76
- # '../../stage/lib/tree_sitter'
77
- # end
78
-
79
- # def downloaded_dll_path
80
- # "tree-sitter-#{version}"
81
- # end
82
-
83
- # def add_tree_sitter_dll_to_gem
84
- # puts ">>>>>>>>>>>>> #{`pwd`}"
85
- # path = "#{downloaded_dll_path}/libtree-sitter*.#{dll_ext}"
86
- # files =
87
- # Dir.glob(path)
88
- # .map { |f| Pathname(f) }
89
- # .filter { |f| !f.symlink? && f.file? }
90
- # dll = files.first
91
- # dst = Pathname(staging_path) / "libtree-sitter.#{dll_ext}"
92
- # FileUtils.cp(dll, dst)
93
- # TREESITTER_SPEC.files << dst
94
- # end
95
-
96
- # add_tree_sitter_dll_to_gem
97
-
98
56
  # ################################## #
99
57
  # Generate Makefile #
100
58
  # ################################## #
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../lib/tree_sitter/version'
4
+
5
+ module TreeSitter
6
+ # Fetches tree-sitter sources.
7
+ class Repo
8
+ attr_reader :exe, :src, :url, :version
9
+
10
+ def initialize
11
+ @version = TREESITTER_VERSION
12
+
13
+ # `tree-sitter-@version` is the name produced by tagged releases of sources
14
+ # by git, so we use it everywhere, including when cloning from git.
15
+ @src = Pathname.pwd / "tree-sitter-#{@version}"
16
+
17
+ @url = {
18
+ git: 'https://github.com/tree-sitter/tree-sitter',
19
+ tar: "https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v#{@version}.tar.gz",
20
+ zip: "https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v#{@version}.zip"
21
+ }
22
+
23
+ @exe = {}
24
+ %i[curl git tar wget zip].each do |cmd|
25
+ @exe[cmd] = find_executable(cmd.to_s)
26
+ end
27
+ end
28
+
29
+ def compile
30
+ # We need to clean because the same folder is used over and over
31
+ # by rake-compiler-dock
32
+ sh "cd #{src} && make clean && make"
33
+ end
34
+
35
+ def exe?(name)
36
+ @exe[name]
37
+ end
38
+
39
+ def extract?
40
+ !exe.filter { |k, v| %i[tar zip].include?(k) && v }.empty?
41
+ end
42
+
43
+ def download
44
+ # TODO: should we force re-download? Maybe with a flag?
45
+ return true if Dir.exist? src
46
+
47
+ res = false
48
+ %w[git curl wget].each do |cmd|
49
+ res =
50
+ if find_executable(cmd)
51
+ send("sources_from_#{cmd}")
52
+ else
53
+ false
54
+ end
55
+ break if res
56
+ end
57
+
58
+ res
59
+ end
60
+
61
+ def include_and_lib_dirs
62
+ [[src / 'lib' / 'include'], [src.to_s]]
63
+ end
64
+
65
+ def keep_static_lib
66
+ src
67
+ .children
68
+ .filter { |f| /\.(dylib|so)/ =~ f.basename.to_s }
69
+ .each(&:unlink)
70
+ end
71
+
72
+ def sh(cmd)
73
+ return if system(cmd)
74
+
75
+ abort <<~MSG
76
+
77
+ Failed to run: #{cmd}
78
+
79
+ exiting …
80
+
81
+ MSG
82
+ end
83
+
84
+ def sources_from_curl
85
+ return false if !exe?(:curl) || !extract?
86
+
87
+ if exe?(:tar)
88
+ sh "curl -L #{url[:tar]} -o tree-sitter-v#{version}.tar.gz"
89
+ sh "tar -xf tree-sitter-v#{version}.tar.gz"
90
+ elsif exe?(:zip)
91
+ sh "curl -L #{url[:zip]} -o tree-sitter-v#{version}.zip"
92
+ sh "unzip -q tree-sitter-v#{version}.zip"
93
+ end
94
+
95
+ true
96
+ end
97
+
98
+ def sources_from_git
99
+ return false if !exe?(:git)
100
+
101
+ sh "git clone #{url[:git]} #{src}"
102
+ sh "cd #{src} && git checkout tags/v#{version}"
103
+
104
+ true
105
+ end
106
+
107
+ def sources_from_wget
108
+ return false if !exe?(:wget) || !extract?
109
+
110
+ if exe?(:tar)
111
+ sh "wget #{url[:tar]} -O tree-sitter-v#{version}.tar.gz"
112
+ sh "tar -xf tree-sitter-v#{version}.tar.gz"
113
+ elsif exe?(:zip)
114
+ sh "wget #{url[:zip]} -O tree-sitter-v#{version}.zip"
115
+ sh "unzip -q tree-sitter-v#{version}.zip"
116
+ end
117
+
118
+ true
119
+ end
120
+ end
121
+ end
@@ -142,7 +142,12 @@ module TreeSitter
142
142
  # uses named_child | field_name_for_child
143
143
  # child_by_field_name | via each_node
144
144
  # ------------------------------+----------------------
145
- def fetch(*keys)
145
+ # @param all [Boolean] If `true`, return an array of nodes for all the
146
+ # demanded keys, putting `nil` for missing ones. If `false`, return the
147
+ # same array after calling `compact`. Defaults to `false`.
148
+ #
149
+ # See {#fetch_all}.
150
+ def fetch(*keys, all: false, **_kwargs)
146
151
  dict = {}
147
152
  keys.each.with_index do |k, i|
148
153
  dict[k.to_s] = i
@@ -151,13 +156,25 @@ module TreeSitter
151
156
  res = {}
152
157
  each_field do |f, c|
153
158
  if dict.key?(f)
154
- res[dict[f]] = c
159
+ res[f] = c
155
160
  dict.delete(f)
156
161
  end
157
162
  break if dict.empty?
158
163
  end
159
164
 
160
- res.sort.map { |_, v| v }
165
+ res = keys.uniq.map { |k| res[k.to_s] }
166
+ res = res.compact if !all
167
+ res
168
+ end
169
+
170
+ # Access all named children of a node, returning `nil` for missing ones.
171
+ #
172
+ # Equivalent to `fetch(…, all: true)`.
173
+ #
174
+ # See {#fetch}.
175
+ def fetch_all(*keys, **kwargs)
176
+ kwargs[:all] = true
177
+ fetch(*keys, **kwargs)
161
178
  end
162
179
  end
163
180
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module TreeSitter
4
4
  TREESITTER_VERSION = '0.20.8'
5
- VERSION = "#{TREESITTER_VERSION}.2".freeze
5
+ VERSION = "#{TREESITTER_VERSION}.3".freeze
6
6
  end
@@ -352,4 +352,60 @@ describe 'fetch' do
352
352
  assert_equal 1, m.length
353
353
  assert_equal method, m.first
354
354
  end
355
+
356
+ it 'should return an empty array when asked for non-existent fields' do
357
+ b = @child.fetch(:a)
358
+ assert_empty b
359
+
360
+ b = @child.fetch(:b, :focus)
361
+ assert_empty b
362
+ end
363
+
364
+ it 'should return an array of `nil` values when asked for non-existent fields with `all: true`' do
365
+ b = @child.fetch(:d, all: true)
366
+ refute_empty b
367
+ assert b.all?(&:nil?)
368
+
369
+ b = @child.fetch(:e, :f, all: true)
370
+ refute_empty b
371
+ assert b.all?(&:nil?)
372
+ end
373
+
374
+ it 'should return values, even if `nil`, for all keys when `all: true`' do
375
+ method = @child.child(0)
376
+ arguments = @child.child(1)
377
+
378
+ m, a, f = @child.fetch(:method, :arguments, :fake, all: true)
379
+
380
+ assert_equal method, m
381
+ assert_equal arguments, a
382
+ assert_nil f
383
+ end
384
+ end
385
+
386
+ describe 'fetch_all' do
387
+ before do
388
+ @child = root.child(0).child(4)
389
+ end
390
+
391
+ it 'should return an array of `nil` values when asked for non-existent fields' do
392
+ b = @child.fetch_all(:d)
393
+ refute_empty b
394
+ assert b.all?(&:nil?)
395
+
396
+ b = @child.fetch_all(:e, :f)
397
+ refute_empty b
398
+ assert b.all?(&:nil?)
399
+ end
400
+
401
+ it 'should return values, even if `nil`, for all keys' do
402
+ method = @child.child(0)
403
+ arguments = @child.child(1)
404
+
405
+ m, a, f = @child.fetch_all(:method, :arguments, :fake)
406
+
407
+ assert_equal method, m
408
+ assert_equal arguments, a
409
+ assert_nil f
410
+ end
355
411
  end
data/tree_sitter.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.extensions = %(ext/tree_sitter/extconf.rb)
20
20
  spec.files = %w[LICENSE README.md tree_sitter.gemspec]
21
- spec.files += Dir.glob('ext/**/*.[ch]')
21
+ spec.files += Dir.glob('ext/**/*.{c,h,rb}')
22
22
  spec.files += Dir.glob('lib/**/*.rb')
23
23
  spec.test_files = Dir.glob('test/**/*')
24
24
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_tree_sitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.8.2
4
+ version: 0.20.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Firas al-Khalil
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-08 00:00:00.000000000 Z
11
+ date: 2023-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -136,6 +136,7 @@ files:
136
136
  - ext/tree_sitter/query_match.c
137
137
  - ext/tree_sitter/query_predicate_step.c
138
138
  - ext/tree_sitter/range.c
139
+ - ext/tree_sitter/repo.rb
139
140
  - ext/tree_sitter/symbol_type.c
140
141
  - ext/tree_sitter/tree.c
141
142
  - ext/tree_sitter/tree_cursor.c