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 edit = popup.getByRole("button", { name: "Edit Source" }); const more = popup.getByRole("button", { name: "More Actions" }); await expect(edit).toBeVisible(); await expect(more).toBeVisible(); await expect(popup.getByRole("button")).toHaveCount(2); const bounds = (await popup.boundingBox())!; expect(bounds.width).toBeGreaterThanOrEqual(298); expect(bounds.width).toBeLessThan(320); const editBounds = (await edit.boundingBox())!; const moreBounds = (await more.boundingBox())!; expect(editBounds.x).toBeGreaterThan(bounds.x + bounds.width / 2); expect(bounds.x + bounds.width - moreBounds.x - moreBounds.width).toBeLessThan(16); await expect(popup.locator(".cm-lsp-markdown")).toHaveCSS("font-size", "13px"); await more.press("Enter"); const menu = page.getByRole("menu", { name: "More Actions" }); const dock = menu.getByRole("menuitem", { name: "Show in Documentation Tool Window" }); await expect(dock).toBeFocused(); await expect(menu.getByRole("menuitem", { name: "Back" })).toBeDisabled(); await expect(menu.getByRole("menuitem", { name: "Forward" })).toBeDisabled(); await page.keyboard.press("Escape"); await expect(menu).toBeHidden(); await expect(more).toBeFocused(); await expect(popup).toBeVisible(); await more.press("Enter"); await dock.press("Enter"); await expect(popup).toBeHidden(); await expect(page.locator('[data-lsp-doc="window"]')).toContainText("Returns the total."); }); 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 more = popup.getByRole("button", { name: "More Actions" }); await expect(more).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."); // Repositioning after a viewport resize lands a frame later: read the actions once they settle. await expect .poll(async () => { const first = await more.boundingBox(); await page.evaluate(() => new Promise((done) => requestAnimationFrame(() => requestAnimationFrame(done)))); return JSON.stringify(first) === JSON.stringify(await more.boundingBox()); }) .toBe(true); const actionsBefore = (await more.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(actionsBefore.y + 1); expect(await more.boundingBox()).toEqual(actionsBefore); await expect(more).toBeInViewport(); await more.click(); await expect(page.getByRole("menu", { name: "More Actions" })).toBeInViewport({ ratio: 1 }); }); test("trusted Quick Documentation opens its external manual from More Actions", async ({ workbench: page }) => { const url = "https://www.php.net/manual/en/function.empty.php"; await page.evaluate(async (externalUrl) => { window.open = (opened?: string | URL) => { (window as unknown as { __externalDocumentation: string }).__externalDocumentation = String(opened); return null; }; const popupModule = "/src/editor/lsp/popupStore.ts"; const { openPopup } = await import(/* @vite-ignore */ popupModule); openPopup({ kind: "doc", anchor: { x: 240, y: 180 }, entries: [ { markup: { kind: "plaintext", value: "empty(mixed $var): bool" }, language: "PHP", externalUrl, }, ], index: 0, themeClasses: "", }); }, url); const popup = page.getByRole("dialog", { name: "Documentation", exact: true }); await expect(popup).toContainText("empty(mixed $var): bool"); await popup.getByRole("button", { name: "More Actions" }).click(); await page.getByRole("menuitem", { name: "External Documentation" }).click(); await expect .poll(async () => page.evaluate(() => (window as unknown as { __externalDocumentation?: string }).__externalDocumentation), ) .toBe(url); }); test("Quick Documentation persists Ctrl+wheel font zoom without scrolling or zooming the editor", async ({ workbench: page, }) => { const markdown = [ "# Pricing documentation", "Returns the total with `ExtendedPricingOptions`.", "```kotlin\nfun calculateTotal(order: Order): CalculationResult\n```", ...Array.from({ length: 24 }, (_, i) => { return `Section ${i + 1}. Explains pricing rules, shipping restrictions and currency options.`; }), ].join("\n\n"); const popup = await quickDocumentation(page, markdown); const body = popup.locator(".cm-lsp-markdown").locator(".."); const content = popup.locator(".cm-lsp-markdown"); const editorContent = page.locator(".cm-editor .cm-content").first(); const editorFontSize = await editorContent.evaluate((el) => getComputedStyle(el).fontSize); const more = popup.getByRole("button", { name: "More Actions" }); const moreSize = (await more.boundingBox())!; await expect(content).toHaveCSS("font-size", "13px"); await expect(content.locator("h1")).toHaveCSS("font-size", "13px"); await expect(content.locator("pre")).toHaveCSS("font-size", "13px"); await expect(content.locator("p code")).toHaveCSS("font-size", "12px"); await page.evaluate(async () => { const projectPath = "/src/platform/project.ts"; const { useProject } = await import(/* @vite-ignore */ projectPath); const settings = useProject.getState().settings; useProject.setState({ settings: { ...settings, editor: { ...settings?.editor, general: { wheelZoom: true } } }, }); }); await body.evaluate((el) => { // Font reflow must not move the viewport and obscure whether the wheel itself scrolled it. el.style.overflowAnchor = "none"; el.scrollTop = 80; }); const scrollTop = await body.evaluate((el) => el.scrollTop); expect(scrollTop).toBeGreaterThan(0); const box = (await body.boundingBox())!; await page.mouse.move(box.x + 24, box.y + 24); await page.keyboard.down("Control"); await page.mouse.wheel(0, -120); await page.keyboard.up("Control"); await expect(content).toHaveCSS("font-size", "14px"); await expect(content.locator("h1")).toHaveCSS("font-size", "14px"); await expect(content.locator("pre")).toHaveCSS("font-size", "14px"); await expect(content.locator("p code")).toHaveCSS("font-size", "13px"); await expect(editorContent).toHaveCSS("font-size", editorFontSize); expect(await body.evaluate((el) => el.scrollTop)).toBe(scrollTop); await expect.poll(() => page.evaluate(() => localStorage.getItem("openidea.lsp.documentationFontZoom"))).toBe("1"); const zoomedMoreSize = (await more.boundingBox())!; expect(zoomedMoreSize.width).toBe(moreSize.width); expect(zoomedMoreSize.height).toBe(moreSize.height); await page.keyboard.press("Escape"); await expect(popup).toBeHidden(); await page.keyboard.press("F1"); const reopened = page.getByRole("dialog", { name: "Documentation", exact: true }); await expect(reopened.locator(".cm-lsp-markdown")).toHaveCSS("font-size", "14px"); await expect.poll(() => page.evaluate(() => localStorage.getItem("openidea.lsp.documentationFontZoom"))).toBe("1"); await reopened.getByRole("button", { name: "More Actions" }).click(); await page.getByRole("menuitem", { name: "Show in Documentation Tool Window" }).click(); await expect(reopened).toBeHidden(); await expect(page.locator('[data-lsp-doc="window"] .cm-lsp-markdown')).toHaveCSS("font-size", "14px"); });