Skip to content

Fix semantic token rendering glitch with injected text#320178

Merged
alexdima merged 1 commit into
mainfrom
agents/bug-report-semantic-token-glitch
Jun 5, 2026
Merged

Fix semantic token rendering glitch with injected text#320178
alexdima merged 1 commit into
mainfrom
agents/bug-report-semantic-token-glitch

Conversation

@alexdima
Copy link
Copy Markdown
Member

@alexdima alexdima commented Jun 5, 2026

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):

  • An inline decoration with injected text (before: { content: '...' }) on the line
  • Adjacent semantic tokens on the same line
  • Cursor at a boundary between two semantic tokens
  • Type any character

Root Cause

  1. acceptInsertText expands one semantic token's range while leaving the adjacent token at its old position → overlapping ranges (e.g., (3,5) and (4,5))
  2. addSparseTokens merges these into a backward endOffset sequence because emitToken only rejected equal endOffsets (===), not backward ones
  3. LineTokens.withInserted() iterates these backward boundaries, re-copying characters and duplicating them in the DOM

Fix

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:

  • Merged token endOffsets are monotonically increasing
  • withInserted() produces correct text without duplication

Fixes microsoft/monaco-editor#5332

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>
Copilot AI review requested due to automatic review settings June 5, 2026 20:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/vs/editor/test/common/model/tokensStore.test.ts
Comment thread src/vs/editor/test/common/model/tokensStore.test.ts
@alexdima alexdima enabled auto-merge (squash) June 5, 2026 20:34
@alexdima alexdima added this to the 1.124.0 milestone Jun 5, 2026
@alexdima alexdima self-assigned this Jun 5, 2026
@alexdima alexdima merged commit b1bbbe5 into main Jun 5, 2026
26 checks passed
@alexdima alexdima deleted the agents/bug-report-semantic-token-glitch branch June 5, 2026 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Rendering glitch at semantic token boundaries when inline decorations are present in the same line

3 participants