From 4ccff88ccf1d44f93c51e692bfa9e0a39ac8aac5 Mon Sep 17 00:00:00 2001 From: zhzhsx Date: Sat, 14 Nov 2026 13:50:26 +0800 Subject: [PATCH] fix(editor): align completion labels with the word being completed (#3297) CodeMirror anchors the completion popup at the start of the word being completed, but each row starts with a kind icon, so labels sat one icon column to the right of the text. The popup is offset left by the row's leading inset (border, row margin, padding, icon and gap), kept in custom properties that the row rules use too, so the labels line up with the word. Co-authored-by: zhzhsx <1379025+zhzhsx@users.noreply.github.com> --- e2e/codeInsight.spec.ts | 12 ++++++++++++ src/editor/lsp/theme.ts | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/e2e/codeInsight.spec.ts b/e2e/codeInsight.spec.ts index 0f94d16bf..49ca88e83 100644 --- a/e2e/codeInsight.spec.ts +++ b/e2e/codeInsight.spec.ts @@ -71,6 +71,18 @@ test("completion lists the server's items and inserts the chosen one", async ({ const method = popup.locator("li").filter({ has: page.locator(".cm-completionLabel", { hasText: /^toDto$/ }) }); await expect(method.locator(".cm-completionDetail")).toHaveText("()"); await expect(method.locator(".cm-lsp-type")).toHaveText("OrderDto"); + // CodeMirror anchors the popup at the completion's `from` (the word start), and the kind icon + // hangs left of the label. With no prefix typed yet `from` is the caret, so the label's x must + // match the caret column rather than the popup's left edge. + const wordStart = await editor.locator(".cm-content").evaluate(async (content) => { + const viewModule = "/node_modules/.vite/deps/@codemirror_view.js"; + const { EditorView } = await import(/* @vite-ignore */ viewModule); + const view = EditorView.findFromDOM(content as HTMLElement)!; + return view.coordsAtPos(view.state.selection.main.head)!.left; + }); + const label = await method.locator(".cm-completionLabel").boundingBox(); + // Subpixel rounding differs per platform/font; a mis-set inset is a whole icon column off. + expect(Math.abs(label!.x - wordStart)).toBeLessThanOrEqual(2); await page.keyboard.type("toD"); await expect(popup.getByText("total", { exact: true })).toBeHidden(); // Refreshing the results rebuilds the list; the hint bar must stay below it, not jump to the top. diff --git a/src/editor/lsp/theme.ts b/src/editor/lsp/theme.ts index 3e4fc61b7..d0c58c7de 100644 --- a/src/editor/lsp/theme.ts +++ b/src/editor/lsp/theme.ts @@ -10,6 +10,19 @@ const tooltipSurface = { color: "var(--fg)", }; +/** + * Row metrics that put a label one icon column right of the popup's left edge. They live on the + * popup as custom properties so every rule consuming them — including the popup's negative + * offset below — changes together. The remaining 1px is tooltipSurface's border, shared with + * the hover and signature tooltips. + */ +const completionRowMetrics = { + "--cm-completion-edge": "4px", + "--cm-completion-inset": "6px", + "--cm-completion-icon": "16px", + "--cm-completion-gap": "6px", +}; + const markdown = { ".cm-lsp-markdown": { fontFamily: "var(--font-ui)", @@ -84,9 +97,18 @@ export const lspTheme = EditorView.theme({ // --- completion (IDEA lookup: icon column, bold blue match, grey tail/type, advertiser) --- ".cm-tooltip.cm-tooltip-autocomplete.cm-lsp-completion": { ...tooltipSurface, + ...completionRowMetrics, fontFamily: "var(--openidea-editor-font, var(--font-mono))", fontWeight: "var(--fw-code, 400)", fontSize: "var(--fs-code)", + // CodeMirror anchors the popup at the completed word's start. Pull it left by one row's + // leading inset so the labels, not the icon column, sit on the text. CodeMirror's horizontal + // clamp measures the border box (margins excluded), so this offset sits outside its math: + // near the right edge it leaves that much extra space, and a word starting within one inset + // of the viewport's left edge would let the popup clip there. The normal editor layout + // (gutter + project sidebar) keeps the word well clear of that edge. + marginLeft: + "calc(-1 * (1px + var(--cm-completion-edge) + var(--cm-completion-inset) + var(--cm-completion-icon) + var(--cm-completion-gap)))", overflow: "hidden", }, ".cm-tooltip.cm-tooltip-autocomplete.cm-lsp-completion > ul": { @@ -100,11 +122,11 @@ export const lspTheme = EditorView.theme({ ".cm-tooltip.cm-tooltip-autocomplete.cm-lsp-completion > ul > li": { display: "flex", alignItems: "center", - gap: "6px", + gap: "var(--cm-completion-gap)", height: "var(--h-list-row)", lineHeight: "var(--h-list-row)", - margin: "0 4px", - padding: "0 8px 0 6px", + margin: "0 var(--cm-completion-edge)", + padding: "0 8px 0 var(--cm-completion-inset)", borderRadius: "var(--radius-small)", color: "var(--fg-editor, var(--fg-strong))", whiteSpace: "nowrap", @@ -122,8 +144,8 @@ export const lspTheme = EditorView.theme({ }, ".cm-lsp-completion .cm-lsp-kind": { flex: "none", - width: "16px", - height: "16px", + width: "var(--cm-completion-icon)", + height: "var(--cm-completion-icon)", display: "inline-flex", alignItems: "center", justifyContent: "center", @@ -143,7 +165,9 @@ export const lspTheme = EditorView.theme({ color: "var(--fg-completion-match, var(--link))", }, // A server's label detail (`(a: string)`) hugs the name; template descriptions keep the gap. - ".cm-lsp-completion .cm-lsp-option .cm-completionDetail": { marginLeft: "-6px" }, + ".cm-lsp-completion .cm-lsp-option .cm-completionDetail": { + marginLeft: "calc(-1 * var(--cm-completion-gap))", + }, ".cm-lsp-completion .cm-lsp-type": { flex: "1 0 auto", maxWidth: "60%",