highlighter: Clip stale injection ranges to char boundaries before slicing - #3317
Merged
Merged
Conversation
…icing A syntax tree can be stale relative to the current text, so injection ranges taken from it may start or end inside a multi-byte character. Slicing the `Rope` there panicked in `markdown_inline_range_has_trigger`. - `markdown_inline_range_has_trigger` clips the range to the text's char boundaries (and length) before scanning. - `captured_injection_language` returns `None` for a range that does not land on char boundaries. Fixes #3315 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
huacnlee
enabled auto-merge (squash)
September 29, 2026 11:03
trancong12102
added a commit
to aislopware/gpui-kit
that referenced
this pull request
Sep 29, 2026
…bridge#3315) longbridge#3317 clips the injection ranges that longbridge#3315 found inside multi-byte characters, which ends the panic but leaves where those ranges came from. They came from a tree parsed incrementally from a tree that never took the change: `edit_tree(None, text)` swapped the text and kept the tree, and the next parse reused its untouched nodes at their old offsets. The result is not a stale moment but a wrong tree that every later incremental parse builds on (a heading kept at 0..9 where the text has it at 0..12), so the colours stay wrong until an edit happens to cover it. A change without an edit now drops the tree: `edit_tree(None, _)` clears it like it already cleared the injection layers, and `update` without edits parses from nothing instead of from the old tree under a made-up edit that inserted the whole text at 0 (with a (0, 0) end point). The parse with edits takes the old tree only when there is one, rather than parsing "" first on every update to stand in for a missing tree. longbridge#3317's clipping stays as a guard; with this, the ranges it clips come only from a correctly edited tree. Tests: test_a_change_without_an_edit_is_parsed_afresh compares the tree after both paths with a fresh parse (fails before: atx_heading 0..9 vs 0..12); test_stale_tree_styles_snap_to_char_boundaries now makes its stale tree the way the editor does, a timed-out parse of an edit.
trancong12102
added a commit
to aislopware/gpui-kit
that referenced
this pull request
Sep 30, 2026
…bridge#3315) longbridge#3317 clips the injection ranges that longbridge#3315 found inside multi-byte characters, which ends the panic but leaves where those ranges came from. They came from a tree parsed incrementally from a tree that never took the change: `edit_tree(None, text)` swapped the text and kept the tree, and the next parse reused its untouched nodes at their old offsets. The result is not a stale moment but a wrong tree that every later incremental parse builds on (a heading kept at 0..9 where the text has it at 0..12), so the colours stay wrong until an edit happens to cover it. A change without an edit now drops the tree: `edit_tree(None, _)` clears it like it already cleared the injection layers, and `update` without edits parses from nothing instead of from the old tree under a made-up edit that inserted the whole text at 0 (with a (0, 0) end point). The parse with edits takes the old tree only when there is one, rather than parsing "" first on every update to stand in for a missing tree. longbridge#3317's clipping stays as a guard; with this, the ranges it clips come only from a correctly edited tree. Tests: test_a_change_without_an_edit_is_parsed_afresh compares the tree after both paths with a fresh parse (fails before: atx_heading 0..9 vs 0..12); test_stale_tree_styles_snap_to_char_boundaries now makes its stale tree the way the editor does, a timed-out parse of an edit.
trancong12102
added a commit
to aislopware/gpui-kit
that referenced
this pull request
Sep 30, 2026
…bridge#3315) longbridge#3317 clips the injection ranges that longbridge#3315 found inside multi-byte characters, which ends the panic but leaves where those ranges came from. They came from a tree parsed incrementally from a tree that never took the change: `edit_tree(None, text)` swapped the text and kept the tree, and the next parse reused its untouched nodes at their old offsets. The result is not a stale moment but a wrong tree that every later incremental parse builds on (a heading kept at 0..9 where the text has it at 0..12), so the colours stay wrong until an edit happens to cover it. A change without an edit now drops the tree: `edit_tree(None, _)` clears it like it already cleared the injection layers, and `update` without edits parses from nothing instead of from the old tree under a made-up edit that inserted the whole text at 0 (with a (0, 0) end point). The parse with edits takes the old tree only when there is one, rather than parsing "" first on every update to stand in for a missing tree. longbridge#3317's clipping stays as a guard; with this, the ranges it clips come only from a correctly edited tree. Tests: test_a_change_without_an_edit_is_parsed_afresh compares the tree after both paths with a fresh parse (fails before: atx_heading 0..9 vs 0..12); test_stale_tree_styles_snap_to_char_boundaries now makes its stale tree the way the editor does, a timed-out parse of an edit.
trancong12102
added a commit
to aislopware/gpui-kit
that referenced
this pull request
Oct 1, 2026
…bridge#3315) longbridge#3317 clips the injection ranges that longbridge#3315 found inside multi-byte characters, which ends the panic but leaves where those ranges came from. They came from a tree parsed incrementally from a tree that never took the change: `edit_tree(None, text)` swapped the text and kept the tree, and the next parse reused its untouched nodes at their old offsets. The result is not a stale moment but a wrong tree that every later incremental parse builds on (a heading kept at 0..9 where the text has it at 0..12), so the colours stay wrong until an edit happens to cover it. A change without an edit now drops the tree: `edit_tree(None, _)` clears it like it already cleared the injection layers, and `update` without edits parses from nothing instead of from the old tree under a made-up edit that inserted the whole text at 0 (with a (0, 0) end point). The parse with edits takes the old tree only when there is one, rather than parsing "" first on every update to stand in for a missing tree. longbridge#3317's clipping stays as a guard; with this, the ranges it clips come only from a correctly edited tree. Tests: test_a_change_without_an_edit_is_parsed_afresh compares the tree after both paths with a fresh parse (fails before: atx_heading 0..9 vs 0..12); test_stale_tree_styles_snap_to_char_boundaries now makes its stale tree the way the editor does, a timed-out parse of an edit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #3315.
A syntax tree can be stale relative to the current text — after a sync parse times out, or after
SyntaxHighlighter::edit_tree— so byte ranges taken from it can start or end inside a multi-byte character. 0.7.0 made the tree-sitter read callback byte-safe, but two helpers still sliced theRopedirectly at those ranges:markdown_inline_range_has_triggerpanicked withbyte index is not on a char boundary(no bounds or boundary check). It now clips the range to the text's char boundaries and length before scanning.captured_injection_languagechecked bounds but not char boundaries. A range that no longer lands on char boundaries cannot name a language, so it now returnsNone.No public API changes.
Test
test_injection_ranges_inside_multibyte_charcovers ranges starting mid-character, running past the text end, and lying entirely beyond it.highlighter.rs:280) #3315, run against this branch, completes instead of panicking athighlighter.rs:280.🤖 Generated with Claude Code