fix(editor): show completion documentation beside the list (#3522)

The completion documentation was drawn inside the lookup popup, which clipped it or let it cover the options. positionInfo now places it beside the list on the side with more room, top edges aligned, with capped width and height.

Co-authored-by: zhzhsx <1379025+zhzhsx@users.noreply.github.com>
This commit is contained in:
zhzhsx
2026-11-15 22:35:12 +08:00
committed by GitHub
co-authored by zhzhsx
parent 6295cc2237
commit e06c890ca4
4 changed files with 190 additions and 2 deletions
+95
View File
@@ -98,6 +98,101 @@ test("completion lists the server's items and inserts the chosen one", async ({
expect((await ipcCalls(page, "lsp_completion")).length).toBeGreaterThan(0);
});
test("documentation shows beside the lookup instead of being clipped by it", async ({ workbench: page }, info) => {
const editor = await openSample(page);
await page.evaluate(async () => {
// Long enough for the pane to reach its max-width: a short doc leaves it at
// `width: max-content`, so the placement assertions would never exercise the room calculation.
const longDocs =
"Computes the order total including tax, discounts and shipping, rounded to the currency's scale. " +
"The result is cached until the order changes, so callers should not hold on to it across edits; " +
"ask for a fresh value after every update.";
const item = (label: string, detail: string) => ({
label,
labelDetail: "()",
detail,
kind: "method",
documentation: { kind: "markdown", value: `Docs for **${label}**. ${longDocs}` },
sortText: null,
filterText: null,
preselect: false,
deprecated: false,
insertText: `${label}()`,
isSnippet: false,
replaceRange: null,
replaceRangeTab: null,
additionalEdits: [],
commitCharacters: [],
command: null,
data: null,
server: 1,
});
window.__ipc.handlers.lsp_completion = () => ({
isIncomplete: false,
items: [item("total", "BigDecimal"), item("toDto", "OrderDto")],
});
const projectPath = "/src/platform/project.ts";
const { useProject } = await import(/* @vite-ignore */ projectPath);
const settings = structuredClone(useProject.getState().settings ?? {});
const editor = settings.editor ?? {};
settings.editor = {
...editor,
completion: { ...editor.completion, showDocumentation: true, documentationDelay: 0 },
};
useProject.setState({ settings });
});
const popup = page.locator(".cm-tooltip-autocomplete");
const openLookup = async () => {
await editor.locator(".cm-line", { hasText: "return order.toDto()" }).click();
await page.keyboard.press("End");
await page.keyboard.press("Enter");
await page.keyboard.type("order.");
await page.keyboard.press("Control+Space");
await expect(popup.getByText("total", { exact: true })).toBeVisible();
await expect(page.locator(".cm-completionInfo")).toContainText("Docs for");
};
const geometry = () =>
page.evaluate(() => {
const info = document.querySelector(".cm-completionInfo") as HTMLElement;
const list = (document.querySelector(".cm-tooltip-autocomplete") as HTMLElement).getBoundingClientRect();
const rect = info.getBoundingClientRect();
const overlapX = Math.max(0, Math.min(rect.right, list.right) - Math.max(rect.left, list.left));
const overlapY = Math.max(0, Math.min(rect.bottom, list.bottom) - Math.max(rect.top, list.top));
const side = info.classList.contains("cm-completionInfo-left")
? "left"
: info.classList.contains("cm-completionInfo-right")
? "right"
: "none";
return {
overlap: overlapX * overlapY,
visible: !!document.elementFromPoint(rect.left + 4, rect.top + 4)?.closest(".cm-completionInfo"),
inside: rect.left >= 0 && rect.right <= document.documentElement.clientWidth,
side,
};
});
await page.setViewportSize({ width: 760, height: 900 });
await openLookup();
await page.screenshot({ path: info.outputPath("completion-documentation.png"), animations: "disabled" });
const narrow = await geometry();
expect(narrow.overlap).toBe(0);
expect(narrow.visible).toBe(true);
expect(narrow.inside).toBe(true);
expect(narrow.side).toBe("left");
await page.keyboard.press("Escape");
await expect(popup).toBeHidden();
await page.setViewportSize({ width: 1400, height: 900 });
await openLookup();
const wide = await geometry();
expect(wide.overlap).toBe(0);
expect(wide.visible).toBe(true);
expect(wide.inside).toBe(true);
expect(wide.side).toBe("right");
});
test("typing closes an open documentation tooltip", async ({ workbench: page }) => {
const editor = await openSample(page);
await page.evaluate(() => {
+53
View File
@@ -22,6 +22,7 @@ import {
explicitCompletion,
makeCompletion,
matchRanges,
positionCompletionInfo,
sortAlphabetically,
} from "./completion";
import {
@@ -659,6 +660,58 @@ describe("explicit completion", () => {
});
});
describe("documentation beside the lookup", () => {
const view = {} as EditorView;
const space = { left: 0, top: 0, right: 1200, bottom: 800 };
const option = { left: 100, right: 300, top: 120, bottom: 144 };
const info = { left: 0, right: 400, top: 0, bottom: 300 };
const beside = { left: 100, right: 520, top: 100, bottom: 400 };
const originalWidth = document.documentElement.clientWidth;
afterEach(() => {
Object.defineProperty(document.documentElement, "clientWidth", { value: originalWidth, configurable: true });
});
/** `windowWidth` is the room the pane may use; the editor's own box does not limit it. */
function place(list: typeof beside, windowWidth = 1200, bounds = space) {
Object.defineProperty(document.documentElement, "clientWidth", { value: windowWidth, configurable: true });
return positionCompletionInfo(view, list, option, info, bounds);
}
it("sits right of the list when there is room, top edges aligned", () => {
const pos = place(beside);
expect(pos.class).toBe("cm-completionInfo-right");
expect(pos.style).toContain("top: 0");
expect(pos.style).toContain("left: calc(100% + 6px)");
expect(pos.style).toContain("max-width: 440px");
expect(pos.style).toContain("max-height: 320px");
});
it("moves to the left of the list when the right side is narrower", () => {
const pos = place({ ...beside, left: 700, right: 1120 });
expect(pos.class).toBe("cm-completionInfo-left");
expect(pos.style).toContain("right: calc(100% + 6px)");
});
it("narrows the pane to the room the window has, not the editor's", () => {
// A 620px-wide editor would leave 94px; the window leaves 274px.
const pos = place(beside, 800, { ...space, right: 620 });
expect(pos.class).toBe("cm-completionInfo-right");
expect(pos.style).toContain("max-width: 274px");
});
it("keeps a readable width in a narrow window rather than covering the list", () => {
const pos = place(beside, 620);
expect(pos.class).toBe("cm-completionInfo-right");
expect(pos.style).toContain("max-width: 220px");
});
it("caps the height to the room below the list and to 320px", () => {
expect(place({ ...beside, top: 700, bottom: 780 }).style).toContain("max-height: 100px");
expect(place(beside, 1200, { ...space, bottom: 600 }).style).toContain("max-height: 320px");
});
});
describe("completion footer", () => {
let view: EditorView | null = null;
afterEach(() => {
+39 -1
View File
@@ -25,7 +25,7 @@ import {
type CompletionSource,
} from "@codemirror/autocomplete";
import { EditorSelection, EditorState, Prec, StateEffect, StateField, type Text } from "@codemirror/state";
import { EditorView, ViewPlugin, keymap, type ViewUpdate } from "@codemirror/view";
import { EditorView, ViewPlugin, keymap, type Rect, type ViewUpdate } from "@codemirror/view";
import {
call,
commands,
@@ -159,6 +159,43 @@ function resetLookupDocs() {
docWaiters.clear();
}
const DOC_MIN_WIDTH = 220;
const DOC_MAX_WIDTH = 440;
const DOC_MAX_HEIGHT = 320;
const DOC_GAP = 6;
/**
* Places the documentation beside the lookup, on whichever side has more room,
* with the two popups' top edges aligned. Room is measured to the window's
* edges, so a narrow editor still has space for a readable pane; when even the
* window is too narrow, the pane keeps a readable width rather than covering the
* options.
*/
export function positionCompletionInfo(
_view: EditorView,
list: Rect,
_option: Rect,
_info: Rect,
space: Rect,
): { style: string; class: string } {
const windowWidth = document.documentElement.clientWidth;
const rightRoom = windowWidth - list.right - DOC_GAP;
const leftRoom = list.left - DOC_GAP;
const side = rightRoom >= leftRoom ? "right" : "left";
const room = side === "right" ? rightRoom : leftRoom;
const width = Math.max(DOC_MIN_WIDTH, Math.min(DOC_MAX_WIDTH, room));
return {
class: `cm-completionInfo-${side}`,
style: [
"top: 0",
// `left`/`right` names the side the pane sits on; the offset goes on the opposite edge.
`${side === "right" ? "left" : "right"}: calc(100% + ${DOC_GAP}px)`,
`max-width: ${width}px`,
`max-height: ${Math.min(DOC_MAX_HEIGHT, space.bottom - list.top)}px`,
].join("; "),
};
}
/**
* F1 while the lookup is open: shows the selected item's docs beside it
* (`shown`), or reports that they are already showing (`showing`) so the
@@ -867,6 +904,7 @@ export function completionExtension() {
);
},
tooltipClass: () => "cm-lsp-completion",
positionInfo: positionCompletionInfo,
}),
completionFooter,
lookupTracker,
+3 -1
View File
@@ -109,7 +109,8 @@ export const lspTheme = EditorView.theme({
// (gutter + project sidebar) keeps the word well clear of that edge.
marginLeft:
"calc(-1 * (1px + var(--cm-completion-edge) + var(--cm-completion-inset) + var(--cm-completion-icon) + var(--cm-completion-gap)))",
overflow: "hidden",
// No overflow clipping: CodeMirror nests the documentation pane in this box and
// positions it outside it (see `positionCompletionInfo`). The footer rounds itself.
},
".cm-tooltip.cm-tooltip-autocomplete.cm-lsp-completion > ul": {
fontFamily: "var(--openidea-editor-font, var(--font-mono))",
@@ -194,6 +195,7 @@ export const lspTheme = EditorView.theme({
whiteSpace: "nowrap",
gap: "12px",
justifyContent: "space-between",
borderRadius: "0 0 calc(var(--radius-popup) - 1px) calc(var(--radius-popup) - 1px)",
},
".cm-lsp-completion-sort": {
padding: "0",