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

Have group.closeEditors return a boolean promise #146188

Merged
merged 3 commits into from Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vs/workbench/browser/parts/editor/editorActions.ts
Expand Up @@ -523,7 +523,7 @@ export class CloseLeftEditorsInGroupAction extends Action {
override async run(context?: IEditorIdentifier): Promise<void> {
const { group, editor } = this.getTarget(context);
if (group && editor) {
return group.closeEditors({ direction: CloseDirection.LEFT, except: editor, excludeSticky: true });
await group.closeEditors({ direction: CloseDirection.LEFT, except: editor, excludeSticky: true });
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/vs/workbench/browser/parts/editor/editorCommands.ts
Expand Up @@ -816,7 +816,7 @@ function registerCloseEditorCommands() {
.map(editor => typeof editor.editorIndex === 'number' ? group.getEditorByIndex(editor.editorIndex) : group.activeEditor))
.filter(editor => !keepStickyEditors || !group.isSticky(editor));

return group.closeEditors(editorsToClose, { preserveFocus: context?.preserveFocus });
await group.closeEditors(editorsToClose, { preserveFocus: context?.preserveFocus });
}
}));
}
Expand Down Expand Up @@ -881,7 +881,7 @@ function registerCloseEditorCommands() {
handler: (accessor, resourceOrContext?: URI | IEditorCommandsContext, context?: IEditorCommandsContext) => {
return Promise.all(getEditorsContext(accessor, resourceOrContext, context).groups.map(async group => {
if (group) {
return group.closeEditors({ savedOnly: true, excludeSticky: true }, { preserveFocus: context?.preserveFocus });
await group.closeEditors({ savedOnly: true, excludeSticky: true }, { preserveFocus: context?.preserveFocus });
}
}));
}
Expand Down Expand Up @@ -909,7 +909,7 @@ function registerCloseEditorCommands() {
}
}

return group.closeEditors(editorsToClose, { preserveFocus: context?.preserveFocus });
await group.closeEditors(editorsToClose, { preserveFocus: context?.preserveFocus });
}
}));
}
Expand All @@ -929,7 +929,7 @@ function registerCloseEditorCommands() {
group.pinEditor(group.activeEditor);
}

return group.closeEditors({ direction: CloseDirection.RIGHT, except: editor, excludeSticky: true }, { preserveFocus: context?.preserveFocus });
await group.closeEditors({ direction: CloseDirection.RIGHT, except: editor, excludeSticky: true }, { preserveFocus: context?.preserveFocus });
}
}
});
Expand Down
8 changes: 5 additions & 3 deletions src/vs/workbench/browser/parts/editor/editorGroupView.ts
Expand Up @@ -1710,21 +1710,23 @@ export class EditorGroupView extends Themable implements IEditorGroupView {

//#region closeEditors()

async closeEditors(args: EditorInput[] | ICloseEditorsFilter, options?: ICloseEditorOptions): Promise<void> {
async closeEditors(args: EditorInput[] | ICloseEditorsFilter, options?: ICloseEditorOptions): Promise<boolean> {
if (this.isEmpty) {
return;
return true;
}

const editors = this.doGetEditorsToClose(args);

// Check for dirty and veto
const veto = await this.handleDirtyClosing(editors.slice(0));
if (veto) {
return;
return false;
}

// Do close
this.doCloseEditors(editors, options);

return true;
}

private doGetEditorsToClose(args: EditorInput[] | ICloseEditorsFilter): EditorInput[] {
Expand Down
Expand Up @@ -640,9 +640,11 @@ export interface IEditorGroup {
* Closes specific editors in this group. This may trigger a confirmation dialog if
* there are dirty editors and thus returns a promise as value.
*
* @returns a promise when all editors are closed.
* @returns a promise whether the editors were closed or not. If `true`, the editors
* were closed and if `false` there was a veto closing the editors, e.g. when one
* is dirty.
*/
closeEditors(editors: EditorInput[] | ICloseEditorsFilter, options?: ICloseEditorOptions): Promise<void>;
closeEditors(editors: EditorInput[] | ICloseEditorsFilter, options?: ICloseEditorOptions): Promise<boolean>;

/**
* Closes all editors from the group. This may trigger a confirmation dialog if
Expand Down
Expand Up @@ -609,6 +609,7 @@ suite('EditorGroupsService', () => {

const accessor = instantiationService.createInstance(TestServiceAccessor);
accessor.fileDialogService.setConfirmResult(ConfirmResult.DONT_SAVE);
let closeResult = false;

const group = part.activeGroup;

Expand All @@ -621,13 +622,15 @@ suite('EditorGroupsService', () => {
await group.openEditor(input2);

accessor.fileDialogService.setConfirmResult(ConfirmResult.CANCEL);
await group.closeEditors([input1, input2]);
closeResult = await group.closeEditors([input1, input2]);
assert.strictEqual(closeResult, false);

assert.ok(!input1.gotDisposed);
assert.ok(!input2.gotDisposed);

accessor.fileDialogService.setConfirmResult(ConfirmResult.DONT_SAVE);
await group.closeEditors([input1, input2]);
closeResult = await group.closeEditors([input1, input2]);
assert.strictEqual(closeResult, true);

assert.ok(input1.gotDisposed);
assert.ok(input2.gotDisposed);
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/test/browser/workbenchTestServices.ts
Expand Up @@ -854,7 +854,7 @@ export class TestEditorGroupView implements IEditorGroupView {
copyEditor(_editor: EditorInput, _target: IEditorGroup, _options?: IEditorOptions): void { }
copyEditors(_editors: EditorInputWithOptions[], _target: IEditorGroup): void { }
async closeEditor(_editor?: EditorInput, options?: ICloseEditorOptions): Promise<boolean> { return true; }
async closeEditors(_editors: EditorInput[] | ICloseEditorsFilter, options?: ICloseEditorOptions): Promise<void> { }
async closeEditors(_editors: EditorInput[] | ICloseEditorsFilter, options?: ICloseEditorOptions): Promise<boolean> { return true; }
async closeAllEditors(options?: ICloseAllEditorsOptions): Promise<void> { }
async replaceEditors(_editors: IEditorReplacement[]): Promise<void> { }
pinEditor(_editor?: EditorInput): void { }
Expand Down