From 923325aeae9c5caa66ee6f256d4b95ae6e431fed Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Wed, 18 Nov 2026 17:37:49 +0900 Subject: [PATCH] feat(color-scheme): foreground for Identifier under caret (#4135) Identifier under caret and Identifier under caret (write) can now set a Foreground, as in IDEA; imported .icls schemes carry it too. The highlight mark wraps the syntax and semantic token spans, so the colour is applied with the inherited -webkit-text-fill-color: unset, the declaration drops and tokens keep their syntax colours; find matches, selection, links, inlay hints and fold placeholders keep their own colours. Multi-line highlights colour only the highlighted text, not the indent. Closes #4109 Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com> --- docs/en/plugins/guides/themes.mdx | 5 +- docs/zh/plugins/guides/themes.mdx | 5 +- .../themes/harbor-night.scheme.json | 2 + plugins/themes.md | 7 ++- scripts/theme/theme.test.ts | 17 +++++++ src/editor/core/cm/find.test.ts | 46 ++++++++++++++++++- src/editor/core/cm/selectionMarks.ts | 1 + src/editor/core/cm/theme.ts | 10 ++++ src/editor/lsp/navigation.ts | 8 +++- src/editor/lsp/theme.ts | 7 +++ src/editor/lsp/usageHighlight.test.ts | 20 ++++++++ src/features/plugins/intellijTheme.ts | 10 +++- .../settings/__tests__/contrast.test.ts | 4 +- src/features/vcs/cm.ts | 3 +- src/platform/colorScheme.test.ts | 15 ++++++ src/platform/colorScheme.ts | 9 +++- src/platform/icls.test.ts | 27 +++++++++++ src/platform/icls.ts | 1 + src/styles/tokens.css | 4 ++ 19 files changed, 189 insertions(+), 12 deletions(-) diff --git a/docs/en/plugins/guides/themes.mdx b/docs/en/plugins/guides/themes.mdx index fef39524b..79679599b 100644 --- a/docs/en/plugins/guides/themes.mdx +++ b/docs/en/plugins/guides/themes.mdx @@ -238,7 +238,8 @@ The full table is `UI_KEYS` in | `--syn-tag`, `--syn-attribute`, `--syn-todo`, `--syn-invalid` | `DEFAULT_TAG`, `DEFAULT_ATTRIBUTE`, `TODO_DEFAULT_ATTRIBUTES`, `BAD_CHARACTER` | | `--syn-rainbow-1` … `--syn-rainbow-5` (Semantic highlighting) | `RAINBOW_COLOR1` … `RAINBOW_COLOR5` | | `--wave-error`, `--wave-warning` | `ERRORS_ATTRIBUTES`, `WARNING_ATTRIBUTES` (effect colour) | -| `--bg-matched-brace`, `--bg-identifier-under-caret` | `MATCHED_BRACE_ATTRIBUTES`, `IDENTIFIER_UNDER_CARET_ATTRIBUTES` | +| `--bg-matched-brace`, `--bg-identifier-under-caret`, `--fg-identifier-under-caret` | `MATCHED_BRACE_ATTRIBUTES`, `IDENTIFIER_UNDER_CARET_ATTRIBUTES` | +| `--bg-identifier-under-caret-write`, `--fg-identifier-under-caret-write` | `WRITE_IDENTIFIER_UNDER_CARET_ATTRIBUTES` | | `--bg-text-search-result`, `--bg-search-result` | `TEXT_SEARCH_RESULT_ATTRIBUTES`, `SEARCH_RESULT_ATTRIBUTES` | | `--syn-self`, `--syn-keyword-arg`, `--syn-docstring`, `--syn-predefined` | `PY.SELF_PARAMETER`, `PY.KEYWORD_ARGUMENT`, `PY.DOC_COMMENT`, `PY.BUILTIN_NAME` | | `--ansi-*` | `CONSOLE_*_OUTPUT` (`CONSOLE_DARKGRAY_OUTPUT` is bright black, `CONSOLE_GRAY_OUTPUT` white) | @@ -519,6 +520,8 @@ conventionally go in the theme file, groups marked _editor scheme_ in the scheme | `--fg-unused` | `#6f737a` | `#8c8c8c` | | `--bg-identifier-under-caret` | `#373b39` | `#edebfc` | | `--bg-identifier-under-caret-write` | `#402f33` | `#fce8f4` | +| `--fg-identifier-under-caret` | `inherit` | `inherit` | +| `--fg-identifier-under-caret-write` | `inherit` | `inherit` | | `--fg-inlay` | `#868a91` | `#73767c` | | `--bg-inlay` | `#393b40` | `#00000012` | | `--wave-error` | `#fa6675` | `#f50000` | diff --git a/docs/zh/plugins/guides/themes.mdx b/docs/zh/plugins/guides/themes.mdx index 5cd00d574..20c1783c3 100644 --- a/docs/zh/plugins/guides/themes.mdx +++ b/docs/zh/plugins/guides/themes.mdx @@ -211,7 +211,8 @@ pnpm theme:import-intellij path/to/MyTheme.theme.json path/to/MyTheme.icls --out | `--syn-tag`, `--syn-attribute`, `--syn-todo`, `--syn-invalid` | `DEFAULT_TAG`, `DEFAULT_ATTRIBUTE`, `TODO_DEFAULT_ATTRIBUTES`, `BAD_CHARACTER` | | `--syn-rainbow-1` … `--syn-rainbow-5`(语义高亮) | `RAINBOW_COLOR1` … `RAINBOW_COLOR5` | | `--wave-error`, `--wave-warning` | `ERRORS_ATTRIBUTES`, `WARNING_ATTRIBUTES`(效果颜色) | -| `--bg-matched-brace`, `--bg-identifier-under-caret` | `MATCHED_BRACE_ATTRIBUTES`, `IDENTIFIER_UNDER_CARET_ATTRIBUTES` | +| `--bg-matched-brace`, `--bg-identifier-under-caret`, `--fg-identifier-under-caret` | `MATCHED_BRACE_ATTRIBUTES`, `IDENTIFIER_UNDER_CARET_ATTRIBUTES` | +| `--bg-identifier-under-caret-write`, `--fg-identifier-under-caret-write` | `WRITE_IDENTIFIER_UNDER_CARET_ATTRIBUTES` | | `--bg-text-search-result`, `--bg-search-result` | `TEXT_SEARCH_RESULT_ATTRIBUTES`, `SEARCH_RESULT_ATTRIBUTES` | | `--syn-self`, `--syn-keyword-arg`, `--syn-docstring`, `--syn-predefined` | `PY.SELF_PARAMETER`, `PY.KEYWORD_ARGUMENT`, `PY.DOC_COMMENT`, `PY.BUILTIN_NAME` | | `--ansi-*` | `CONSOLE_*_OUTPUT`(`CONSOLE_DARKGRAY_OUTPUT` 为亮黑,`CONSOLE_GRAY_OUTPUT` 为白色) | @@ -485,6 +486,8 @@ VS Code 主题(扩展中的 `themes/*.json`)包含 `type`(`dark` / `light` | `--fg-unused` | `#6f737a` | `#8c8c8c` | | `--bg-identifier-under-caret` | `#373b39` | `#edebfc` | | `--bg-identifier-under-caret-write` | `#402f33` | `#fce8f4` | +| `--fg-identifier-under-caret` | `inherit` | `inherit` | +| `--fg-identifier-under-caret-write` | `inherit` | `inherit` | | `--fg-inlay` | `#868a91` | `#73767c` | | `--bg-inlay` | `#393b40` | `#00000012` | | `--wave-error` | `#fa6675` | `#f50000` | diff --git a/plugins/official/harbor-night/themes/harbor-night.scheme.json b/plugins/official/harbor-night/themes/harbor-night.scheme.json index 9b0e06b5a..428261854 100644 --- a/plugins/official/harbor-night/themes/harbor-night.scheme.json +++ b/plugins/official/harbor-night/themes/harbor-night.scheme.json @@ -104,6 +104,8 @@ "--fg-deprecated": "#8c8f95", "--bg-identifier-under-caret": "#2e353f", "--bg-identifier-under-caret-write": "#402f33", + "--fg-identifier-under-caret": "inherit", + "--fg-identifier-under-caret-write": "inherit", "--fg-inlay": "#75869d", "--bg-inlay": "#303944", "--wave-typo": "#59a869", diff --git a/plugins/themes.md b/plugins/themes.md index fed0b5a5f..6229ae702 100644 --- a/plugins/themes.md +++ b/plugins/themes.md @@ -232,7 +232,8 @@ The full table is `UI_KEYS` in [`src/features/plugins/intellijTheme.ts`](../src/ | `--syn-tag`, `--syn-attribute`, `--syn-todo`, `--syn-invalid` | `DEFAULT_TAG`, `DEFAULT_ATTRIBUTE`, `TODO_DEFAULT_ATTRIBUTES`, `BAD_CHARACTER` | | `--syn-rainbow-1` … `--syn-rainbow-5` (Semantic highlighting, rainbow indent guides) | `RAINBOW_COLOR1` … `RAINBOW_COLOR5` | | `--wave-error`, `--wave-warning` | `ERRORS_ATTRIBUTES`, `WARNING_ATTRIBUTES` (effect colour) | -| `--bg-matched-brace`, `--bg-identifier-under-caret` | `MATCHED_BRACE_ATTRIBUTES`, `IDENTIFIER_UNDER_CARET_ATTRIBUTES` | +| `--bg-matched-brace`, `--bg-identifier-under-caret`, `--fg-identifier-under-caret` | `MATCHED_BRACE_ATTRIBUTES`, `IDENTIFIER_UNDER_CARET_ATTRIBUTES` | +| `--bg-identifier-under-caret-write`, `--fg-identifier-under-caret-write` | `WRITE_IDENTIFIER_UNDER_CARET_ATTRIBUTES` | | `--bg-text-search-result`, `--bg-search-result` | `TEXT_SEARCH_RESULT_ATTRIBUTES`, `SEARCH_RESULT_ATTRIBUTES` | | `--syn-self`, `--syn-keyword-arg`, `--syn-docstring`, `--syn-predefined` | `PY.SELF_PARAMETER`, `PY.KEYWORD_ARGUMENT`, `PY.DOC_COMMENT`, `PY.BUILTIN_NAME` | | `--ansi-*` | `CONSOLE_*_OUTPUT` (`CONSOLE_DARKGRAY_OUTPUT` is bright black, `CONSOLE_GRAY_OUTPUT` white) | @@ -332,7 +333,7 @@ a token. -269 themeable tokens: 125 in the UI theme file, 144 in the editor colour scheme file. +271 themeable tokens: 125 in the UI theme file, 146 in the editor colour scheme file. ### Surfaces (UI theme) @@ -528,6 +529,8 @@ a token. | `--fg-deprecated` | `#8c8f95` | `#6c707e` | | `--bg-identifier-under-caret` | `#373b39` | `#edebfc` | | `--bg-identifier-under-caret-write` | `#402f33` | `#fce8f4` | +| `--fg-identifier-under-caret` | `inherit` | `inherit` | +| `--fg-identifier-under-caret-write` | `inherit` | `inherit` | | `--fg-inlay` | `#868a91` | `#73767c` | | `--bg-inlay` | `#393b40` | `#00000012` | | `--wave-error` | `#fa6675` | `#f50000` | diff --git a/scripts/theme/theme.test.ts b/scripts/theme/theme.test.ts index 03653e9a2..67ee3b388 100644 --- a/scripts/theme/theme.test.ts +++ b/scripts/theme/theme.test.ts @@ -170,6 +170,23 @@ describe("IntelliJ theme import", () => { }); }); + it("maps the caret identifier foregrounds (#4109)", () => { + const s = convertIcls(` + + + `); + expect(s.tokens).toMatchObject({ + "--fg-identifier-under-caret": "#aa0000", + "--bg-identifier-under-caret": "#220000", + "--fg-identifier-under-caret-write": "#00aa00", + "--bg-identifier-under-caret-write": "#002200", + }); + }); + it("only targets tokens that exist in tokens.css", () => { const declared = new Set([...css.matchAll(/(--[a-z0-9-]+)\s*:/g)].map((m) => m[1]!)); // Read with a fallback by the editor (no tokens.css default), still themeable. diff --git a/src/editor/core/cm/find.test.ts b/src/editor/core/cm/find.test.ts index 5881082a4..0f5be27a0 100644 --- a/src/editor/core/cm/find.test.ts +++ b/src/editor/core/cm/find.test.ts @@ -1,10 +1,11 @@ import { afterEach, describe, expect, it } from "vitest"; import { EditorSelection, EditorState, Prec } from "@codemirror/state"; -import { Decoration, EditorView } from "@codemirror/view"; +import { Decoration, EditorView, WidgetType } from "@codemirror/view"; import { setSearchQuery } from "@codemirror/search"; import { StreamLanguage } from "@codemirror/language"; import { literalKindAt } from "./literals"; import { selectionMarks } from "./selectionMarks"; +import { findStyles, markWidgetStyles } from "./theme"; import { countMatches, findResultLines, @@ -41,6 +42,49 @@ function make(doc: string, o: Partial, scope?: { from: number; to: const sel = (v: EditorView) => [v.state.selection.main.from, v.state.selection.main.to]; +class TextWidget extends WidgetType { + toDOM() { + const el = document.createElement("span"); + el.className = "widget"; + el.textContent = "hint"; + return el; + } +} + +describe("widgets inside find matches", () => { + it.each([ + ["before the cursor side", Decoration.widget({ widget: new TextWidget(), side: -1 }).range(3)], + ["after the cursor side", Decoration.widget({ widget: new TextWidget(), side: 1 }).range(3)], + ["replacing text", Decoration.replace({ widget: new TextWidget() }).range(2, 4)], + ])("keep their own colour %s", (_name, widget) => { + const view = new EditorView({ + parent: document.body, + doc: "Session", + extensions: [ + // At the top precedence, as only then does the match wrap the widget. + Prec.highest(EditorView.decorations.of(Decoration.set(widget))), + findExtension(), + ], + }); + views.push(view); + view.dispatch({ + effects: [ + setFindOpen.of(true), + setSearchQuery.of( + makeQuery({ search: "Session", caseSensitive: true, wholeWord: false, regexp: false, replace: "" }), + ), + ], + }); + const el = view.contentDOM.querySelector(".cm-openidea-findMatch .widget")!; + // Browsers reflect this property to the attribute; jsdom does not. + el.setAttribute("contenteditable", el.contentEditable); + expect(el.matches(Object.keys(markWidgetStyles)[0]!)).toBe(true); + expect(Object.values(markWidgetStyles)[0]!.WebkitTextFillColor).toBe("currentcolor"); + // A text fill of `--fg-search-match` here would reach every widget in the match. + expect(findStyles[".cm-openidea-findMatch"].WebkitTextFillColor).toBe("currentcolor"); + }); +}); + describe("find backgrounds", () => { it.each([ ["semantic", Prec.highest], diff --git a/src/editor/core/cm/selectionMarks.ts b/src/editor/core/cm/selectionMarks.ts index 660335628..02e865a3f 100644 --- a/src/editor/core/cm/selectionMarks.ts +++ b/src/editor/core/cm/selectionMarks.ts @@ -46,6 +46,7 @@ const theme = EditorView.baseTheme({ boxDecorationBreak: "clone", // Unset (the default `inherit`): the declaration drops and the syntax colours show through. color: "var(--fg-editor-selection)", + WebkitTextFillColor: "var(--fg-editor-selection)", }, "&.cm-focused .cm-openidea-selected": { "--openidea-selected-bg": "var(--editor-selection, var(--bg-editor-selection))", diff --git a/src/editor/core/cm/theme.ts b/src/editor/core/cm/theme.ts index 3f4bd67d9..fb5639ed3 100644 --- a/src/editor/core/cm/theme.ts +++ b/src/editor/core/cm/theme.ts @@ -24,6 +24,8 @@ export const findStyles = { ".cm-openidea-findMatch": { backgroundColor: "var(--editor-search)", color: "var(--fg-search-match, inherit)", + // Its own colour, over the text fill of an identifier highlight around the match. + WebkitTextFillColor: "currentcolor", borderRadius: "2px", }, ".cm-openidea-findExcluded": { @@ -40,6 +42,11 @@ export const findStyles = { }, }; +/** Marks colour text with an inherited text fill; widgets inside them keep their own colours. */ +export const markWidgetStyles = { + '.cm-content [contenteditable="false"]': { WebkitTextFillColor: "currentcolor" }, +}; + /** The caret and Settings › Editor › General › Appearance caret options (`caretExtension`), also for viewers. */ export const caretStyles = { ".cm-cursor, .cm-dropCursor": { @@ -219,6 +226,8 @@ export const openideaTheme = EditorView.theme({ ".cm-selectionMatch": { backgroundColor: "var(--bg-identifier-under-caret, color-mix(in srgb, var(--fg) 9%, transparent))", }, + // IDEA paints these with IDENTIFIER_UNDER_CARET_ATTRIBUTES, except the selection itself. + ".cm-selectionMatch:not(.cm-selectionMatch-main)": { WebkitTextFillColor: "var(--fg-identifier-under-caret)" }, // IDEA's MATCHED_BRACE_ATTRIBUTES (bold in dark schemes only) and MATCHED_TAG_NAME (never bold). ".cm-matchingBracket, &.cm-focused .cm-matchingBracket": { backgroundColor: "var(--editor-brace)", @@ -343,6 +352,7 @@ export const openideaTheme = EditorView.theme({ // Find in file (cm/find.ts) ...findStyles, + ...markWidgetStyles, // Find in Files preview matches (search/find/parts.tsx) ".cm-openidea-searchResult": { backgroundColor: "var(--bg-search-result)" }, diff --git a/src/editor/lsp/navigation.ts b/src/editor/lsp/navigation.ts index 586a19d3c..18c9a9a51 100644 --- a/src/editor/lsp/navigation.ts +++ b/src/editor/lsp/navigation.ts @@ -1240,6 +1240,12 @@ const marks = { read: Decoration.mark({ class: KIND_CLASS.read }), write: Decoration.mark({ class: KIND_CLASS.write }), }; +/** The text a multi-line highlight paints with line backgrounds: a line class would also colour its indent. */ +const lineText = { + text: Decoration.mark({ class: "cm-lsp-occurrence-text" }), + read: Decoration.mark({ class: "cm-lsp-occurrence-text" }), + write: Decoration.mark({ class: "cm-lsp-occurrence-text cm-lsp-occurrence-text-write" }), +}; const fullLines = { text: Decoration.line({ class: KIND_CLASS.text }), read: Decoration.line({ class: KIND_CLASS.read }), @@ -1266,7 +1272,7 @@ function highlightDeco(doc: Text, tabSize: number, items: readonly HighlightItem class: i.kind === "write" ? "cm-lsp-occurrence-tail cm-lsp-occurrence-tail-write" : "cm-lsp-occurrence-tail", attributes: { style: `--lsp-occurrence-from: calc(${LINE_INSET}px + ${col}ch)` }, }); - out.push(tail.range(first.from)); + out.push(tail.range(first.from), lineText[i.kind].range(i.from, last.from)); for (let n = first.number + 1; n < last.number; n++) out.push(fullLines[i.kind].range(doc.line(n).from)); if (i.to > last.from) out.push(marks[i.kind].range(last.from, i.to)); } diff --git a/src/editor/lsp/theme.ts b/src/editor/lsp/theme.ts index deea34805..5cd4270d0 100644 --- a/src/editor/lsp/theme.ts +++ b/src/editor/lsp/theme.ts @@ -314,6 +314,7 @@ export const lspTheme = EditorView.theme({ // --- navigation --------------------------------------------------------- ".cm-lsp-link": { color: "var(--link)", + WebkitTextFillColor: "currentcolor", textDecoration: "underline", textUnderlineOffset: "2px", cursor: "pointer", @@ -325,6 +326,12 @@ export const lspTheme = EditorView.theme({ "--lsp-occurrence-bg": "var(--bg-identifier-under-caret-write, color-mix(in srgb, var(--danger) 12%, transparent))", }, ".cm-lsp-occurrence": { backgroundColor: "var(--lsp-occurrence-bg)" }, + // Text fill, not `color`: it reaches the syntax and semantic spans nested inside, and with the + // variable unset the declaration drops, so they keep their own colours. + "span.cm-lsp-occurrence, .cm-lsp-occurrence-text": { WebkitTextFillColor: "var(--fg-identifier-under-caret)" }, + "span.cm-lsp-occurrence-write, .cm-lsp-occurrence-text-write": { + WebkitTextFillColor: "var(--fg-identifier-under-caret-write)", + }, // First line of a highlight over several lines: from its start column to the right edge, // over the line's own background (the caret row's). ".cm-lsp-occurrence-tail": { diff --git a/src/editor/lsp/usageHighlight.test.ts b/src/editor/lsp/usageHighlight.test.ts index e0fac91ae..a42471361 100644 --- a/src/editor/lsp/usageHighlight.test.ts +++ b/src/editor/lsp/usageHighlight.test.ts @@ -72,6 +72,26 @@ describe("highlights of the element at the caret", () => { expect(lines(v)[4]!.className).not.toContain("cm-lsp-occurrence"); }); + it.each([ + ["text", ""], + ["write", "-write"], + ] as const)( + "marks exactly the text of a %s highlight over several lines for its foreground", + async (kind, suffix) => { + cmd.lspDocumentHighlights.mockImplementation(() => ok([{ range: range(1, 4, 3, 13), kind }])); + const v = editor(at(1, 4)); + highlightUsages(v); + await vi.waitFor(() => expect(v.contentDOM.querySelector(".cm-lsp-occurrence-text")).not.toBeNull()); + const text = (line: number) => + [...lines(v)[line]!.querySelectorAll(`.cm-lsp-occurrence-text${suffix}`)].map((e) => e.textContent).join(""); + // Not the indent before the start column; the last line is the highlight's own mark. + expect(text(1)).toBe("return a"); + expect(text(2)).toBe(" .b()"); + expect(text(3)).toBe(""); + expect(lines(v)[3]!.querySelector(`span.cm-lsp-occurrence${suffix}`)?.textContent).toBe(" .c();"); + }, + ); + it("keeps a single-line highlight on its text", async () => { cmd.lspDocumentHighlights.mockImplementation(() => ok([{ range: range(6, 11, 6, 12), kind: "read" }])); const v = editor(at(6, 11)); diff --git a/src/features/plugins/intellijTheme.ts b/src/features/plugins/intellijTheme.ts index 385974b9d..67eeddd32 100644 --- a/src/features/plugins/intellijTheme.ts +++ b/src/features/plugins/intellijTheme.ts @@ -180,8 +180,14 @@ export const ICLS_ATTRIBUTES: Record> = { MATCHED_BRACE_ATTRIBUTES: [["BACKGROUND", "--bg-matched-brace"]], SEARCH_RESULT_ATTRIBUTES: [["BACKGROUND", "--bg-search-result"]], TEXT_SEARCH_RESULT_ATTRIBUTES: [["BACKGROUND", "--bg-text-search-result"]], - IDENTIFIER_UNDER_CARET_ATTRIBUTES: [["BACKGROUND", "--bg-identifier-under-caret"]], - WRITE_IDENTIFIER_UNDER_CARET_ATTRIBUTES: [["BACKGROUND", "--bg-identifier-under-caret-write"]], + IDENTIFIER_UNDER_CARET_ATTRIBUTES: [ + ["FOREGROUND", "--fg-identifier-under-caret"], + ["BACKGROUND", "--bg-identifier-under-caret"], + ], + WRITE_IDENTIFIER_UNDER_CARET_ATTRIBUTES: [ + ["FOREGROUND", "--fg-identifier-under-caret-write"], + ["BACKGROUND", "--bg-identifier-under-caret-write"], + ], FOLDED_TEXT_ATTRIBUTES: [ ["FOREGROUND", "--fg-fold-placeholder"], ["BACKGROUND", "--bg-fold-placeholder"], diff --git a/src/features/settings/__tests__/contrast.test.ts b/src/features/settings/__tests__/contrast.test.ts index 1e465aaf7..c05084bb1 100644 --- a/src/features/settings/__tests__/contrast.test.ts +++ b/src/features/settings/__tests__/contrast.test.ts @@ -122,10 +122,10 @@ describe("theme tokens", () => { * Tokens the palette themes (Solarized, One) take from their light / dark base * on purpose: colours the user assigns (project and data source colours), the * platform's caption close and macOS window buttons, the tint strength, and - * the selection foreground, whose base default preserves syntax colours. + * the selection and caret identifier foregrounds, whose base default preserves syntax colours. */ const INHERITED_FROM_BASE = - /^--(project-color-|db-color-|file-color-|caption-close-|fg-caption-close-|tint-strength$|file-tint-strength$|fg-editor-selection$)/; + /^--(project-color-|db-color-|file-color-|caption-close-|fg-caption-close-|tint-strength$|file-tint-strength$|fg-editor-selection$|fg-identifier-under-caret(-write)?$)/; describe("palette themes", () => { const ISLANDS = ["light", "dark", "dark-hc"]; diff --git a/src/features/vcs/cm.ts b/src/features/vcs/cm.ts index abecc974e..0c899394d 100644 --- a/src/features/vcs/cm.ts +++ b/src/features/vcs/cm.ts @@ -42,7 +42,7 @@ import { overviewLane } from "@/editor/core/cm/lanes"; import { LINE_INSET } from "@/editor/core/cm/lineInset"; import { caretExtension } from "@/editor/core/cm/prefs"; import { fontExtension, tooltipBounds } from "@/editor/core/cm/setup"; -import { caretStyles, findLocals, findStyles, overviewStyles } from "@/editor/core/cm/theme"; +import { caretStyles, findLocals, findStyles, markWidgetStyles, overviewStyles } from "@/editor/core/cm/theme"; import { editorPrefs } from "@/platform/editorSettings"; import { useProject } from "@/platform/project"; import { getDiffInsights, hasInnerFragments } from "./diffOptions"; @@ -413,6 +413,7 @@ export const viewerTheme = EditorView.theme( ".cm-panels": { backgroundColor: "var(--bg-elev)", color: "var(--fg)" }, ".cm-searchMatch": { backgroundColor: "color-mix(in srgb, var(--accent) 22%, transparent)" }, ...findStyles, + ...markWidgetStyles, }, { dark: true }, ); diff --git a/src/platform/colorScheme.test.ts b/src/platform/colorScheme.test.ts index 13b4b7168..2297a8b16 100644 --- a/src/platform/colorScheme.test.ts +++ b/src/platform/colorScheme.test.ts @@ -3,6 +3,7 @@ import { beforeAll, describe, expect, it } from "vitest"; import { ATTRIBUTES, CLEAR_COLOR, + SCHEMES, _setSchemeSource, overrideDeclarations, resolvedAttribute, @@ -221,6 +222,20 @@ describe("color schemes", () => { ); }); + it.each([ + ["identifierUnderCaret", "--fg-identifier-under-caret"], + ["identifierUnderCaretWrite", "--fg-identifier-under-caret-write"], + ])("lets %s set a foreground over the syntax colours (#4109)", (id, token) => { + expect(ATTRIBUTES.find((a) => a.id === id)?.fg).toBe(token); + // The `inherit` default is no colour: every built-in scheme keeps the syntax colours. + for (const scheme of SCHEMES) expect(resolvedAttribute(scheme.id, id).foreground).toBeUndefined(); + expect(schemeCss("dark", { dark: { [id]: { foreground: "#112233" } } })).toContain(`${token}: #112233 !important`); + // Unticked, the syntax colours show again rather than one flat default text colour. + expect(schemeCss("dark", { dark: { [id]: { foreground: CLEAR_COLOR } } })).toContain( + `${token}: inherit !important`, + ); + }); + // #3851: the editor showed the red-green colours while Color Scheme showed the plain ones. it("resolves the red-green variant of the bundled schemes only", () => { const rg = (id: Parameters[0], name: string) => schemeTokens(id, undefined, true).get(name); diff --git a/src/platform/colorScheme.ts b/src/platform/colorScheme.ts index 6d8714ef4..07a241efe 100644 --- a/src/platform/colorScheme.ts +++ b/src/platform/colorScheme.ts @@ -25,6 +25,8 @@ export interface AttributeDef { path: string[]; /** CSS variable of the foreground / background / effect colour, if editable. */ fg?: string; + /** `fg` paints over the syntax colours: without one, they show through. */ + fgOverlay?: true; bg?: string; effect?: string; /** Syntax token: bold / italic / background / effect come from `--syn--*`. */ @@ -112,12 +114,16 @@ const BASE_ATTRIBUTES: readonly AttributeDef[] = [ id: "identifierUnderCaret", name: "Identifier under caret", path: ["General", "Code"], + fg: "--fg-identifier-under-caret", + fgOverlay: true, bg: "--bg-identifier-under-caret", }, { id: "identifierUnderCaretWrite", name: "Identifier under caret (write)", path: ["General", "Code"], + fg: "--fg-identifier-under-caret-write", + fgOverlay: true, bg: "--bg-identifier-under-caret-write", }, { id: "inlay", name: "Inlay hints", path: ["General", "Code"], fg: "--fg-inlay", bg: "--bg-inlay" }, @@ -606,7 +612,8 @@ export function overrideDeclarations( if (!o) continue; if (setOrCleared(o.foreground) && attr.fg) { // No foreground means the default text colour, not invisible text. - const fg = o.foreground === CLEAR_COLOR && attr.id !== "text" ? "var(--fg-editor)" : o.foreground!; + const cleared = attr.fgOverlay ? "inherit" : attr.id === "text" ? CLEAR_COLOR : "var(--fg-editor)"; + const fg = o.foreground === CLEAR_COLOR ? cleared : o.foreground!; out.push([attr.fg, fg]); // Terminal prefers `--terminal-ansi-*`; keep it in sync with console `--ansi-*`. if (attr.fg.startsWith("--ansi-")) out.push([`--terminal-ansi-${attr.fg.slice("--ansi-".length)}`, fg]); diff --git a/src/platform/icls.test.ts b/src/platform/icls.test.ts index ad17409c5..532199a52 100644 --- a/src/platform/icls.test.ts +++ b/src/platform/icls.test.ts @@ -48,6 +48,33 @@ describe(".icls schemes", () => { expect(parseIcls(xml).scheme.attributes.selectionForeground).toEqual({ foreground: "#ffffff" }); }); + it("round trips both caret identifier attributes with their foregrounds (#4109)", () => { + const { scheme } = parseIcls(` + + + + `); + expect(scheme.attributes.identifierUnderCaret).toEqual({ foreground: "#aa0000", background: "#220000" }); + expect(scheme.attributes.identifierUnderCaretWrite).toEqual({ foreground: "#00aa00", background: "#002200" }); + const { id, customs } = importScheme({}, scheme); + const xml = exportIcls(id, customs); + expect(xml).toContain('name="WRITE_IDENTIFIER_UNDER_CARET_ATTRIBUTES"'); + expect(parseIcls(xml).scheme.attributes.identifierUnderCaretWrite).toEqual( + scheme.attributes.identifierUnderCaretWrite, + ); + }); + + it("still reads the write highlight from WRITE_SEARCH_RESULT_ATTRIBUTES alone", () => { + const { scheme } = parseIcls(` + + `); + expect(scheme.attributes.identifierUnderCaretWrite).toEqual({ background: "#111111" }); + }); + it("resolves explicit attribute inheritance independent of XML order and stops cycles", () => { const { scheme } = parseIcls(`