Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workspace: fix untitled file-saving #12577

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions packages/core/src/browser/common-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -1171,21 +1171,38 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
}

onWillStop(): OnWillStopAction | undefined {
try {
if (this.shouldPreventClose || this.shell.canSaveAll()) {
const captionsToSave = this.unsavedTabsCaptions();
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;
}

return { reason: 'Dirty editors present', action: async () => confirmExitWithOrWithoutSaving(captionsToSave, async () => this.shell.saveAll()) };
}
} finally {
this.shouldPreventClose = false;
}
};
}
}
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 =>
NavigatableWidget.getUri(widget)?.scheme === UNTITLED_SCHEME && 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 @@ -1196,7 +1213,14 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
this.windowService.reload();
}
}

protected async saveDirty(toSave: Widget[]): Promise<void> {
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);
Expand Down
Loading