fix(editor): keep the hover documentation from moving as it opens (#4682)

The hover documentation was placed twice: create() returned a popup holding only its toolbar, CodeMirror measured and placed it, and it moved once the text arrived. The body is now built in a layout effect and rendered before create() returns (flushSync), so the tooltip is measured once at its final size; the F1 popup and Documentation tool window share DocView and lose their empty first frame too.

Co-authored-by: zhzhsx <1379025+zhzhsx@users.noreply.github.com>
This commit is contained in:
zhzhsx
2026-11-20 11:50:04 +08:00
committed by GitHub
co-authored by zhzhsx
parent 899fe888b7
commit b66439ceda
2 changed files with 14 additions and 7 deletions
+8 -3
View File
@@ -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<HTMLDivElement>(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(<HoverDocView doc={doc} />);
// 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(<HoverDocView doc={doc} />));
return () => queueMicrotask(() => root.unmount());
}
+6 -4
View File
@@ -261,7 +261,7 @@ describe("hover documentation", () => {
const frame = (el: Element) => el.querySelector<HTMLElement>('[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();