Skip to content

Commit a6d692e

Browse files
author
Benjamin Pasero
committed
quick access - editors picker to have an action to close editors and indicate dirty state
1 parent 5788cf9 commit a6d692e

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

src/vs/platform/quickinput/common/quickAccess.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ export interface IPickerQuickAccessItem extends IQuickPickItem {
177177
* @param buttonIndex index of the button of the item that
178178
* was clicked.
179179
*
180-
* @returns a value that indicates what should happen after the trigger.
180+
* @returns a value that indicates what should happen after the trigger
181+
* which can be a `Promise` for long running operations.
181182
*/
182-
trigger?(buttonIndex: number): TriggerAction;
183+
trigger?(buttonIndex: number): TriggerAction | Promise<TriggerAction>;
183184
}
184185

185186
export abstract class PickerQuickAccessProvider<T extends IPickerQuickAccessItem> implements IQuickAccessProvider {
@@ -210,13 +211,18 @@ export abstract class PickerQuickAccessProvider<T extends IPickerQuickAccessItem
210211
} else {
211212
picker.busy = true;
212213
try {
213-
picker.items = await res;
214+
const items = await res;
215+
if (token.isCancellationRequested) {
216+
return;
217+
}
218+
219+
picker.items = items;
214220
} finally {
215-
picker.busy = false;
221+
if (!token.isCancellationRequested) {
222+
picker.busy = false;
223+
}
216224
}
217225
}
218-
219-
this.getPicks(picker.value.substr(this.prefix.length).trim(), picksCts.token);
220226
};
221227
disposables.add(picker.onDidChangeValue(() => updatePickerItems()));
222228
updatePickerItems();
@@ -231,11 +237,17 @@ export abstract class PickerQuickAccessProvider<T extends IPickerQuickAccessItem
231237
}));
232238

233239
// Trigger the pick with button index if button triggered
234-
disposables.add(picker.onDidTriggerItemButton(({ button, item }) => {
240+
disposables.add(picker.onDidTriggerItemButton(async ({ button, item }) => {
235241
if (typeof item.trigger === 'function') {
236242
const buttonIndex = item.buttons?.indexOf(button) ?? -1;
237243
if (buttonIndex >= 0) {
238-
const action = item.trigger(buttonIndex);
244+
const result = item.trigger(buttonIndex);
245+
const action = (typeof result === 'number') ? result : await result;
246+
247+
if (token.isCancellationRequested) {
248+
return;
249+
}
250+
239251
switch (action) {
240252
case TriggerAction.NO_ACTION:
241253
break;

src/vs/workbench/browser/parts/editor/editorQuickAccess.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { localize } from 'vs/nls';
77
import { IQuickPickSeparator, quickPickItemScorerAccessor, IQuickPickItemWithResource } from 'vs/platform/quickinput/common/quickInput';
8-
import { PickerQuickAccessProvider, IPickerQuickAccessItem } from 'vs/platform/quickinput/common/quickAccess';
8+
import { PickerQuickAccessProvider, IPickerQuickAccessItem, TriggerAction } from 'vs/platform/quickinput/common/quickAccess';
99
import { IEditorGroupsService, GroupsOrder } from 'vs/workbench/services/editor/common/editorGroupsService';
1010
import { EditorsOrder, IEditorIdentifier, toResource, SideBySideEditor } from 'vs/workbench/common/editor';
1111
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
@@ -83,19 +83,32 @@ export abstract class BaseEditorQuickAccessProvider extends PickerQuickAccessPro
8383
}
8484

8585
private doGetEditorPickItems(): Array<IEditorQuickPickItem> {
86-
return this.doGetEditors().map(({ editor, groupId }) => {
86+
return this.doGetEditors().map(({ editor, groupId }): IEditorQuickPickItem => {
8787
const resource = toResource(editor, { supportSideBySide: SideBySideEditor.MASTER });
88+
const isDirty = editor.isDirty() && !editor.isSaving();
8889

8990
return {
9091
editor,
9192
groupId,
9293
resource,
93-
label: editor.isDirty() && !editor.isSaving() ? `$(circle-filled) ${editor.getName()}` : editor.getName(),
94-
ariaLabel: localize('entryAriaLabel', "{0}, editor picker", editor.getName()),
94+
label: editor.getName(),
95+
ariaLabel: localize('entryAriaLabel', "{0}, editors picker", editor.getName()),
9596
description: editor.getDescription(),
9697
iconClasses: getIconClasses(this.modelService, this.modeService, resource),
9798
italic: !this.editorGroupService.getGroup(groupId)?.isPinned(editor),
98-
accept: () => this.editorGroupService.getGroup(groupId)?.openEditor(editor)
99+
buttonsAlwaysVisible: isDirty,
100+
buttons: [
101+
{
102+
iconClass: isDirty ? 'codicon-circle-filled' : 'codicon-close',
103+
tooltip: localize('closeEditor', "Close Editor")
104+
}
105+
],
106+
trigger: async () => {
107+
await this.editorGroupService.getGroup(groupId)?.closeEditor(editor, { preserveFocus: true });
108+
109+
return TriggerAction.REFRESH_PICKER;
110+
},
111+
accept: () => this.editorGroupService.getGroup(groupId)?.openEditor(editor),
99112
};
100113
});
101114
}

0 commit comments

Comments
 (0)