fix(editor): keep syntax highlighting after reformatting JavaScript

Re-parsing after an edit could reuse a whole doc comment as one node. The
parser then moved its reduce position past the comment, so the declaration
before it grew to swallow the comment and the rest of the file, and the tree
lost every node in view: highlighting disappeared until the next edit.
Reformatting a file with long doc comments hit it reliably.

Patch @lezer/lr so a reused skipped node leaves the reduce position alone and
is moved out of the node being reduced, like a shifted skipped token.

Fixes #4468
Цей коміт міститься в:
l0ng-ai committed 2026-11-20 21:50:16 +08:00
1 джерело 2ec2a6c262
коміт f9b22cdc91
4 files changed
+111 -26

No files matched your search

+27
Переглянути файл
@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import { ensureSyntaxTree, syntaxTree } from "@codemirror/language";
import { javascript } from "@codemirror/lang-javascript";
import { EditorState } from "@codemirror/state";
/** Functions with long doc comments: re-parsing reuses a whole comment as one node. */
const source = Array.from(
{ length: 5 },
(_, i) =>
`/**\n${Array.from({ length: 40 }, (_, j) => ` * line ${j} of the comment on f${i}\n`).join("")} */\n` +
`export const f${i} = (a, b = {}) => {\n // body\n if (a) return b\n return a\n}\n\n`,
).join("");
describe("incremental parsing (#4468)", () => {
it("keeps a reused comment out of the declaration before it", () => {
let state = EditorState.create({ doc: source, extensions: [javascript()] });
ensureSyntaxTree(state, state.doc.length, 5000);
// Reindenting, as a formatter does.
const at = source.indexOf(" // body");
state = state.update({ changes: { from: at, to: at + 4, insert: " " } }).state;
const tree = ensureSyntaxTree(state, state.doc.length, 5000) ?? syntaxTree(state);
const fresh = javascript().language.parser.parse(state.doc.toString());
expect(tree.toString()).toBe(fresh.toString());
expect(tree.topNode.firstChild?.name).toBe("BlockComment");
expect(tree.topNode.firstChild?.from).toBe(0);
});
});