Skip to content

[lexical-code-core][lexical-code-prism][lexical-code-shiki] Bug Fix: The code highlighter writes an app tokenizer's token over an old token it adds to - #9862

Merged
zurfyx merged 1 commit into
mainfrom
zurfyx/code-tokenizer-class-beside-state
Nov 10, 2026
Merged

zurfyx merged 1 commit into
mainfrom
zurfyx/code-tokenizer-class-beside-state

Conversation

@zurfyx

@zurfyx zurfyx commented Nov 10, 2026

Copy link
Copy Markdown
Member

Description

When the code highlighter tokenizes a code block again, it compares the
block's children with the new tokens and keeps each old token whose text
and highlight type (and, with Shiki, style) did not change. Since #9832,
$keepCodeNodeState splits a new token at the boundaries of each node with
NodeState and gives each part the NodeState of its own text, so the parts
now line up with the old nodes. With an app's tokenizer that returns its own
CodeHighlightNode subclass (with its own field and NodeState), or sets a
NodeState value on its tokens, over code text whose nodes have NodeState
(comment ids, say), the comparison then kept the old stock tokens in place
of the parts: the tokenizer's class, fields and NodeState were dropped
there, which contradicts #9832's description. Before #9832 the unsplit
token did not line up, so it replaced them (and dropped the comments
instead); where an old token's text already matched a new token's, the old
token was kept then too.

