Roughly track the last token in each AST node #14391
Closed
+58
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is important for tools like LSP servers, because we can't properly reconstruct the extents of an AST node just from its structure. Take the following example:
If the cursor is positioned after the
if (ba
, zls will not autocompletebar
. This is becauseAst.lastToken
tries to find the end of the function by traversing the children, so it never includes the final}
, meaning zls doesn't think that the cursor is insidefoo
.To remedy this, I've added an parser option that will record the last token of ast nodes as well as the first. Optional so that we don't take a performance&memory hit when we don't need to track this data.
I've been using a forked zig with this based on
0.10.0
along with a patched zls for the past couple months, and it works fine.Here is an example that you can run to see what I'm talking about:
This prints:
It thinks the last token of the function decl
foo
starts at 32, but the source is 45 bytes long. With correct tracking of AST extents, this should show 44 instead.