diff --git a/src/editor/refactor/cancellable.test.ts b/src/editor/refactor/cancellable.test.ts index 54b578852..141362245 100644 --- a/src/editor/refactor/cancellable.test.ts +++ b/src/editor/refactor/cancellable.test.ts @@ -40,4 +40,17 @@ describe("cancellable refactoring requests", () => { expect(isCancelled(err)).toBe(true); expect(send).not.toHaveBeenCalled(); }); + + it("tells the sender once the dialog closed after the request was handed over", async () => { + const run = new AbortController(); + let wanted!: () => boolean; + const reply = cancellable(run.signal, async (_id, w) => { + wanted = w; + return "sent"; + }); + expect(wanted()).toBe(true); + run.abort(); + expect(wanted()).toBe(false); + await reply; + }); }); diff --git a/src/editor/refactor/extractInterface.ts b/src/editor/refactor/extractInterface.ts index c0c5578a9..91aee4b64 100644 --- a/src/editor/refactor/extractInterface.ts +++ b/src/editor/refactor/extractInterface.ts @@ -136,9 +136,10 @@ export async function extractInterface( for (;;) { await whenSynced(view); const token: string | null = confirmation; - extracted = await cancellable(signal, (id) => - lspRequest(() => - commands.lspExtractInterface(id, request.doc, request.pos, [...members], name, pkg.json, token), + extracted = await cancellable(signal, (id, wanted) => + lspRequest( + () => commands.lspExtractInterface(id, request.doc, request.pos, [...members], name, pkg.json, token), + wanted, ), ); if (signal?.aborted) return { status: "stay" }; diff --git a/src/editor/refactor/signature.ts b/src/editor/refactor/signature.ts index d5122a120..82cd570c6 100644 --- a/src/editor/refactor/signature.ts +++ b/src/editor/refactor/signature.ts @@ -134,21 +134,30 @@ export async function applyEdit(view: EditorView, edit: WorkspaceEdit, signal?: await whenAllSynced(); // The dialog closed while the editors were syncing: nothing is applied. if (signal?.aborted) return; - await lspRequest(() => commands.applyWorkspaceEdit(edit)); + await lspRequest( + () => commands.applyWorkspaceEdit(edit), + () => !signal?.aborted, + ); } catch (e) { reportLspError(view, e, true); } } -/** Sends `send(id)` with a fresh request id that `signal` cancels. */ -export async function cancellable(signal: AbortSignal | undefined, send: (id: number) => Promise): Promise { +/** + * Sends `send(id, wanted)` with a fresh request id that `signal` cancels. `wanted` goes to + * `lspRequest`, which syncs editors first: a dialog closed meanwhile never sends the request. + */ +export async function cancellable( + signal: AbortSignal | undefined, + send: (id: number, wanted: () => boolean) => Promise, +): Promise { // Closed before the request went out: it never does (a cancel can't reach an unsent request). if (signal?.aborted) throw new IpcError({ kind: "cancelled", message: "Cancelled" }); const id = nextRequestId(); const abort = () => void commands.cancelRequest(id); signal?.addEventListener("abort", abort); try { - return await send(id); + return await send(id, () => !signal?.aborted); } finally { signal?.removeEventListener("abort", abort); } @@ -186,8 +195,8 @@ export async function changeSignature( for (;;) { await whenSynced(view); const token: string | null = confirmation; - changed = await cancellable(signal, (id) => - lspRequest(() => commands.lspChangeSignature(id, request.doc, request.pos, signature, delegate, token)), + changed = await cancellable(signal, (id, wanted) => + lspRequest(() => commands.lspChangeSignature(id, request.doc, request.pos, signature, delegate, token), wanted), ); if (signal?.aborted) return { status: "stay" }; const fatal = changed.problems.find((p) => p.severity === "fatal");