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

Prompt user with dialog if triggering open with on unsaved file #115264

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 25 additions & 1 deletion src/vs/workbench/browser/parts/editor/editorActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import { IEditorService } from 'vs/workbench/services/editor/common/editorServic
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
import { IFileDialogService, ConfirmResult } from 'vs/platform/dialogs/common/dialogs';
import { IFileDialogService, ConfirmResult, IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { ItemActivation, IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import { AllEditorsByMostRecentlyUsedQuickAccess, ActiveGroupEditorsByMostRecentlyUsedQuickAccess, AllEditorsByAppearanceQuickAccess } from 'vs/workbench/browser/parts/editor/editorQuickAccess';
import { Codicon } from 'vs/base/common/codicons';
import { IFilesConfigurationService, AutoSaveMode } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
import { openEditorWith, getAllAvailableEditors } from 'vs/workbench/services/editor/common/editorOpenWith';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import Severity from 'vs/base/common/severity';

export class ExecuteCommandAction extends Action {

Expand Down Expand Up @@ -1895,6 +1896,7 @@ export class ReopenResourcesAction extends Action {
id: string,
label: string,
@IEditorService private readonly editorService: IEditorService,
@IDialogService private readonly dialogService: IDialogService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
) {
super(id, label);
Expand All @@ -1913,6 +1915,28 @@ export class ReopenResourcesAction extends Action {

const options = activeEditorPane.options;
const group = activeEditorPane.group;
// The current editor needs to be saved before it can be triggered with open with
if (activeInput.isDirty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this fire even if you select the default VS Code editor? Also, should it only be fired if you select a custom binary editor since a custom text editor should be able to handle this case correctly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly wondering if it should be moved into the call to openEditorWith instead after you have selected an editor but before it has actually replaced the old editor

const message = nls.localize('promptOpenWith.unsaved', 'This file has unsaved changes and is attempting to be opened in a custom editor.');
const buttonActions: string[] = [
nls.localize('save', 'Save'),
nls.localize('discard', 'Discard Changes'),
nls.localize('abort', 'Abort')
];
enum UnsavedEditorAction {
SAVE = 0,
DISCARD = 1,
ABORT = 2,
}
const res = await this.dialogService.show(Severity.Warning, message, buttonActions);
if (res.choice === UnsavedEditorAction.SAVE) {
await activeInput.save(group.id, { reason: SaveReason.EXPLICIT });
} else if (res.choice === UnsavedEditorAction.DISCARD) {
await this.editorService.revert({ groupId: group.id, editor: activeInput });
} else {
return;
}
}
await this.instantiationService.invokeFunction(openEditorWith, activeInput, undefined, options, group);
}
}
Expand Down