Repository navigation
[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
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…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) ```
a7780cf to
2e1b7a9
Compare
|
AI review status for this pull request.
|
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: |
There was a problem hiding this comment.
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:
- 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 ownCodeHighlightNodesubclass, or sets a NodeState value, also replaces old stock tokens that carry no NodeState at all;mainkept them. That matches the changeset's first clause, but every new test, and theVerify:paragraph, uses old tokens with comments. One more case per test file, for exampleappTokenizerorkindTokenizerover stock tokens withoutmembersState, would pin it down. The unit tier fits here (AGENTS.md, "Testing Strategy"). - Collaboration.
@lexical/yjsapplies remote changes withskipTransforms, 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.
| const sameClass: boolean = next.constructor === prev.constructor; | ||
| if ( | ||
| sameClass | ||
| ? !hasNodeState(next) | ||
| : prev.getType() !== CodeHighlightNode.getType() || | ||
| !(next instanceof CodeHighlightNode) | ||
| ) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
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.
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,
$keepCodeNodeStatesplits a new token at the boundaries of each node withNodeState 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 (
$replaceCodeTokensAddedToin @lexical/code-core): the new token is ofthe 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-prismand@lexical/code-shiki.Changeset
.changeset/code-tokenizer-class-beside-state.md: a patch for@lexical/code-core,@lexical/code-prismand@lexical/code-shiki.Test plan
CodePrismRetokenizeKeepsState.test.tsandCodeShikiRetokenizeKeepsState.test.ts, with old tokens that have commentsof their own, read as a collaborator's change is (no transforms), then
tokenized again:
and a NodeState value of its own), over old tokens
alphaandbetathat 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;
has it, and every character keeps its own comment;
another value for it, is kept as it is;
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;
where the new tokens of that class have another value, also when they add
a NodeState value named as the field.
range forward and backward, and a range from the paragraph after the block
into it;
1becomes1+2, three tokens), the old tokens the comparison keeps before and afterit 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
After