Fix semantic token rendering glitch with injected text#320178
Merged
Conversation
When typing at a semantic token boundary with injected text present (e.g., inline decorations with `before.content`), characters could briefly appear duplicated in the DOM for one render frame. Root cause: `acceptInsertText` can expand one semantic token's range while leaving the adjacent token at its old position, creating overlapping ranges. `addSparseTokens` merged these into a backward endOffset sequence because `emitToken` only rejected equal endOffsets (`===`), not backward ones. When `LineTokens.withInserted()` iterated these backward boundaries, it re-copied characters, duplicating them. Fix: change `emitToken`'s guard from `===` to `<=` so backward endOffsets are never emitted into the merged token array. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a one-frame semantic token rendering glitch that could duplicate characters in the DOM when typing at a semantic-token boundary while injected text is present. The change hardens SparseTokensStore.addSparseTokens so it never emits non-monotonic (backward) token end offsets, which previously could confuse LineTokens.withInserted() and cause text to be re-copied.
Changes:
- Prevent emission of backward token boundaries by tightening
emitToken’s guard from===to<=. - Add a unit test that simulates overlapping semantic token ranges and verifies monotonic endOffsets plus correct
withInserted()line content.
Show a summary per file
| File | Description |
|---|---|
| src/vs/editor/common/tokens/sparseTokensStore.ts | Ensures merged token streams never contain backward endOffsets by skipping endOffset <= lastEndOffset. |
| src/vs/editor/test/common/model/tokensStore.test.ts | Adds a regression test covering overlapping semantic tokens + injected text to prevent DOM duplication scenarios. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 2
dmitrivMS
approved these changes
Jun 5, 2026
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.
Problem
When typing at a semantic token boundary with injected text present (e.g., inline decorations with
before.content), characters briefly appear duplicated in the DOM for one render frame. The glitch self-corrects on the next frame when new semantic tokens arrive.Repro conditions (all required):
before: { content: '...' }) on the lineRoot Cause
acceptInsertTextexpands one semantic token's range while leaving the adjacent token at its old position → overlapping ranges (e.g.,(3,5)and(4,5))addSparseTokensmerges these into a backward endOffset sequence becauseemitTokenonly rejected equal endOffsets (===), not backward onesLineTokens.withInserted()iterates these backward boundaries, re-copying characters and duplicating them in the DOMFix
One-line change:
emitToken's guard from===to<=, preventing backward endOffsets from ever being emitted.Test
Added a unit test that creates overlapping semantic tokens (simulating post-edit state) and verifies:
withInserted()produces correct text without duplicationFixes microsoft/monaco-editor#5332