Skip to content

[lexical-code-core][lexical-code-prism][lexical-code-shiki] Bug Fix: A tab or line break in a code block keeps its NodeState when the block is tokenized again - #9855

Merged
zurfyx merged 1 commit into
mainfrom
zurfyx/code-retokenize-keeps-tab-state
Nov 9, 2026

Conversation

@zurfyx

@zurfyx zurfyx commented Nov 9, 2026

Copy link
Copy Markdown
Member

Description

When the code highlighter tokenizes a code block again, it splices in the
tokenizer's fresh nodes, and $keepCodeNodeState gives each new highlight
token the NodeState of the text it came from (comment ids, a suggestion's
members, which collaboration stores on the text). A TabNode or LineBreakNode
the tokenizer makes is fresh too, but it was used as it was, so the NodeState
of the old tab or line break at that offset was dropped: a comment that spans
foo⇥bar↵baz, merged into a code block, had holes at the tab and the line
break after the next highlight pass.

$keepCodeNodeState now also gives a new tab or line break the NodeState of
the tab or line break at the same offset among the old children, through the
same copy as the tokens (what the new node holds already stays). Nothing
changes for a block whose tabs and line breaks have no NodeState.

Changeset

.changeset/code-retokenize-keeps-tab-state.md: a patch for
@lexical/code-core.

Test plan

CodePrismRetokenizeKeepsState.test.ts and
CodeShikiRetokenizeKeepsState.test.ts: text, a tab and a line break inside
a code block, all with a NodeState value and read as a collaborator's change
is (no transforms), keep it when a paragraph inserted right after the block
makes the highlighter tokenize it again.

Before

$ vitest --run --project unit CodePrismRetokenizeKeepsState CodeShikiRetokenizeKeepsState
 × a tab and a line break with NodeState inside a code block keep it, as the text around them does (Prism, Shiki)
AssertionError: expected [ [ 'code-highlight', 'kept' ], …(4) ] to deeply equal [ [ 'code-highlight', 'kept' ], …(4) ]

- Expected
+ Received

@@ -3,19 +3,19 @@
      "code-highlight",
      "kept",
    ],
    [
      "tab",
-     "kept",
+     "",
    ],
    [
      "code-highlight",
      "kept",
    ],
    [
      "linebreak",
-     "kept",
+     "",
    ],
    [
      "code-highlight",
      "kept",
    ],
      Tests  2 failed | 22 passed (24)

After

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

The whole suite, the types and Flow:

$ LEXICAL_Y_GC=0 vitest run --project unit --project scripts-unit
 Test Files  932 passed | 6 skipped (938)
      Tests  38106 passed | 371 expected fail | 161 skipped (38638)
$ LEXICAL_Y_GC=1 vitest run --project unit --project scripts-unit
 Test Files  932 passed | 6 skipped (938)
      Tests  38106 passed | 371 expected fail | 161 skipped (38638)
$ pnpm run test-integration
 Test Files  7 passed (7)
      Tests  664 passed (664)
$ pnpm run tsc
$ pnpm run flow
Found 0 errors

@vercel

vercel Bot commented Nov 9, 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 9, 2026 3:05pm UTC
lexical-playground Ready Ready Preview Nov 9, 2026 3:05pm 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 9, 2026
…A tab or line break in a code block keeps its NodeState when the block is tokenized again

## Description

When the code highlighter tokenizes a code block again, it splices in the
tokenizer's fresh nodes, and `$keepCodeNodeState` gives each new highlight
token the NodeState of the text it came from (comment ids, a suggestion's
members, which collaboration stores on the text). A TabNode or LineBreakNode
the tokenizer makes is fresh too, but it was used as it was, so the NodeState
of the old tab or line break at that offset was dropped: a comment that spans
`foo⇥bar↵baz`, merged into a code block, had holes at the tab and the line
break after the next highlight pass.

