fix(painter): rebuild pairs in style_range instead of splitting in place - #1167
Open
kronberger-droid wants to merge 3 commits into
Open
fix(painter): rebuild pairs in style_range instead of splitting in place#1167kronberger-droid wants to merge 3 commits into
kronberger-droid wants to merge 3 commits into
Conversation
Red on purpose. Three cases show style_range panicking when a byte offset lands inside a multi-byte char, since split_off asserts the boundary. Four cases pin that a range starting or ending on a pair boundary leaves no zero-length pair, which the split-and-insert version produces today; the rewrite that follows makes them green.
The painter needs the same floor/ceil the rest policy uses. Also pin that both accept a position inside a multi-byte char, which is the contract the painter is about to lean on.
String::split_off asserts a char boundary, so a highlighter handing style_range a byte offset inside a multi-byte char took the paint path down with it. The bounds now snap outward to grapheme boundaries and the pairs are rebuilt into a fresh Vec, thus no insert-during-walk, no index into the buffer, and no zero-length pair left where a range starts or ends on a boundary.
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
Started as a lint site (
self.buffer[pair_idx]instyle_range) and turned into a real crash while probing edge cases:String::split_offasserts a char boundary,so a highlighter handing
style_rangea byte offset that lands inside a multi-byte character panicked the paint path.Any highlighter computing offsets from chars or graphemes hits it on the first accented character.
The loop is now a rebuild.
Each pair is split into at most three pieces (before, styled, after) with the bounds snapped outward to grapheme boundaries,
non-empty pieces go into a fresh
Vec, and the result replaces the buffer.No insert-during-walk, no index into the buffer, no
split_off.The snapping reuses
ensure_grapheme_boundary_prev/nextfromcore_editor::graphemes, nowpub(crate).Two observable changes, both stated in the doc:
a range landing inside a grapheme styles the whole grapheme rather than panicking,
and a range starting or ending on a pair boundary no longer leaves a zero-length pair behind.
Otherwise the segmentation is unchanged; the seven existing tests pass untouched.
Before
After