Skip to content

Commit 1d87ce7

Browse files
authored
docs: update query usage examples
1 parent dd89ca6 commit 1d87ce7

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ for changed_range in tree.changed_ranges(new_tree):
251251
You can search for patterns in a syntax tree using a [tree query]:
252252

253253
```python
254-
query = PY_LANGUAGE.query(
254+
query = Query(
255+
PY_LANGUAGE,
255256
"""
256257
(function_definition
257258
name: (identifier) @function.def
@@ -260,14 +261,15 @@ query = PY_LANGUAGE.query(
260261
(call
261262
function: (identifier) @function.call
262263
arguments: (argument_list) @function.args)
263-
"""
264+
""",
264265
)
265266
```
266267

267268
#### Captures
268269

269270
```python
270-
captures = query.captures(tree.root_node)
271+
query_cursor = QueryCursor(query)
272+
captures = query_cursor.captures(tree.root_node)
271273
assert len(captures) == 4
272274
assert captures["function.def"][0] == function_name_node
273275
assert captures["function.block"][0] == function_body_node
@@ -278,7 +280,7 @@ assert captures["function.args"][0] == function_call_args_node
278280
#### Matches
279281

280282
```python
281-
matches = query.matches(tree.root_node)
283+
matches = query_cursor.matches(tree.root_node)
282284
assert len(matches) == 2
283285

284286
# first match
@@ -290,7 +292,7 @@ assert matches[1][1]["function.call"] == [function_call_name_node]
290292
assert matches[1][1]["function.args"] == [function_call_args_node]
291293
```
292294

293-
The difference between the two methods is that `Query.matches()` groups captures into matches,
295+
The difference between the two methods is that `QueryCursor.matches()` groups captures into matches,
294296
which is much more useful when your captures within a query relate to each other.
295297

296298
To try out and explore the code referenced in this README, check out [examples/usage.py].

examples/usage.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from tree_sitter import Language, Parser
1+
from tree_sitter import Language, Parser, Query, QueryCursor
22
import tree_sitter_python
33

44
PY_LANGUAGE = Language(tree_sitter_python.language())
@@ -140,7 +140,8 @@ def read_callable_point(_, point):
140140

141141

142142
# querying the tree
143-
query = PY_LANGUAGE.query(
143+
query = Query(
144+
PY_LANGUAGE,
144145
"""
145146
(function_definition
146147
name: (identifier) @function.def
@@ -149,19 +150,20 @@ def read_callable_point(_, point):
149150
(call
150151
function: (identifier) @function.call
151152
arguments: (argument_list) @function.args)
152-
"""
153+
""",
153154
)
154155

155156
# ...with captures
156-
captures = query.captures(tree.root_node)
157+
query_cursor = QueryCursor(query)
158+
captures = query_cursor.captures(tree.root_node)
157159
assert len(captures) == 4
158160
assert captures["function.def"][0] == function_name_node
159161
assert captures["function.block"][0] == function_body_node
160162
assert captures["function.call"][0] == function_call_name_node
161163
assert captures["function.args"][0] == function_call_args_node
162164

163165
# ...with matches
164-
matches = query.matches(tree.root_node)
166+
matches = query_cursor.matches(tree.root_node)
165167
assert len(matches) == 2
166168

167169
# first match

0 commit comments

Comments
 (0)