The highlighter now replaces each old token it keeps, one for one, with the
new token at its place, when the new token adds to it and loses nothing of
it ($replaceCodeTokensAddedTo in @lexical/code-core): the new token is of
the old token's class with the same fields, or of an app's own subclass
where the old token is a stock CodeHighlightNode; it has the old token's
format, style and every NodeState value it has; and it has a class or a
NodeState value of its own besides. The comparison itself and the range it
replaces are unchanged, so every other old token stays as it is, as before:
one that has something the new token does not (a NodeState value the app
set, where the new token has another; a field value of the app's class), or
that the new token adds nothing to (the common case, with the stock
tokenizers and no NodeState), and nothing is written there on each pass. A
selection point on a replaced token goes to the new token at the same
offset, so the selection stays where it was.

Apps that do not run a code highlighter are unaffected: the change is
limited to @lexical/code-core, @lexical/code-prism and
@lexical/code-shiki.

Changeset

.changeset/code-tokenizer-class-beside-state.md: a patch for
@lexical/code-core, @lexical/code-prism and @lexical/code-shiki.

Test plan

CodePrismRetokenizeKeepsState.test.ts and
CodeShikiRetokenizeKeepsState.test.ts, with old tokens that have comments
of their own, read as a collaborator's change is (no transforms), then
tokenized again:

  • with an app's tokenizer making each token of its own class (with a field
    and a NodeState value of its own), over old tokens alpha and beta
    that one new token covers: every token is of that class, with its field
    and the tokenizer's value, and every character keeps its own comment;
  • with a tokenizer that sets a NodeState value on each token: every token
    has it, and every character keeps its own comment;
  • an old token with a NodeState value the app set, where the new token has
    another value for it, is kept as it is;
  • over old tokens as the stock tokenizer makes them (matching the new
    tokens' text, type and style), one of them with a NodeState value the app
    set: the others are replaced by the app's tokens, one for one, and that
    one is kept as it is; another pass replaces nothing;
  • old tokens of the app's class with a field value of their own are kept,
    where the new tokens of that class have another value, also when they add
    a NodeState value named as the field.
  • the selection stays where it was when old tokens are replaced: a caret, a
    range forward and backward, and a range from the paragraph after the block
    into it;
  • with a change inside the line that the highlighter replaces (1 becomes
    1+2, three tokens), the old tokens the comparison keeps before and after
    it are replaced too, one for one.

On main, the first two fail with Prism (the old stock tokens are kept), and
the fourth and the last two fail with both highlighters.

Before

$ vitest --run --project unit CodePrismRetokenizeKeepsState CodeShikiRetokenizeKeepsState
 × each part of a token an app's tokenizer makes of its own class is of that class, with its field and the tokenizer's NodeState, and has the NodeState of its own text (Prism)
 × a NodeState value an app's tokenizer sets is on each token, beside the NodeState of its own text (Prism)
 × among old tokens as the stock tokenizer makes them, each with a comment, the ones a new token adds to are replaced, one for one, and one with a value the app set is kept, on every pass (Prism, Shiki)
AssertionError: expected { …(2) } to deeply equal { …(2) }
 × replacing old tokens a new token adds to keeps the selection where it was: a caret, a range forward and backward, and a range from outside the block (Prism, Shiki)
 × with a change inside the line that the highlighter replaces, the old tokens a new token adds to before and after it are replaced too, one for one (Prism, Shiki)
AssertionError: expected [ [ false, '', '', 'c1' ], …(5) ] to deeply equal [ …(6) ]
AssertionError: expected false to be true // Object.is equality
      Tests  8 failed | 30 passed (38)

After

$ vitest --run --project unit CodePrismRetokenizeKeepsState CodeShikiRetokenizeKeepsState
      Tests  38 passed (38)

@vercel

vercel Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 10, 2026 1:30am UTC
lexical-playground Ready Ready Preview Nov 10, 2026 1:30am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 10, 2026
@zurfyx

zurfyx commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

Please run a full review of the current head (a7780cf, on main 8c369d7).

…The code highlighter writes an app tokenizer's token over an old token it adds to

## Description

When the code highlighter tokenizes a code block again, it compares the
block's children with the new tokens and keeps each old token whose text
and highlight type (and, with Shiki, style) did not change. Since #9832,
`$keepCodeNodeState` splits a new token at the boundaries of each node with
NodeState and gives each part the NodeState of its own text, so the parts
now line up with the old nodes. With an app's tokenizer that returns its own
CodeHighlightNode subclass (with its own field and NodeState), or sets a
NodeState value on its tokens, over code text whose nodes have NodeState
(comment ids, say), the comparison then kept the old stock tokens in place
of the parts: the tokenizer's class, fields and NodeState were dropped
there, which contradicts #9832's description. Before #9832 the unsplit
token did not line up, so it replaced them (and dropped the comments
instead); where an old token's text already matched a new token's, the old
token was kept then too.

The highlighter now replaces each old token it keeps, one for one, with the
new token at its place, when the new token adds to it and loses nothing of
it (`$replaceCodeTokensAddedTo` in @lexical/code-core): the new token is of
the old token's class with the same fields, or of an app's own subclass
where the old token is a stock CodeHighlightNode; it has the old token's
format, style and every NodeState value it has; and it has a class or a
NodeState value of its own besides. The comparison itself and the range it
replaces are unchanged, so every other old token stays as it is, as before:
one that has something the new token does not (a NodeState value the app
set, where the new token has another; a field value of the app's class), or
that the new token adds nothing to (the common case, with the stock
tokenizers and no NodeState), and nothing is written there on each pass. A
selection point on a replaced token goes to the new token at the same
offset, so the selection stays where it was.

Apps that do not run a code highlighter are unaffected: the change is
limited to `@lexical/code-core`, `@lexical/code-prism` and
`@lexical/code-shiki`.

## Changeset

`.changeset/code-tokenizer-class-beside-state.md`: a patch for
`@lexical/code-core`, `@lexical/code-prism` and `@lexical/code-shiki`.

## Test plan

`CodePrismRetokenizeKeepsState.test.ts` and
`CodeShikiRetokenizeKeepsState.test.ts`, with old tokens that have comments
of their own, read as a collaborator's change is (no transforms), then
tokenized again:
- with an app's tokenizer making each token of its own class (with a field
  and a NodeState value of its own), over old tokens `alpha ` and `beta`
  that one new token covers: every token is of that class, with its field
  and the tokenizer's value, and every character keeps its own comment;
- with a tokenizer that sets a NodeState value on each token: every token
  has it, and every character keeps its own comment;
- an old token with a NodeState value the app set, where the new token has
  another value for it, is kept as it is;
- over old tokens as the stock tokenizer makes them (matching the new
  tokens' text, type and style), one of them with a NodeState value the app
  set: the others are replaced by the app's tokens, one for one, and that
  one is kept as it is; another pass replaces nothing;
- old tokens of the app's class with a field value of their own are kept,
  where the new tokens of that class have another value, also when they add
  a NodeState value named as the field.
- the selection stays where it was when old tokens are replaced: a caret, a
  range forward and backward, and a range from the paragraph after the block
  into it;
- with a change inside the line that the highlighter replaces (`1` becomes
  `1+2`, three tokens), the old tokens the comparison keeps before and after
  it are replaced too, one for one.

On main, the first two fail with Prism (the old stock tokens are kept), and
the fourth and the last two fail with both highlighters.

### Before

```
$ vitest --run --project unit CodePrismRetokenizeKeepsState CodeShikiRetokenizeKeepsState
 × each part of a token an app's tokenizer makes of its own class is of that class, with its field and the tokenizer's NodeState, and has the NodeState of its own text (Prism)
 × a NodeState value an app's tokenizer sets is on each token, beside the NodeState of its own text (Prism)
 × among old tokens as the stock tokenizer makes them, each with a comment, the ones a new token adds to are replaced, one for one, and one with a value the app set is kept, on every pass (Prism, Shiki)
AssertionError: expected { …(2) } to deeply equal { …(2) }
 × replacing old tokens a new token adds to keeps the selection where it was: a caret, a range forward and backward, and a range from outside the block (Prism, Shiki)
 × with a change inside the line that the highlighter replaces, the old tokens a new token adds to before and after it are replaced too, one for one (Prism, Shiki)
AssertionError: expected [ [ false, '', '', 'c1' ], …(5) ] to deeply equal [ …(6) ]
AssertionError: expected false to be true // Object.is equality
      Tests  8 failed | 30 passed (38)
```

### After

```
$ vitest --run --project unit CodePrismRetokenizeKeepsState CodeShikiRetokenizeKeepsState
      Tests  38 passed (38)
```
@zurfyx

zurfyx commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

Please run a full review of the current head (2e1b7a9, on main 8c369d7). Since a7780cf: a type annotation that fixes tsc (the emitted JavaScript is unchanged) and test typing.

@zurfyx
zurfyx force-pushed the zurfyx/code-tokenizer-class-beside-state branch from a7780cf to 2e1b7a9 Compare November 10, 2026 01:28
@rootnode-bot

rootnode-bot Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 10, 2026, 1:42 AM UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 10, 2026

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9862

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9862

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9862

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9862

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9862

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9862

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9862

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9862

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9862

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9862

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9862

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9862

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9862

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9862

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9862

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9862

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9862

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9862

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9862

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9862

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9862

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9862

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9862

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9862

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9862

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9862

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9862

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9862

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9862

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9862

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9862

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9862

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9862

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9862

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9862

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9862

commit: 2e1b7a9

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks good, and thanks for the thorough test matrix. The rule in $addsToCodeToken keeps every field and NodeState value of the old token and only ever adds to it. A second pass is therefore stable, and tokenizers that disagree cannot keep replacing each other's tokens. Selection points stay put through LexicalNode.replace. The new Prism and Shiki cases fail on main and pass here, matching the Before/After in the description.

Two optional, non-blocking suggestions:

  1. Test the no-NodeState case (packages/lexical-code-core/src/FlatStructureUtils.ts:366-398). The rule does not require the old token to have NodeState. So on the first pass, an app tokenizer that returns its own CodeHighlightNode subclass, or sets a NodeState value, also replaces old stock tokens that carry no NodeState at all; main kept them. That matches the changeset's first clause, but every new test, and the Verify: paragraph, uses old tokens with comments. One more case per test file, for example appTokenizer or kindTokenizer over stock tokens without membersState, would pin it down. The unit tier fits here (AGENTS.md, "Testing Strategy").
  2. Collaboration. @lexical/yjs applies remote changes with skipTransforms, so old stock tokens are now rewritten on the first local edit to their block. A two-peer test where both peers make that first pass on the same block would show how the concurrent replacements merge. The base already rewrites tokens whenever a retokenized range changes, so this is a nice-to-have.

Comment on lines +366 to +374
const sameClass: boolean = next.constructor === prev.constructor;
if (
sameClass
? !hasNodeState(next)
: prev.getType() !== CodeHighlightNode.getType() ||
!(next instanceof CodeHighlightNode)
) {
return false;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional, non-blocking: nothing here requires the old token to carry NodeState. So with an app tokenizer that returns its own subclass, or sets a NodeState value, old stock tokens with no NodeState at all are also replaced on the first pass, where main kept them. That matches the changeset's first clause, but all the new tests use old tokens with comments. One case per test file over old tokens without membersState would pin it.

@zurfyx
zurfyx added this pull request to the merge queue Nov 10, 2026
@zurfyx
zurfyx removed this pull request from the merge queue due to a manual request Nov 10, 2026
@zurfyx
zurfyx added this pull request to the merge queue Nov 10, 2026
Merged via the queue into main with commit c4bfbfc Nov 10, 2026
57 checks passed

This branch was successfully deployed

2 active deployments
Preview – lexical — 2e1b7a9c Deployed Nov 10, 2026 by vercel[bot]
Preview – lexical-playground — 2e1b7a9c Deployed Nov 10, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant