From 1005dc136385da754ef37f5a8885d0efdbb9f414 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 22 Nov 2026 04:25:59 +0800 Subject: [PATCH] fix(editor): lay out inline refactoring names as a lookup list (#5072) Inline refactoring name suggestions align with the identifier being edited. Rows follow the editor font and zoom, with a separate footer for the shortcut; the list fits inside the editor and scrolls to keep the active suggestion visible. --- e2e/inplaceNameSuggestionsLayout.spec.ts | 64 +++++++++++++ src/editor/lsp/inplaceRename.test.ts | 4 +- src/editor/lsp/inplaceRename.ts | 110 +++++++++++++++++------ src/editor/lsp/theme.ts | 54 ++++++++--- src/platform/locales/text/en.json | 1 + 5 files changed, 195 insertions(+), 38 deletions(-) create mode 100644 e2e/inplaceNameSuggestionsLayout.spec.ts diff --git a/e2e/inplaceNameSuggestionsLayout.spec.ts b/e2e/inplaceNameSuggestionsLayout.spec.ts new file mode 100644 index 000000000..bed935cf5 --- /dev/null +++ b/e2e/inplaceNameSuggestionsLayout.spec.ts @@ -0,0 +1,64 @@ +import { expect, openFromTree, test } from "./fixtures"; + +const longName = "estimatedInvoiceTotal".repeat(6); + +test("inline refactoring names use the editor font and keep the shortcut in a footer", async ({ workbench: page }) => { + await page.setViewportSize({ width: 600, height: 640 }); + const editor = await openFromTree(page, "build.gradle.kts"); + await page.evaluate(async () => { + const project = "/src/platform/project.ts"; + const { useProject } = await import(/* @vite-ignore */ project); + const { settings } = useProject.getState(); + useProject.setState({ settings: { ...settings, editor: { ...settings?.editor, fontSize: 20 } } }); + }); + const content = editor.locator(".cm-content"); + await expect(content).toHaveCSS("font-size", "20px"); + await content.click(); + await page.evaluate(async (longName) => { + const editorApi = "/src/editor/api.ts"; + const inplace = "/src/editor/lsp/inplaceRename.ts"; + const { activeEditor } = await import(/* @vite-ignore */ editorApi); + const { startInplaceRename } = await import(/* @vite-ignore */ inplace); + const { view } = activeEditor(); + const text = view.state.doc.toString(); + const from = text.indexOf("toDto"); + view.focus(); + startInplaceRename(view, { + primary: { from, to: from + 5 }, + others: [], + suggestions: ["toDto", "orderDto", "order", ...Array.from({ length: 8 }, (_, i) => `candidate${i}`), longName], + hint: "Press ⇧F6 to show dialog with more options", + commit: () => {}, + dialog: () => {}, + }); + }, longName); + + const names = page.getByRole("list", { name: "Suggested names" }); + await expect(names).toBeVisible(); + await expect(names).toHaveCSS("font-size", "20px"); + const shortcut = page.getByText("Press ⇧F6 to show dialog with more options", { exact: true }); + await expect(shortcut).toHaveCSS("font-size", "12px"); + const listBox = (await names.boundingBox())!; + const editorBox = (await editor.boundingBox())!; + expect(listBox.x + listBox.width).toBeLessThanOrEqual(editorBox.x + editorBox.width); + + const primary = editor.locator(".cm-lsp-inplace-primary"); + const first = names.getByRole("button", { name: "toDto", exact: true }); + await expect(first).toHaveAttribute("aria-current", "true"); + expect((await first.boundingBox())!.height).toBeGreaterThanOrEqual(34); + await page.keyboard.press("ArrowUp"); + const last = names.getByRole("button", { name: longName, exact: true }); + await expect(last).toHaveAttribute("aria-current", "true"); + await expect(last).toBeInViewport({ ratio: 1 }); + await page.keyboard.press("ArrowDown"); + await expect(first).toHaveAttribute("aria-current", "true"); + await page.keyboard.press("ArrowDown"); + await expect(primary).toHaveText("orderDto"); + await expect(names.getByRole("button", { name: "orderDto", exact: true })).toHaveAttribute("aria-current", "true"); + await names.getByRole("button", { name: "order", exact: true }).click(); + await expect(primary).toHaveText("order"); + await expect(content).toBeFocused(); + await page.keyboard.press("Escape"); + await expect(names).toHaveCount(0); + await expect(content).toContainText("toDto"); +}); diff --git a/src/editor/lsp/inplaceRename.test.ts b/src/editor/lsp/inplaceRename.test.ts index 97aea83e3..f07027b58 100644 --- a/src/editor/lsp/inplaceRename.test.ts +++ b/src/editor/lsp/inplaceRename.test.ts @@ -140,8 +140,8 @@ describe("in-place rename edits elsewhere", () => { dialog: vi.fn(), }); const item = (name: string) => - [...view.dom.querySelectorAll(".cm-lsp-inplace-suggestions li")].find( - (li) => li.textContent === name, + [...view.dom.querySelectorAll(".cm-lsp-inplace-suggestions button")].find( + (button) => button.textContent === name, )!; item("total").dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, detail: 1 })); expect(view.state.doc.toString()).toBe("let total = 1; total + foo;"); diff --git a/src/editor/lsp/inplaceRename.ts b/src/editor/lsp/inplaceRename.ts index 2ad68f1d4..9181f1a40 100644 --- a/src/editor/lsp/inplaceRename.ts +++ b/src/editor/lsp/inplaceRename.ts @@ -25,12 +25,15 @@ import { EditorView, ViewPlugin, keymap, + repositionTooltips, showTooltip, type Tooltip, type ViewUpdate, } from "@codemirror/view"; import { completionStatus } from "@codemirror/autocomplete"; import { isExternalChange } from "@/editor/api"; +import { tooltipBounds } from "@/editor/core/cm/setup"; +import { tx } from "@/platform/i18n"; export interface Span { from: number; @@ -94,34 +97,11 @@ const sessionField = StateField.define({ pos: s.primary.from, above: false, create: (view) => { + if (s.suggestions?.length) return nameLookup(view, s, s.suggestions); const dom = document.createElement("div"); dom.className = "cm-lsp-hint cm-lsp-inplace-hint"; dom.setAttribute("role", "status"); - if (s.suggestions?.length) { - const current = view.state.sliceDoc(s.primary.from, s.primary.to); - const list = document.createElement("ul"); - list.className = "cm-lsp-inplace-suggestions"; - list.setAttribute("aria-label", "Suggested names"); - for (const name of s.suggestions) { - const li = document.createElement("li"); - li.textContent = name; - if (name === current) li.className = "cm-lsp-inplace-suggestion-active"; - // A click puts the name in; a double click also finishes, like Enter. - li.addEventListener("mousedown", (e) => { - e.preventDefault(); - putSuggestion(view, name); - if (e.detail >= 2) endInplaceRename(view, "commit"); - }); - li.addEventListener("dblclick", () => { - if (putSuggestion(view, name)) endInplaceRename(view, "commit"); - }); - list.appendChild(li); - } - dom.appendChild(list); - } - const hint = document.createElement("div"); - hint.textContent = s.hint; - dom.appendChild(hint); + dom.textContent = s.hint; return { dom }; }, } @@ -130,6 +110,86 @@ const sessionField = StateField.define({ ], }); +function nameLookup(view: EditorView, session: InplaceSession, suggestions: readonly string[]) { + const dom = document.createElement("div"); + dom.className = "cm-lsp-inplace-lookup"; + const current = view.state.sliceDoc(session.primary.from, session.primary.to); + const list = document.createElement("ul"); + list.className = "cm-lsp-inplace-suggestions"; + list.setAttribute("role", "list"); + list.setAttribute("aria-label", tx("Suggested names")); + for (const name of suggestions) { + const row = document.createElement("li"); + const button = document.createElement("button"); + button.type = "button"; + button.tabIndex = -1; + button.textContent = name; + button.title = name; + if (name === current) button.setAttribute("aria-current", "true"); + // A click puts the name in; a double click also finishes, like Enter. + button.addEventListener("mousedown", (event) => { + event.preventDefault(); + putSuggestion(view, name); + if (event.detail >= 2) endInplaceRename(view, "commit"); + }); + button.addEventListener("click", (event) => { + if (event.detail === 0) putSuggestion(view, name); + }); + button.addEventListener("dblclick", () => { + if (putSuggestion(view, name)) endInplaceRename(view, "commit"); + }); + row.appendChild(button); + list.appendChild(row); + } + dom.appendChild(list); + const selected = list.querySelector('button[aria-current="true"]'); + if (session.hint) { + const hint = document.createElement("div"); + hint.className = "cm-lsp-inplace-footer"; + hint.setAttribute("role", "status"); + hint.textContent = session.hint; + dom.appendChild(hint); + } + + // The body-level tooltip needs the font and available width from its own editor. + const measure = () => + view.requestMeasure({ + key: dom, + read: () => { + const { left, right } = tooltipBounds(view); + const { fontFamily, fontSize, fontWeight } = getComputedStyle(view.contentDOM); + return { maxWidth: `${Math.min(480, Math.max(0, right - left))}px`, fontFamily, fontSize, fontWeight }; + }, + write: ({ maxWidth, fontFamily, fontSize, fontWeight }) => { + if (!dom.isConnected) return; + if ( + dom.style.maxWidth === maxWidth && + dom.style.fontFamily === fontFamily && + dom.style.fontWeight === fontWeight && + dom.style.getPropertyValue("--openidea-editor-font-size") === fontSize + ) + return; + Object.assign(dom.style, { maxWidth, fontFamily, fontWeight }); + dom.style.setProperty("--openidea-editor-font-size", fontSize); + repositionTooltips(view); + }, + }); + return { + dom, + offset: { x: -13, y: 0 }, + mount: measure, + update: (update: ViewUpdate) => { + if (update.geometryChanged) measure(); + }, + positioned: () => { + if (!selected || list.scrollHeight <= list.clientHeight) return; + const row = selected.getBoundingClientRect(); + const bounds = list.getBoundingClientRect(); + list.scrollTop += Math.min(0, row.top - bounds.top) + Math.max(0, row.bottom - bounds.bottom); + }, + }; +} + const primaryMark = Decoration.mark({ class: "cm-lsp-inplace cm-lsp-inplace-primary" }); const occurrenceMark = Decoration.mark({ class: "cm-lsp-inplace" }); diff --git a/src/editor/lsp/theme.ts b/src/editor/lsp/theme.ts index f1b9110fb..214c3b051 100644 --- a/src/editor/lsp/theme.ts +++ b/src/editor/lsp/theme.ts @@ -11,12 +11,13 @@ const tooltipSurface = { }; const completionInsets = { edge: 4, padding: 6, icon: 16, gap: 6 }; +const lookupRowHeight = "max(var(--h-list-row), calc(var(--openidea-editor-font-size, var(--fs-code)) * 1.7))"; export const completionLabelOffset = 1 + completionInsets.edge + completionInsets.padding + completionInsets.icon + completionInsets.gap; const completionRowMetrics = { // Rows grow with the editor font size (lookup text uses the editor font, as typed text does). - "--cm-completion-row": "max(var(--h-list-row), calc(var(--openidea-editor-font-size, var(--fs-code)) * 1.7))", + "--cm-completion-row": lookupRowHeight, "--cm-completion-edge": `${completionInsets.edge}px`, "--cm-completion-inset": `${completionInsets.padding}px`, "--cm-completion-icon": `${completionInsets.icon}px`, @@ -202,7 +203,7 @@ export const lspTheme = EditorView.theme({ textDecoration: "line-through", color: "var(--fg-muted)", }, - ".cm-lsp-completion-footer": { + ".cm-lsp-completion-footer, .cm-lsp-inplace-footer": { display: "flex", flex: "none", flexWrap: "wrap", @@ -438,18 +439,49 @@ export const lspTheme = EditorView.theme({ padding: "3px 8px", cursor: "default", }, + ".cm-tooltip.cm-lsp-inplace-lookup": { + ...tooltipSurface, + "--cm-inplace-row": lookupRowHeight, + boxSizing: "border-box", + display: "flex", + flexDirection: "column", + width: "max-content", + maxWidth: "min(480px, 100vw)", + overflow: "hidden", + fontFamily: "var(--openidea-editor-font, var(--font-mono))", + fontSize: "var(--openidea-editor-font-size, var(--fs-code))", + fontWeight: "var(--fw-code, 400)", + }, ".cm-lsp-inplace-suggestions": { listStyle: "none", - margin: "0 -8px 4px", - padding: "0", - fontFamily: "var(--openidea-editor-font, var(--font-mono))", - fontWeight: "var(--fw-code, 400)", - fontSize: "var(--fs-ui)", - color: "var(--fg)", + margin: "0", + padding: "var(--space-1)", + minHeight: "0", + maxHeight: "calc(var(--cm-inplace-row) * 8 + var(--space-2))", + overflowY: "auto", + color: "var(--fg-editor, var(--fg-strong))", }, - ".cm-lsp-inplace-suggestions li": { padding: "1px 8px" }, - ".cm-lsp-inplace-suggestions li:hover": { backgroundColor: "var(--bg-hover)" }, - ".cm-lsp-inplace-suggestion-active": { backgroundColor: "var(--bg-selected)" }, + ".cm-lsp-inplace-suggestions button": { + display: "block", + width: "100%", + minHeight: "var(--cm-inplace-row)", + padding: "0 var(--space-2)", + border: "none", + borderRadius: "var(--radius-small)", + background: "none", + color: "inherit", + font: "inherit", + textAlign: "start", + whiteSpace: "nowrap", + overflow: "hidden", + textOverflow: "ellipsis", + cursor: "pointer", + }, + ".cm-lsp-inplace-suggestions button:hover": { backgroundColor: "var(--bg-hover)" }, + '.cm-lsp-inplace-suggestions button[aria-current="true"]': { + backgroundColor: "var(--bg-list-selected, var(--bg-tree-selected, var(--bg-selected-strong)))", + }, + ".cm-lsp-inplace-footer": { overflowWrap: "anywhere" }, // --- floating lightbulb (intentions.ts) ----------------------------------- ".cm-lsp-bulb": { diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 27b39256c..dfa47090f 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -3,6 +3,7 @@ "Expand unchanged lines": "Expand unchanged lines", "Matching scope": "Matching scope", "Generate": "Generate", + "Suggested names": "Suggested names", "Choose Test for {stem} ({count} found)": "Choose Test for {stem} ({count} found)", "Choose Test Subject for {stem} ({count} found)": "Choose Test Subject for {stem} ({count} found)", "Code Style": "Code Style",