From cf15013b497e5be309bb82676ef86d0b9699404a Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 20 Nov 2026 22:18:11 +0800 Subject: [PATCH] fix(templates): align and scroll the Surround With menu (#4796) Surround With gets a separate shortcut column and uses the UI font for action labels. The list scrolls within the space beside the caret, and the title stays visible while the keyboard moves through it. --- e2e/surroundMenuLayout.spec.ts | 74 +++++++++++++++++++ .../templates/popup/Chooser.module.css | 44 +++++++++++ src/features/templates/popup/Chooser.tsx | 38 ++++++---- src/features/templates/popup/store.ts | 2 +- 4 files changed, 141 insertions(+), 17 deletions(-) create mode 100644 e2e/surroundMenuLayout.spec.ts diff --git a/e2e/surroundMenuLayout.spec.ts b/e2e/surroundMenuLayout.spec.ts new file mode 100644 index 000000000..f0f14bd4d --- /dev/null +++ b/e2e/surroundMenuLayout.spec.ts @@ -0,0 +1,74 @@ +import type { EditorView } from "@codemirror/view"; +import { expect, openFromTree, test } from "./fixtures"; + +test("Surround With keeps its title and keyboard-selected actions visible in a short window", async ({ + workbench: page, +}) => { + await page.setViewportSize({ width: 680, height: 320 }); + const editor = await openFromTree(page, "App.tsx"); + const content = editor.locator(".cm-content"); + const source = [ + "const fee = 5;", + "", + "function settle(amount: number) {", + " const adjusted = amount - fee;", + " const receipt = adjusted.toString();", + " console.log(receipt);", + " return adjusted;", + "}", + ].join("\n"); + const selectedText = " console.log(receipt);"; + await content.evaluate( + (node, { source, selectedText }) => { + const view = (node as HTMLElement & { cmTile: { view: EditorView } }).cmTile.view; + const start = source.indexOf(selectedText); + view.dispatch({ + changes: { from: 0, to: view.state.doc.length, insert: source }, + selection: { anchor: start, head: start + selectedText.length }, + scrollIntoView: true, + }); + view.focus(); + }, + { source, selectedText }, + ); + const openSurround = () => + page.evaluate(async () => { + const module = "/src/platform/actions.ts"; + const { executeAction } = await import(/* @vite-ignore */ module); + await executeAction("code.surroundWith"); + }); + + await openSurround(); + const popup = page.getByRole("dialog", { name: "Surround With", exact: true }); + const title = popup.getByText("Surround With", { exact: true }); + const list = popup.getByRole("listbox"); + await expect(title).toBeInViewport({ ratio: 1 }); + await expect.poll(() => list.evaluate((node) => node.scrollHeight - node.clientHeight)).toBeGreaterThan(0); + await expect.poll(() => popup.evaluate((node) => node.scrollHeight - node.clientHeight)).toBe(0); + + const first = list.getByRole("option", { name: /^1\.?\s+if$/ }); + await expect(first).toHaveAttribute("aria-selected", "true"); + const action = first.getByText("if", { exact: true }); + const mnemonic = first.getByText(/^1\.?$/, { exact: true }); + expect((await mnemonic.boundingBox())!.x + (await mnemonic.boundingBox())!.width).toBeLessThan( + (await action.boundingBox())!.x, + ); + expect(await action.evaluate((node) => getComputedStyle(node).fontSize)).toBe("13px"); + await page.keyboard.press("End"); + const last = list.getByRole("option", { name: /!\(expr\)/ }); + await expect(last).toHaveAttribute("aria-selected", "true"); + await expect(last).toBeInViewport({ ratio: 1 }); + await expect(title).toBeInViewport({ ratio: 1 }); + + await page.keyboard.press("Escape"); + await expect(popup).toBeHidden(); + await expect(content).toBeFocused(); + await expect(content).toContainText("console.log(receipt);"); + + await openSurround(); + await expect(first).toBeVisible(); + await page.keyboard.press("1"); + await expect(popup).toBeHidden(); + await expect(content).toContainText(/if \([^)]*\) \{/); + await expect(content).toContainText("console.log(receipt);"); +}); diff --git a/src/features/templates/popup/Chooser.module.css b/src/features/templates/popup/Chooser.module.css index 3c53b9530..e227833e5 100644 --- a/src/features/templates/popup/Chooser.module.css +++ b/src/features/templates/popup/Chooser.module.css @@ -82,3 +82,47 @@ flex: 1; border-top: 1px solid var(--border-popup); } +.actions { + display: flex; + flex-direction: column; + width: 320px; + min-width: 0; +} +.actions .body { + min-height: 0; +} +.actions .header { + flex: none; + padding-inline: 14px; + color: var(--fg-strong); + font-size: var(--fs-ui); + font-weight: 600; +} +.actions .title { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.actions .list { + max-height: none; +} +.actions .row { + gap: var(--space-2); +} +.actions .label { + flex-shrink: 1; + min-width: 0; + max-width: none; + font-family: var(--font-ui); + font-size: var(--fs-ui); +} +.actions .section { + width: 100%; + padding: 0; +} +.mnemonic { + flex: 0 0 var(--space-4); + color: var(--fg-muted); + font-variant-numeric: tabular-nums; + text-align: end; +} diff --git a/src/features/templates/popup/Chooser.tsx b/src/features/templates/popup/Chooser.tsx index 0a1cb8e52..081164cb1 100644 --- a/src/features/templates/popup/Chooser.tsx +++ b/src/features/templates/popup/Chooser.tsx @@ -37,17 +37,10 @@ export function filterChooserItems(items: readonly ChooserItem[], filter: string const isSection = (r: Row) => !!r.item.section; -/** The label with its mnemonic underlined (IDEA: `1. if`, `T. Surround with…`). */ +/** The abbreviation with its mnemonic underlined. */ function MnemonicLabel({ item }: { item: ChooserItem }) { - const m = item.mnemonic; - if (!m) return <>{item.label}; const i = item.mnemonicIndex; - if (i === undefined) - return ( - <> - {m}. {item.label} - - ); + if (i === undefined) return <>{item.label}; return ( <> {item.label.slice(0, i)} @@ -58,7 +51,11 @@ function MnemonicLabel({ item }: { item: ChooserItem }) { } function ChooserPopup({ request }: { request: ChooserRequest }) { - const rowHeight = useRowHeight(22); + const rowHeight = useRowHeight(request.mnemonics ? 24 : 22); + // Keep the scroll viewport stable as the popup finds room above or below the caret. + const maxHeight = request.mnemonics + ? `calc(max(${request.anchor.y}px, 100vh - ${request.anchor.y}px) - 8px)` + : undefined; const [filter, setFilter] = useState(request.filter ?? ""); const rows = useMemo(() => filterChooserItems(request.items, filter), [request.items, filter]); const [active, setActive] = useState(() => @@ -113,11 +110,12 @@ function ChooserPopup({ request }: { request: ChooserRequest }) { onClose={close} role="dialog" aria-label={request.title} - className={styles.popup} + className={clsx(styles.popup, request.mnemonics && styles.actions)} + style={maxHeight ? { maxHeight } : undefined} >
- {request.title} + {tx(request.title)} {request.filterable && filter && {filter}}
pick(r)} isSection={isSection} sectionHeight={22} - renderSection={(r) =>
{r.item.label}
} + sectionHeightOf={request.mnemonics ? (r) => (r.item.label ? undefined : 12) : undefined} + renderSection={(r) =>
{tx(r.item.label)}
} focusable={false} hoverSelects rowHeight={rowHeight} @@ -137,14 +136,21 @@ function ChooserPopup({ request }: { request: ChooserRequest }) { empty={
{request.emptyText ?? tx("Nothing to show")}
} renderItem={(r, state) => (
- - {r.item.mnemonic ? ( + {request.mnemonics && ( + {r.item.mnemonicIndex === undefined && r.item.mnemonic} + )} + + {r.item.mnemonicIndex !== undefined ? ( ) : ( )} - {r.item.detail && {r.item.detail}} + {r.item.detail && ( + + {r.item.detail} + + )}
)} /> diff --git a/src/features/templates/popup/store.ts b/src/features/templates/popup/store.ts index f8878aa87..2abac4f70 100644 --- a/src/features/templates/popup/store.ts +++ b/src/features/templates/popup/store.ts @@ -11,7 +11,7 @@ export interface ChooserItem { section?: boolean; /** Typing this character picks the item (see `ChooserRequest.mnemonics`). */ mnemonic?: string; - /** Where the mnemonic is underlined in the label; without it the label gets an `M. ` prefix. */ + /** Where the mnemonic is underlined in the label. Omit for a separate leading column. */ mnemonicIndex?: number; }