From f9b22cdc91f7f87f5b9d67a4e97cc569f6070ebb Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:50:16 +0800 Subject: [PATCH] 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 --- patches/@lezer__lr.patch | 56 +++++++++++++++++++++ pnpm-lock.yaml | 53 +++++++++---------- pnpm-workspace.yaml | 1 + src/editor/core/cm/incrementalParse.test.ts | 27 ++++++++++ 4 files changed, 111 insertions(+), 26 deletions(-) create mode 100644 patches/@lezer__lr.patch create mode 100644 src/editor/core/cm/incrementalParse.test.ts diff --git a/patches/@lezer__lr.patch b/patches/@lezer__lr.patch new file mode 100644 index 000000000..92902b397 --- /dev/null +++ b/patches/@lezer__lr.patch @@ -0,0 +1,56 @@ +diff --git a/dist/index.cjs b/dist/index.cjs +index 4172cb0f2befc00da9ae09eb3486cae743403a97..ed3c542377fc6dff0195dd9116c16464fb615919 100644 +--- a/dist/index.cjs ++++ b/dist/index.cjs +@@ -208,7 +208,9 @@ class Stack { + if (index > 0 && (this.buffer[index - 4] != 0 /* Term.Err */ || this.buffer[index - 1] < 0)) { + let mustMove = false; + for (let scan = index; scan > 0 && this.buffer[scan - 2] > end; scan -= 4) { +- if (this.buffer[scan - 1] >= 0) { ++ // Reused skipped nodes after the end must be moved out too. ++ if (this.buffer[scan - 1] >= 0 || ++ this.buffer[scan - 1] == -1 && this.p.reused[this.buffer[scan - 4]].type.isSkipped) { + mustMove = true; + break; + } +@@ -278,7 +280,11 @@ class Stack { + this.p.reused.push(value); + index++; + } +- let end = this.reducePos = this.pos = start + value.length; ++ // A reused skipped node (a comment) is not part of the node being ++ // reduced: like a shifted skipped token, it leaves reducePos alone. ++ let end = this.pos = start + value.length; ++ if (!value.type.isSkipped) ++ this.reducePos = end; + this.pushState(next, start); + this.buffer.push(index, start, end, -1 /* size == -1 means this is a reused value */); + if (this.curContext) +diff --git a/dist/index.js b/dist/index.js +index 8fd3f87829c312b93ce022867fd17bf34da0dbc0..f67b16d2f46d683f12ddc81534efbf50925e7c65 100644 +--- a/dist/index.js ++++ b/dist/index.js +@@ -206,7 +206,9 @@ class Stack { + if (index > 0 && (this.buffer[index - 4] != 0 /* Term.Err */ || this.buffer[index - 1] < 0)) { + let mustMove = false; + for (let scan = index; scan > 0 && this.buffer[scan - 2] > end; scan -= 4) { +- if (this.buffer[scan - 1] >= 0) { ++ // Reused skipped nodes after the end must be moved out too. ++ if (this.buffer[scan - 1] >= 0 || ++ this.buffer[scan - 1] == -1 && this.p.reused[this.buffer[scan - 4]].type.isSkipped) { + mustMove = true; + break; + } +@@ -276,7 +278,11 @@ class Stack { + this.p.reused.push(value); + index++; + } +- let end = this.reducePos = this.pos = start + value.length; ++ // A reused skipped node (a comment) is not part of the node being ++ // reduced: like a shifted skipped token, it leaves reducePos alone. ++ let end = this.pos = start + value.length; ++ if (!value.type.isSkipped) ++ this.reducePos = end; + this.pushState(next, start); + this.buffer.push(index, start, end, -1 /* size == -1 means this is a reused value */); + if (this.curContext) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 96a557542..5428f8436 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,6 +9,7 @@ overrides: patchedDependencies: '@codemirror/view': 0ee18e0fe711bd749ccc9f89f3566c74b5e08910f2892e166d207f286b0da0c5 + '@lezer/lr': 1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8 importers: @@ -82,7 +83,7 @@ importers: version: 1.2.5 '@lezer/lr': specifier: ^1.4.11 - version: 1.4.11 + version: 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@plutojl/lang-julia': specifier: ^1.2.0 version: 1.2.0 @@ -2408,7 +2409,7 @@ snapshots: '@codemirror/language': 6.13.1 '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@codemirror/lang-cpp@6.0.3': dependencies: @@ -2467,7 +2468,7 @@ snapshots: '@codemirror/view': 6.43.14(patch_hash=0ee18e0fe711bd749ccc9f89f3566c74b5e08910f2892e166d207f286b0da0c5) '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@codemirror/lang-json@6.0.2': dependencies: @@ -2480,7 +2481,7 @@ snapshots: '@codemirror/language': 6.13.1 '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@codemirror/lang-liquid@6.3.3': dependencies: @@ -2491,7 +2492,7 @@ snapshots: '@codemirror/view': 6.43.14(patch_hash=0ee18e0fe711bd749ccc9f89f3566c74b5e08910f2892e166d207f286b0da0c5) '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@codemirror/lang-markdown@6.5.2': dependencies: @@ -2539,7 +2540,7 @@ snapshots: '@codemirror/state': 6.7.6 '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@codemirror/lang-vue@0.1.3': dependencies: @@ -2548,14 +2549,14 @@ snapshots: '@codemirror/language': 6.13.1 '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@codemirror/lang-wast@6.0.2': dependencies: '@codemirror/language': 6.13.1 '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@codemirror/lang-xml@6.1.0': dependencies: @@ -2573,7 +2574,7 @@ snapshots: '@codemirror/state': 6.7.6 '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/yaml': 1.0.4 '@codemirror/language-data@6.5.2': @@ -2609,7 +2610,7 @@ snapshots: '@codemirror/view': 6.43.14(patch_hash=0ee18e0fe711bd749ccc9f89f3566c74b5e08910f2892e166d207f286b0da0c5) '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) style-mod: 4.1.4 '@codemirror/legacy-modes@6.5.5': @@ -2745,24 +2746,24 @@ snapshots: dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/css@1.3.9': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/generator@1.8.1': dependencies: '@lezer/common': 1.5.3 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/go@1.0.1': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/highlight@1.2.5': dependencies: @@ -2772,27 +2773,27 @@ snapshots: dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/java@1.1.5': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/javascript@1.5.6': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/json@1.0.3': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) - '@lezer/lr@1.4.11': + '@lezer/lr@1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8)': dependencies: '@lezer/common': 1.5.3 @@ -2805,37 +2806,37 @@ snapshots: dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/python@1.1.19': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/rust@1.0.3': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/sass@1.1.0': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/xml@1.0.6': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@lezer/yaml@1.0.4': dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@marijn/find-cluster-break@1.0.4': {} @@ -3101,7 +3102,7 @@ snapshots: dependencies: '@lezer/common': 1.5.3 '@lezer/highlight': 1.2.5 - '@lezer/lr': 1.4.11 + '@lezer/lr': 1.4.11(patch_hash=1561ac9e9bbbc302f48ea274ae33602e598a7833edaadd7031559c893c7853b8) '@replit/codemirror-vim-core@0.1.0': {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 28d9eac99..f237b9f57 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -5,3 +5,4 @@ overrides: mermaid>katex: ^0.18.2 patchedDependencies: "@codemirror/view": patches/@codemirror__view.patch + "@lezer/lr": patches/@lezer__lr.patch diff --git a/src/editor/core/cm/incrementalParse.test.ts b/src/editor/core/cm/incrementalParse.test.ts new file mode 100644 index 000000000..feb4de80f --- /dev/null +++ b/src/editor/core/cm/incrementalParse.test.ts @@ -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); + }); +});