mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-19 00:10:00 +00:00
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.
This commit is contained in:
@@ -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<typeof styleTags>[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<typeof styleTags>[0] = { "?.": t.derefOperator, "this super": t.keyword };
|
||||
|
||||
@@ -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<string>(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"],
|
||||
|
||||
@@ -293,9 +293,11 @@ const CSS_TAGS: Parameters<typeof styleTags>[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<typeof styleTags>[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<string, Parameters<typeof styleTags>[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,
|
||||
|
||||
Reference in New Issue
Block a user