From 63abedaadd569af1faaae5e4d19933d4e7acde09 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:51:20 +0800 Subject: [PATCH] fix(editor): ask before ending a busy tab's session ahead of discarding changes (#3336) Bulk closes discarded the documents answered with Discard before asking whether to end busy tabs' sessions (#3177), so Cancel there lost the changes. The busy-tab question is now asked before any discard. --- src/editor/core/controller.ts | 10 +++++++++- src/editor/core/customDocuments.test.ts | 26 ++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/editor/core/controller.ts b/src/editor/core/controller.ts index 4b0773e36..73431259d 100644 --- a/src/editor/core/controller.ts +++ b/src/editor/core/controller.ts @@ -1388,6 +1388,7 @@ export class EditorController implements SlotHost { try { const authorized = new Set(); const discards = new Map(); + let busyConfirmed = false; for (;;) { const current = useTabs.getState(); const d = closedDocuments(current, currentRemoval(current), useContributions.getState().editorKinds).find( @@ -1397,6 +1398,13 @@ export class EditorController implements SlotHost { // A later Cancel/save failure must not discard earlier documents. const discard = [...discards.values()].find(stillLast); if (!discard) break; + // Nor may a Cancel on ending a busy tab's session: ask before discarding anything. + if (!busyConfirmed) { + if (!(await this.confirmBusyClose(currentRemoval))) return false; + if (generation !== this.projectGeneration) return false; + busyConfirmed = true; + continue; + } discards.delete(discard.input.key); await discard.lifecycle.discard(discard.input); // The await may have changed which documents lose their last tab. @@ -1427,7 +1435,7 @@ export class EditorController implements SlotHost { } authorized.add(d.input.key); } - if (!(await this.confirmBusyClose(currentRemoval))) return false; + if (!busyConfirmed && !(await this.confirmBusyClose(currentRemoval))) return false; if (generation !== this.projectGeneration) return false; if (options.beforeRemove && !(await options.beforeRemove())) return false; // Commit only the original target ids against the latest tab state. diff --git a/src/editor/core/customDocuments.test.ts b/src/editor/core/customDocuments.test.ts index 77e0fe033..353a847e3 100644 --- a/src/editor/core/customDocuments.test.ts +++ b/src/editor/core/customDocuments.test.ts @@ -4,7 +4,7 @@ import { createMemoryBackend } from "./backend"; import { findTab, initialTabsState, paneIds } from "./model/tabs"; import { useDocMeta } from "./model/document"; import { useProject } from "@/platform/project"; -import { ask } from "@/workbench/messages"; +import { ask, confirm } from "@/workbench/messages"; import type * as Messages from "@/workbench/messages"; import * as virtualDocs from "@/platform/virtualDocs"; import type { VirtualDocument } from "@/platform/ipc"; @@ -18,6 +18,7 @@ import { vi.mock("@/workbench/messages", async (original) => ({ ...(await original()), ask: vi.fn(), + confirm: vi.fn(), })); class TestDocument implements EditorDocumentLifecycle { @@ -248,6 +249,29 @@ describe("custom editor document lifetime", () => { expect(findTab(useTabs.getState(), input().key)).toBeNull(); }); + it("keeps discarded changes when ending a busy tab's session is cancelled", async () => { + const busy = registerEditorKind({ + kind: "busy-test", + component: () => null, + title: (i) => i.key, + isBusy: () => true, + }); + try { + const doc = await open(); + await ctrl.openInput({ kind: "busy-test", key: "busy:1" }, { focus: false }); + vi.mocked(ask).mockResolvedValue({ button: "discard", checked: false }); + vi.mocked(confirm).mockResolvedValue(false); + const pane = useTabs.getState().activePane; + expect(await ctrl.closeAll(pane)).toBe(false); + expect(confirm).toHaveBeenCalledOnce(); + expect(doc.text).toBe("edited"); + expect(useTabs.getState().panes[pane]!.tabs.map((tab) => tab.input.key)).toEqual(["custom:a", "busy:1"]); + } finally { + busy(); + vi.mocked(confirm).mockReset(); + } + }); + it("discards without a removal-triggered autosave", async () => { const doc = await open(); useProject.setState({ settings: { files: { autoSave: true } } as never });