diff --git a/src/editor/refactor/cancellable.test.ts b/src/editor/refactor/cancellable.test.ts index cbfe3c388..54b578852 100644 --- a/src/editor/refactor/cancellable.test.ts +++ b/src/editor/refactor/cancellable.test.ts @@ -7,6 +7,7 @@ vi.mock("@/platform/ipc", async (original) => { }); const { cancellable } = await import("./signature"); +const { isCancelled } = await import("@/platform/ipc"); describe("cancellable refactoring requests", () => { it("cancels the request in flight when the dialog closes", async () => { @@ -30,4 +31,13 @@ describe("cancellable refactoring requests", () => { run.abort(); expect(cancelRequest).not.toHaveBeenCalled(); }); + + it("never sends a request the dialog closed before", async () => { + const run = new AbortController(); + run.abort(); + const send = vi.fn(async () => "sent"); + const err = await cancellable(run.signal, send).catch((e: unknown) => e); + expect(isCancelled(err)).toBe(true); + expect(send).not.toHaveBeenCalled(); + }); }); diff --git a/src/editor/refactor/extractInterface.ts b/src/editor/refactor/extractInterface.ts index 95e55ab50..c0c5578a9 100644 --- a/src/editor/refactor/extractInterface.ts +++ b/src/editor/refactor/extractInterface.ts @@ -158,9 +158,9 @@ export async function extractInterface( const summary = summarizeWorkspaceEdit(edit); if (!summary.files) return { status: "stay", error: "Nothing to change" }; const created = edit.files.find((f) => f.kind === "create")?.path; - const apply = async () => { - await applyEdit(view, edit); - if (created) await editorService().openFile(created, { focus: true }); + const apply = async (cancel?: AbortSignal) => { + await applyEdit(view, edit, cancel); + if (created && !cancel?.aborted) await editorService().openFile(created, { focus: true }); }; if (preview) { openPopup({ @@ -171,7 +171,7 @@ export async function extractInterface( onConfirm: () => void apply(), }); } else { - await apply(); + await apply(signal); } return { status: "done" }; } diff --git a/src/editor/refactor/signature.ts b/src/editor/refactor/signature.ts index 3b79722f9..d5122a120 100644 --- a/src/editor/refactor/signature.ts +++ b/src/editor/refactor/signature.ts @@ -15,7 +15,7 @@ import { showHint } from "@/editor/lsp/hint"; import { openPopup } from "@/editor/lsp/popupStore"; import { summarizeWorkspaceEdit } from "@/editor/lsp/workspaceEdit"; import { tx } from "@/platform/i18n"; -import { commands, isCancelled, nextRequestId, unavailableText } from "@/platform/ipc"; +import { IpcError, commands, isCancelled, nextRequestId, unavailableText } from "@/platform/ipc"; import { ask } from "@/workbench/messages"; import { cannotPerform } from "./catalog"; @@ -129,9 +129,11 @@ export async function startChangeSignature(view: EditorView): Promise { } } -export async function applyEdit(view: EditorView, edit: WorkspaceEdit) { +export async function applyEdit(view: EditorView, edit: WorkspaceEdit, signal?: AbortSignal) { try { await whenAllSynced(); + // The dialog closed while the editors were syncing: nothing is applied. + if (signal?.aborted) return; await lspRequest(() => commands.applyWorkspaceEdit(edit)); } catch (e) { reportLspError(view, e, true); @@ -140,6 +142,8 @@ export async function applyEdit(view: EditorView, edit: WorkspaceEdit) { /** Sends `send(id)` with a fresh request id that `signal` cancels. */ export async function cancellable(signal: AbortSignal | undefined, send: (id: number) => 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); @@ -210,7 +214,7 @@ export async function changeSignature( onConfirm: () => void applyEdit(view, edit), }); } else { - await applyEdit(view, edit); + await applyEdit(view, edit, signal); } return { status: "done" }; }