Fix tree sitter spans not being aligned with text after saving #65
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 PR ensures that after saving a buffer, we recreate a fresh copy of the tree sitter parser.
Context: on save, we strip white-space which causes the tree sitter AST spans to not be aligned with the text any more. This is the bug reported in #42.
We are typically able to compute diffs incrementally as the buffer is edited and update the tree sitter parser accordingly. We could probably compute the diffs for the
strip_trailing_whitespace
function. However, in general it's expected that on save alinter
or similar would be run. This makes updating the tree sitter parsers impractical.The solution in this PR is to compute a fresh copy of the AST. This means we can run any arbitrary transformation of the text on save, without having to create a diff to update the parsers. However, the downside is that it means we need to remove the existing tree on save.
IMPORTANT: As the new tree is computed async-ly, there's (a short) period when there's no available tree. This causes some flickering on save which is not ideal.
@kevinmatthes @iainh Thank you for your investigations and patience.
Fixes #42
Based on #64