From e4eec1db9c612bf44201f40085f0e624d3045c8a Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 24 Jan 2020 09:27:02 +0100 Subject: [PATCH] :lipstick: backup tracker --- .../backup/electron-browser/backupTracker.ts | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/vs/workbench/contrib/backup/electron-browser/backupTracker.ts b/src/vs/workbench/contrib/backup/electron-browser/backupTracker.ts index be8f29a2f77..1aba5636ccb 100644 --- a/src/vs/workbench/contrib/backup/electron-browser/backupTracker.ts +++ b/src/vs/workbench/contrib/backup/electron-browser/backupTracker.ts @@ -44,29 +44,34 @@ export class NativeBackupTracker extends BackupTracker implements IWorkbenchCont // Dirty working copies need treatment on shutdown const dirtyWorkingCopies = this.workingCopyService.dirtyWorkingCopies; if (dirtyWorkingCopies.length) { - - // If auto save is enabled, save all non-untitled working copies - // and then check again for dirty copies - if (this.filesConfigurationService.getAutoSaveMode() !== AutoSaveMode.OFF) { - return this.doSaveAllBeforeShutdown(false /* not untitled */, SaveReason.AUTO).then(() => { - - // If we still have dirty working copies, we either have untitled ones or working copies that cannot be saved - const remainingDirtyWorkingCopies = this.workingCopyService.dirtyWorkingCopies; - if (remainingDirtyWorkingCopies.length) { - return this.handleDirtyBeforeShutdown(remainingDirtyWorkingCopies, reason); - } - - return false; // no veto (there are no remaining dirty working copies) - }); - } - - // Auto save is not enabled - return this.handleDirtyBeforeShutdown(dirtyWorkingCopies, reason); + return this.onBeforeShutdownWithDirty(reason, dirtyWorkingCopies); } return false; // no veto (no dirty working copies) } + protected async onBeforeShutdownWithDirty(reason: ShutdownReason, workingCopies: IWorkingCopy[]): Promise { + + // If auto save is enabled, save all non-untitled working copies + // and then check again for dirty copies + if (this.filesConfigurationService.getAutoSaveMode() !== AutoSaveMode.OFF) { + + // Save all files + await this.doSaveAllBeforeShutdown(false /* not untitled */, SaveReason.AUTO); + + // If we still have dirty working copies, we either have untitled ones or working copies that cannot be saved + const remainingDirtyWorkingCopies = this.workingCopyService.dirtyWorkingCopies; + if (remainingDirtyWorkingCopies.length) { + return this.handleDirtyBeforeShutdown(remainingDirtyWorkingCopies, reason); + } + + return false; // no veto (there are no remaining dirty working copies) + } + + // Auto save is not enabled + return this.handleDirtyBeforeShutdown(workingCopies, reason); + } + private async handleDirtyBeforeShutdown(workingCopies: IWorkingCopy[], reason: ShutdownReason): Promise { // Trigger backup if configured @@ -95,14 +100,14 @@ export class NativeBackupTracker extends BackupTracker implements IWorkbenchCont try { return await this.confirmBeforeShutdown(workingCopies.filter(workingCopy => backups.indexOf(workingCopy) === -1)); } catch (error) { - this.showErrorDialog(localize('backupTrackerConfirmFailed', "Editors that are dirty could not be saved or reverted."), error); + this.showErrorDialog(localize('backupTrackerConfirmFailed', "One or many editors that are dirty could not be saved or reverted."), error); return true; // veto (save or revert failed) } } private showErrorDialog(msg: string, error?: Error): void { - this.dialogService.show(Severity.Error, msg, [localize('ok', 'OK')], { detail: localize('backupErrorDetails', "Try saving the dirty editors first and then try again.") }); + this.dialogService.show(Severity.Error, msg, [localize('ok', 'OK')], { detail: localize('backupErrorDetails', "Try saving or reverting the dirty editors first and then try again.") }); this.logService.error(error ? `[backup tracker] ${msg}: ${error}` : `[backup tracker] ${msg}`); } @@ -174,10 +179,10 @@ export class NativeBackupTracker extends BackupTracker implements IWorkbenchCont // Save const confirm = await this.fileDialogService.showSaveConfirm(workingCopies.map(workingCopy => workingCopy.name)); if (confirm === ConfirmResult.SAVE) { - const dirtyCount = this.workingCopyService.dirtyCount; + const dirtyCountBeforeSave = this.workingCopyService.dirtyCount; await this.doSaveAllBeforeShutdown(workingCopies, SaveReason.EXPLICIT); - const savedWorkingCopies = dirtyCount - this.workingCopyService.dirtyCount; + const savedWorkingCopies = dirtyCountBeforeSave - this.workingCopyService.dirtyCount; if (savedWorkingCopies < workingCopies.length) { return true; // veto (save failed or was canceled) } @@ -224,7 +229,7 @@ export class NativeBackupTracker extends BackupTracker implements IWorkbenchCont } } - private async doRevertAllBeforeShutdown(workingCopies = this.workingCopyService.dirtyWorkingCopies): Promise { + private async doRevertAllBeforeShutdown(workingCopies: IWorkingCopy[]): Promise { // Soft revert is good enough on shutdown const revertOptions = { soft: true };