Compare commits

...
Author SHA1 Message Date
l0ng-ai 2210278cf5 fix(search): improve structural template layout 2026-11-23 10:07:58 +08:00
3 changed files with 146 additions and 33 deletions

No files matched your search

+81
View File
@@ -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();
});
+23 -5
View File
@@ -214,21 +214,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;
+42 -28
View File
@@ -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<StructuralForm>(loadStructuralForm);
const [scope, setScope] = useState<ScopeChoice>(() => 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" },
]}
>
<div className={styles.form}>
<div className={styles.fieldRow}>
<span>
<T>Language</T>
</span>
<Select
value={language || null}
options={options}
onChange={(language) => setForm({ ...form, language })}
placeholder={languages ? "No languages" : "Loading…"}
className={styles.languageSelect}
aria-label="Language"
<div className={clsx(styles.form, replace && styles.replaceForm)}>
<div className={styles.templateSection}>
<div className={styles.templateHeader}>
<label htmlFor="ssr-template">
<T>Search template</T>
</label>
<Select
value={language || null}
options={options}
onChange={(language) => setForm({ ...form, language })}
placeholder={languages ? "No languages" : "Loading…"}
className={styles.languageSelect}
variant="ghost"
aria-label="Language"
/>
</div>
<textarea
id="ssr-template"
className={styles.template}
style={templateFont}
spellCheck={false}
value={form.pattern}
placeholder="$a$.equals($b$)"
onChange={(e) => setForm({ ...form, pattern: e.target.value })}
data-autofocus=""
/>
{error && <div className={styles.error}>{error}</div>}
</div>
<label className={styles.fieldLabel} htmlFor="ssr-template">
<T>Search template</T>
</label>
<textarea
id="ssr-template"
className={styles.template}
spellCheck={false}
value={form.pattern}
placeholder="$a$.equals($b$)"
onChange={(e) => setForm({ ...form, pattern: e.target.value })}
data-autofocus=""
/>
{error && <div className={styles.error}>{error}</div>}
{replace && (
<>
<label className={styles.fieldLabel} htmlFor="ssr-replacement">
<div className={styles.templateSection}>
<label htmlFor="ssr-replacement">
<T>Replace template</T>
</label>
<textarea
id="ssr-replacement"
className={styles.template}
style={templateFont}
spellCheck={false}
value={form.replacement}
placeholder="Objects.equals($a$, $b$)"
onChange={(e) => setForm({ ...form, replacement: e.target.value })}
/>
</>
</div>
)}
<div className={styles.hint}>
<T>Placeholders:</T> <code>$name$</code> or <code>$NAME</code> <T>match one node,</T> <code>$$$NAME</code>{" "}