Skip to content

Commit

Permalink
[tree-sitter] Fix injection bug that happens…
Browse files Browse the repository at this point in the history
…when some amount of text is deleted at the beginning of a file.

When this happens, the “affected range” of the buffer is an empty range from (0, 0) to (0, 0). Tree-sitter's `descendantsOfType` function incorrectly returns results when querying this specific range, so we'll sidestep this by explicitly returning early whenever the affected range is empty instead of trying to (re-)populate injections for a range that can't have any.
  • Loading branch information
savetheclocktower committed Dec 2, 2023
1 parent fd9229b commit c77f361
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/wasm-tree-sitter-language-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3852,6 +3852,12 @@ class LanguageLayer {
range = range.union(new Range(earliest, latest));
}

// Why do we have to do this explicitly? Because `descendantsOfType` will
// incorrectly return nodes if the range runs from (0, 0) to (0, 0). All
// other empty ranges seem not to have this problem. Upon cursory
// inspection, this bug doesn't seem to be limited to `web-tree-sitter`.
if (range.isEmpty()) { return; }

// Now that we've enlarged the range, we might have more existing injection
// markers to consider. But check for containment rather than intersection
// so that we don't have to enlarge it again.
Expand Down

0 comments on commit c77f361

Please sign in to comment.