From ca778e2382db96da1a8a4178ff005d0cb9a07d58 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Mon, 23 Nov 2026 12:08:57 +0800 Subject: [PATCH] fix(search): improve structural template layout (#5298) - Put the language picker in the Search template header and keep each label next to its template. - Give both templates 150 px of editing space in a standard window. Tighten the editors on shorter screens and keep the dialog footer accessible. - Use the configured editor font, size, and line height in both templates. --- e2e/structuralTemplateLayout.spec.ts | 81 +++++++++++++++++++++++ src/features/analysis/Analysis.module.css | 28 ++++++-- src/features/analysis/AnalysisDialogs.tsx | 70 ++++++++++++-------- 3 files changed, 146 insertions(+), 33 deletions(-) create mode 100644 e2e/structuralTemplateLayout.spec.ts diff --git a/e2e/structuralTemplateLayout.spec.ts b/e2e/structuralTemplateLayout.spec.ts new file mode 100644 index 000000000..2f80a2c5e --- /dev/null +++ b/e2e/structuralTemplateLayout.spec.ts @@ -0,0 +1,81 @@ +import type { Page } from "@playwright/test"; +import { expect, ipcCalls, stubDesktop, test } from "./fixtures"; + +async function openDialog(page: Page, id: "find.structural" | "find.replaceStructural") { + await stubDesktop(page); + await page.evaluate(async (id) => { + window.__ipc.handlers.structural_languages = () => [ + { id: "java", name: "Java" }, + { id: "typescript", name: "TypeScript" }, + ]; + window.__ipc.handlers.structural_validate = () => null; + const actionsModule = "/src/platform/actions.ts"; + const { executeAction } = await import(/* @vite-ignore */ actionsModule); + await executeAction(id); + }, id); +} + +test("Structural Search puts language selection beside the taller template editor", async ({ workbench: page }) => { + await page.setViewportSize({ width: 1080, height: 720 }); + await openDialog(page, "find.structural"); + const dialog = page.getByRole("dialog", { name: "Structural Search" }); + const editor = dialog.getByRole("textbox", { name: "Search template" }); + const label = dialog.locator("label").filter({ hasText: /^Search template$/ }); + const language = dialog.getByRole("button", { name: "Language", exact: true }); + await expect(editor).toBeFocused(); + await expect(language).toHaveText("Java"); + + const { x, y, width, height } = (await editor.boundingBox())!; + const caption = (await label.boundingBox())!; + const picker = (await language.boundingBox())!; + expect(Math.abs(caption.y + caption.height / 2 - (picker.y + picker.height / 2))).toBeLessThanOrEqual(1); + expect(Math.abs(caption.x - x)).toBeLessThanOrEqual(1); + expect(Math.abs(picker.x + picker.width - (x + width))).toBeLessThanOrEqual(1); + expect(y - (picker.y + picker.height)).toBeLessThanOrEqual(6); + expect(Math.abs(height - 150)).toBeLessThanOrEqual(1); + + await language.click(); + await page.getByRole("option", { name: "TypeScript" }).click(); + await editor.fill("console.log($value$)"); + await expect + .poll(() => ipcCalls(page, "structural_validate")) + .toContainEqual({ + query: { language: "typescript", pattern: "console.log($value$)", replacement: null, maxMatches: 0 }, + }); + await expect(dialog.getByRole("button", { name: "Find", exact: true })).toBeEnabled(); +}); + +test("Structural Replace uses editor typography and fits the template controls in a narrow window", async ({ + workbench: page, +}) => { + await page.setViewportSize({ width: 400, height: 620 }); + await openDialog(page, "find.replaceStructural"); + const dialog = page.getByRole("dialog", { name: "Structural Replace" }); + const search = dialog.getByRole("textbox", { name: "Search template" }); + const replacement = dialog.getByRole("textbox", { name: "Replace template" }); + await expect(search).toBeFocused(); + await page.evaluate(async () => { + const projectModule = "/src/platform/project.ts"; + const { useProject } = await import(/* @vite-ignore */ projectModule); + const { settings, project } = useProject.getState(); + const current = settings ?? project?.settings; + if (!current) throw new Error("Expected project settings"); + useProject.setState({ + settings: { ...current, editor: { ...current.editor, fontFamily: "Consolas", fontSize: 17, lineHeight: 26 } }, + }); + }); + + for (const template of [search, replacement]) { + await expect(template).toHaveCSS("font-family", /^Consolas,/); + await expect(template).toHaveCSS("font-size", "17px"); + await expect(template).toHaveCSS("line-height", "26px"); + const box = (await template.boundingBox())!; + expect(box.x).toBeGreaterThanOrEqual(0); + expect(box.x + box.width).toBeLessThanOrEqual(400); + expect(Math.abs(box.height - 105)).toBeLessThanOrEqual(1); + } + await expect(dialog.getByRole("radio", { name: "Whole project" })).toBeInViewport(); + await replacement.fill("Objects.equals($a$, $b$)"); + await expect(replacement).toHaveValue("Objects.equals($a$, $b$)"); + await expect(dialog.getByRole("button", { name: "Cancel" })).toBeInViewport(); +}); diff --git a/src/features/analysis/Analysis.module.css b/src/features/analysis/Analysis.module.css index d09df0480..3e3ddf7bf 100644 --- a/src/features/analysis/Analysis.module.css +++ b/src/features/analysis/Analysis.module.css @@ -231,21 +231,39 @@ flex: 1 1 12ch; min-width: 0; } +.templateSection { + display: flex; + flex-direction: column; + gap: var(--space-1); + color: var(--fg); + font-size: var(--fs-ui); +} +.templateHeader { + display: flex; + align-items: center; + justify-content: space-between; + gap: var(--space-2); +} .languageSelect { - min-width: 180px; + max-width: 50%; } .template { width: 100%; - min-height: 96px; + height: clamp(96px, 24vh, 150px); + min-height: 50px; resize: vertical; - padding: 6px 8px; + padding: var(--space-1-5) var(--space-2); border: 1px solid var(--border-strong); border-radius: var(--radius-control); - background: var(--bg-input); + background: var(--bg-editor); color: var(--fg-editor); font-family: var(--font-mono); font-size: var(--fs-code); - line-height: 1.5; + line-height: var(--lh-code); +} +.replaceForm .template { + /* Leave room for the scope and dialog controls when both templates are shown. */ + height: clamp(96px, calc(50vh - 205px), 150px); } .template:focus { outline: none; diff --git a/src/features/analysis/AnalysisDialogs.tsx b/src/features/analysis/AnalysisDialogs.tsx index 653ce7aee..df9fc1fd5 100644 --- a/src/features/analysis/AnalysisDialogs.tsx +++ b/src/features/analysis/AnalysisDialogs.tsx @@ -1,5 +1,6 @@ /** Inspect Code, Locate Duplicates and Search / Replace Structurally dialogs. */ import { useEffect, useMemo, useState } from "react"; +import clsx from "clsx"; import { call, commands, @@ -10,6 +11,8 @@ import { type WorkspaceEdit, } from "@/platform/ipc"; import { executeAction } from "@/platform/actions"; +import { editorPrefs } from "@/platform/editorSettings"; +import { monoFontFamily } from "@/platform/fonts"; import { activeProfileId, listProfiles, validateInspections, type ProfileId } from "@/platform/inspections"; import { basename, relativePath, useProject } from "@/platform/project"; import { Button, Checkbox, Dialog, Input, Select, Spinner, T } from "@/ui"; @@ -319,6 +322,15 @@ function StructuralDialog({ replace, onClose }: { replace: boolean; onClose: () const [form, setForm] = useState(loadStructuralForm); const [scope, setScope] = useState(() => defaultScope()); const [busy, setBusy] = useState(false); + const settings = useProject((s) => s.settings); + const font = settings?.editor; + const fallback = editorPrefs(settings).fallbackFont; + const templateFont = { + fontFamily: + font?.fontFamily || fallback ? monoFontFamily(font?.fontFamily, fallback, "var(--font-mono)") : undefined, + fontSize: font?.fontSize ?? undefined, + lineHeight: font?.lineHeight != null ? `${font.lineHeight}px` : undefined, + }; const languages = useStructuralLanguages(); // The editor's language when it has a grammar, else the first supported one. const language = @@ -357,47 +369,49 @@ function StructuralDialog({ replace, onClose }: { replace: boolean; onClose: () { label: "Cancel", role: "cancel" }, ]} > -
-
- - Language - - setForm({ ...form, language })} + placeholder={languages ? "No languages" : "Loading…"} + className={styles.languageSelect} + variant="ghost" + aria-label="Language" + /> +
+