Skip to content

Commit 65600c1

Browse files
authored
Merge pull request microsoft#183473 from microsoft/aamunger/scratchpadHotExit
backup scratchpads on window close for hotexit=onexit
2 parents 75c2f32 + 6cea9da commit 65600c1

File tree

2 files changed

+201
-39
lines changed

2 files changed

+201
-39
lines changed

src/vs/workbench/services/workingCopy/electron-sandbox/workingCopyBackupTracker.ts

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ export class NativeWorkingCopyBackupTracker extends WorkingCopyBackupTracker imp
114114
// Trigger backup if configured and enabled for shutdown reason
115115
let backups: IWorkingCopy[] = [];
116116
let backupError: Error | undefined = undefined;
117-
const backup = await this.shouldBackupBeforeShutdown(reason);
118-
if (backup) {
117+
const modifiedWorkingCopiesToBackup = await this.shouldBackupBeforeShutdown(reason, modifiedWorkingCopies);
118+
if (modifiedWorkingCopiesToBackup.length > 0) {
119119
try {
120-
const backupResult = await this.backupBeforeShutdown(modifiedWorkingCopies);
120+
const backupResult = await this.backupBeforeShutdown(modifiedWorkingCopiesToBackup);
121121
backups = backupResult.backups;
122122
backupError = backupResult.error;
123123

@@ -162,49 +162,53 @@ export class NativeWorkingCopyBackupTracker extends WorkingCopyBackupTracker imp
162162
}
163163
}
164164

165-
private async shouldBackupBeforeShutdown(reason: ShutdownReason): Promise<boolean> {
166-
let backup: boolean | undefined;
165+
private async shouldBackupBeforeShutdown(reason: ShutdownReason, modifiedWorkingCopies: readonly IWorkingCopy[]): Promise<readonly IWorkingCopy[]> {
167166
if (!this.filesConfigurationService.isHotExitEnabled) {
168-
backup = false; // never backup when hot exit is disabled via settings
169-
} else if (this.environmentService.isExtensionDevelopment) {
170-
backup = true; // always backup closing extension development window without asking to speed up debugging
171-
} else {
172-
173-
// When quit is requested skip the confirm callback and attempt to backup all workspaces.
174-
// When quit is not requested the confirm callback should be shown when the window being
175-
// closed is the only VS Code window open, except for on Mac where hot exit is only
176-
// ever activated when quit is requested.
177-
178-
switch (reason) {
179-
case ShutdownReason.CLOSE:
180-
if (this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY && this.filesConfigurationService.hotExitConfiguration === HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE) {
181-
backup = true; // backup if a folder is open and onExitAndWindowClose is configured
182-
} else if (await this.nativeHostService.getWindowCount() > 1 || isMacintosh) {
183-
backup = false; // do not backup if a window is closed that does not cause quitting of the application
184-
} else {
185-
backup = true; // backup if last window is closed on win/linux where the application quits right after
167+
return []; // never backup when hot exit is disabled via settings
168+
}
169+
170+
if (this.environmentService.isExtensionDevelopment) {
171+
return modifiedWorkingCopies; // always backup closing extension development window without asking to speed up debugging
172+
}
173+
174+
switch (reason) {
175+
176+
// Window Close
177+
case ShutdownReason.CLOSE:
178+
if (this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY && this.filesConfigurationService.hotExitConfiguration === HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE) {
179+
return modifiedWorkingCopies; // backup if a workspace/folder is open and onExitAndWindowClose is configured
180+
}
181+
182+
if (isMacintosh || await this.nativeHostService.getWindowCount() > 1) {
183+
if (this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY) {
184+
return modifiedWorkingCopies.filter(modifiedWorkingCopy => modifiedWorkingCopy.capabilities & WorkingCopyCapabilities.Scratchpad); // backup scratchpads automatically to avoid user confirmation
186185
}
187-
break;
188186

189-
case ShutdownReason.QUIT:
190-
backup = true; // backup because next start we restore all backups
191-
break;
187+
return []; // do not backup if a window is closed that does not cause quitting of the application
188+
}
192189

193-
case ShutdownReason.RELOAD:
194-
backup = true; // backup because after window reload, backups restore
195-
break;
190+
return modifiedWorkingCopies; // backup if last window is closed on win/linux where the application quits right after
196191

197-
case ShutdownReason.LOAD:
198-
if (this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY && this.filesConfigurationService.hotExitConfiguration === HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE) {
199-
backup = true; // backup if a folder is open and onExitAndWindowClose is configured
200-
} else {
201-
backup = false; // do not backup because we are switching contexts
192+
// Application Quit
193+
case ShutdownReason.QUIT:
194+
return modifiedWorkingCopies; // backup because next start we restore all backups
195+
196+
// Window Reload
197+
case ShutdownReason.RELOAD:
198+
return modifiedWorkingCopies; // backup because after window reload, backups restore
199+
200+
// Workspace Change
201+
case ShutdownReason.LOAD:
202+
if (this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY) {
203+
if (this.filesConfigurationService.hotExitConfiguration === HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE) {
204+
return modifiedWorkingCopies; // backup if a workspace/folder is open and onExitAndWindowClose is configured
202205
}
203-
break;
204-
}
205-
}
206206

207-
return backup;
207+
return modifiedWorkingCopies.filter(modifiedWorkingCopy => modifiedWorkingCopy.capabilities & WorkingCopyCapabilities.Scratchpad); // backup scratchpads automatically to avoid user confirmation
208+
}
209+
210+
return []; // do not backup because we are switching contexts with no workspace/folder open
211+
}
208212
}
209213

210214
private showErrorDialog(msg: string, workingCopies: readonly IWorkingCopy[], error?: Error): void {

src/vs/workbench/services/workingCopy/test/electron-sandbox/workingCopyBackupTracker.test.ts

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,109 @@ suite('WorkingCopyBackupTracker (native)', function () {
565565
});
566566
});
567567

568+
suite('"onExit" setting - scratchpad', () => {
569+
test('should hot exit (reason: CLOSE, windows: single, workspace)', function () {
570+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.CLOSE, false, true, false);
571+
});
572+
test('should hot exit (reason: CLOSE, windows: single, empty workspace)', function () {
573+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.CLOSE, false, false, false);
574+
});
575+
test('should hot exit (reason: CLOSE, windows: multiple, workspace)', function () {
576+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.CLOSE, true, true, false);
577+
});
578+
test('should NOT hot exit (reason: CLOSE, windows: multiple, empty workspace)', function () {
579+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.CLOSE, true, false, true);
580+
});
581+
test('should hot exit (reason: QUIT, windows: single, workspace)', function () {
582+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.QUIT, false, true, false);
583+
});
584+
test('should hot exit (reason: QUIT, windows: single, empty workspace)', function () {
585+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.QUIT, false, false, false);
586+
});
587+
test('should hot exit (reason: QUIT, windows: multiple, workspace)', function () {
588+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.QUIT, true, true, false);
589+
});
590+
test('should hot exit (reason: QUIT, windows: multiple, empty workspace)', function () {
591+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.QUIT, true, false, false);
592+
});
593+
test('should hot exit (reason: RELOAD, windows: single, workspace)', function () {
594+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.RELOAD, false, true, false);
595+
});
596+
test('should hot exit (reason: RELOAD, windows: single, empty workspace)', function () {
597+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.RELOAD, false, false, false);
598+
});
599+
test('should hot exit (reason: RELOAD, windows: multiple, workspace)', function () {
600+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.RELOAD, true, true, false);
601+
});
602+
test('should hot exit (reason: RELOAD, windows: multiple, empty workspace)', function () {
603+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.RELOAD, true, false, false);
604+
});
605+
test('should hot exit (reason: LOAD, windows: single, workspace)', function () {
606+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.LOAD, false, true, false);
607+
});
608+
test('should NOT hot exit (reason: LOAD, windows: single, empty workspace)', function () {
609+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.LOAD, false, false, true);
610+
});
611+
test('should hot exit (reason: LOAD, windows: multiple, workspace)', function () {
612+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.LOAD, true, true, false);
613+
});
614+
test('should NOT hot exit (reason: LOAD, windows: multiple, empty workspace)', function () {
615+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT, ShutdownReason.LOAD, true, false, true);
616+
});
617+
});
618+
619+
suite('"onExitAndWindowClose" setting - scratchpad', () => {
620+
test('should hot exit (reason: CLOSE, windows: single, workspace)', function () {
621+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.CLOSE, false, true, false);
622+
});
623+
test('should hot exit (reason: CLOSE, windows: single, empty workspace)', function () {
624+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.CLOSE, false, false, !!isMacintosh);
625+
});
626+
test('should hot exit (reason: CLOSE, windows: multiple, workspace)', function () {
627+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.CLOSE, true, true, false);
628+
});
629+
test('should NOT hot exit (reason: CLOSE, windows: multiple, empty workspace)', function () {
630+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.CLOSE, true, false, true);
631+
});
632+
test('should hot exit (reason: QUIT, windows: single, workspace)', function () {
633+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.QUIT, false, true, false);
634+
});
635+
test('should hot exit (reason: QUIT, windows: single, empty workspace)', function () {
636+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.QUIT, false, false, false);
637+
});
638+
test('should hot exit (reason: QUIT, windows: multiple, workspace)', function () {
639+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.QUIT, true, true, false);
640+
});
641+
test('should hot exit (reason: QUIT, windows: multiple, empty workspace)', function () {
642+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.QUIT, true, false, false);
643+
});
644+
test('should hot exit (reason: RELOAD, windows: single, workspace)', function () {
645+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.RELOAD, false, true, false);
646+
});
647+
test('should hot exit (reason: RELOAD, windows: single, empty workspace)', function () {
648+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.RELOAD, false, false, false);
649+
});
650+
test('should hot exit (reason: RELOAD, windows: multiple, workspace)', function () {
651+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.RELOAD, true, true, false);
652+
});
653+
test('should hot exit (reason: RELOAD, windows: multiple, empty workspace)', function () {
654+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.RELOAD, true, false, false);
655+
});
656+
test('should hot exit (reason: LOAD, windows: single, workspace)', function () {
657+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.LOAD, false, true, false);
658+
});
659+
test('should NOT hot exit (reason: LOAD, windows: single, empty workspace)', function () {
660+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.LOAD, false, false, true);
661+
});
662+
test('should hot exit (reason: LOAD, windows: multiple, workspace)', function () {
663+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.LOAD, true, true, false);
664+
});
665+
test('should NOT hot exit (reason: LOAD, windows: multiple, empty workspace)', function () {
666+
return scratchpadHotExitTest.call(this, HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE, ShutdownReason.LOAD, true, false, true);
667+
});
668+
});
669+
670+
568671
async function hotExitTest(this: any, setting: string, shutdownReason: ShutdownReason, multipleWindows: boolean, workspace: boolean, shouldVeto: boolean): Promise<void> {
569672
const { accessor, cleanup } = await createTracker();
570673

@@ -604,5 +707,60 @@ suite('WorkingCopyBackupTracker (native)', function () {
604707

605708
await cleanup();
606709
}
710+
711+
async function scratchpadHotExitTest(this: any, setting: string, shutdownReason: ShutdownReason, multipleWindows: boolean, workspace: boolean, shouldVeto: boolean): Promise<void> {
712+
const { accessor, cleanup } = await createTracker();
713+
714+
class TestBackupWorkingCopy extends TestWorkingCopy {
715+
716+
constructor(resource: URI) {
717+
super(resource);
718+
719+
accessor.workingCopyService.registerWorkingCopy(this);
720+
}
721+
722+
override capabilities = WorkingCopyCapabilities.Untitled | WorkingCopyCapabilities.Scratchpad;
723+
724+
override isDirty(): boolean {
725+
return false;
726+
}
727+
728+
override isModified(): boolean {
729+
return true;
730+
}
731+
}
732+
733+
// Set hot exit config
734+
accessor.filesConfigurationService.testOnFilesConfigurationChange({ files: { hotExit: setting } });
735+
736+
// Set empty workspace if required
737+
if (!workspace) {
738+
accessor.contextService.setWorkspace(new Workspace('empty:1508317022751'));
739+
}
740+
741+
// Set multiple windows if required
742+
if (multipleWindows) {
743+
accessor.nativeHostService.windowCount = Promise.resolve(2);
744+
}
745+
746+
// Set cancel to force a veto if hot exit does not trigger
747+
accessor.fileDialogService.setConfirmResult(ConfirmResult.CANCEL);
748+
749+
const resource = toResource.call(this, '/path/custom.txt');
750+
new TestBackupWorkingCopy(resource);
751+
752+
const event = new TestBeforeShutdownEvent();
753+
event.reason = shutdownReason;
754+
accessor.lifecycleService.fireBeforeShutdown(event);
755+
756+
const veto = await event.value;
757+
assert.ok(typeof event.finalValue === 'function'); // assert the tracker uses the internal finalVeto API
758+
assert.strictEqual(accessor.workingCopyBackupService.discardedBackups.length, 0); // When hot exit is set, backups should never be cleaned since the confirm result is cancel
759+
assert.strictEqual(veto, shouldVeto);
760+
761+
await cleanup();
762+
763+
await cleanup();
764+
}
607765
});
608766
});

0 commit comments

Comments
 (0)