From c77f3611f1bb0752f017b779566939baad4561dd Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Sat, 2 Dec 2023 14:53:50 -0800 Subject: [PATCH] =?UTF-8?q?[tree-sitter]=20Fix=20injection=20bug=20that=20?= =?UTF-8?q?happens=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …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. --- src/wasm-tree-sitter-language-mode.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wasm-tree-sitter-language-mode.js b/src/wasm-tree-sitter-language-mode.js index c931ecad8c..4e2de3e4c9 100644 --- a/src/wasm-tree-sitter-language-mode.js +++ b/src/wasm-tree-sitter-language-mode.js @@ -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.