`$keepCodeNodeState` now also gives a new tab or line break the NodeState of
the tab or line break at the same offset among the old children, through the
same copy as the tokens (what the new node holds already stays). Nothing
changes for a block whose tabs and line breaks have no NodeState.

## Changeset

`.changeset/code-retokenize-keeps-tab-state.md`: a patch for
`@lexical/code-core`.

## Test plan

`CodePrismRetokenizeKeepsState.test.ts` and
`CodeShikiRetokenizeKeepsState.test.ts`: text, a tab and a line break inside
a code block, all with a NodeState value and read as a collaborator's change
is (no transforms), keep it when a paragraph inserted right after the block
makes the highlighter tokenize it again.

### Before

```
$ vitest --run --project unit CodePrismRetokenizeKeepsState CodeShikiRetokenizeKeepsState
 × a tab and a line break with NodeState inside a code block keep it, as the text around them does (Prism, Shiki)
AssertionError: expected [ [ 'code-highlight', 'kept' ], …(4) ] to deeply equal [ [ 'code-highlight', 'kept' ], …(4) ]

- Expected
+ Received

@@ -3,19 +3,19 @@
      "code-highlight",
      "kept",
    ],
    [
      "tab",
-     "kept",
+     "",
    ],
    [
      "code-highlight",
      "kept",
    ],
    [
      "linebreak",
-     "kept",
+     "",
    ],
    [
      "code-highlight",
      "kept",
    ],
      Tests  2 failed | 22 passed (24)
```

### After

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

The whole suite, the types and Flow:

```
$ LEXICAL_Y_GC=0 vitest run --project unit --project scripts-unit
 Test Files  932 passed | 6 skipped (938)
      Tests  38106 passed | 371 expected fail | 161 skipped (38638)
$ LEXICAL_Y_GC=1 vitest run --project unit --project scripts-unit
 Test Files  932 passed | 6 skipped (938)
      Tests  38106 passed | 371 expected fail | 161 skipped (38638)
$ pnpm run test-integration
 Test Files  7 passed (7)
      Tests  664 passed (664)
$ pnpm run tsc
$ pnpm run flow
Found 0 errors
```
@pkg-pr-new

pkg-pr-new Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 24912a8

@zurfyx
zurfyx force-pushed the zurfyx/code-retokenize-keeps-tab-state branch from 73f53a6 to 24912a8 Compare November 9, 2026 15:04
@zurfyx

zurfyx commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Please run a full review of the current head (24912a8, on main 7a853fc).

@rootnode-bot

rootnode-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 9, 2026, 4:03 PM UTC

@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.

Thanks, this looks good to me.

The fix is small and in the right place. $keepCodeNodeState now records each tab and line break that carries NodeState by its offset, and copies that state onto the tab or line break the tokenizer creates at the same offset. It uses the same $copyNodeState as the highlight tokens (packages/lexical-code-core/src/FlatStructureUtils.ts, lines 346-383).

The highlighters' isEqual treats any two tabs, and any two line breaks, as equal. So a tab outside the replaced range stays as it was, and one inside it is swapped one for one for a node with the same state: nothing gets duplicated. Blocks with no NodeState still take the early return.

The new Prism and Shiki tests pin the bug. Without the fix, the tab and line break come back with an empty value. The same holds for a paragraph turned into a code block with $setBlocksType, and for an edit that re-types the tokens on both sides of an existing tab.

The changeset follows .changeset/README.md, including the Verify: paragraph that AGENTS.md asks for on a behavior fix.

No changes requested.

@zurfyx
zurfyx added this pull request to the merge queue Nov 9, 2026
@rootnode-bot

rootnode-bot Bot commented Nov 9, 2026

Copy link
Copy Markdown

In reply to @zurfyx

I'll run a full review to re-check this with your note.

Merged via the queue into main with commit 8c369d7 Nov 9, 2026
57 checks passed
@potatowagon potatowagon mentioned this pull request Dec 2, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 24912a8d Deployed Nov 9, 2026 by vercel[bot]
Preview – lexical-playground — 24912a8d Deployed Nov 9, 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