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 +4 -4
- data/lib/tree_stand/capture.rb +1 -0
- data/lib/tree_stand/match.rb +10 -9
- data/lib/tree_stand/node.rb +32 -0
- data/lib/tree_stand/tree.rb +7 -17
- data/lib/tree_stand/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0dec13e65fde2c739a206944e34805122f065982b6a87bf5720baca17f0fb8a5
|
4
|
+
data.tar.gz: b00f3d2b463196d0336ec45ca38294d5f41bab931f546ce5224e1f695268448b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e00d9f5b809a264bb19672dc0a0b28b58ea37944004fd2c63ca76042eed15f1c1b9f0d539f627d0565a16492522a51cd69e30574cc540c60d66c66fc1484bb3b
|
7
|
+
data.tar.gz: b28e29fd57802ef79b0a9e88ff2fdaa9f0e79e33ef49173c56f109cdaf2737438f6bb2c23aa5a75fc6af2b4b2c96831a973e921bbcbcfd2f11c2735b525b4085
|
data/lib/tree_stand/capture.rb
CHANGED
data/lib/tree_stand/match.rb
CHANGED
@@ -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
|
-
#
|
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)
|
data/lib/tree_stand/node.rb
CHANGED
@@ -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(
|
data/lib/tree_stand/tree.rb
CHANGED
@@ -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
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
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
|
-
|
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
|
data/lib/tree_stand/version.rb
CHANGED