From fd8f0eb99cfbaa9b760a0e5a4398b8e2680fa10f Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 20 Nov 2026 02:52:46 +0800 Subject: [PATCH] fix(editor): make quick documentation fit narrow windows (#4568) Quick Documentation used a fixed 600px cap and kept its caret anchor after the window shrank, so short documents looked cramped and the actions could end up off screen. The popup now sizes to a 300-500px content width, keeps its actions together in the lower-right corner while the document scrolls, and clamps the anchor to the current viewport. --- e2e/quickDocumentationLayout.spec.ts | 73 ++++++++++++++++++++++++++++ src/editor/lsp/Popups.module.css | 19 +++++++- src/editor/lsp/Popups.tsx | 15 +++++- 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 e2e/quickDocumentationLayout.spec.ts diff --git a/e2e/quickDocumentationLayout.spec.ts b/e2e/quickDocumentationLayout.spec.ts new file mode 100644 index 000000000..19d33315b --- /dev/null +++ b/e2e/quickDocumentationLayout.spec.ts @@ -0,0 +1,73 @@ +import type { Page } from "@playwright/test"; +import { expect, openFromTree, stubDesktop, test } from "./fixtures"; + +async function quickDocumentation(page: Page, markdown: string) { + const editor = await openFromTree(page, "build.gradle.kts"); + await stubDesktop(page); + await page.evaluate((value) => { + window.__ipc.handlers.lsp_hover = () => ({ contents: { kind: "markdown", value }, range: null }); + }, markdown); + const line = editor.locator(".cm-line", { hasText: "return order.toDto()" }); + await line.click(); + await page.keyboard.press("End"); + await page.keyboard.press("ArrowLeft"); + await page.keyboard.press("ArrowLeft"); + await page.keyboard.press("ArrowLeft"); + await page.keyboard.press("F1"); + const popup = page.getByRole("dialog", { name: "Documentation", exact: true }); + await expect(popup.locator(".cm-lsp-markdown")).toBeVisible(); + return popup; +} + +test("short quick documentation uses a readable width and a compact corner of actions", async ({ workbench: page }) => { + const popup = await quickDocumentation(page, "Returns the total."); + const back = popup.getByRole("button", { name: "Back", exact: true }); + const dock = popup.getByRole("button", { name: "Show in Documentation Tool Window" }); + await expect(dock).toBeVisible(); + const bounds = (await popup.boundingBox())!; + expect(bounds.width).toBeGreaterThanOrEqual(298); + expect(bounds.width).toBeLessThan(320); + const backBounds = (await back.boundingBox())!; + const dockBounds = (await dock.boundingBox())!; + expect(backBounds.x).toBeGreaterThan(bounds.x + bounds.width / 2); + expect(bounds.x + bounds.width - dockBounds.x - dockBounds.width).toBeLessThan(16); + await expect(popup.locator(".cm-lsp-markdown")).toHaveCSS("font-size", "13px"); + await page.keyboard.press("Escape"); + await expect(popup).toBeHidden(); +}); + +test("long quick documentation scrolls with its actions in view", async ({ workbench: page }) => { + const signature = "fun calculateTotal(order: Order, options: ExtendedPricingOptions): CalculationResult"; + const markdown = [ + `\`\`\`kotlin\n${signature}\n\`\`\``, + ...Array.from({ length: 22 }, (_, i) => { + return `Section ${i + 1}. Explains the applicable pricing rules, shipping restrictions and currency options.`; + }), + "End of pricing documentation.", + ].join("\n\n"); + const popup = await quickDocumentation(page, markdown); + const dock = popup.getByRole("button", { name: "Show in Documentation Tool Window" }); + await expect(dock).toBeVisible(); + expect((await popup.boundingBox())!.width).toBeLessThan(508); + await page.setViewportSize({ width: 430, height: 500 }); + await expect + .poll(async () => { + const box = (await popup.boundingBox())!; + return Math.max(-box.x, -box.y, box.x + box.width - 430, box.y + box.height - 500); + }) + .toBeLessThanOrEqual(1); + const body = popup.locator(".cm-lsp-markdown").locator(".."); + const last = popup.getByText("End of pricing documentation."); + const dockBefore = (await dock.boundingBox())!; + await expect(body.locator("pre")).toContainText(signature); + const scroll = await body.evaluate((node) => { + node.scrollTop = node.scrollHeight; + return { top: node.scrollTop, horizontalOverflow: node.scrollWidth - node.clientWidth }; + }); + expect(scroll.top).toBeGreaterThan(0); + expect(scroll.horizontalOverflow).toBeLessThanOrEqual(1); + const lastBounds = (await last.boundingBox())!; + expect(lastBounds.y + lastBounds.height).toBeLessThanOrEqual(dockBefore.y + 1); + expect(await dock.boundingBox()).toEqual(dockBefore); + await expect(dock).toBeInViewport(); +}); diff --git a/src/editor/lsp/Popups.module.css b/src/editor/lsp/Popups.module.css index e6eefb4d8..707f1921f 100644 --- a/src/editor/lsp/Popups.module.css +++ b/src/editor/lsp/Popups.module.css @@ -238,6 +238,10 @@ flex-direction: column; overflow: hidden; } +.docPopup:not([data-resized]) { + width: max-content; + min-width: min(300px, 90vw); +} .docScope { display: contents; } @@ -246,21 +250,32 @@ } .docPopup .docFrame { flex: 1 1 auto; + min-width: 0; min-height: 0; display: flex; flex-direction: column; } .docPopup:not([data-resized]) .docFrame { - max-width: min(600px, 90vw); - max-height: 420px; + max-width: min(500px, 90vw); + max-height: 500px; } .docPopup .docBody { flex: 1 1 auto; min-height: 0; overflow: auto; + overscroll-behavior: contain; + padding: var(--space-2) var(--space-4) var(--space-1); } .docPopup .docToolbar { + position: static; flex: none; + align-self: flex-end; + margin: 0 var(--space-2) var(--space-1); + padding: 0; + border: 0; +} +.docPopup .docToolbar .spacer { + display: none; } .docWindowFrame { height: 100%; diff --git a/src/editor/lsp/Popups.tsx b/src/editor/lsp/Popups.tsx index 7a36b0aa8..fa8cb8a21 100644 --- a/src/editor/lsp/Popups.tsx +++ b/src/editor/lsp/Popups.tsx @@ -356,6 +356,19 @@ function DocPopup({ popup }: { popup: Extract }) { const focusSeq = useLspPopup((s) => s.focusSeq); const scope = useRef(null); const opened = useRef(focusSeq); + // The captured caret can end up outside the viewport while the popup is open. + const anchor = useMemo( + () => ({ + getBoundingClientRect: () => + new DOMRect( + Math.max(0, Math.min(popup.anchor.x, window.innerWidth)), + Math.max(0, Math.min(popup.anchor.y, window.innerHeight)), + 0, + 0, + ), + }), + [popup.anchor], + ); useEffect(() => { if (focusSeq !== opened.current) scope.current?.querySelector("[data-lsp-doc]")?.focus({ preventScroll: true }); @@ -364,7 +377,7 @@ function DocPopup({ popup }: { popup: Extract }) { return (