diff --git a/e2e/changeSignatureLayout.spec.ts b/e2e/changeSignatureLayout.spec.ts new file mode 100644 index 000000000..9c68512e5 --- /dev/null +++ b/e2e/changeSignatureLayout.spec.ts @@ -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", "taxRatesByJurisdiction"], + ["List", "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(); +}); diff --git a/src/editor/refactor/ChangeSignatureDialog.module.css b/src/editor/refactor/ChangeSignatureDialog.module.css index 4ee923e4f..e59baa100 100644 --- a/src/editor/refactor/ChangeSignatureDialog.module.css +++ b/src/editor/refactor/ChangeSignatureDialog.module.css @@ -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); } diff --git a/src/editor/refactor/ChangeSignatureDialog.tsx b/src/editor/refactor/ChangeSignatureDialog.tsx index 1fc82f810..616475a83 100644 --- a/src/editor/refactor/ChangeSignatureDialog.tsx +++ b/src/editor/refactor/ChangeSignatureDialog.tsx @@ -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(null); + const previewLabel = useId(); + const selectedRow = useRef(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(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 } > -
+
@@ -142,59 +164,83 @@ function ChangeSignatureDialogInner({ request }: { request: SignatureRequest }) onClick={() => move(1)} /> -
-
- - Type - - - Name - - - Default value - -
-
- {rows.map((row, i) => ( -
setSelected(i)} - > - update(row.key, { type: e.target.value })} - /> - update(row.key, { name: e.target.value })} - /> - = 0} - onChange={(e) => update(row.key, { defaultValue: e.target.value })} - /> -
- ))} -
+
+ + + + + + {hasNewParameters && ( + + )} + + + + {rows.map((row, i) => ( + setSelected(i)} + > + + + {hasNewParameters && ( + + )} + + ))} + +
+ Type + + Name + + Default value +
+ update(row.key, { type: e.target.value })} + /> + + update(row.key, { name: e.target.value })} + /> + + {row.originalIndex < 0 && ( + update(row.key, { defaultValue: e.target.value })} + /> + )} +
Delegate via overloading method} />
- Signature Preview -
{signatureText(signature)}
+ + Signature Preview + +
+            {signatureText(signature)}
+          
{(error ?? (signatureChanged(info, signature) ? problem : null)) && ( diff --git a/src/editor/refactor/signature.test.ts b/src/editor/refactor/signature.test.ts index f3db23f19..d5a33c34d 100644 --- a/src/editor/refactor/signature.test.ts +++ b/src/editor/refactor/signature.test.ts @@ -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()"); }); diff --git a/src/editor/refactor/signature.ts b/src/editor/refactor/signature.ts index 82cd570c6..3d2adcc02 100644 --- a/src/editor/refactor/signature.ts +++ b/src/editor/refactor/signature.ts @@ -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; }