Skip to content

Commit

Permalink
workspace: fix untitled file-saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Arama committed May 29, 2023
1 parent c1a2b7b commit 448a4b1
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions packages/core/src/browser/common-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<boolean> => {
await this.confirmSaveUntitledBeforeClose(untitledCaptionsToSave);
const captionsToSave = this.unsavedTabsCaptions();
await new Promise<void>(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<void> {
const languageInfo = await this.languageQuickPickService.pickDisplayLanguage();
if (languageInfo && !nls.isSelectedLocale(languageInfo.languageId) && await this.confirmRestart(
Expand All @@ -1167,7 +1186,22 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
this.windowService.reload();
}
}

protected async confirmSaveUntitledBeforeClose(toClose: Iterable<Widget>): Promise<boolean | undefined> {
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);
Expand Down

0 comments on commit 448a4b1

Please sign in to comment.