Skip to content

Commit

Permalink
docs: update usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Sep 15, 2024
1 parent e164972 commit 2d9b607
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,36 +268,30 @@ query = PY_LANGUAGE.query(

```python
captures = query.captures(tree.root_node)
assert len(captures) == 2
assert captures[0][0] == function_name_node
assert captures[0][1] == "function.def"
assert len(captures) == 4
assert captures["function.def"][0] == function_name_node
assert captures["function.block"][0] == function_body_node
assert captures["function.call"][0] == function_call_name_node
assert captures["function.args"][0] == function_call_args_node
```

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

#### Matches

```python
matches = query.matches(tree.root_node)
assert len(matches) == 2

# first match
assert matches[0][1]["function.def"] == function_name_node
assert matches[0][1]["function.block"] == function_body_node
assert matches[0][1]["function.def"] == [function_name_node]
assert matches[0][1]["function.block"] == [function_body_node]

# second match
assert matches[1][1]["function.call"] == function_call_name_node
assert matches[1][1]["function.args"] == function_call_args_node
assert matches[1][1]["function.call"] == [function_call_name_node]
assert matches[1][1]["function.args"] == [function_call_args_node]
```

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

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

Expand Down
20 changes: 8 additions & 12 deletions examples/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,19 @@ def read_callable_point(_, point):
# ...with captures
captures = query.captures(tree.root_node)
assert len(captures) == 4
assert captures[0][0] == function_name_node
assert captures[0][1] == "function.def"
assert captures[1][0] == function_body_node
assert captures[1][1] == "function.block"
assert captures[2][0] == function_call_name_node
assert captures[2][1] == "function.call"
assert captures[3][0] == function_call_args_node
assert captures[3][1] == "function.args"
assert captures["function.def"][0] == function_name_node
assert captures["function.block"][0] == function_body_node
assert captures["function.call"][0] == function_call_name_node
assert captures["function.args"][0] == function_call_args_node

# ...with matches
matches = query.matches(tree.root_node)
assert len(matches) == 2

# first match
assert matches[0][1]["function.def"] == function_name_node
assert matches[0][1]["function.block"] == function_body_node
assert matches[0][1]["function.def"] == [function_name_node]
assert matches[0][1]["function.block"] == [function_body_node]

# second match
assert matches[1][1]["function.call"] == function_call_name_node
assert matches[1][1]["function.args"] == function_call_args_node
assert matches[1][1]["function.call"] == [function_call_name_node]
assert matches[1][1]["function.args"] == [function_call_args_node]

0 comments on commit 2d9b607

Please sign in to comment.