From dc3d2050cb2e96b14a460ec1dfd2fae0ef66fab1 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 15 Nov 2026 23:37:06 +0800 Subject: [PATCH] fix(editor): colour `this` and `super` as keywords in Java, JavaScript, TypeScript and C++ (#3564) The Lezer grammars tag `this` as `self`, which the colour scheme paints with Python's self-parameter colour, and @lezer/javascript tags `super` as an atom. JAVA_TAGS (also used by JSP), JS_TAGS and a new C++ entry restyle them as keywords. Python's `self` is unchanged. --- src/editor/core/cm/jsTags.ts | 7 +++++-- src/editor/core/cm/languages.test.ts | 25 +++++++++++++++++++++++++ src/editor/core/cm/languages.ts | 6 +++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/editor/core/cm/jsTags.ts b/src/editor/core/cm/jsTags.ts index 5601082d4..b041e0d40 100644 --- a/src/editor/core/cm/jsTags.ts +++ b/src/editor/core/cm/jsTags.ts @@ -1,4 +1,7 @@ import { tags as t, type styleTags } from "@lezer/highlight"; -/** @lezer/javascript styles `.` as a deref operator, but not `?.`. */ -export const JS_TAGS: Parameters[0] = { "?.": t.derefOperator }; +/** + * @lezer/javascript styles `.` as a deref operator, but not `?.`; it tags `this` as `self` (Python's + * `self` colour) and `super` as an atom, which WebStorm colours as keywords. + */ +export const JS_TAGS: Parameters[0] = { "?.": t.derefOperator, "this super": t.keyword }; diff --git a/src/editor/core/cm/languages.test.ts b/src/editor/core/cm/languages.test.ts index 33cf41cc2..b9768fe43 100644 --- a/src/editor/core/cm/languages.test.ts +++ b/src/editor/core/cm/languages.test.ts @@ -270,6 +270,31 @@ describe("language modes", () => { } }); + it("colours `this` and `super` as keywords in Java, JavaScript, TypeScript and C++", async () => { + const highlighter = tagHighlighter([ + { tag: t.keyword, class: "keyword" }, + { tag: t.self, class: "self" }, + { tag: t.atom, class: "atom" }, + ]); + for (const [mode, path, doc, expected] of [ + ["Java", "/p/A.java", "class A extends B { A() { super(); this.a = 1; } }", ["super", "this"]], + ["JavaScript", "/p/a.js", "class A extends B { m() { super.m(); this.a = 1; } }", ["super", "this"]], + ["TypeScript", "/p/a.ts", "class A extends B { m() { super.m(); this.a = 1; } }", ["super", "this"]], + ["C++", "/p/a.cpp", "void A::m() { this->a = 1; }", ["this"]], + ] as const) { + const support = await loadSupport(findLanguageDescription(mode, path)!); + const found: Array<[string, string]> = []; + highlightTree(support!.language.parser.parse(doc), highlighter, (from, to, style) => + found.push([doc.slice(from, to), style]), + ); + const words = new Set(expected); + expect( + found.filter(([text]) => words.has(text)), + mode, + ).toEqual(expected.map((word) => [word, "keyword"])); + } + }); + it("recognizes go.mod and go.work files by mode and filename", async () => { for (const [mode, path] of [ ["GoMod", "/project/go.mod"], diff --git a/src/editor/core/cm/languages.ts b/src/editor/core/cm/languages.ts index bd73cd992..0f07be3f2 100644 --- a/src/editor/core/cm/languages.ts +++ b/src/editor/core/cm/languages.ts @@ -293,9 +293,11 @@ const CSS_TAGS: Parameters[0] = { "ClassName IdName PseudoClassName ColorLiteral CallTag": t.tagName, }; -// @lezer/java tags annotations as plain identifiers. +// @lezer/java tags annotations as plain identifiers, and `this` as `self` (Python's `self` colour); +// IntelliJ colours `this` as a keyword. export const JAVA_TAGS: Parameters[0] = { "MarkerAnnotation/Identifier Annotation/Identifier MarkerAnnotation Annotation": t.annotation, + this: t.keyword, }; /** Highlighting the stock grammars lack or colour unlike IntelliJ. */ @@ -306,6 +308,8 @@ const EXTRA_TAGS: Record[0]> = { JSX: JS_TAGS, TSX: JS_TAGS, Java: JAVA_TAGS, + // CLion colours `this` as a keyword. + "C++": { this: t.keyword }, PHP: { "; ,": t.separator, "ClassDeclaration/Name BaseClause/Name InterfaceClause/Name": t.className,