From 6dab294dbff1ee2a02652c0faa279d55b57efd11 Mon Sep 17 00:00:00 2001 From: Deng Liu Date: Wed, 25 Nov 2026 05:55:45 -0800 Subject: [PATCH] fix(editor): keep syntax and semantic backgrounds on their own row (#5787) Syntax and semantic token backgrounds no longer cover characters on neighboring lines when the font box is taller than the line height. Token fills now go through the same 1lh-capped inline background as Find and occurrence highlights, with the colour in --openidea-token-bg so semantic roles can override nested syntax fills; an unset fill paints nothing. The background size is a separate declaration, so WebKit without lh support still shows the fill. Co-authored-by: dengliu <5108773+dengliu@users.noreply.github.com> --- e2e/kotlinHighlight.fixture.ts | 12 +++++++--- e2e/kotlinHighlight.spec.ts | 28 +++++++++++++++++++++-- e2e/semanticStyle.spec.ts | 2 +- src/editor/core/cm/highlight.ts | 4 +++- src/editor/core/cm/inlineBg.ts | 14 ++++++++---- src/editor/core/cm/selectionMarks.test.ts | 6 ++--- src/editor/lsp/theme.ts | 26 +++++++++++++++------ 7 files changed, 70 insertions(+), 22 deletions(-) diff --git a/e2e/kotlinHighlight.fixture.ts b/e2e/kotlinHighlight.fixture.ts index 0ef2ad6aef..c1f30769d1 100644 --- a/e2e/kotlinHighlight.fixture.ts +++ b/e2e/kotlinHighlight.fixture.ts @@ -17,7 +17,7 @@ commands.lspSemanticTokens = async () => ({ data: { tokenTypes: ["operator", "parameter"], tokenModifiers: ["declaration", "modification"], - data: [0, 4, 5, 0, 1, 0, 10, 6, 1, 2], + data: [1, 4, 5, 0, 1, 1, 10, 6, 1, 2], }, }); @@ -35,7 +35,7 @@ document.body.append(host); const support = await loadSupport(findLanguageDescription("Kotlin", "Demo.kt")!); const view = new EditorView({ parent: host, - doc: "fun greet(prefix: String) {}", + doc: "// before\nfun greet(prefix: String) {}\n// after", extensions: [ support!, syntaxHighlightField, @@ -49,4 +49,10 @@ const view = new EditorView({ : []), ], }); -view.dispatch({ effects: setSyntaxHighlights.of({ kinds: ["function", "parameter"], data: [4, 9, 0, 10, 16, 1] }) }); +const start = view.state.doc.line(2).from; +view.dispatch({ + effects: setSyntaxHighlights.of({ + kinds: ["function", "parameter"], + data: [start + 4, start + 9, 0, start + 10, start + 16, 1], + }), +}); diff --git a/e2e/kotlinHighlight.spec.ts b/e2e/kotlinHighlight.spec.ts index 6bac8fe0f3..be1eb17022 100644 --- a/e2e/kotlinHighlight.spec.ts +++ b/e2e/kotlinHighlight.spec.ts @@ -1,5 +1,29 @@ import { expect, test } from "@playwright/test"; +for (const semantic of [false, true]) { + test(`${semantic ? "semantic" : "syntax"} token backgrounds paint only their own row`, async ({ page }) => { + await page.goto(`/e2e/kotlinHighlight.html${semantic ? "?semantic" : ""}`); + await page.addStyleTag({ content: ".cm-editor .cm-scroller { font: 18px/18px monospace; }" }); + const editor = page.locator(".cm-editor"); + const row = editor.locator(".cm-line", { hasText: "fun greet" }); + const token = semantic + ? row.locator(".cm-sem-function") + : row + .locator("span") + .filter({ hasText: /^greet$/ }) + .last(); + await expect(token).toHaveCSS("color", "rgb(85, 119, 153)"); + expect((await token.boundingBox())!.height).toBeGreaterThan((await row.boundingBox())!.height); + + const neighbors = () => editor.screenshot({ mask: [row] }); + const before = await neighbors(); + const highlighted = await row.screenshot(); + await page.addStyleTag({ content: ".cm-editor .cm-line span { --syn-function-bg: transparent !important; }" }); + expect((await row.screenshot()).equals(highlighted), "the token fill should be visible").toBe(false); + expect((await neighbors()).equals(before), "changing the fill must not repaint adjacent rows").toBe(true); + }); +} + test("Kotlin backend roles override lexer styles", async ({ page }) => { await page.goto("/e2e/kotlinHighlight.html"); const greet = page @@ -7,7 +31,7 @@ test("Kotlin backend roles override lexer styles", async ({ page }) => { .filter({ hasText: /^greet$/ }) .last(); await expect(greet).toHaveCSS("color", "rgb(85, 119, 153)"); - await expect(greet).toHaveCSS("background-color", "rgb(221, 238, 255)"); + await expect(greet).toHaveCSS("background-image", /rgb\(221, 238, 255\)/); await expect(greet).toHaveCSS("font-weight", "700"); await expect( page @@ -22,7 +46,7 @@ test("Kotlin semantic roles stay inside backend and lexer marks", async ({ page const declaration = page.locator(".cm-sem-function.cm-sem-declaration"); await expect(declaration).toHaveText("greet"); await expect(declaration).toHaveCSS("color", "rgb(85, 119, 153)"); - await expect(declaration).toHaveCSS("background-color", "rgb(221, 238, 255)"); + await expect(declaration).toHaveCSS("background-image", /rgb\(221, 238, 255\)/); await expect(declaration).toHaveCSS("font-weight", "700"); await expect(declaration.locator("span")).toHaveCount(0); const parameter = page.locator(".cm-sem-parameter.cm-sem-mutable"); diff --git a/e2e/semanticStyle.spec.ts b/e2e/semanticStyle.spec.ts index 46e2852c9b..9badce1acb 100644 --- a/e2e/semanticStyle.spec.ts +++ b/e2e/semanticStyle.spec.ts @@ -12,7 +12,7 @@ test("semantic roles honor all Color Scheme attributes and keep modifier styles" ] as const) { const role = page.locator(selector); await expect(role).toHaveCSS("color", color); - await expect(role).toHaveCSS("background-color", "rgb(221, 238, 255)"); + await expect(role).toHaveCSS("background-image", /rgb\(221, 238, 255\)/); } await expect(page.locator(".cm-sem-namespace")).toHaveCSS("font-style", "italic"); for (const selector of [".cm-sem-type", ".cm-sem-function", ".cm-sem-parameter", ".cm-sem-constant"]) { diff --git a/src/editor/core/cm/highlight.ts b/src/editor/core/cm/highlight.ts index eb6959b5e4..e9be93e554 100644 --- a/src/editor/core/cm/highlight.ts +++ b/src/editor/core/cm/highlight.ts @@ -5,6 +5,7 @@ */ import { HighlightStyle, type TagStyle } from "@codemirror/language"; import { Tag, tags as t } from "@lezer/highlight"; +import { inlineTokenBackground } from "./inlineBg"; /** Log warnings: the UI warning colour, since no syntax token reads as one in every scheme. */ export const logWarning = Tag.define("logWarning"); @@ -28,7 +29,8 @@ function token( color: defaults.color ? variable("", "inherit") : `var(--syn-${name})`, fontStyle: variable("-italic", defaults.italic ?? "inherit"), fontWeight: variable("-bold", defaults.bold ?? "inherit"), - backgroundColor: variable("-bg", "transparent"), + "--openidea-token-bg": variable("-bg", "initial"), + ...inlineTokenBackground, textDecoration: variable("-effect", defaults.effect ?? "none"), // WebKit takes a decoration with a style or colour only through the prefixed property. WebkitTextDecoration: variable("-effect", defaults.effect ?? "none"), diff --git a/src/editor/core/cm/inlineBg.ts b/src/editor/core/cm/inlineBg.ts index 310e3062e9..5bc5b787ac 100644 --- a/src/editor/core/cm/inlineBg.ts +++ b/src/editor/core/cm/inlineBg.ts @@ -1,22 +1,26 @@ /** * Paints an inline mark's background capped at `1lh` so a font taller than the line height * does not bleed into the rows above and below. When the inline box is shorter than `1lh`, - * its own border box clips the gradient. Safari before 16.4 has no `lh` and drops the - * shorthand, keeping the plain colour before it. + * its own border box clips the gradient. Keep the size separate so Safari before 16.4 + * ignores only the unsupported `lh` and still paints the fill. */ export function inlineBackground(color: string) { return { - backgroundColor: color, - background: `linear-gradient(${color}, ${color}) center / 100% 1lh no-repeat`, + background: `linear-gradient(${color}, ${color}) center no-repeat`, + backgroundSize: "100% 1lh", WebkitBoxDecorationBreak: "clone", boxDecorationBreak: "clone", }; } +// Semantic roles can override this colour on nested syntax tokens without repainting +// unrelated descendants (e.g. a selection). An unset colour produces no background image. +export const inlineTokenBackground = inlineBackground("var(--openidea-token-bg)"); + /** Fallback style for an inline mark with a 1px frame (`inlineFramedModern` caps it at `1lh`). */ export function inlineFramedFallback(fill: string, frame: string) { return { - backgroundColor: fill, + background: fill, outline: `1px solid ${frame}`, outlineOffset: "-1px", WebkitBoxDecorationBreak: "clone", diff --git a/src/editor/core/cm/selectionMarks.test.ts b/src/editor/core/cm/selectionMarks.test.ts index a3acebf89d..e2fe8de3a1 100644 --- a/src/editor/core/cm/selectionMarks.test.ts +++ b/src/editor/core/cm/selectionMarks.test.ts @@ -58,13 +58,13 @@ describe("selectionMarks", () => { it("caps inline highlight backgrounds and 1px frames at 1lh with pre-16.4 fallbacks", () => { expect(inlineBackground("var(--editor-search)")).toEqual({ - backgroundColor: "var(--editor-search)", - background: "linear-gradient(var(--editor-search), var(--editor-search)) center / 100% 1lh no-repeat", + background: "linear-gradient(var(--editor-search), var(--editor-search)) center no-repeat", + backgroundSize: "100% 1lh", WebkitBoxDecorationBreak: "clone", boxDecorationBreak: "clone", }); expect(inlineFramedFallback("var(--fill)", "var(--frame)")).toMatchObject({ - backgroundColor: "var(--fill)", + background: "var(--fill)", outline: "1px solid var(--frame)", outlineOffset: "-1px", }); diff --git a/src/editor/lsp/theme.ts b/src/editor/lsp/theme.ts index 94bc3cb82f..b09a86ba0b 100644 --- a/src/editor/lsp/theme.ts +++ b/src/editor/lsp/theme.ts @@ -1,6 +1,11 @@ /** Editor-side styling for LSP features (CodeMirror theme; tokens only). */ import { EditorView } from "@codemirror/view"; -import { inlineBackground, inlineFramedFallback, inlineFramedModern } from "@/editor/core/cm/inlineBg"; +import { + inlineBackground, + inlineFramedFallback, + inlineFramedModern, + inlineTokenBackground, +} from "@/editor/core/cm/inlineBg"; import { RAINBOW_COLORS } from "./semantic"; const tooltipSurface = { @@ -84,17 +89,21 @@ const markdown = { ".tok-invalid": { color: "var(--syn-invalid)" }, }; -function semanticStyle(token: string, fallback: string) { +function semanticAttributes(token: string, fallback: string) { return { color: `var(--syn-${token}, var(--syn-${fallback}))`, fontWeight: `var(--syn-${token}-bold, inherit)`, fontStyle: `var(--syn-${token}-italic, inherit)`, - backgroundColor: `var(--syn-${token}-bg, transparent)`, + "--openidea-token-bg": `var(--syn-${token}-bg, initial)`, textDecoration: `var(--syn-${token}-effect, none)`, WebkitTextDecoration: `var(--syn-${token}-effect, none)`, }; } +function semanticStyle(token: string, fallback: string) { + return { ...semanticAttributes(token, fallback), ...inlineTokenBackground }; +} + /** A token that shows `parent`'s colour and style while its own are unset (an `inherit` attribute). */ function inheritedStyle(token: string, parent: string) { const v = (suffix: string, fallback: string) => @@ -103,7 +112,8 @@ function inheritedStyle(token: string, parent: string) { color: v("", "inherit"), fontWeight: v("-bold", "inherit"), fontStyle: v("-italic", "inherit"), - backgroundColor: v("-bg", "transparent"), + "--openidea-token-bg": v("-bg", "initial"), + ...inlineTokenBackground, textDecoration: v("-effect", "none"), WebkitTextDecoration: v("-effect", "none"), }; @@ -117,7 +127,8 @@ function fallbackStyle(token: string, parents: string[]) { color: v("", "inherit"), fontWeight: v("-bold", "inherit"), fontStyle: v("-italic", "inherit"), - backgroundColor: v("-bg", "transparent"), + "--openidea-token-bg": v("-bg", "initial"), + ...inlineTokenBackground, textDecoration: v("-effect", "none"), WebkitTextDecoration: v("-effect", "none"), }; @@ -575,7 +586,8 @@ export const lspTheme = EditorView.theme({ ".cm-sem-python-nested": semanticStyle("python-nested-function", "function"), ".cm-sem-python-predefined-definition": semanticStyle("python-predefined-definition", "predefined"), ".cm-sem-python-predefined-usage": semanticStyle("python-predefined-usage", "predefined"), - ".cm-sem-python-docstring, .cm-sem-python-docstring *": semanticStyle("docstring", "string"), + ".cm-sem-python-docstring": inlineTokenBackground, + ".cm-sem-python-docstring, .cm-sem-python-docstring *": semanticAttributes("docstring", "string"), // After the docstring wildcard above so the tag's own colour wins inside it. ".cm-sem-python-docstring-tag": { ...semanticStyle("python-docstring-tag", "doc-comment-tag"), @@ -645,7 +657,7 @@ export const lspTheme = EditorView.theme({ color: "var(--syn-static-property, var(--syn-property))", fontStyle: "var(--syn-static-property-italic, var(--syn-static-italic, italic))", fontWeight: "var(--syn-static-property-bold, var(--syn-property-bold, inherit))", - backgroundColor: "var(--syn-static-property-bg, transparent)", + "--openidea-token-bg": "var(--syn-static-property-bg, initial)", textDecoration: "var(--syn-static-property-effect, none)", WebkitTextDecoration: "var(--syn-static-property-effect, none)", },