From c83b0b1442021d2da02489428f6cf79dd43bf20b Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sat, 21 Nov 2026 00:41:46 +0800 Subject: [PATCH] fix(settings): pair postfix templates with a code preview (#4843) Settings > Editor > General > Postfix Completion puts the template list beside a preview of the selected template, showing the typed expression and its expansion from the existing template body; in narrow panes the preview moves below the list. --- e2e/postfixSettingsLayout.spec.ts | 80 ++++++++ .../templates/settings/PostfixPage.tsx | 180 +++++++++++++----- .../templates/settings/Settings.module.css | 105 +++++++++- src/platform/locales/text/en.json | 2 + src/platform/locales/text/zh.json | 2 + 5 files changed, 318 insertions(+), 51 deletions(-) create mode 100644 e2e/postfixSettingsLayout.spec.ts diff --git a/e2e/postfixSettingsLayout.spec.ts b/e2e/postfixSettingsLayout.spec.ts new file mode 100644 index 000000000..afd06f9a8 --- /dev/null +++ b/e2e/postfixSettingsLayout.spec.ts @@ -0,0 +1,80 @@ +import type { Page } from "@playwright/test"; +import { expect, test } from "./fixtures"; + +async function openPostfixSettings(page: Page) { + await page.keyboard.press("Meta+,"); + const settings = page.getByRole("dialog", { name: "Settings", exact: true }); + const search = settings.getByRole("textbox", { name: "Search settings" }); + await search.fill("Postfix Completion"); + await settings.getByRole("treeitem", { name: "Postfix Completion", exact: true }).click(); + await search.clear(); + return settings; +} + +test("Postfix Completion pairs the template list with a responsive code preview", async ({ workbench: page }) => { + await page.setViewportSize({ width: 1120, height: 760 }); + const settings = await openPostfixSettings(page); + const list = settings.getByRole("list", { name: "Postfix templates", exact: true }); + const preview = settings.getByRole("region", { name: "Template preview", exact: true }); + const before = preview.locator("pre").first(); + const after = preview.locator("pre").last(); + await expect(before).toHaveText("expr.if"); + await expect(after).toContainText("if (expr)"); + const listBounds = (await list.boundingBox())!; + const widePreview = (await preview.boundingBox())!; + expect(widePreview.x).toBeGreaterThanOrEqual(listBounds.x + listBounds.width); + expect(widePreview.y).toBeLessThan(listBounds.y); + + await list.getByRole("button", { name: ".if", exact: true }).press("ArrowDown"); + const elseTemplate = list.getByRole("button", { name: ".else", exact: true }); + await expect(elseTemplate).toBeFocused(); + await expect(elseTemplate).toHaveAttribute("aria-pressed", "true"); + await expect(before).toHaveText("expr.else"); + await expect(after).toContainText("if (!expr)"); + + await page.setViewportSize({ width: 520, height: 760 }); + await settings.getByRole("tab", { name: "Python", exact: true }).click(); + await expect(before).toHaveText("expr.if"); + await expect(after).toContainText("if expr:"); + await after.scrollIntoViewIfNeeded(); + await expect(after).toBeInViewport({ ratio: 0.99 }); + const narrowList = (await list.boundingBox())!; + const narrowPreview = (await preview.boundingBox())!; + expect(narrowPreview.y).toBeGreaterThanOrEqual(narrowList.y + narrowList.height); + expect(Math.abs(narrowList.x - narrowPreview.x)).toBeLessThan(2); + expect(await preview.evaluate((el) => el.scrollWidth - el.clientWidth)).toBeLessThanOrEqual(1); + + await settings.getByRole("tab", { name: "JavaScript / TypeScript", exact: true }).click(); + await expect(elseTemplate).toHaveAttribute("aria-pressed", "true"); + await expect(before).toHaveText("expr.else"); + + await settings.getByRole("tab", { name: "Java", exact: true }).click(); + await list.getByRole("button", { name: ".var", exact: true }).click(); + await expect(preview.getByText("Fallback expansion", { exact: true })).toBeVisible(); + await expect(after).toContainText("var value = expr;"); +}); + +test("Postfix Completion keeps template selection separate from enabled preferences", async ({ workbench: page }) => { + const settings = await openPostfixSettings(page); + const master = settings.getByRole("checkbox", { name: "Enable postfix completion", exact: true }); + const expand = settings.getByRole("checkbox", { name: "Expand templates with Tab", exact: true }); + const ifEnabled = settings.getByRole("checkbox", { name: "Enable .if", exact: true }); + const preview = settings.getByRole("region", { name: "Template preview", exact: true }); + const before = preview.locator("pre").first(); + await settings.getByRole("button", { name: ".else", exact: true }).click(); + await expect(ifEnabled).toBeChecked(); + await ifEnabled.uncheck(); + await expect(before).toHaveText("expr.if"); + await master.uncheck(); + await expect(expand).toBeDisabled(); + await expect(ifEnabled).toBeDisabled(); + await settings.getByRole("button", { name: ".else", exact: true }).click(); + await expect(before).toHaveText("expr.else"); + await master.check(); + await expect(ifEnabled).not.toBeChecked(); + + await settings.getByRole("button", { name: "OK", exact: true }).click(); + await openPostfixSettings(page); + await expect(ifEnabled).not.toBeChecked(); + await expect(master).toBeChecked(); +}); diff --git a/src/features/templates/settings/PostfixPage.tsx b/src/features/templates/settings/PostfixPage.tsx index 6b2bec52a..b6e93be93 100644 --- a/src/features/templates/settings/PostfixPage.tsx +++ b/src/features/templates/settings/PostfixPage.tsx @@ -1,73 +1,157 @@ /** Settings › Editor › Postfix Completion. */ -import { useMemo, useState } from "react"; +import { useCallback, useMemo, useState } from "react"; import clsx from "clsx"; import { Checkbox, T } from "@/ui"; -import { POSTFIX_GROUPS, POSTFIX_TEMPLATES } from "../postfix/model"; +import { listNavigate } from "@/ui/List"; +import { expandTemplate } from "../live/model"; +import { POSTFIX_GROUPS, POSTFIX_TEMPLATES, type PostfixTemplate } from "../postfix/model"; import { setPostfixTemplateEnabled, updatePostfixSettings, usePostfixSettings } from "../postfix/store"; import styles from "./Settings.module.css"; import { tx } from "@/platform/i18n"; +const EXAMPLE_EXPRESSION = "expr"; + +function PostfixPreview({ template }: { template: PostfixTemplate }) { + const preview = expandTemplate(template, { + predefined: { EXPR: EXAMPLE_EXPRESSION, TYPE: "Type", PAT: "pattern" }, + }).preview; + return ( +
+
+ .{template.key} + {template.description} +
+
+ + Before + +
+          
+            {EXAMPLE_EXPRESSION}.{template.key}
+          
+        
+
+
+ + {template.action ? "Fallback expansion" : "After"} + +
+          {preview}
+        
+
+
+ ); +} + export function PostfixPage() { const settings = usePostfixSettings(); const disabled = useMemo(() => new Set(settings.disabled), [settings.disabled]); const [group, setGroup] = useState(POSTFIX_GROUPS[0]!.id); + const [selections, setSelections] = useState>({}); const list = POSTFIX_TEMPLATES.filter((t) => t.id.startsWith(`${group}.`)); + const selectedIndex = Math.max( + 0, + list.findIndex((t) => t.id === selections[group]), + ); + const selected = list[selectedIndex]; + const select = (id: string) => setSelections((current) => ({ ...current, [group]: id })); + const revealSelected = useCallback((button: HTMLButtonElement | null) => { + button?.scrollIntoView?.({ block: "nearest", inline: "nearest" }); + }, []); return (
-
+
updatePostfixSettings({ enabled: v })} label="Enable postfix completion" /> - updatePostfixSettings({ expandWithTab: v })} - label="Expand templates with Tab" - /> - updatePostfixSettings({ showInCompletion: v })} - label="Show postfix templates in code completion" - /> +
+ updatePostfixSettings({ showInCompletion: v })} + label="Show postfix templates in code completion" + /> + updatePostfixSettings({ expandWithTab: v })} + label="Expand templates with Tab" + /> +
-
- Type an expression followed by .key, e.g. user.name.nn or{" "} - items.for - , then press Tab. -
-
- {POSTFIX_GROUPS.map((g) => ( - - ))} -
-
- {list.map((t) => ( -
- setPostfixTemplateEnabled(t.id, v)} - aria-label={tx`Enable .${t.key}`} - /> - .{t.key} - - {t.description} - + {settings.enabled && settings.expandWithTab && ( +
+ Type an expression followed by .key, e.g. user.name.nn or{" "} + items.for + , then press Tab. +
+ )} +
+
+
+ {POSTFIX_GROUPS.map((g) => ( + + ))}
- ))} +
{ + const next = listNavigate(event.key, { items: list, active: selectedIndex }); + if (next === null) return; + event.preventDefault(); + const template = list[next]; + if (!template) return; + select(template.id); + event.currentTarget.querySelectorAll("button")[next]?.focus(); + }} + > + {list.map((t) => { + const active = t.id === selected?.id; + return ( +
+ { + select(t.id); + setPostfixTemplateEnabled(t.id, v); + }} + aria-label={tx`Enable .${t.key}`} + /> + +
+ ); + })} +
+
+ {selected && }
); diff --git a/src/features/templates/settings/Settings.module.css b/src/features/templates/settings/Settings.module.css index a1976483c..86504554c 100644 --- a/src/features/templates/settings/Settings.module.css +++ b/src/features/templates/settings/Settings.module.css @@ -173,11 +173,40 @@ gap: var(--space-1-5); justify-content: flex-end; } +.postfixOptions, +.postfixDependentOptions { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: var(--space-1); +} +.postfixDependentOptions { + padding-inline-start: calc(var(--space-4) + var(--space-2)); +} +.postfixWorkspace { + display: grid; + grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr); + flex: 1; + min-height: 320px; + border: 1px solid var(--border-strong); + border-radius: var(--radius-small); + overflow: hidden; +} +.postfixBrowser { + display: flex; + flex-direction: column; + min-height: 0; + min-width: 0; + border-inline-end: 1px solid var(--border); +} +.postfixBrowser .tabs { + flex: none; + padding: var(--space-1); + border-bottom: 1px solid var(--border); +} .list { display: flex; flex-direction: column; - border: 1px solid var(--border-strong); - border-radius: var(--radius-small); overflow: auto; flex: 1; min-height: 0; @@ -185,13 +214,83 @@ .listRow { display: flex; align-items: center; - gap: 10px; + gap: var(--space-2); min-height: var(--h-row); padding: 0 8px; } .listRow:hover { background: var(--bg-hover); } +.listRow.itemSelected { + background: var(--bg-selected); +} +.listRow.itemSelected .itemDetail { + color: var(--fg); +} +.postfixChoice { + display: flex; + align-items: center; + gap: var(--space-2); + flex: 1; + min-width: 0; + height: var(--h-row); + padding: 0; + border: 0; + background: none; + color: inherit; + font: inherit; + text-align: start; +} +.postfixChoice:focus-visible { + outline-offset: -2px; +} +.postfixPreview { + display: flex; + flex-direction: column; + gap: var(--space-4); + min-width: 0; + overflow: auto; + padding: var(--space-3); +} +.postfixDescription { + display: flex; + align-items: baseline; + flex-wrap: wrap; + gap: var(--space-1) var(--space-2); + overflow-wrap: anywhere; + color: var(--fg-strong); +} +.postfixDescription code, +.postfixPreview pre { + font-family: var(--font-mono); +} +.postfixPreview pre { + margin: var(--space-1) 0 0; + padding: var(--space-2); + border: 1px solid var(--border); + border-radius: var(--radius-small); + background: var(--bg-app); + color: var(--fg-emphasis); + font-size: var(--fs-code); + line-height: var(--lh-code); + white-space: pre-wrap; + overflow-wrap: anywhere; + tab-size: 4; +} +@container settings-content (width < 600px) { + .postfixWorkspace { + grid-template-columns: minmax(0, 1fr); + flex: none; + min-height: 0; + } + .postfixBrowser { + border-inline-end: 0; + border-bottom: 1px solid var(--border); + } + .postfixBrowser .list { + max-height: clamp(128px, 24vh, 192px); + } +} .mono { font-family: var(--font-mono); color: var(--fg-emphasis); diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index aa9f33dd4..cf09c4ad7 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -1680,6 +1680,8 @@ "New Project": "New Project", "{path} already exists and is not empty": "{path} already exists and is not empty", "Example": "Example", + "Template preview": "Template preview", + "Fallback expansion": "Fallback expansion", "Shows how to use the plugin API; not maintained as a product.": "Shows how to use the plugin API; not maintained as a product.", "{count} downloads": "{count} downloads", "This plugin shows how to use the plugin API and is not maintained as a product. Build on it in your own repository.": "This plugin shows how to use the plugin API and is not maintained as a product. Build on it in your own repository.", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 92f239cfe..77c49c763 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -2196,6 +2196,8 @@ "Postfix Completion": "后缀补全", "Poetry Environment": "Poetry 环境", "Postfix templates": "后缀模板", + "Template preview": "模板预览", + "Fallback expansion": "备用展开结果", "Prefer": "首选", "Prefer moving caret line to minimize editor scrolling": "优先移动光标所在行,以尽量减少编辑器滚动", "Prefer scrolling editor canvas to keep caret line centered": "优先滚动编辑器画布,使光标所在行保持居中",