Compare commits

...
4 changed files with 36 additions and 13 deletions

No files matched your search

@@ -115,6 +115,23 @@ describe("native window close", () => {
expect(forget).not.toHaveBeenCalled();
});
it("stays open when an untitled buffer's Save As is cancelled", async () => {
vi.mocked(ipc.commands.windowCloseContext).mockResolvedValue({ quitting: false, windows: 2 });
vi.mocked(services.maybeEditorService).mockReturnValue({
forgetSavedTabs: forget,
keepSavedTabs: keep,
dirtyDocumentIds: () => [],
untitledDirtyDocumentIds: () => ["untitled:1"],
cancelClose: vi.fn(),
} as never);
deps.prepareClose = async () => ({ dirty: 1, discard: vi.fn(), commit: vi.fn(), cancel: vi.fn() });
deps.ask = async () => ({ button: "save", checked: false });
const prevented = await close({});
expect(deps.saveAll).toHaveBeenCalledOnce();
expect(prevented).toHaveBeenCalledOnce();
expect(ipc.commands.windowGroupCancelClose).toHaveBeenCalled();
});
it("restores saved tabs if returning to Welcome fails", async () => {
vi.mocked(ipc.commands.closeProject).mockRejectedValue(new Error("close failed"));
const prevented = await close({ closeTabsWithWindow: true });
@@ -148,6 +148,8 @@ describe("openProjectInWindow", () => {
expect(wslResolve).toHaveBeenCalledWith(share);
expect(connectRemote).toHaveBeenCalledWith(target, linuxPath);
expect(openProject).not.toHaveBeenCalled();
// The remote window doesn't keep untitled buffers, so the guard counts them.
expect(confirmLeaveProject).toHaveBeenCalledWith(false);
});
it("asks for a new window when this one already has a project", async () => {
+14 -10
View File
@@ -250,7 +250,7 @@ async function onCloseRequested(deps: CloseDeps): Promise<boolean> {
dirty: dirtyCount(),
project: s.project?.displayName || s.project?.name || null,
},
deps,
savingUntitled(deps),
);
if (!ok) {
await (ctx.quitting ? commands.appCancelQuit() : commands.windowGroupCancelClose());
@@ -270,6 +270,18 @@ async function onCloseRequested(deps: CloseDeps): Promise<boolean> {
}
}
/** Save asks for each untitled buffer's path; one left unsaved (Save As cancelled) keeps the window open. */
function savingUntitled(deps: CloseDeps): CloseDeps {
return {
...deps,
saveAll: async () => {
await deps.saveAll();
if (maybeEditorService()?.untitledDirtyDocumentIds?.().length)
throw new IpcError({ kind: "cancelled", message: "Untitled files were not saved" });
},
};
}
/**
* Unsaved changes before this window opens another project in place or closes it. `project` names
* the project in the prompt when this window doesn't hold it (a comparison window).
@@ -289,15 +301,7 @@ export async function confirmLeave(
? { ...deps, saveAll: () => Promise.resolve(maybeEditorService()?.saveAll({ skipUntitled: true })) }
: keepUntitled
? deps
: {
...deps,
// Save asks for each untitled buffer's path; one left unsaved (Save As cancelled) keeps the project open.
saveAll: async () => {
await deps.saveAll();
if (maybeEditorService()?.untitledDirtyDocumentIds?.().length)
throw new IpcError({ kind: "cancelled", message: "Untitled files were not saved" });
},
};
: savingUntitled(deps);
return decideClose(
{
quitting: false,
+3 -3
View File
@@ -55,10 +55,10 @@ function openFailed(path: string, e: unknown) {
/** Opens project `path` in this window; true once it is this window's project. */
async function openHere(path: string): Promise<boolean> {
if (!isRemoteWindow() && useProject.getState().project?.root === path) return true;
if (!(await confirmLeaveProject())) return false;
// A folder of a WSL distro runs there, not as a network folder; the window then drops untitled buffers.
const wsl = await commands.wslResolve(path).catch(() => null);
if (!(await confirmLeaveProject(!wsl))) return false;
try {
// A folder of a WSL distro runs there, not as a network folder.
const wsl = await commands.wslResolve(path).catch(() => null);
if (wsl) await connectRemote(wsl.target, wsl.path);
else await openProject(path);
return true;