From 2413cc4b435d6a59646801435588d665029758e5 Mon Sep 17 00:00:00 2001 From: txdywmwddhxs <56292188+txdywmwddhxs@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:02:58 +0800 Subject: [PATCH] fix(editor): preserve C++ namespace colors for library symbols (#6133) clangd marks std and other standard-library namespaces with the defaultLibrary modifier, so the generic builtin (Predefined symbol) class overrode the Namespace colour. A C++ semantic hook drops the builtin class for namespace tokens only; other symbol kinds, modifiers and languages keep their styling. Fixes #6123 Co-authored-by: txdywmwddhxs <56292188+txdywmwddhxs@users.noreply.github.com> --- e2e/cppNamespaceStyle.fixture.ts | 35 +++++++++++++++++++++++ e2e/cppNamespaceStyle.html | 9 ++++++ e2e/cppNamespaceStyle.spec.ts | 12 ++++++++ src/editor/lsp/semantic/cpp/index.test.ts | 35 +++++++++++++++++++++++ src/editor/lsp/semantic/cpp/index.ts | 10 +++++++ src/editor/lsp/semantic/index.ts | 1 + 6 files changed, 102 insertions(+) create mode 100644 e2e/cppNamespaceStyle.fixture.ts create mode 100644 e2e/cppNamespaceStyle.html create mode 100644 e2e/cppNamespaceStyle.spec.ts create mode 100644 src/editor/lsp/semantic/cpp/index.test.ts create mode 100644 src/editor/lsp/semantic/cpp/index.ts diff --git a/e2e/cppNamespaceStyle.fixture.ts b/e2e/cppNamespaceStyle.fixture.ts new file mode 100644 index 0000000000..0a18d7a056 --- /dev/null +++ b/e2e/cppNamespaceStyle.fixture.ts @@ -0,0 +1,35 @@ +import { EditorView } from "@codemirror/view"; +import { commands } from "../src/platform/ipc"; +import { setInvokeRoute } from "../src/platform/tauriCore"; +import { schemeCss } from "../src/platform/colorScheme"; +import { lspDoc } from "../src/editor/lsp/lsp"; +import { semanticExtension } from "../src/editor/lsp/semantic"; +import { lspTheme } from "../src/editor/lsp/theme"; + +setInvokeRoute(() => null); +commands.lspSemanticTokens = async () => ({ + status: "ok", + data: { + tokenTypes: ["namespace"], + tokenModifiers: ["defaultLibrary", "globalScope"], + data: [0, 0, 7, 0, 2, 1, 0, 3, 0, 3], + }, +}); +const mode = new URLSearchParams(location.search).has("light") ? "light" : "dark"; +const style = document.createElement("style"); +style.textContent = schemeCss(mode, { + [mode]: { + packageReference: { foreground: "#446688" }, + predefined: { foreground: "#cc5500" }, + }, +}); +document.head.append(style); +new EditorView({ + parent: document.body, + doc: "example::hello();\nstd::cout << 1;", + extensions: [ + lspTheme, + lspDoc.of({ doc: 1, path: "demo.cpp", language: "cpp", uiMode: "C++", readOnly: false }), + semanticExtension(), + ], +}); diff --git a/e2e/cppNamespaceStyle.html b/e2e/cppNamespaceStyle.html new file mode 100644 index 0000000000..13696b2d6d --- /dev/null +++ b/e2e/cppNamespaceStyle.html @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/e2e/cppNamespaceStyle.spec.ts b/e2e/cppNamespaceStyle.spec.ts new file mode 100644 index 0000000000..6d42bc7c36 --- /dev/null +++ b/e2e/cppNamespaceStyle.spec.ts @@ -0,0 +1,12 @@ +import { expect, test } from "@playwright/test"; + +for (const mode of ["dark", "light"]) { + test(`C++ standard and user namespaces share the namespace colour (${mode})`, async ({ page }) => { + await page.goto(`/e2e/cppNamespaceStyle.html${mode === "light" ? "?light" : ""}`); + const namespaces = page.locator(".cm-sem-namespace"); + await expect(namespaces).toHaveCount(2); + for (const namespace of await namespaces.all()) { + await expect(namespace).toHaveCSS("color", "rgb(68, 102, 136)"); + } + }); +} diff --git a/src/editor/lsp/semantic/cpp/index.test.ts b/src/editor/lsp/semantic/cpp/index.test.ts new file mode 100644 index 0000000000..f38ea3c113 --- /dev/null +++ b/src/editor/lsp/semantic/cpp/index.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from "vitest"; +import { Text } from "@codemirror/state"; +import { semanticRanges } from "../index"; + +const doc = Text.of(["std"]); +const token = (type: string, modifiers: string[], language = "cpp") => + semanticRanges( + doc, + { tokenTypes: [type], tokenModifiers: modifiers, data: [0, 0, 3, 0, (1 << modifiers.length) - 1] }, + false, + language, + )[0]?.className.split(" "); + +describe("C++ namespace semantic tokens", () => { + it.each(["cpp", "CPP"])("keeps standard-library namespaces in the namespace colour (%s)", (language) => { + expect(token("namespace", ["defaultLibrary", "globalScope"], language)).toEqual(["cm-sem-namespace"]); + expect(token("namespace", ["defaultLibrary"], language)).toEqual(token("namespace", [], language)); + }); + + it("preserves namespace declaration and deprecation styles", () => { + expect(token("namespace", ["defaultLibrary", "declaration", "deprecated"])).toEqual([ + "cm-sem-namespace", + "cm-sem-declaration", + "cm-sem-deprecated", + ]); + }); + + it.each(["class", "function", "variable"])("preserves builtin colouring for %s tokens", (type) => { + expect(token(type, ["defaultLibrary"])).toContain("cm-sem-builtin"); + }); + + it("leaves other languages' namespace colouring unchanged", () => { + expect(token("namespace", ["defaultLibrary"], "rust")).toEqual(["cm-sem-namespace", "cm-sem-builtin"]); + }); +}); diff --git a/src/editor/lsp/semantic/cpp/index.ts b/src/editor/lsp/semantic/cpp/index.ts new file mode 100644 index 0000000000..012b15aede --- /dev/null +++ b/src/editor/lsp/semantic/cpp/index.ts @@ -0,0 +1,10 @@ +import { registerLanguageSemantics } from "../languages"; + +registerLanguageSemantics("cpp", { + tokenClasses({ type, classes }) { + // clangd marks std and other library namespaces as defaultLibrary; keep their namespace colour. + if (type !== "namespace") return; + const builtin = classes.indexOf("cm-sem-builtin"); + if (builtin >= 0) classes.splice(builtin, 1); + }, +}); diff --git a/src/editor/lsp/semantic/index.ts b/src/editor/lsp/semantic/index.ts index 1ca686f420..22235c9e77 100644 --- a/src/editor/lsp/semantic/index.ts +++ b/src/editor/lsp/semantic/index.ts @@ -33,6 +33,7 @@ import "./php"; import "./go"; import "./java"; import "./javascript"; +import "./cpp"; const semanticBackoff = featureBackoff();