tree_stand 0.1.2 → 0.1.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: c800b4009161889917105434b7f93ec8f75e678edf643cc4705e9a4e98da8aef
4
- data.tar.gz: 6313e2c9035433aae6023ffa1d49fff987ae4d13addcfe097684e4a73c2ceab5
3
+ metadata.gz: 0dec13e65fde2c739a206944e34805122f065982b6a87bf5720baca17f0fb8a5
4
+ data.tar.gz: b00f3d2b463196d0336ec45ca38294d5f41bab931f546ce5224e1f695268448b
5
5
  SHA512:
6
- metadata.gz: d4c2690b25d485b36e0e1d9718295dd7d5e941fa40cb127fc908ab8b11b2fd5bab94d0d396f0d0db6955dd9299c7750364d747246fcd514466a2b3570bff4632
7
- data.tar.gz: acc8c9ef231e3c4a05f64c33c26871a9ca7cbb41b54319ab43db2f99dd9bc3fc3c263c038315df751cbf7595528fd814340ad6e26f149eee3354586c77bd072a
6
+ metadata.gz: e00d9f5b809a264bb19672dc0a0b28b58ea37944004fd2c63ca76042eed15f1c1b9f0d539f627d0565a16492522a51cd69e30574cc540c60d66c66fc1484bb3b
7
+ data.tar.gz: b28e29fd57802ef79b0a9e88ff2fdaa9f0e79e33ef49173c56f109cdaf2737438f6bb2c23aa5a75fc6af2b4b2c96831a973e921bbcbcfd2f11c2735b525b4085
@@ -1,6 +1,7 @@
1
1
  module TreeStand
2
2
  # Wrapper around a TreeSitter capture.
3
3
  # @see TreeStand::Tree#query
4
+ # @see TreeStand::Node#query
4
5
  # @see TreeStand::Match
5
6
  class Capture
6
7
  # @return [TreeStand::Match]
@@ -1,6 +1,7 @@
1
1
  module TreeStand
2
2
  # Wrapper around a TreeSitter match.
3
3
  # @see TreeStand::Tree#query
4
+ # @see TreeStand::Node#query
4
5
  # @see TreeStand::Capture
5
6
  class Match
6
7
  # @return [TreeStand::Tree]
@@ -9,6 +10,8 @@ module TreeStand
9
10
  attr_reader :ts_query
10
11
  # @return [TreeSitter::Match]
11
12
  attr_reader :ts_match
13
+ # @return [Array<TreeStand::Capture>]
14
+ attr_reader :captures
12
15
 
13
16
  # @api private
14
17
  def initialize(tree, ts_query, ts_match)
@@ -16,9 +19,14 @@ module TreeStand
16
19
  @ts_query = ts_query
17
20
  @ts_match = ts_match
18
21
 
19
- # TODO: This is a hack to get the captures to be populated.
22
+ # It's important to load all of the captures when a Match is
23
+ # instantiated, otherwise the ts_match will be invalid after
24
+ # TreeSitter::Cursor#next_match is called.
25
+ #
20
26
  # See: https://github.com/Faveod/ruby-tree-sitter/pull/16
21
- captures
27
+ @captures = @ts_match.captures.map do |capture|
28
+ TreeStand::Capture.new(self, capture)
29
+ end
22
30
  end
23
31
 
24
32
  # Looks up a capture by name. TreeSitter strips the `@` from the capture name.
@@ -34,13 +42,6 @@ module TreeStand
34
42
  captures.find { |capture| capture.name == capture_name }
35
43
  end
36
44
 
37
- # @return [Array<TreeStand::Capture>]
38
- def captures
39
- @captures ||= @ts_match.captures.map do |capture|
40
- TreeStand::Capture.new(self, capture)
41
- end
42
- end
43
-
44
45
  # @param other [Object]
45
46
  # @return [bool]
46
47
  def ==(other)
@@ -21,6 +21,38 @@ module TreeStand
21
21
  @fields = @ts_node.each_field.to_a.map(&:first)
22
22
  end
23
23
 
24
+ # TreeSitter uses a `TreeSitter::Cursor` to iterate over matches by calling
25
+ # `curser#next_match` repeatedly until it returns `nil`.
26
+ #
27
+ # This method does all of that for you and collects all of the
28
+ # {TreeStand::Match matches} into an array.
29
+ #
30
+ # @example
31
+ # # This will return a match for each identifier nodes in the tree.
32
+ # tree_matches = tree.query(<<~QUERY)
33
+ # (identifier) @identifier
34
+ # QUERY
35
+ #
36
+ # # It is equivalent to:
37
+ # tree.root_node.query(<<~QUERY)
38
+ # (identifier) @identifier
39
+ # QUERY
40
+ #
41
+ # @see TreeStand::Match
42
+ # @see TreeStand::Capture
43
+ #
44
+ # @param query_string [String]
45
+ # @return [Array<TreeStand::Match>]
46
+ def query(query_string)
47
+ ts_query = TreeSitter::Query.new(@tree.parser.ts_language, query_string)
48
+ ts_cursor = TreeSitter::QueryCursor.exec(ts_query, ts_node)
49
+ matches = []
50
+ while match = ts_cursor.next_match
51
+ matches << TreeStand::Match.new(@tree, ts_query, match)
52
+ end
53
+ matches
54
+ end
55
+
24
56
  # @return [TreeStand::Range]
25
57
  def range
26
58
  TreeStand::Range.new(
@@ -26,6 +26,8 @@ module TreeStand
26
26
  attr_reader :document
27
27
  # @return [TreeSitter::Tree]
28
28
  attr_reader :ts_tree
29
+ # @return [TreeStand::Parser]
30
+ attr_reader :parser
29
31
 
30
32
  # @api private
31
33
  def initialize(parser, tree, document)
@@ -39,24 +41,12 @@ module TreeStand
39
41
  TreeStand::Node.new(self, @ts_tree.root_node)
40
42
  end
41
43
 
42
- # TreeSitter uses a `TreeSitter::Cursor` to iterate over matches by calling
43
- # `curser#next_match` repeatedly until it returns `nil`.
44
- #
45
- # This method does all of that for you and collects them into an array.
46
- #
47
- # @see TreeStand::Match
48
- # @see TreeStand::Capture
49
- #
50
- # @param query_string [String]
51
- # @return [Array<TreeStand::Match>]
44
+ # (see TreeStand::Node#query)
45
+ # @note This is a convenience method that calls {TreeStand::Node#query} on
46
+ # {#root_node}.
47
+ # @see TreeStand::Node#query
52
48
  def query(query_string)
53
- ts_query = TreeSitter::Query.new(@parser.ts_language, query_string)
54
- ts_cursor = TreeSitter::QueryCursor.exec(ts_query, @ts_tree.root_node)
55
- matches = []
56
- while match = ts_cursor.next_match
57
- matches << TreeStand::Match.new(self, ts_query, match)
58
- end
59
- matches
49
+ root_node.query(query_string)
60
50
  end
61
51
 
62
52
  # This method replaces the section of the document specified by range and
@@ -1,4 +1,4 @@
1
1
  module TreeStand
2
2
  # The current version of the gem.
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tree_stand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - derekstride