Skip to content

Commit 12c323f

Browse files
author
Erin van der Veen
committed
chore: a note on anchors
1 parent 0126f36 commit 12c323f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,36 @@ between the words.
566566
This assumes you are already familiar with the [Tree-sitter query
567567
language][tree-sitter-query].
568568

569+
### A note on anchors
570+
The behaviour of "anchors" can be counterintuitive. Consider, for instance, the
571+
following query:
572+
```scheme
573+
(
574+
(list_entry) @append_space
575+
.
576+
)
577+
```
578+
One might assume that this query only matches the final element in the list but
579+
this is not true. Since we did not explicitly march a parent node, the engine
580+
will match on every `list_entry`. After all, the when looking only at the nodes
581+
in the query, the `list_entry` is indeed the last node.
582+
583+
To resolve this issue, match explicitly on the parent node:
584+
```scheme
585+
(list
586+
(list_entry) @append_space
587+
.
588+
)
589+
```
590+
591+
Or even implicitly:
592+
```scheme
593+
(_
594+
(list_entry) @append_space
595+
.
596+
)
597+
```
598+
569599
Note that a capture is put after the node it is associated with. If you
570600
want to put a space in front of a node, you do it like this:
571601

0 commit comments

Comments
 (0)