Repository navigation
[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
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…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
```
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
73f53a6 to
24912a8
Compare
|
AI review status for this pull request.
|
There was a problem hiding this comment.
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.
|
I'll run a full review to re-check this with your note. |
Description
When the code highlighter tokenizes a code block again, it splices in the
tokenizer's fresh nodes, and
$keepCodeNodeStategives each new highlighttoken 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 linebreak after the next highlight pass.
$keepCodeNodeStatenow also gives a new tab or line break the NodeState ofthe 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.tsandCodeShikiRetokenizeKeepsState.test.ts: text, a tab and a line break insidea 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
After
The whole suite, the types and Flow: