diff --git a/e2e/csharpHints.spec.ts b/e2e/csharpHints.spec.ts index 35f11b4fd4..195551c171 100644 --- a/e2e/csharpHints.spec.ts +++ b/e2e/csharpHints.spec.ts @@ -151,6 +151,9 @@ test("C# parameter info bolds the argument being typed, `params` included", asyn // The third argument: past the last parameter. Reopened, so the old popup can't answer for it. await page.keyboard.press("Escape"); await expect(popup).toBeHidden(); + // Past the refresh the typing scheduled: it must not bring the closed popup back. + await page.waitForTimeout(500); + await expect(popup).toBeHidden({ timeout: 10 }); await page.keyboard.press("Meta+p"); await expect(popup.locator("b")).toHaveText("args"); if (SHOTS) await page.screenshot({ path: `${SHOTS}/csharp-parameter-info.png` }); diff --git a/src/editor/lsp/signature.test.ts b/src/editor/lsp/signature.test.ts index b5a2e0a9fc..0b78a9ea34 100644 --- a/src/editor/lsp/signature.test.ts +++ b/src/editor/lsp/signature.test.ts @@ -224,6 +224,59 @@ describe("parameter info replies", () => { await vi.waitFor(() => expect(shown(a)).toBe("f")); expect(shown(b)).toBe("g"); }); + + const escape = (view: EditorView) => + view.contentDOM.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true })); + + it("stays closed after Escape while the refresh for the last keystroke is pending", async () => { + lspSignatureHelp.mockResolvedValueOnce(named("f") as never); + const view = editor(); + triggerSignatureHelp(view, true); + await vi.waitFor(() => expect(shown(view)).toBe("f")); + typeChar(view, ","); + escape(view); + expect(shown(view)).toBeNull(); + await new Promise((r) => setTimeout(r, 300)); + expect(shown(view)).toBeNull(); + triggerSignatureHelp(view, true); + await vi.waitFor(() => expect(shown(view)).not.toBeNull()); + }); + + it("stays closed after the editor loses focus while a refresh is pending", async () => { + lspSignatureHelp.mockResolvedValueOnce(named("f") as never); + const view = editor(); + view.focus(); + triggerSignatureHelp(view, true); + await vi.waitFor(() => expect(shown(view)).toBe("f")); + typeChar(view, ","); + view.contentDOM.blur(); + await vi.waitFor(() => expect(shown(view)).toBeNull()); + await new Promise((r) => setTimeout(r, 300)); + expect(shown(view)).toBeNull(); + }); + + it("doesn't open the auto-popup once the editor lost focus before it showed", async () => { + const view = editor("f", 1); + view.focus(); + typeChar(view, "("); + view.contentDOM.blur(); + await new Promise((r) => setTimeout(r, 1200)); + expect(lspSignatureHelp).not.toHaveBeenCalled(); + expect(shown(view)).toBeNull(); + }); + + it("stays closed after Escape when an automatic reply was already on its way", async () => { + const late = pending(); + lspSignatureHelp.mockResolvedValueOnce(named("f") as never).mockReturnValueOnce(late.promise as never); + const view = editor(); + triggerSignatureHelp(view, true); + await vi.waitFor(() => expect(shown(view)).toBe("f")); + triggerSignatureHelp(view, false); + escape(view); + late.resolve(named("f2")); + await settle(); + expect(shown(view)).toBeNull(); + }); }); describe("C# parameter info", () => { diff --git a/src/editor/lsp/signature.ts b/src/editor/lsp/signature.ts index 288e0c6ceb..2357668e46 100644 --- a/src/editor/lsp/signature.ts +++ b/src/editor/lsp/signature.ts @@ -188,10 +188,13 @@ function signatureTooltip(s: SigState): Tooltip { }; } -/** Per view: requests sent, and the tooltip value the newest shown reply set, with the document and caret it was asked at. */ +/** + * Per view: requests sent, the newest request sent before the user closed the tooltip, + * and the tooltip value the newest shown reply set, with the document and caret it was asked at. + */ const requests = new WeakMap< EditorView, - { sent: number; shown?: { id: number; value: SigState; doc: Text; head: number } } + { sent: number; dismissed?: number; shown?: { id: number; value: SigState; doc: Text; head: number } } >(); async function fetchSignature(view: EditorView, explicit: boolean) { @@ -204,6 +207,8 @@ async function fetchSignature(view: EditorView, explicit: boolean) { const head = state.selection.main.head; // A late reply asked at another caret never overrides what a newer request still shows for this one. const superseded = () => { + // The user closed the tooltip after this automatic request was sent: its reply must not reopen it. + if (!explicit && id <= (order.dismissed ?? 0)) return true; const shown = order.shown; return ( !!shown && @@ -273,7 +278,16 @@ const signaturePlugin = ViewPlugin.fromClass( clearTimeout(this.popupTimer); this.popupTimer = setTimeout(() => void fetchSignature(this.view, false), Math.max(0, p.parameterInfoDelay)); } + /** The user closed the tooltip: automatic requests already scheduled or sent don't reopen it. */ + dismiss() { + clearTimeout(this.popupTimer); + this.refresh.cancel(); + const order = requests.get(this.view); + if (order) order.dismissed = order.sent; + } update(u: ViewUpdate) { + // Leaving the editor closes the tooltip like Escape does (`focusChangeEffect` below), and nothing pending opens it. + if (u.focusChanged && !u.view.hasFocus) this.dismiss(); const active = u.state.field(signatureField, false); let typedTrigger = false; if ( @@ -410,6 +424,7 @@ export function signatureExtension(): Extension { key: "Escape", run: (view) => { if (!view.state.field(signatureField, false)) return false; + view.plugin(signaturePlugin)?.dismiss(); view.dispatch({ effects: setSignature.of(null) }); return true; },