feat(go): distinct colours for variables, calls, packages and types (#7561)

gopls semantic roles for variables, package qualifiers, calls and types map to distinct colours in every bundled theme and are exposed in the Go colour scheme settings.

Fixes #7487
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-02 23:23:29 +08:00
1 джерело ba5eb92c70
коміт 37f2dd3ced
7 files changed
+62 -20

No files matched your search

+3
Переглянути файл
@@ -26,6 +26,9 @@
"--syn-variable": "#d9dfe8",
"--syn-global": "#d9dfe8",
"--syn-namespace": "var(--fg-editor)",
"--syn-go-package": "#9ccc7a",
"--syn-go-type": "#8fd4c9",
"--syn-go-call": "#e5be91",
"--syn-param": "#d9dfe8",
"--syn-property": "#b9a3f0",
"--syn-constant": "#e58fa3",
+4 -1
Переглянути файл
@@ -333,7 +333,7 @@ a token.
<!-- BEGIN GENERATED TOKEN REFERENCE: pnpm gen:theme-docs -->
275 themeable tokens: 128 in the UI theme file, 147 in the editor colour scheme file.
278 themeable tokens: 128 in the UI theme file, 150 in the editor colour scheme file.
### Surfaces (UI theme)
@@ -448,6 +448,9 @@ a token.
| `--syn-variable` | `#bcbec4` | `#080808` |
| `--syn-global` | `var(--syn-variable)` | `var(--syn-variable)` |
| `--syn-namespace` | `var(--fg-editor)` | `var(--fg-editor)` |
| `--syn-go-package` | `#a9bb75` | `#526a25` |
| `--syn-go-type` | `#6bb7c8` | `#007083` |
| `--syn-go-call` | `#bda77a` | `#7d5d26` |
| `--syn-param` | `#bcbec4` | `#080808` |
| `--syn-property` | `#c77dbb` | `#871094` |
| `--syn-constant` | `#c77dbb` | `#871094` |
+14 -13
Переглянути файл
@@ -41,19 +41,20 @@ describe("Go semantic tokens", () => {
);
});
it("retains package, field and function roles from the common decoder", () => {
for (const [type, expected] of [
["namespace", "cm-sem-namespace"],
["property", "cm-sem-property"],
["function", "cm-sem-function"],
["type", "cm-sem-type"],
["typeParameter", "cm-sem-typeParameter"],
]) {
expect(
semanticRanges(doc, { tokenTypes: [type!], tokenModifiers: [], data: [0, 0, 4, 0, 0] }, false, "go")[0]
?.className,
).toBe(expected);
}
it.each([
["namespace", ["defaultLibrary"], "cm-sem-namespace cm-sem-builtin cm-sem-go-package"],
["property", [], "cm-sem-property"],
["function", [], "cm-sem-function cm-sem-go-call"],
["method", [], "cm-sem-function cm-sem-go-call"],
["function", ["definition"], "cm-sem-function cm-sem-declaration"],
["type", [], "cm-sem-type cm-sem-go-type"],
["struct", [], "cm-sem-type cm-sem-go-type"],
["typeParameter", [], "cm-sem-typeParameter cm-sem-go-type"],
["type", ["defaultLibrary"], "cm-sem-type cm-sem-builtin"],
["variable", [], "cm-sem-variable"],
])("colors %s with modifiers %j", (type, modifiers, expected) => {
const tokens = { tokenTypes: [type], tokenModifiers: modifiers, data: [0, 0, 4, 0, modifiers.length ? 1 : 0] };
expect(semanticRanges(doc, tokens, false, "go")[0]?.className).toBe(expected);
});
it("leaves branch labels and missing server tokens to syntax highlighting", () => {
+7
Переглянути файл
@@ -2,9 +2,16 @@ import { registerLanguageSemantics, type LanguageSemantics } from "../languages"
const goSemantics: LanguageSemantics = {
tokenClasses({ type, modifiers, classes }) {
const builtin = modifiers.includes("defaultLibrary");
if (type === "namespace") classes.push("cm-sem-go-package");
if (!builtin && (classes.includes("cm-sem-type") || type === "typeParameter")) classes.push("cm-sem-go-type");
if (!builtin && classes.includes("cm-sem-function") && !classes.includes("cm-sem-declaration"))
classes.push("cm-sem-go-call");
if (type === "variable") {
if (modifiers.includes("readonly") && !classes.includes("cm-sem-constant")) classes.push("cm-sem-constant");
else if (modifiers.includes("static") && !classes.includes("cm-sem-constant")) classes.push("cm-sem-global");
else if (!builtin && !classes.includes("cm-sem-constant") && !classes.includes("cm-sem-global"))
classes.push("cm-sem-variable");
}
},
isLocal: ({ type, modifiers }) => type === "parameter" || (type === "variable" && !modifiers.includes("readonly")),
+3
Переглянути файл
@@ -669,6 +669,9 @@ export const lspTheme = EditorView.theme({
".cm-sem-builtin:where(:not(.cm-sem-function))": semanticStyle("builtin", "variable"),
".cm-sem-builtin:where(.cm-sem-type)": inheritedStyle("builtin-type", "builtin"),
".cm-sem-builtin.cm-sem-python-builtin": semanticStyle("python-builtin", "builtin"),
".cm-sem-go-package": semanticStyle("go-package", "namespace"),
".cm-sem-go-type": semanticStyle("go-type", "type"),
".cm-sem-go-call": semanticStyle("go-call", "function-call"),
".cm-sem-static": { fontStyle: "var(--syn-static-italic, italic)" },
".cm-sem-property.cm-sem-static, .cm-sem-property.cm-sem-static *": {
color: "var(--syn-static-property, var(--syn-property))",
+16 -6
Переглянути файл
@@ -460,18 +460,28 @@ const SCHEME_EDITORS: Record<string, readonly string[]> = {
"C/C++": ["C", "C++"],
};
const GO_SCHEME_TOKENS = new Map([
["packageReference", "go-package"],
["className", "go-type"],
["functionCall", "go-call"],
]);
export const ATTRIBUTES: readonly AttributeDef[] = [
...BASE_ATTRIBUTES,
...Object.entries(SCHEME_LANGUAGES).flatMap(([id, language]) =>
BASE_ATTRIBUTES.filter(
// WebStorm's page has a Decorator instead of Metadata.
(a) => a.path[0] === LD && !/^(semantic|bracket)\d$/.test(a.id) && !(language === JS && a.id === "metadata"),
).map((a) => ({
...a,
id: `${id}.${a.id}`,
path: [language, ...a.path.slice(1)],
language,
})),
).map((a) => {
const token = id === "go" ? GO_SCHEME_TOKENS.get(a.id) : undefined;
return {
...a,
...(token && { token, fg: `--syn-${token}` }),
id: `${id}.${a.id}`,
path: [language, ...a.path.slice(1)],
language,
};
}),
),
// WebStorm's JavaScript / TypeScript page: the whole `@Name` of a decorator, and module-level functions.
{ ...syn("javascript.decorator", "Decorator", [JS], "decorator"), language: JS },
+15
Переглянути файл
@@ -167,6 +167,9 @@ html[data-os="windows"] {
--syn-variable: #bcbec4;
--syn-global: var(--syn-variable);
--syn-namespace: var(--fg-editor);
--syn-go-package: #a9bb75;
--syn-go-type: #6bb7c8;
--syn-go-call: #bda77a;
--syn-param: #bcbec4;
--syn-property: #c77dbb;
--syn-constant: #c77dbb;
@@ -659,6 +662,9 @@ html[data-os="windows"] {
--syn-variable: #080808;
--syn-global: var(--syn-variable);
--syn-namespace: var(--fg-editor);
--syn-go-package: #526a25;
--syn-go-type: #007083;
--syn-go-call: #7d5d26;
--syn-param: #080808;
--syn-property: #871094;
--syn-constant: #871094;
@@ -965,6 +971,9 @@ html[data-os="windows"] {
--syn-variable: #e3e4e8;
--syn-global: var(--syn-variable);
--syn-namespace: var(--fg-editor);
--syn-go-package: #c7db96;
--syn-go-type: #6ddef3;
--syn-go-call: #ebbe83;
--syn-param: #e3e4e8;
--syn-property: #e0a0d6;
--syn-constant: #e0a0d6;
@@ -1362,6 +1371,9 @@ html[data-os="windows"] {
--syn-variable: var(--palette-variable);
--syn-global: var(--syn-variable);
--syn-namespace: var(--fg-editor);
--syn-go-package: #859900;
--syn-go-type: #2aa198;
--syn-go-call: #b58900;
--syn-param: var(--palette-fg);
--syn-property: var(--palette-property);
--syn-constant: var(--palette-number);
@@ -1455,6 +1467,9 @@ html[data-os="windows"] {
}
:root[data-color-theme="solarized-light"] {
--syn-go-package: #586600;
--syn-go-type: #006d66;
--syn-go-call: #805f00;
--palette-yellow: #b58900;
--palette-orange: #cb4b16;
--palette-red: #dc322f;