дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
fix(editor): skip empty semantic recovery nodes (#8387)
Deleting a Java method or field name (void run() {} -> void () {}) threw 'RangeError: Mark decorations may not be empty' from refreshSyntax: Lezer represents the missing name with a zero-width Definition recovery node, and the Java declaration fallback colouring pushed it as a range. The same happened through the TypeScript syntax colours (@x.() -> @.()). The Java fallback now skips empty names, and the semantic plugin drops empty ranges from syntax, fallback and server ranges for every language before building marks.
Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
1 джерело
bc51679ddf
коміт
7228d77a4f
4 files changed
+45
-4
No files matched your search
@@ -114,6 +114,9 @@ export function rainbowIndex(name: string): number {
|
||||
const RAINBOW_TYPES = new Set(["variable", "parameter"]);
|
||||
const NOT_LOCAL = new Set(["static", "constant", "defaultLibrary", "global", "classScope", "fileScope"]);
|
||||
|
||||
/** A name being typed parses as a zero-width recovery node, and a mark decoration may not be empty. */
|
||||
const nonEmpty = (r: { from: number; to: number }) => r.from < r.to;
|
||||
|
||||
function rangeCoverage(ranges: Array<{ from: number; to: number }>) {
|
||||
ranges.sort((a, b) => a.from - b.from || a.to - b.to);
|
||||
const ends: number[] = [];
|
||||
@@ -202,7 +205,7 @@ export function semanticRanges(
|
||||
);
|
||||
}
|
||||
out.push(...syntax);
|
||||
return out.sort((a, b) => a.from - b.from || a.to - b.to);
|
||||
return out.filter(nonEmpty).sort((a, b) => a.from - b.from || a.to - b.to);
|
||||
}
|
||||
|
||||
const markCache = new Map<string, Decoration>();
|
||||
@@ -305,7 +308,7 @@ const semanticPlugin = ViewPlugin.fromClass(
|
||||
: [];
|
||||
const covered = rangeCoverage(syntax);
|
||||
const retained: Array<{ from: number; to: number }> = [];
|
||||
const marks = syntax.map((r) => markFor(r.className, r.source).range(r.from, r.to));
|
||||
const marks = syntax.filter(nonEmpty).map((r) => markFor(r.className, r.source).range(r.from, r.to));
|
||||
this.view.state.field(semanticField).between(0, doc.length, (from, to, decoration) => {
|
||||
if (
|
||||
!decoration.spec.languageSyntax &&
|
||||
@@ -324,7 +327,7 @@ const semanticPlugin = ViewPlugin.fromClass(
|
||||
this.rainbow ? (name) => `cm-sem-rainbow-${rainbowIndex(name)}` : undefined,
|
||||
symbols ?? new Map(),
|
||||
))
|
||||
if (!covered(r.from, r.to) && !retainedCovered(r.from, r.to))
|
||||
if (nonEmpty(r) && !covered(r.from, r.to) && !retainedCovered(r.from, r.to))
|
||||
marks.push(markFor(r.className, r.source).range(r.from, r.to));
|
||||
}
|
||||
this.view.dispatch({ effects: setSemantic.of(Decoration.set(marks, true)) });
|
||||
|
||||
@@ -23,6 +23,18 @@ const named = (ranges: Array<{ from: number; to: number; className: string }>) =
|
||||
Object.fromEntries(ranges.map((r) => [SRC.slice(r.from, r.to), r.className]));
|
||||
|
||||
describe("javaDeclarationRanges", () => {
|
||||
it.each([
|
||||
"class A { void (){} }",
|
||||
"class A { () {} }",
|
||||
"class A { static int = 1; }",
|
||||
"class A { int x, = 2; }",
|
||||
"interface I { int = 1; }",
|
||||
"record R() { void (){} }",
|
||||
])("does not color zero-width recovery nodes in %j", (source) => {
|
||||
const ranges = javaDeclarationRanges(javaLanguage.parser.parse(source));
|
||||
expect(ranges.filter((range) => range.from >= range.to)).toEqual([]);
|
||||
});
|
||||
|
||||
it("colours declaration names as jdtls does, and nothing else", () => {
|
||||
expect(named(javaDeclarationRanges(tree))).toEqual({
|
||||
count: "cm-sem-property cm-sem-declaration cm-sem-java-instance-field",
|
||||
|
||||
@@ -50,7 +50,8 @@ export function javaDeclarationRanges(tree: Tree): SemanticRange[] {
|
||||
const declarators = decl.getChildren("VariableDeclarator");
|
||||
for (const owner of declarators.length ? declarators : [decl]) {
|
||||
const name = owner.getChild("Definition");
|
||||
if (name) out.push({ from: name.from, to: name.to, className, source: "fallback" });
|
||||
// A declaration being typed has a zero-width recovery node for its missing name.
|
||||
if (name && name.from < name.to) out.push({ from: name.from, to: name.to, className, source: "fallback" });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
@@ -24,6 +24,31 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe("semantic colours while typing", () => {
|
||||
it.each([
|
||||
["TypeScript", "demo.ts", "typescript", "@x.() class C {}", "x"],
|
||||
["Java", "A.java", "java", "class A { void run() {} }", "run"],
|
||||
])("ignores the empty recovery node of a name deleted in %s", async (name, file, language, source, deleted) => {
|
||||
const support = await loadSupport(findLanguageDescription(name, file)!);
|
||||
vi.useFakeTimers();
|
||||
const view = new EditorView({
|
||||
parent: document.body,
|
||||
state: EditorState.create({
|
||||
doc: source,
|
||||
extensions: [support!, lspDoc.of({ doc: 93, language } as EditorDocContext), semanticExtension()],
|
||||
}),
|
||||
});
|
||||
try {
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
const from = source.indexOf(deleted);
|
||||
view.dispatch({ changes: { from, to: from + deleted.length } });
|
||||
// The syntax colours refresh in a microtask; an empty mark range there was an uncaught RangeError.
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(view.state.doc.toString()).toBe(source.replace(deleted, ""));
|
||||
} finally {
|
||||
view.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
it("colours a newly completed builtin and parameters before any LSP response", async () => {
|
||||
const support = await loadSupport(findLanguageDescription("Python", "demo.py")!);
|
||||
vi.useFakeTimers();
|
||||
|
||||
Reference in new issue
Заблокувати користувача