Compare commits

...
5 changed files with 247 additions and 93 deletions

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
import type { MethodSignature } from "../src/bindings";
import { expect, stubDesktop, test } from "./fixtures";
const signature: MethodSignature = {
methodIdentifier: "invoice",
modifier: "",
returnType: "Invoice",
methodName: "prepareInvoiceForInternationalSettlement",
parameters: [
["Order", "order"],
["String", "billingCurrency"],
["BigDecimal", "amountBeforeDiscount"],
["Map<String, BigDecimal>", "taxRatesByJurisdiction"],
["List<Adjustment>", "settlementAdjustments"],
["RoundingMode", "roundingMode"],
["LocalDate", "scheduledSettlementDate"],
["boolean", "includeTaxBreakdown"],
].map(([type, name], originalIndex) => ({ type: type!, name: name!, originalIndex, defaultValue: "" })),
exceptions: [],
};
test("keeps parameter rows and a long signature preview usable in a short dialog", async ({ workbench: page }) => {
await page.setViewportSize({ width: 490, height: 520 });
await page.evaluate(async () => {
const workbench = "/src/workbench/contributions.ts";
const { editorService } = await import(/* @vite-ignore */ workbench);
await editorService().openFile("/dev/acme-api/InvoiceService.java");
});
const editor = page.locator(".cm-editor .cm-content");
await expect(editor).toBeVisible();
await stubDesktop(page);
const fontSize = await editor.evaluate(async (content, signature) => {
window.__ipc.handlers.lsp_signature_info = () => signature;
const viewModule = "/node_modules/.vite/deps/@codemirror_view.js";
const prefsModule = "/src/editor/core/cm/prefs.ts";
const { EditorView } = await import(/* @vite-ignore */ viewModule);
const { stepFontZoom } = await import(/* @vite-ignore */ prefsModule);
const base = Number.parseFloat(getComputedStyle(content).fontSize);
const view = EditorView.findFromDOM(content as HTMLElement)!;
stepFontZoom(view, 4);
view.focus();
return `${base + 4}px`;
}, signature);
await expect(editor).toHaveCSS("font-size", fontSize);
await page.keyboard.press("Meta+F6");
const dialog = page.getByRole("dialog", { name: "Change Signature" });
const table = dialog.getByRole("table", { name: "Parameters" });
const names = table.getByRole("textbox", { name: "Name", exact: true });
await expect(names.first()).toBeInViewport({ ratio: 1 });
const defaults = table.getByRole("columnheader", { name: "Default value", exact: true });
await expect(defaults).toHaveCount(0);
const preview = dialog.locator("pre");
await expect(preview).toBeInViewport({ ratio: 1 });
await expect(preview).toHaveCSS("font-size", fontSize);
await expect(names.first()).toHaveCSS("font-size", fontSize);
expect(
await preview.evaluate((el) => el.scrollHeight > el.clientHeight && el.scrollWidth <= el.clientWidth + 1),
).toBe(true);
await expect(dialog.getByRole("button", { name: "Cancel" })).toBeInViewport({ ratio: 1 });
await dialog.getByRole("button", { name: "Add", exact: true }).click();
await expect(defaults).toBeInViewport({ ratio: 1 });
const addedType = table.getByRole("textbox", { name: "Type", exact: true }).last();
await expect(addedType).toBeInViewport({ ratio: 1 });
await addedType.fill("boolean");
await names.last().fill("attachReceipt");
await table.getByRole("textbox", { name: "Default value", exact: true }).last().fill("false");
await dialog.getByRole("button", { name: "Move Up", exact: true }).click();
await expect(names.nth(7)).toHaveValue("attachReceipt");
await expect(preview).toContainText("boolean attachReceipt");
await expect(dialog.getByRole("button", { name: "Preview", exact: true })).toBeEnabled();
await page.keyboard.press("Escape");
await expect(dialog).toBeHidden();
});
@@ -1,7 +1,6 @@
.dialog [data-dialog-body] {
display: flex;
flex-direction: column;
overflow: hidden;
}
.body {
@@ -9,8 +8,8 @@
min-height: 0;
display: flex;
flex-direction: column;
gap: var(--space-3);
padding: 14px;
gap: var(--space-2);
padding: var(--space-3);
font-size: var(--fs-ui);
color: var(--fg);
}
@@ -18,54 +17,34 @@
.header {
flex: none;
display: grid;
grid-template-columns: 150px 1fr 1fr;
gap: var(--space-3);
grid-template-columns: minmax(0, 0.75fr) minmax(0, 1fr) minmax(0, 1.25fr);
align-items: end;
gap: var(--space-2);
}
.field {
flex: none;
display: flex;
flex-direction: column;
gap: var(--space-1-5);
gap: var(--space-1);
min-width: 0;
}
.parameters {
flex: 1 1 auto;
min-height: 0;
min-height: calc(var(--h-list-row) * 5);
display: flex;
flex-direction: column;
border: 1px solid var(--border);
border-radius: 4px;
border-radius: var(--radius-control);
overflow: hidden;
}
.toolbar {
flex: none;
display: flex;
gap: var(--space-1);
padding: 2px 4px;
border-bottom: 1px solid var(--border);
}
.table {
flex: 1 1 auto;
min-height: 0;
display: flex;
flex-direction: column;
}
.head,
.row {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
align-items: center;
gap: var(--space-2);
padding: 0 8px;
min-height: var(--h-list-row);
}
.head {
color: var(--fg-muted);
padding: var(--space-0-5) var(--space-1);
border-bottom: 1px solid var(--border);
}
@@ -73,6 +52,57 @@
flex: 1 1 auto;
min-height: 0;
overflow: auto;
scroll-padding-block-start: var(--h-list-row);
}
.table {
width: 100%;
table-layout: fixed;
border-spacing: 0;
}
.table :is(th, td) {
padding: 0 var(--space-2);
}
.table th {
position: sticky;
top: 0;
z-index: 1;
height: var(--h-list-row);
border-bottom: 1px solid var(--border);
background: var(--bg-dialog, var(--bg-elev));
color: var(--fg-muted);
text-align: start;
font-weight: 400;
}
.table th:first-child {
width: 42%;
}
.table th:first-child:nth-last-child(3) {
width: 36%;
}
.table th:nth-child(3) {
width: 28%;
}
.body .code,
.preview {
font-family: var(--signature-font-family, var(--font-mono));
font-size: var(--signature-font-size, var(--fs-code));
font-weight: var(--signature-font-weight, var(--fw-code));
}
.body .code {
width: 100%;
height: max(var(--h-field), calc(var(--signature-font-size, var(--fs-code)) + var(--space-2)));
}
.row .code:focus-visible {
box-shadow: inset 0 -1px var(--focus-ring);
}
.row:hover {
@@ -84,14 +114,16 @@
}
.preview {
height: clamp(56px, 18vh, 128px);
margin: 0;
padding: 6px 8px;
padding: var(--space-1-5) var(--space-2);
border: 1px solid var(--border);
border-radius: 4px;
font-family: var(--font-mono);
font-size: var(--fs-code);
border-radius: var(--radius-control);
overflow: auto;
white-space: pre-wrap;
overflow-wrap: anywhere;
line-height: 1.5;
user-select: text;
color: var(--fg);
background: var(--bg-input);
}
+100 -54
View File
@@ -1,5 +1,5 @@
/** IDEA's Change Signature dialog for Java methods (`signature.ts`). */
import { useEffect, useRef, useState } from "react";
import { useEffect, useId, useLayoutEffect, useRef, useState, type CSSProperties } from "react";
import type { MethodSignature } from "@/bindings";
import { Checkbox, Dialog, IconButton, Input, Select, Spinner, T, Toolbar } from "@/ui";
import { useTx } from "@/platform/i18n";
@@ -34,6 +34,16 @@ function ChangeSignatureDialogInner({ request }: { request: SignatureRequest })
const [delegate, setDelegate] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const previewLabel = useId();
const selectedRow = useRef<HTMLTableRowElement>(null);
const [codeStyle] = useState(() => {
const { fontFamily, fontSize, fontWeight } = getComputedStyle(request.view.contentDOM);
return {
"--signature-font-family": fontFamily,
"--signature-font-size": fontSize,
"--signature-font-weight": fontWeight,
} as CSSProperties;
});
const signature: MethodSignature = {
...info,
@@ -45,6 +55,10 @@ function ChangeSignatureDialogInner({ request }: { request: SignatureRequest })
const problem = signatureProblem(signature);
const ready = !busy && !problem && signatureChanged(info, signature);
const current = Math.min(selected, rows.length - 1);
const hasNewParameters = rows.some((row) => row.originalIndex < 0);
useLayoutEffect(() => {
selectedRow.current?.scrollIntoView({ block: "nearest", inline: "nearest" });
}, [current, rows.length]);
// Closing the dialog cancels a refactoring still being checked: it applies nothing.
const running = useRef<AbortController | null>(null);
@@ -93,7 +107,8 @@ function ChangeSignatureDialogInner({ request }: { request: SignatureRequest })
title="Change Signature"
className={styles.dialog}
width={640}
minHeight={380}
minWidth={360}
minHeight={360}
maxHeight={640}
resizable="both"
buttons={[
@@ -110,7 +125,7 @@ function ChangeSignatureDialogInner({ request }: { request: SignatureRequest })
) : undefined
}
>
<div className={styles.body}>
<div className={styles.body} style={codeStyle}>
<div className={styles.header}>
<label className={styles.field}>
<T>Visibility:</T>
@@ -123,11 +138,18 @@ function ChangeSignatureDialogInner({ request }: { request: SignatureRequest })
</label>
<label className={styles.field}>
<T>Return type:</T>
<Input mono value={returnType} onChange={(e) => setReturnType(e.target.value)} />
<Input className={styles.code} mono value={returnType} onChange={(e) => setReturnType(e.target.value)} />
</label>
<label className={styles.field}>
<T>Name:</T>
<Input mono autoFocus autoSelect value={methodName} onChange={(e) => setMethodName(e.target.value)} />
<Input
className={styles.code}
mono
data-autofocus
autoSelect
value={methodName}
onChange={(e) => setMethodName(e.target.value)}
/>
</label>
</div>
<div className={styles.parameters}>
@@ -142,59 +164,83 @@ function ChangeSignatureDialogInner({ request }: { request: SignatureRequest })
onClick={() => move(1)}
/>
</Toolbar>
<div className={styles.table} role="table" aria-label={tx("Parameters")}>
<div className={styles.head} role="row">
<span role="columnheader">
<T>Type</T>
</span>
<span role="columnheader">
<T>Name</T>
</span>
<span role="columnheader">
<T>Default value</T>
</span>
</div>
<div className={styles.rows}>
{rows.map((row, i) => (
<div
key={row.key}
className={styles.row}
role="row"
aria-selected={i === current}
onFocusCapture={() => setSelected(i)}
>
<Input
mono
variant="bare"
aria-label={tx("Type")}
value={row.type}
autoFocus={row.originalIndex < 0 && !row.type}
onChange={(e) => update(row.key, { type: e.target.value })}
/>
<Input
mono
variant="bare"
aria-label={tx("Name")}
value={row.name}
onChange={(e) => update(row.key, { name: e.target.value })}
/>
<Input
mono
variant="bare"
aria-label={tx("Default value")}
value={row.originalIndex < 0 ? row.defaultValue : ""}
disabled={row.originalIndex >= 0}
onChange={(e) => update(row.key, { defaultValue: e.target.value })}
/>
</div>
))}
</div>
<div className={styles.rows}>
<table className={styles.table} aria-label={tx("Parameters")}>
<thead>
<tr>
<th scope="col">
<T>Type</T>
</th>
<th scope="col">
<T>Name</T>
</th>
{hasNewParameters && (
<th scope="col">
<T>Default value</T>
</th>
)}
</tr>
</thead>
<tbody>
{rows.map((row, i) => (
<tr
key={row.key}
ref={i === current ? selectedRow : undefined}
className={styles.row}
aria-selected={i === current}
onFocusCapture={() => setSelected(i)}
>
<td>
<Input
className={styles.code}
mono
variant="bare"
aria-label={tx("Type")}
value={row.type}
title={row.type}
autoFocus={row.originalIndex < 0 && !row.type}
onChange={(e) => update(row.key, { type: e.target.value })}
/>
</td>
<td>
<Input
className={styles.code}
mono
variant="bare"
aria-label={tx("Name")}
value={row.name}
title={row.name}
onChange={(e) => update(row.key, { name: e.target.value })}
/>
</td>
{hasNewParameters && (
<td>
{row.originalIndex < 0 && (
<Input
className={styles.code}
mono
variant="bare"
aria-label={tx("Default value")}
value={row.defaultValue}
onChange={(e) => update(row.key, { defaultValue: e.target.value })}
/>
)}
</td>
)}
</tr>
))}
</tbody>
</table>
</div>
</div>
<Checkbox checked={delegate} onChange={setDelegate} label={<T>Delegate via overloading method</T>} />
<div className={styles.field}>
<T>Signature Preview</T>
<pre className={styles.preview}>{signatureText(signature)}</pre>
<span id={previewLabel}>
<T>Signature Preview</T>
</span>
<pre className={styles.preview} role="region" aria-labelledby={previewLabel} tabIndex={0}>
{signatureText(signature)}
</pre>
</div>
{(error ?? (signatureChanged(info, signature) ? problem : null)) && (
<span className={styles.error} role="alert">
+1 -1
View File
@@ -16,7 +16,7 @@ const info: MethodSignature = {
describe("change signature", () => {
it("previews the signature like the declaration", () => {
expect(signatureText(info)).toBe("public int sum(int a, int b) throws java.io.IOException");
expect(signatureText(info)).toBe("public int sum(\n int a,\n int b\n) throws java.io.IOException");
expect(signatureText({ ...info, modifier: "", parameters: [], exceptions: [] })).toBe("int sum()");
});
+4 -3
View File
@@ -36,7 +36,7 @@ export const parameterRow = (p: SignatureParameter): ParameterRow => ({ ...p, ke
export const newParameter = (): ParameterRow =>
parameterRow({ type: "", name: "", defaultValue: "", originalIndex: -1 });
/** `public int sum(int a, int b) throws IOException`, as the dialog's preview shows it. */
/** Formats the declaration for the dialog's preview. */
export function signatureText(sig: {
modifier: string;
returnType: string;
@@ -44,8 +44,9 @@ export function signatureText(sig: {
parameters: readonly SignatureParameter[];
exceptions?: readonly { type: string }[];
}): string {
const params = sig.parameters.map((p) => `${p.type} ${p.name}`.trim()).join(", ");
const head = [sig.modifier, sig.returnType, `${sig.methodName}(${params})`].filter(Boolean).join(" ");
const params = sig.parameters.map((p) => `${p.type} ${p.name}`.trim());
const args = params.length > 1 ? `\n ${params.join(",\n ")}\n` : (params[0] ?? "");
const head = [sig.modifier, sig.returnType, `${sig.methodName}(${args})`].filter(Boolean).join(" ");
const throws = sig.exceptions?.length ? ` throws ${sig.exceptions.map((e) => e.type).join(", ")}` : "";
return head + throws;
}