Skip to content

Commit 2d9b607

Browse files
docs: update usage example
1 parent e164972 commit 2d9b607

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

README.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -268,36 +268,30 @@ query = PY_LANGUAGE.query(
268268

269269
```python
270270
captures = query.captures(tree.root_node)
271-
assert len(captures) == 2
272-
assert captures[0][0] == function_name_node
273-
assert captures[0][1] == "function.def"
271+
assert len(captures) == 4
272+
assert captures["function.def"][0] == function_name_node
273+
assert captures["function.block"][0] == function_body_node
274+
assert captures["function.call"][0] == function_call_name_node
275+
assert captures["function.args"][0] == function_call_args_node
274276
```
275277

276-
The `Query.captures()` method takes optional `start_point`, `end_point`,
277-
`start_byte` and `end_byte` keyword arguments, which can be used to restrict the
278-
query's range. Only one of the `..._byte` or `..._point` pairs need to be given
279-
to restrict the range. If all are omitted, the entire range of the passed node
280-
is used.
281-
282278
#### Matches
283279

284280
```python
285281
matches = query.matches(tree.root_node)
286282
assert len(matches) == 2
287283

288284
# first match
289-
assert matches[0][1]["function.def"] == function_name_node
290-
assert matches[0][1]["function.block"] == function_body_node
285+
assert matches[0][1]["function.def"] == [function_name_node]
286+
assert matches[0][1]["function.block"] == [function_body_node]
291287

292288
# second match
293-
assert matches[1][1]["function.call"] == function_call_name_node
294-
assert matches[1][1]["function.args"] == function_call_args_node
289+
assert matches[1][1]["function.call"] == [function_call_name_node]
290+
assert matches[1][1]["function.args"] == [function_call_args_node]
295291
```
296292

297-
The `Query.matches()` method takes the same optional arguments as `Query.captures()`.
298293
The difference between the two methods is that `Query.matches()` groups captures into matches,
299-
which is much more useful when your captures within a query relate to each other. It maps the
300-
capture's name to the node that was captured via a dictionary.
294+
which is much more useful when your captures within a query relate to each other.
301295

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

examples/usage.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,19 @@ def read_callable_point(_, point):
155155
# ...with captures
156156
captures = query.captures(tree.root_node)
157157
assert len(captures) == 4
158-
assert captures[0][0] == function_name_node
159-
assert captures[0][1] == "function.def"
160-
assert captures[1][0] == function_body_node
161-
assert captures[1][1] == "function.block"
162-
assert captures[2][0] == function_call_name_node
163-
assert captures[2][1] == "function.call"
164-
assert captures[3][0] == function_call_args_node
165-
assert captures[3][1] == "function.args"
158+
assert captures["function.def"][0] == function_name_node
159+
assert captures["function.block"][0] == function_body_node
160+
assert captures["function.call"][0] == function_call_name_node
161+
assert captures["function.args"][0] == function_call_args_node
166162

167163
# ...with matches
168164
matches = query.matches(tree.root_node)
169165
assert len(matches) == 2
170166

171167
# first match
172-
assert matches[0][1]["function.def"] == function_name_node
173-
assert matches[0][1]["function.block"] == function_body_node
168+
assert matches[0][1]["function.def"] == [function_name_node]
169+
assert matches[0][1]["function.block"] == [function_body_node]
174170

175171
# second match
176-
assert matches[1][1]["function.call"] == function_call_name_node
177-
assert matches[1][1]["function.args"] == function_call_args_node
172+
assert matches[1][1]["function.call"] == [function_call_name_node]
173+
assert matches[1][1]["function.args"] == [function_call_args_node]

0 commit comments

Comments
 (0)