From 1cfc71ae58af8070e79f28c49cb97e2c1d77b45f Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 20 Nov 2026 03:56:29 +0800 Subject: [PATCH] fix(editor): make symbol rename and its preview readable in narrow windows (#4583) The symbol rename prompt was a caret-anchored popup that could be clipped in narrow windows, and its preview truncated long names. When in-place rename isn't available, Rename now opens a dialog with the symbol context, a New name field in the editor font, and an optional note; the refactoring preview keeps the context, wraps occurrence snippets in the editor font and theme, and truncates long file names with the full path in the tooltip. --- e2e/codeInsight.spec.ts | 75 +++++++++++++++++++++++++++ src/editor/lsp/Popups.module.css | 70 +++++++++++++++++++++++++ src/editor/lsp/Popups.tsx | 68 ++++++++++++++++++++++-- src/editor/lsp/popupStore.ts | 10 ++++ src/editor/lsp/rename.ts | 15 +++--- src/editor/lsp/syntaxFallback.test.ts | 12 ++--- 6 files changed, 232 insertions(+), 18 deletions(-) diff --git a/e2e/codeInsight.spec.ts b/e2e/codeInsight.spec.ts index 74ee676b5..721c6e632 100644 --- a/e2e/codeInsight.spec.ts +++ b/e2e/codeInsight.spec.ts @@ -562,6 +562,81 @@ test("indexed single-file rename previews all anchors and applies only after con expect(apply.edit).toMatchObject({ preconditions: [{ path: FILE, hash: "indexed-snapshot" }] }); }); +test("rename keeps the typed name, uses the editor font and shows long preview names in a narrow dialog", async ({ + workbench: page, +}) => { + const editor = await openSample(page); + await page.evaluate( + async ({ file, prepared }) => { + const projectModule = "/src/platform/project.ts"; + const schemaModule = "/src/features/settings/model/schema.ts"; + const [{ useProject }, { DEFAULT_SETTINGS }] = await Promise.all([ + import(/* @vite-ignore */ projectModule), + import(/* @vite-ignore */ schemaModule), + ]); + const base = useProject.getState().settings ?? DEFAULT_SETTINGS; + useProject.setState({ settings: { ...base, editor: { ...base.editor, fontSize: 17 } } }); + window.__ipc.handlers.lsp_prepare_rename = () => prepared; + window.__ipc.handlers.lsp_rename = (args) => ({ + preview: true, + incomplete: null, + edit: { + files: [ + { + kind: "edit", + path: file, + edits: [{ range: prepared, new_text: args.newName }], + }, + ], + }, + }); + window.__ipc.handlers.apply_workspace_edit = () => null; + }, + { file: FILE, prepared: range(20, 12, 17) }, + ); + + await caretAt(editor, page, "val order: Order", "order"); + await page.keyboard.press("Shift+F6"); + await expect(page.getByText(/Press ⇧F6 to show dialog/)).toBeVisible(); + const name = "checkoutOrderWithDeliveryInstructionsAndInventoryReservation"; + await page.keyboard.insertText(name); + await page.keyboard.press("Shift+F6"); + await page.setViewportSize({ width: 440, height: 520 }); + const dialog = page.getByRole("dialog", { name: "Rename 'order'", exact: true }); + const input = dialog.getByRole("textbox", { name: "New name" }); + await expect(input).toBeFocused(); + await expect(input).toHaveValue(name); + await expect(input).toHaveCSS("font-size", "17px"); + const [label, field] = await Promise.all([ + dialog.getByText("New name", { exact: true }).boundingBox(), + input.boundingBox(), + ]); + expect(label!.x + label!.width).toBeLessThan(field!.x); + const rename = dialog.getByRole("button", { name: "Rename", exact: true }); + + await input.fill(""); + await expect(rename).toBeDisabled(); + await input.press("Enter"); + await expect(dialog).toBeVisible(); + expect(await ipcCalls(page, "lsp_rename")).toHaveLength(0); + await input.fill(name); + await input.press("Enter"); + + const summary = `Rename 'order' to '${name}'`; + const preview = page.getByRole("dialog", { name: summary, exact: true }); + const context = preview.getByText(summary, { exact: true }); + await expect(context).toBeVisible(); + const geometry = await context.evaluate((element) => ({ + client: element.clientWidth, + content: element.scrollWidth, + })); + expect(geometry.content).toBeLessThanOrEqual(geometry.client + 1); + await expect(preview.getByText("1 occurrence in 1 file")).toBeVisible(); + expect(await ipcCalls(page, "apply_workspace_edit")).toHaveLength(0); + await preview.getByRole("button", { name: "Do Refactor" }).click(); + await expect.poll(() => ipcCalls(page, "apply_workspace_edit")).toHaveLength(1); +}); + test("intention previews fit narrow windows and wrap long code lines", async ({ workbench: page }) => { await page.setViewportSize({ width: 1600, height: 900 }); const editor = await openSample(page); diff --git a/src/editor/lsp/Popups.module.css b/src/editor/lsp/Popups.module.css index a3215c0bf..5fa6997c8 100644 --- a/src/editor/lsp/Popups.module.css +++ b/src/editor/lsp/Popups.module.css @@ -261,6 +261,34 @@ font-size: var(--fs-tiny); color: var(--fg-faint); } +.symbolRename { + display: grid; + grid-template-columns: auto minmax(0, 1fr); + align-items: center; + gap: var(--space-2) var(--space-3); + padding: var(--space-3); + font-family: var(--font-ui); + font-size: var(--fs-ui); +} +.renameContext, +.symbolRenameNote { + grid-column: 1 / -1; + min-width: 0; + overflow-wrap: anywhere; +} +.renameContext { + color: var(--fg-emphasis); +} +.symbolRename .renameField { + width: 100%; + height: max(var(--h-field), calc(var(--openidea-editor-font-size, var(--fs-code)) + var(--space-2))); + font-family: var(--openidea-editor-font, var(--font-mono)); + font-size: var(--openidea-editor-font-size, var(--fs-code)); +} +.symbolRenameNote { + color: var(--fg-muted); + font-size: var(--fs-small); +} /* Fits its content up to a cap; resized (`data-resized`), it keeps that size and the body scrolls. */ .docPopup { @@ -398,6 +426,36 @@ flex-direction: column; overflow: hidden; } +.renamePreview [data-dialog-body] { + display: block; + overflow: auto; +} +.previewContext { + padding: var(--space-2) 14px 0; + overflow-wrap: anywhere; + color: var(--fg-emphasis); + font-size: var(--fs-ui); +} +.renamePreview .previewList { + overflow: visible; +} +.renamePreview .previewName { + min-width: 0; + max-width: 55%; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.renamePreview .previewOp { + flex: 0 1 auto; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.renamePreview .previewWarning { + overflow-wrap: anywhere; +} .previewSummary { flex: none; padding: 10px 14px 6px; @@ -469,6 +527,18 @@ font-size: var(--fs-small); color: var(--fg-editor); } +.renamePreview .previewOccurrences .previewRow { + min-height: var(--h-row); + height: auto; + align-items: baseline; + padding-block: var(--space-1); +} +.renamePreview .previewCode { + font-family: var(--openidea-editor-font, var(--font-mono)); + font-size: var(--openidea-editor-font-size, var(--fs-code)); + white-space: pre-wrap; + overflow-wrap: anywhere; +} .previewMatch { background: var(--bg-search-match); color: var(--fg-search-match, inherit); diff --git a/src/editor/lsp/Popups.tsx b/src/editor/lsp/Popups.tsx index 2fe99609d..551543628 100644 --- a/src/editor/lsp/Popups.tsx +++ b/src/editor/lsp/Popups.tsx @@ -1,5 +1,5 @@ /** React popups for LSP editor features, rendered as a workbench overlay. */ -import { useEffect, useMemo, useRef, useState, type CSSProperties, type KeyboardEvent } from "react"; +import { useEffect, useId, useMemo, useRef, useState, type CSSProperties, type KeyboardEvent } from "react"; import clsx from "clsx"; import { Checkbox, @@ -392,6 +392,54 @@ function RenamePopup({ popup }: { popup: Extract } ); } +function SymbolRenameDialog({ popup }: { popup: Extract }) { + const [value, setValue] = useState(popup.initial); + const field = useId(); + const name = value.trim(); + return ( + { + closePopup(); + popup.onSubmit(name); + }, + }, + { label: "Cancel", role: "cancel" }, + ]} + > +
+
+
{popup.title}
+ + setValue(e.target.value)} + invalid={!name} + /> + {popup.note &&
{popup.note}
} +
+
+
+ ); +} + function DocPopup({ popup }: { popup: Extract }) { const focusSeq = useLspPopup((s) => s.focusSeq); const scope = useRef(null); @@ -536,6 +584,7 @@ function savePreviewSize(size: { w: number; h: number }) { } function PreviewDialog({ popup }: { popup: Extract }) { + const rename = popup.renameThemeClasses !== undefined; const root = useProject((s) => s.project?.root ?? ""); const { summary } = popup; const [initialSize] = useState(loadPreviewSize); @@ -543,8 +592,9 @@ function PreviewDialog({ popup }: { popup: Extract + {rename &&
{popup.title}
} {popup.warnings?.map((w) => (
@@ -573,7 +624,10 @@ function PreviewDialog({ popup }: { popup: Extract{describeSummary(summary)}
{popup.occurrences ? ( -
    +
      {popup.occurrences.map((o, i) => { const lead = o.text.length - o.text.trimStart().length; const text = o.text.trim(); @@ -596,7 +650,9 @@ function PreviewDialog({ popup }: { popup: Extract (
    • - {basename(e.path)} + + {basename(e.path)} + {relativePath(root, e.path)} @@ -742,6 +798,8 @@ export function LspPopups() { return ; case "rename": return ; + case "symbolRename": + return ; case "doc": return ; case "definition": diff --git a/src/editor/lsp/popupStore.ts b/src/editor/lsp/popupStore.ts index 2402517c0..2b4a1a8a9 100644 --- a/src/editor/lsp/popupStore.ts +++ b/src/editor/lsp/popupStore.ts @@ -130,6 +130,14 @@ export type LspPopup = hint?: string; onSubmit: (name: string) => void; } + | { + kind: "symbolRename"; + title: string; + initial: string; + note?: string; + themeClasses: string; + onSubmit: (name: string) => void; + } | { kind: "preview"; title: string; @@ -140,6 +148,8 @@ export type LspPopup = warnings?: string[]; /** Individual occurrences with line previews (single-file syntax-based renames). */ occurrences?: PreviewOccurrence[]; + /** The editor theme for a symbol rename and its occurrence snippets. */ + renameThemeClasses?: string; }; export interface PreviewOccurrence { diff --git a/src/editor/lsp/rename.ts b/src/editor/lsp/rename.ts index e71ff0da8..0a59c6d4a 100644 --- a/src/editor/lsp/rename.ts +++ b/src/editor/lsp/rename.ts @@ -27,9 +27,9 @@ import { executeAction } from "@/platform/actions"; import { whenAllSynced, whenSynced, type EditorDocContext } from "@/editor/api"; import { structure } from "@/editor/structure"; import { docOf, incompleteNote, isUnavailable, lspRequest, reportLspError } from "./lsp"; -import { comparePos, offsetToPos, rangeToOffsets } from "./positions"; +import { offsetToPos, rangeToOffsets } from "./positions"; import { showHint } from "./hint"; -import { caretAnchor, openPopup, type PreviewOccurrence } from "./popupStore"; +import { openPopup, type PreviewOccurrence } from "./popupStore"; import { needsPreview, summarizeWorkspaceEdit } from "./workspaceEdit"; import { endInplaceRename, inplaceSession, startInplaceRename, type Span } from "./inplaceRename"; import { shortcutLabel } from "@/platform/keymap"; @@ -127,10 +127,10 @@ export async function startRename(view: EditorView) { const oldName = view.state.sliceDoc(from, to); const dialog = (initial: string) => openPopup({ - kind: "rename", - anchor: caretAnchor(view, from), + kind: "symbolRename", title: tx`Rename '${oldName}'`, initial, + themeClasses: view.themeClasses, onSubmit: (newName) => void finishRename(view, doc, pos, oldName, newName), }); if (from === to) { @@ -304,6 +304,7 @@ async function finishRename(view: EditorView, doc: EditorDocContext, pos: Pos, o kind: "preview", title: tx`Rename '${oldName}' to '${newName}'`, summary, + renameThemeClasses: view.themeClasses, ...(note ? { warnings: [note] } : {}), confirmLabel: note ? "Rename Anyway" : "Do Refactor", onConfirm: () => void applyEdit(view, edit), @@ -350,13 +351,12 @@ async function startSyntaxRename( return true; } const { plan } = result; - const at = plan.occurrences.find((o) => comparePos(o.range.start, pos) <= 0 && comparePos(pos, o.range.end) <= 0); openPopup({ - kind: "rename", - anchor: caretAnchor(view, rangeToOffsets(view.state.doc, (at ?? plan.occurrences[0]!).range).from), + kind: "symbolRename", title: tx`Rename '${plan.symbol.name}'`, initial: plan.symbol.name, note: `${SYNTAX_NOTE}: local occurrences only`, + themeClasses: view.themeClasses, onSubmit: (newName) => void commitSyntaxRename(view, doc, plan, newName), }); return true; @@ -418,6 +418,7 @@ export async function commitSyntaxRename( }, warnings: [...risks, `${SYNTAX_NOTE}. Only the occurrences below will be renamed.`], occurrences, + renameThemeClasses: view.themeClasses, confirmLabel: "Rename Anyway", onConfirm: apply, }); diff --git a/src/editor/lsp/syntaxFallback.test.ts b/src/editor/lsp/syntaxFallback.test.ts index 733192129..e22799d7f 100644 --- a/src/editor/lsp/syntaxFallback.test.ts +++ b/src/editor/lsp/syntaxFallback.test.ts @@ -646,8 +646,8 @@ describe("rename wiring", () => { const view = editor("const x = 1;\nf({ x });", "typescript", 6); await startRename(view); const popup = useLspPopup.getState().popup; - expect(popup?.kind).toBe("rename"); - if (popup?.kind !== "rename") return; + expect(popup?.kind).toBe("symbolRename"); + if (popup?.kind !== "symbolRename") return; expect(popup.note).toContain("Based on syntax"); popup.onSubmit("y"); await vi.waitFor(() => expect(view.state.doc.toString()).toBe("const y = 1;\nf({ x: y });")); @@ -661,7 +661,7 @@ describe("rename wiring", () => { const view = editor("const x = 1;\nx;\nvar x;", "typescript", 6); await startRename(view); const popup = useLspPopup.getState().popup; - if (popup?.kind !== "rename") throw new Error("no rename popup"); + if (popup?.kind !== "symbolRename") throw new Error("no rename dialog"); popup.onSubmit("y"); await vi.waitFor(() => expect(useLspPopup.getState().popup?.kind).toBe("preview")); const preview = useLspPopup.getState().popup; @@ -860,12 +860,12 @@ describe("rename wiring", () => { await startRename(view); expect(inplaceSession(view.state)?.oldName).toBe("x"); await startRename(view); - expect(useLspPopup.getState().popup?.kind).toBe("rename"); + expect(useLspPopup.getState().popup?.kind).toBe("symbolRename"); if (finish === "cancel") closePopup(); release({ status: "ok", data: r(0, 6, 7) }); await older; expect(inplaceSession(view.state)).toBeNull(); - expect(useLspPopup.getState().popup?.kind ?? null).toBe(finish === "dialog" ? "rename" : null); + expect(useLspPopup.getState().popup?.kind ?? null).toBe(finish === "dialog" ? "symbolRename" : null); }); it.each(["text", "document"])("drops pending preparation when the editor %s changes", async (change) => { @@ -938,7 +938,7 @@ describe("rename wiring", () => { ); cmd.syntaxLocals.mockReturnValueOnce(ok(local({ references: [] }))); await startRename(view); - expect(useLspPopup.getState().popup?.kind).toBe("rename"); + expect(useLspPopup.getState().popup?.kind).toBe("symbolRename"); }); });