Skip to content

Commit

Permalink
chore: a note on anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin van der Veen committed Sep 19, 2024
1 parent 0126f36 commit 12c323f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,36 @@ between the words.
This assumes you are already familiar with the [Tree-sitter query
language][tree-sitter-query].

### A note on anchors
The behaviour of "anchors" can be counterintuitive. Consider, for instance, the
following query:
```scheme
(
(list_entry) @append_space
.
)
```
One might assume that this query only matches the final element in the list but
this is not true. Since we did not explicitly march a parent node, the engine
will match on every `list_entry`. After all, the when looking only at the nodes
in the query, the `list_entry` is indeed the last node.

To resolve this issue, match explicitly on the parent node:
```scheme
(list
(list_entry) @append_space
.
)
```

Or even implicitly:
```scheme
(_
(list_entry) @append_space
.
)
```

Note that a capture is put after the node it is associated with. If you
want to put a space in front of a node, you do it like this:

Expand Down

0 comments on commit 12c323f

Please sign in to comment.