From 448a4b13a257b5167725e7fca98e140ae5568ec4 Mon Sep 17 00:00:00 2001 From: Vlad Arama Date: Mon, 29 May 2023 10:45:20 -0400 Subject: [PATCH 1/6] workspace: fix untitled file-saving --- .../browser/common-frontend-contribution.ts | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index 57723845f3aef..f2581c5c552ed 100644 --- a/packages/core/src/browser/common-frontend-contribution.ts +++ b/packages/core/src/browser/common-frontend-contribution.ts @@ -51,7 +51,7 @@ import { EncodingRegistry } from './encoding-registry'; import { UTF8 } from '../common/encodings'; import { EnvVariablesServer } from '../common/env-variables'; import { AuthenticationService } from './authentication-service'; -import { FormatType, Saveable, SaveOptions } from './saveable'; +import { FormatType, Saveable, SaveOptions, ShouldSaveDialog } from './saveable'; import { QuickInputService, QuickPickItem, QuickPickItemOrSeparator } from './quick-input'; import { AsyncLocalizationProvider } from '../common/i18n/localization'; import { nls } from '../common/nls'; @@ -1144,19 +1144,38 @@ export class CommonFrontendContribution implements FrontendApplicationContributi onWillStop(): OnWillStopAction | undefined { try { if (this.shouldPreventClose || this.shell.canSaveAll()) { - const captionsToSave = this.unsavedTabsCaptions(); - - return { reason: 'Dirty editors present', action: async () => confirmExitWithOrWithoutSaving(captionsToSave, async () => this.shell.saveAll()) }; + const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions(); + const action = async (): Promise => { + await this.confirmSaveUntitledBeforeClose(untitledCaptionsToSave); + const captionsToSave = this.unsavedTabsCaptions(); + await new Promise(resolve => { + confirmExitWithOrWithoutSaving(captionsToSave, async () => { + await this.shell.saveAll(); + resolve(); + }); + }); + return true; + }; + return { + reason: 'Dirty editors present', + action, + }; } } finally { this.shouldPreventClose = false; } + return undefined; } protected unsavedTabsCaptions(): string[] { return this.shell.widgets .filter(widget => this.saveResourceService.canSave(widget)) .map(widget => widget.title.label); } + protected unsavedUntitledTabsCaptions(): Widget[] { + return this.shell.widgets.filter(widget => + widget.title.label.includes('Untitled-') && this.saveResourceService.canSaveAs(widget) + ); + } protected async configureDisplayLanguage(): Promise { const languageInfo = await this.languageQuickPickService.pickDisplayLanguage(); if (languageInfo && !nls.isSelectedLocale(languageInfo.languageId) && await this.confirmRestart( @@ -1167,7 +1186,22 @@ export class CommonFrontendContribution implements FrontendApplicationContributi this.windowService.reload(); } } - + protected async confirmSaveUntitledBeforeClose(toClose: Iterable): Promise { + for (const widget of toClose) { + const saveable = Saveable.get(widget); + if (saveable?.dirty) { + const userWantsToSave = await new ShouldSaveDialog(widget).open(); + if (userWantsToSave === undefined) { // User clicked cancel. + return undefined; + } else if (userWantsToSave) { + await this.saveResourceService.save(widget); + } else { + await saveable.revert?.(); + } + } + } + return true; + } protected toggleBreadcrumbs(): void { const value: boolean | undefined = this.preferenceService.get('breadcrumbs.enabled'); this.preferenceService.set('breadcrumbs.enabled', !value, PreferenceScope.User); From 346e6d214d18e70c6aa5e16a773a917367894db7 Mon Sep 17 00:00:00 2001 From: Vlad Arama Date: Thu, 1 Jun 2023 08:37:39 -0400 Subject: [PATCH 2/6] workspace: fix untitled file-saving after review --- .../src/browser/common-frontend-contribution.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index f2581c5c552ed..bb318b99da153 100644 --- a/packages/core/src/browser/common-frontend-contribution.ts +++ b/packages/core/src/browser/common-frontend-contribution.ts @@ -51,7 +51,7 @@ import { EncodingRegistry } from './encoding-registry'; import { UTF8 } from '../common/encodings'; import { EnvVariablesServer } from '../common/env-variables'; import { AuthenticationService } from './authentication-service'; -import { FormatType, Saveable, SaveOptions, ShouldSaveDialog } from './saveable'; +import { FormatType, Saveable, SaveOptions } from './saveable'; import { QuickInputService, QuickPickItem, QuickPickItemOrSeparator } from './quick-input'; import { AsyncLocalizationProvider } from '../common/i18n/localization'; import { nls } from '../common/nls'; @@ -1146,11 +1146,11 @@ export class CommonFrontendContribution implements FrontendApplicationContributi if (this.shouldPreventClose || this.shell.canSaveAll()) { const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions(); const action = async (): Promise => { - await this.confirmSaveUntitledBeforeClose(untitledCaptionsToSave); const captionsToSave = this.unsavedTabsCaptions(); await new Promise(resolve => { confirmExitWithOrWithoutSaving(captionsToSave, async () => { await this.shell.saveAll(); + await this.confirmSaveUntitledBeforeClose(untitledCaptionsToSave); resolve(); }); }); @@ -1190,14 +1190,7 @@ export class CommonFrontendContribution implements FrontendApplicationContributi for (const widget of toClose) { const saveable = Saveable.get(widget); if (saveable?.dirty) { - const userWantsToSave = await new ShouldSaveDialog(widget).open(); - if (userWantsToSave === undefined) { // User clicked cancel. - return undefined; - } else if (userWantsToSave) { - await this.saveResourceService.save(widget); - } else { - await saveable.revert?.(); - } + await this.saveResourceService.save(widget); } } return true; From e4a327378f9faa6f83072b6a518c61370dab1a27 Mon Sep 17 00:00:00 2001 From: Vlad Arama Date: Thu, 1 Jun 2023 10:57:33 -0400 Subject: [PATCH 3/6] workspace: simplifying `onWillStop` --- .../browser/common-frontend-contribution.ts | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index bb318b99da153..7c8d7a354854c 100644 --- a/packages/core/src/browser/common-frontend-contribution.ts +++ b/packages/core/src/browser/common-frontend-contribution.ts @@ -1144,27 +1144,22 @@ export class CommonFrontendContribution implements FrontendApplicationContributi onWillStop(): OnWillStopAction | undefined { try { if (this.shouldPreventClose || this.shell.canSaveAll()) { - const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions(); - const action = async (): Promise => { - const captionsToSave = this.unsavedTabsCaptions(); - await new Promise(resolve => { - confirmExitWithOrWithoutSaving(captionsToSave, async () => { - await this.shell.saveAll(); - await this.confirmSaveUntitledBeforeClose(untitledCaptionsToSave); - resolve(); - }); - }); - return true; - }; return { reason: 'Dirty editors present', - action, - }; - } + action: async () => { + const captionsToSave = this.unsavedTabsCaptions(); + const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions(); + const result = await confirmExitWithOrWithoutSaving(captionsToSave, async () => { + await this.shell.saveAll(); + await this.confirmSaveUntitledBeforeClose(untitledCaptionsToSave); + }); + return result; + } + }; + } } finally { this.shouldPreventClose = false; } - return undefined; } protected unsavedTabsCaptions(): string[] { return this.shell.widgets From 0a4469a3bc41fef0dafcdb61b277095f20f86a36 Mon Sep 17 00:00:00 2001 From: Vlad Arama Date: Fri, 2 Jun 2023 10:27:09 -0400 Subject: [PATCH 4/6] workspace: renaming untitled file-saving func --- packages/core/src/browser/common-frontend-contribution.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index 7c8d7a354854c..fdec759962e23 100644 --- a/packages/core/src/browser/common-frontend-contribution.ts +++ b/packages/core/src/browser/common-frontend-contribution.ts @@ -1151,7 +1151,7 @@ export class CommonFrontendContribution implements FrontendApplicationContributi const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions(); const result = await confirmExitWithOrWithoutSaving(captionsToSave, async () => { await this.shell.saveAll(); - await this.confirmSaveUntitledBeforeClose(untitledCaptionsToSave); + await this.saveDirty(untitledCaptionsToSave); }); return result; } @@ -1181,14 +1181,13 @@ export class CommonFrontendContribution implements FrontendApplicationContributi this.windowService.reload(); } } - protected async confirmSaveUntitledBeforeClose(toClose: Iterable): Promise { - for (const widget of toClose) { + protected async saveDirty(toSave: Widget[]): Promise { + for (const widget of toSave) { const saveable = Saveable.get(widget); if (saveable?.dirty) { await this.saveResourceService.save(widget); } } - return true; } protected toggleBreadcrumbs(): void { const value: boolean | undefined = this.preferenceService.get('breadcrumbs.enabled'); From d430643319854c8f6853c77db6c45da715a02148 Mon Sep 17 00:00:00 2001 From: Vlad Arama Date: Tue, 6 Jun 2023 08:39:23 -0400 Subject: [PATCH 5/6] workspace: fix dirty untitled not saving on close --- packages/core/src/browser/common-frontend-contribution.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index fdec759962e23..9277d6bc7abc1 100644 --- a/packages/core/src/browser/common-frontend-contribution.ts +++ b/packages/core/src/browser/common-frontend-contribution.ts @@ -1188,6 +1188,7 @@ export class CommonFrontendContribution implements FrontendApplicationContributi await this.saveResourceService.save(widget); } } + await this.shell.saveAll(); } protected toggleBreadcrumbs(): void { const value: boolean | undefined = this.preferenceService.get('breadcrumbs.enabled'); From 88bc7d781362db766d979e5b16836ff4bbad4451 Mon Sep 17 00:00:00 2001 From: Vlad Arama Date: Thu, 8 Jun 2023 08:45:25 -0400 Subject: [PATCH 6/6] workspace: modifying retrieval of untitled files --- .../browser/common-frontend-contribution.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index 9277d6bc7abc1..adb269304c741 100644 --- a/packages/core/src/browser/common-frontend-contribution.ts +++ b/packages/core/src/browser/common-frontend-contribution.ts @@ -36,7 +36,7 @@ import { OS, isOSX, isWindows } from '../common/os'; import { ResourceContextKey } from './resource-context-key'; import { UriSelection } from '../common/selection'; import { StorageService } from './storage-service'; -import { Navigatable } from './navigatable'; +import { Navigatable, NavigatableWidget } from './navigatable'; import { QuickViewService } from './quick-input/quick-view-service'; import { environment } from '@theia/application-package/lib/environment'; import { IconTheme, IconThemeService } from './icon-theme-service'; @@ -63,7 +63,7 @@ import { DecorationStyle } from './decoration-style'; import { isPinned, Title, togglePinned, Widget } from './widgets'; import { SaveResourceService } from './save-resource-service'; import { UserWorkingDirectoryProvider } from './user-working-directory-provider'; -import { UntitledResourceResolver } from '../common'; +import { UNTITLED_SCHEME, UntitledResourceResolver } from '../common'; import { LanguageQuickPickService } from './i18n/language-quick-pick-service'; export namespace CommonMenus { @@ -1150,13 +1150,13 @@ export class CommonFrontendContribution implements FrontendApplicationContributi const captionsToSave = this.unsavedTabsCaptions(); const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions(); const result = await confirmExitWithOrWithoutSaving(captionsToSave, async () => { - await this.shell.saveAll(); - await this.saveDirty(untitledCaptionsToSave); - }); - return result; - } - }; - } + await this.shell.saveAll(); + await this.saveDirty(untitledCaptionsToSave); + }); + return result; + } + }; + } } finally { this.shouldPreventClose = false; } @@ -1168,7 +1168,7 @@ export class CommonFrontendContribution implements FrontendApplicationContributi } protected unsavedUntitledTabsCaptions(): Widget[] { return this.shell.widgets.filter(widget => - widget.title.label.includes('Untitled-') && this.saveResourceService.canSaveAs(widget) + NavigatableWidget.getUri(widget)?.scheme === UNTITLED_SCHEME && this.saveResourceService.canSaveAs(widget) ); } protected async configureDisplayLanguage(): Promise {