diff --git a/e2e/usagePreviewLayout.spec.ts b/e2e/usagePreviewLayout.spec.ts new file mode 100644 index 000000000..823070c25 --- /dev/null +++ b/e2e/usagePreviewLayout.spec.ts @@ -0,0 +1,95 @@ +import type { Page } from "@playwright/test"; +import { expect, stubDesktop, test } from "./fixtures"; + +async function setEditorMetrics(page: Page, fontSize: number, lineHeight: number) { + await page.evaluate( + async ({ fontSize, lineHeight }) => { + 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, fontSize, lineHeight } } }); + }, + { fontSize, lineHeight }, + ); +} + +async function openResults(page: Page) { + await page.setViewportSize({ width: 1120, height: 720 }); + await stubDesktop(page); + await setEditorMetrics(page, 20, 34); + await page.evaluate(async () => { + const lines = Array.from({ length: 1240 }, () => ""); + lines[1233] = " record(orderTotal);"; + lines[1237] = " return orderTotal;"; + window.__ipc.handlers.fs_read_text = () => lines.join("\n"); + const actionsModule = "/src/platform/actions.ts"; + const { executeAction } = await import(/* @vite-ignore */ actionsModule); + await executeAction("find.showUsageResults", { + symbol: "orderTotal", + locations: [ + { + path: "/dev/acme-api/src/orderTotals.ts", + range: { start: { line: 1233, col: 9 }, end: { line: 1233, col: 19 } }, + }, + { + path: "/dev/acme-api/src/orderTotals.ts", + range: { start: { line: 1237, col: 9 }, end: { line: 1237, col: 19 } }, + }, + ], + }); + }); + const view = page.getByRole("region", { name: "Find Usages", exact: true }); + await view.getByRole("treeitem").filter({ hasText: "1234" }).click(); + const preview = view.getByRole("region", { name: "Preview", exact: true }); + await expect(preview.locator("mark")).toHaveCount(2); + return { view, preview }; +} + +test("Find Usages preview follows editor line height and fits long line numbers", async ({ workbench: page }) => { + const { preview } = await openResults(page); + await expect(preview).toHaveCSS("font-size", "20px"); + await expect(preview).toHaveCSS("line-height", "34px"); + + const number = preview.getByText("1234", { exact: true }); + const gutter = await number.evaluate((element) => { + const range = document.createRange(); + range.selectNodeContents(element); + const text = range.getBoundingClientRect(); + const box = element.getBoundingClientRect(); + return { textInset: text.left - box.left, width: box.width }; + }); + expect(gutter.textInset).toBeGreaterThanOrEqual(1); + await expect(number).toBeInViewport({ ratio: 0.98 }); + expect((await preview.getByText("999", { exact: true }).boundingBox())!.width).toBeCloseTo(gutter.width, 0); + + await page.setViewportSize({ width: 560, height: 720 }); + await setEditorMetrics(page, 20, 39); + await expect(preview).toHaveCSS("line-height", "39px"); + await expect(number).toBeInViewport({ ratio: 0.98 }); +}); + +test("Find Usages starts with equal panes and keeps a separate saved size for each orientation", async ({ + workbench: page, +}) => { + const { view } = await openResults(page); + const divider = view.locator('[id="usages-preview-split"]').getByRole("separator"); + await expect(divider).toHaveAttribute("aria-valuenow", "50"); + await divider.press("ArrowRight"); + await expect(divider).not.toHaveAttribute("aria-valuenow", "50"); + const horizontal = (await divider.getAttribute("aria-valuenow"))!; + + await page.setViewportSize({ width: 560, height: 720 }); + await expect(divider).toHaveAttribute("aria-orientation", "horizontal"); + await expect(divider).toHaveAttribute("aria-valuenow", "50"); + await divider.press("ArrowDown"); + await expect(divider).not.toHaveAttribute("aria-valuenow", "50"); + const vertical = (await divider.getAttribute("aria-valuenow"))!; + + await page.setViewportSize({ width: 1120, height: 720 }); + await expect(divider).toHaveAttribute("aria-orientation", "vertical"); + await expect(divider).toHaveAttribute("aria-valuenow", horizontal); + await page.setViewportSize({ width: 560, height: 720 }); + await expect(divider).toHaveAttribute("aria-valuenow", vertical); +}); diff --git a/src/features/code-insight/UsagesView.module.css b/src/features/code-insight/UsagesView.module.css index 8e468a66e..bb0a657c0 100644 --- a/src/features/code-insight/UsagesView.module.css +++ b/src/features/code-insight/UsagesView.module.css @@ -159,7 +159,7 @@ flex-direction: column; font-family: var(--font-mono); font-size: var(--fs-code); - line-height: 1.45; + line-height: var(--lh-code); } .sourcePreview .sourceText[role="region"] { font: inherit; diff --git a/src/features/code-insight/UsagesView.tsx b/src/features/code-insight/UsagesView.tsx index e39bd8a81..88e725583 100644 --- a/src/features/code-insight/UsagesView.tsx +++ b/src/features/code-insight/UsagesView.tsx @@ -406,6 +406,8 @@ export function useUsagesHeaderMenu(): MenuItem[] { ]; } +const DEFAULT_PREVIEW_LAYOUT = { usages: 50, preview: 50 }; + function UsagesSplit({ preview, children }: { preview: ReactNode; children: ReactNode }) { const show = useUsagesOptions((s) => s.preview); const frame = useRef(null); @@ -427,12 +429,13 @@ function UsagesSplit({ preview, children }: { preview: ReactNode; children: Reac orientation={stacked ? "vertical" : "horizontal"} id="usages-preview-split" storageKey={stacked ? "openidea.usages.previewSplit.vertical" : "openidea.usages.previewSplit"} + defaultLayout={DEFAULT_PREVIEW_LAYOUT} > {children} - + {preview} @@ -455,13 +458,21 @@ function UsagesPreview({ node }: { node: Node | null }) { return (group?.usages ?? []).map((u) => ({ line: u.line, ranges: [[u.col, u.endCol] as const] })); }, [usage, search?.groups]); return ( -
+
diff --git a/src/ui/SourcePreview.module.css b/src/ui/SourcePreview.module.css index 82a2b95e7..408068eb7 100644 --- a/src/ui/SourcePreview.module.css +++ b/src/ui/SourcePreview.module.css @@ -41,8 +41,8 @@ flex: none; position: sticky; left: 0; - width: 48px; - padding-right: 12px; + width: var(--source-preview-gutter-width, 48px); + padding-right: var(--space-3); text-align: right; color: var(--fg-gutter); background-color: var(--bg-app); diff --git a/src/ui/SourcePreview.tsx b/src/ui/SourcePreview.tsx index 6ae417563..161779ec6 100644 --- a/src/ui/SourcePreview.tsx +++ b/src/ui/SourcePreview.tsx @@ -9,7 +9,7 @@ * Usages. Files beyond `MAX_PARSE` stay plain rather than colouring the window * with the wrong block-comment context. */ -import { useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from "react"; +import { useEffect, useLayoutEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode } from "react"; import clsx from "clsx"; import { T } from "./T"; import { tokenSpans, type SourceToken } from "./tokenSpans"; @@ -46,6 +46,8 @@ export interface SourcePreviewProps { version?: number; /** Stable (module-level) syntax colouring; without it lines stay plain. */ colorize?: SourceColorizer; + /** Fit the gutter to the line numbers and keep the selected line visible when the pane resizes. */ + editorLayout?: boolean; className?: string; } @@ -56,6 +58,7 @@ export function SourcePreview({ load, version = 0, colorize, + editorLayout, className, }: SourcePreviewProps) { const [text, setText] = useState<{ path: string; lines: string[] } | null>(null); @@ -160,6 +163,7 @@ export function SourcePreview({ lines={current.lines} tokensByLine={tokensByLine} highlights={highlights} + editorLayout={editorLayout} className={className} /> ); @@ -176,6 +180,7 @@ function PreviewBody({ lines, tokensByLine, highlights, + editorLayout, className, }: { path: string; @@ -183,6 +188,7 @@ function PreviewBody({ lines: string[]; tokensByLine: Map | null; highlights: readonly SourceHighlight[]; + editorLayout?: boolean; className?: string; }) { const scrollRef = useRef(null); @@ -192,8 +198,21 @@ function PreviewBody({ useLayoutEffect(() => { const box = scrollRef.current; const el = box?.querySelector(`[data-line="${line}"]`); - if (box && el) box.scrollTop = el.offsetTop - box.clientHeight / 2 + el.offsetHeight / 2; - }, [line, path, lines]); + if (!box || !el) return; + const center = () => { + box.scrollTop = el.offsetTop - box.clientHeight / 2 + el.offsetHeight / 2; + }; + center(); + if (!editorLayout || typeof ResizeObserver === "undefined") return; + const observer = new ResizeObserver(() => { + if (el.offsetTop < box.scrollTop || el.offsetTop + el.offsetHeight > box.scrollTop + box.clientHeight) { + center(); + } + }); + observer.observe(box); + observer.observe(el); + return () => observer.disconnect(); + }, [editorLayout, line, path, lines]); const byLine = new Map(highlights.map((h) => [h.line, h.ranges])); const start = Math.max(0, line - WINDOW); @@ -214,7 +233,18 @@ function PreviewBody({ } return (
-
{rows}
+
+        {rows}
+      
); }