From 14fea31f33b25d30cf7f9a693114d0bc3c61c666 Mon Sep 17 00:00:00 2001 From: Vlad Arama Date: Tue, 22 Aug 2023 10:51:38 -0400 Subject: [PATCH 1/2] fix untitled file-saving This commit fixes the untitled file-saving issue by making sure that the untitled files are saved correctly before closing the workspace / closing the application. Signed-off-by: Vlad Arama --- .../browser/common-frontend-contribution.ts | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index 2e95ea0a8eaa9..9ae9d62fbe701 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, EOL } 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 { @@ -1173,9 +1173,18 @@ 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()) }; + return { + reason: 'Dirty editors present', + action: async () => { + const captionsToSave = this.unsavedTabsCaptions(); + const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions(); + const result = await confirmExitWithOrWithoutSaving(captionsToSave, async () => { + await this.saveDirty(untitledCaptionsToSave); + await this.shell.saveAll(); + }); + return result; + } + }; } } finally { this.shouldPreventClose = false; @@ -1186,6 +1195,11 @@ export class CommonFrontendContribution implements FrontendApplicationContributi .filter(widget => this.saveResourceService.canSave(widget)) .map(widget => widget.title.label); } + protected unsavedUntitledTabsCaptions(): Widget[] { + return this.shell.widgets.filter(widget => + NavigatableWidget.getUri(widget)?.scheme === UNTITLED_SCHEME && this.saveResourceService.canSaveAs(widget) + ); + } protected async configureDisplayLanguage(): Promise { const languageInfo = await this.languageQuickPickService.pickDisplayLanguage(); if (languageInfo && !nls.isSelectedLocale(languageInfo.languageId) && await this.confirmRestart( @@ -1196,7 +1210,14 @@ export class CommonFrontendContribution implements FrontendApplicationContributi this.windowService.reload(); } } - + protected async saveDirty(toSave: Widget[]): Promise { + for (const widget of toSave) { + const saveable = Saveable.get(widget); + if (saveable?.dirty) { + await this.saveResourceService.save(widget); + } + } + } protected toggleBreadcrumbs(): void { const value: boolean | undefined = this.preferenceService.get('breadcrumbs.enabled'); this.preferenceService.set('breadcrumbs.enabled', !value, PreferenceScope.User); From 7654ba717d86f528bc226784d637ec64eda297dd Mon Sep 17 00:00:00 2001 From: Vlad Arama Date: Wed, 23 Aug 2023 09:27:46 -0400 Subject: [PATCH 2/2] prevent closing workspace when cancelling dialog This commit includes a refactoring of the `onWillStop()` method to prevent the workspace from closing when we decide to cancel the file saving dialog. Signed-off-by: Vlad Arama --- .../browser/common-frontend-contribution.ts | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/core/src/browser/common-frontend-contribution.ts b/packages/core/src/browser/common-frontend-contribution.ts index 9ae9d62fbe701..7df314fd08cb5 100644 --- a/packages/core/src/browser/common-frontend-contribution.ts +++ b/packages/core/src/browser/common-frontend-contribution.ts @@ -1171,23 +1171,26 @@ export class CommonFrontendContribution implements FrontendApplicationContributi } onWillStop(): OnWillStopAction | undefined { - try { - if (this.shouldPreventClose || this.shell.canSaveAll()) { - return { - reason: 'Dirty editors present', - action: async () => { - const captionsToSave = this.unsavedTabsCaptions(); - const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions(); - const result = await confirmExitWithOrWithoutSaving(captionsToSave, async () => { - await this.saveDirty(untitledCaptionsToSave); - await this.shell.saveAll(); - }); + if (this.shouldPreventClose || this.shell.canSaveAll()) { + return { + reason: 'Dirty editors present', + action: async () => { + const captionsToSave = this.unsavedTabsCaptions(); + const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions(); + const result = await confirmExitWithOrWithoutSaving(captionsToSave, async () => { + await this.saveDirty(untitledCaptionsToSave); + await this.shell.saveAll(); + }); + if (this.shell.canSaveAll()) { + this.shouldPreventClose = true; + return false; + } else { + this.shouldPreventClose = false; return result; } - }; - } - } finally { - this.shouldPreventClose = false; + + } + }; } } protected unsavedTabsCaptions(): string[] {