mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
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.
This commit is contained in:
@@ -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();
|
||||
});
|
||||
@@ -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 (
|
||||
<section className={styles.postfixPreview} aria-label={tx("Template preview")}>
|
||||
<div className={styles.postfixDescription}>
|
||||
<code>.{template.key}</code>
|
||||
<span>{template.description}</span>
|
||||
</div>
|
||||
<div>
|
||||
<span>
|
||||
<T>Before</T>
|
||||
</span>
|
||||
<pre>
|
||||
<code>
|
||||
{EXAMPLE_EXPRESSION}.{template.key}
|
||||
</code>
|
||||
</pre>
|
||||
</div>
|
||||
<div>
|
||||
<span>
|
||||
<T>{template.action ? "Fallback expansion" : "After"}</T>
|
||||
</span>
|
||||
<pre>
|
||||
<code>{preview}</code>
|
||||
</pre>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
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<Record<string, string>>({});
|
||||
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 (
|
||||
<div className={styles.page}>
|
||||
<div className={styles.options}>
|
||||
<div className={styles.postfixOptions}>
|
||||
<Checkbox
|
||||
checked={settings.enabled}
|
||||
onChange={(v) => updatePostfixSettings({ enabled: v })}
|
||||
label="Enable postfix completion"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={settings.expandWithTab}
|
||||
disabled={!settings.enabled}
|
||||
onChange={(v) => updatePostfixSettings({ expandWithTab: v })}
|
||||
label="Expand templates with Tab"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={settings.showInCompletion}
|
||||
disabled={!settings.enabled}
|
||||
onChange={(v) => updatePostfixSettings({ showInCompletion: v })}
|
||||
label="Show postfix templates in code completion"
|
||||
/>
|
||||
<div className={styles.postfixDependentOptions}>
|
||||
<Checkbox
|
||||
checked={settings.showInCompletion}
|
||||
disabled={!settings.enabled}
|
||||
onChange={(v) => updatePostfixSettings({ showInCompletion: v })}
|
||||
label="Show postfix templates in code completion"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={settings.expandWithTab}
|
||||
disabled={!settings.enabled}
|
||||
onChange={(v) => updatePostfixSettings({ expandWithTab: v })}
|
||||
label="Expand templates with Tab"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className={styles.hint}>
|
||||
<T>Type an expression followed by</T> <code>.key</code>, e.g. <code>user.name.nn</code> or{" "}
|
||||
<code>items.for</code>
|
||||
<T>, then press Tab.</T>
|
||||
</div>
|
||||
<div className={styles.tabs} role="tablist" aria-label={tx("Languages")}>
|
||||
{POSTFIX_GROUPS.map((g) => (
|
||||
<button
|
||||
key={g.id}
|
||||
type="button"
|
||||
role="tab"
|
||||
aria-selected={g.id === group}
|
||||
className={clsx(styles.tab, g.id === group && styles.tabActive)}
|
||||
onClick={() => setGroup(g.id)}
|
||||
>
|
||||
{g.title}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<div className={styles.list} role="list" aria-label={tx("Postfix templates")}>
|
||||
{list.map((t) => (
|
||||
<div key={t.id} className={styles.listRow} role="listitem">
|
||||
<Checkbox
|
||||
checked={!disabled.has(t.id)}
|
||||
disabled={!settings.enabled}
|
||||
onChange={(v) => setPostfixTemplateEnabled(t.id, v)}
|
||||
aria-label={tx`Enable .${t.key}`}
|
||||
/>
|
||||
<span className={styles.mono}>.{t.key}</span>
|
||||
<span className={styles.itemDetail} title={t.description}>
|
||||
{t.description}
|
||||
</span>
|
||||
{settings.enabled && settings.expandWithTab && (
|
||||
<div className={styles.hint}>
|
||||
<T>Type an expression followed by</T> <code>.key</code>, e.g. <code>user.name.nn</code> or{" "}
|
||||
<code>items.for</code>
|
||||
<T>, then press Tab.</T>
|
||||
</div>
|
||||
)}
|
||||
<div className={styles.postfixWorkspace}>
|
||||
<div className={styles.postfixBrowser}>
|
||||
<div className={styles.tabs} role="tablist" aria-label={tx("Languages")}>
|
||||
{POSTFIX_GROUPS.map((g) => (
|
||||
<button
|
||||
key={g.id}
|
||||
type="button"
|
||||
role="tab"
|
||||
aria-selected={g.id === group}
|
||||
className={clsx(styles.tab, g.id === group && styles.tabActive)}
|
||||
onClick={() => setGroup(g.id)}
|
||||
>
|
||||
{g.title}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
<div
|
||||
className={styles.list}
|
||||
role="list"
|
||||
aria-label={tx("Postfix templates")}
|
||||
onKeyDown={(event) => {
|
||||
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 (
|
||||
<div key={t.id} className={clsx(styles.listRow, active && styles.itemSelected)} role="listitem">
|
||||
<Checkbox
|
||||
checked={!disabled.has(t.id)}
|
||||
disabled={!settings.enabled}
|
||||
tabIndex={active ? undefined : -1}
|
||||
onChange={(v) => {
|
||||
select(t.id);
|
||||
setPostfixTemplateEnabled(t.id, v);
|
||||
}}
|
||||
aria-label={tx`Enable .${t.key}`}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
ref={active ? revealSelected : undefined}
|
||||
className={styles.postfixChoice}
|
||||
tabIndex={active ? 0 : -1}
|
||||
aria-label={`.${t.key}`}
|
||||
aria-pressed={active}
|
||||
title={t.description}
|
||||
onClick={() => select(t.id)}
|
||||
>
|
||||
<span className={styles.mono}>.{t.key}</span>
|
||||
<span className={styles.itemDetail}>{t.description}</span>
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
{selected && <PostfixPreview template={selected} />}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.",
|
||||
|
||||
@@ -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": "优先滚动编辑器画布,使光标所在行保持居中",
|
||||
|
||||
Reference in New Issue
Block a user