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>
This commit is contained in:
zhzhsx
2026-11-14 13:50:26 +08:00
committed by GitHub
co-authored by zhzhsx
parent fdf29989ec
commit 4ccff88ccf
2 changed files with 42 additions and 6 deletions
+12
View File
@@ -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.
+30 -6
View File
@@ -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%",