diff --git a/src/editor/lsp/DocView.tsx b/src/editor/lsp/DocView.tsx index 7ef9250e3..63bc1137b 100644 --- a/src/editor/lsp/DocView.tsx +++ b/src/editor/lsp/DocView.tsx @@ -2,8 +2,9 @@ * The documentation view with its history toolbar, as IDEA's one documentation * component: the F1 popup, the hover and the Documentation tool window show it. */ -import { useEffect, useRef, type KeyboardEvent } from "react"; +import { useLayoutEffect, useRef, type KeyboardEvent } from "react"; import { createRoot } from "react-dom/client"; +import { flushSync } from "react-dom"; import clsx from "clsx"; import { useStore } from "zustand"; import { IconButton, selectableText } from "@/ui"; @@ -23,7 +24,9 @@ import styles from "./Popups.module.css"; export function DomContent({ build, className }: { build: () => HTMLElement; className?: string }) { const ref = useRef(null); - useEffect(() => { + // Build the body before paint: the popup is measured as soon as it is created, so an empty first + // frame would be placed and then moved once the text appeared. + useLayoutEffect(() => { const el = ref.current; if (!el) return; const node = build(); @@ -113,6 +116,8 @@ function HoverDocView({ doc }: { doc: HoverDoc }) { /** Mounts a hover's documentation into its CodeMirror tooltip; returns its unmount. */ export function mountHoverDoc(el: HTMLElement, doc: HoverDoc): () => void { const root = createRoot(el); - root.render(); + // Render before returning: CodeMirror measures the tooltip right after `create()`, so a popup + // that is still empty at that point is placed a second time when the text arrives. + flushSync(() => root.render()); return () => queueMicrotask(() => root.unmount()); } diff --git a/src/editor/lsp/Popups.test.tsx b/src/editor/lsp/Popups.test.tsx index fd1b6c481..d690c66b7 100644 --- a/src/editor/lsp/Popups.test.tsx +++ b/src/editor/lsp/Popups.test.tsx @@ -261,7 +261,7 @@ describe("hover documentation", () => { const frame = (el: Element) => el.querySelector('[data-lsp-doc="hover"]')!; afterEach(() => document.body.replaceChildren()); - it("shows the hovered documentation in the same view as F1's", async () => { + it("shows the hovered documentation in the same view as F1's, laid out when create() returns", async () => { const docHover: ContributedHover = (state, pos) => { const w = state.wordAt(pos); return w ? { from: w.from, to: w.to, markdown: "hovered docs" } : null; @@ -275,11 +275,13 @@ describe("hover documentation", () => { }); const tooltip = await hoverSource(view, 2); let created!: { dom: HTMLElement; destroy: () => void }; - await act(async () => { + // Asserted inside a synchronous act(): `create()` has to leave a laid-out popup behind, since + // CodeMirror places the tooltip in the measure pass that follows it. + act(() => { created = tooltip!.create() as typeof created; - document.body.appendChild(created.dom); + expect(frame(created.dom).textContent).toContain("hovered docs"); }); - expect(frame(created.dom).textContent).toContain("hovered docs"); + await act(async () => void document.body.appendChild(created.dom)); expect(screen.getByRole("button", { name: "Back" })).toBeTruthy(); created.destroy(); view.destroy();