mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-19 00:10:00 +00:00
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.
This commit is contained in:
@@ -1388,6 +1388,7 @@ export class EditorController implements SlotHost {
|
||||
try {
|
||||
const authorized = new Set<string>();
|
||||
const discards = new Map<string, CustomDocument>();
|
||||
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.
|
||||
|
||||
@@ -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<typeof Messages>()),
|
||||
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 });
|
||||
|
||||
Reference in New Issue
Block a user