-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
editorQuickAccess.ts
272 lines (217 loc) · 9.28 KB
/
editorQuickAccess.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'vs/css!./media/editorquickaccess';
import { localize } from 'vs/nls';
import { IQuickPickSeparator, quickPickItemScorerAccessor, IQuickPickItemWithResource, IQuickPick } from 'vs/platform/quickinput/common/quickInput';
import { PickerQuickAccessProvider, IPickerQuickAccessItem, TriggerAction } from 'vs/platform/quickinput/browser/pickerQuickAccess';
import { IEditorGroupsService, GroupsOrder } from 'vs/workbench/services/editor/common/editorGroupsService';
import { EditorsOrder, IEditorIdentifier, EditorResourceAccessor, SideBySideEditor, GroupIdentifier } from 'vs/workbench/common/editor';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
import { getIconClasses } from 'vs/editor/common/services/getIconClasses';
import { prepareQuery, scoreItemFuzzy, compareItemsByFuzzyScore, FuzzyScorerCache } from 'vs/base/common/fuzzyScorer';
import { CancellationToken } from 'vs/base/common/cancellation';
import { IDisposable } from 'vs/base/common/lifecycle';
import { Codicon } from 'vs/base/common/codicons';
interface IEditorQuickPickItem extends IQuickPickItemWithResource, IPickerQuickAccessItem {
groupId: GroupIdentifier;
}
export abstract class BaseEditorQuickAccessProvider extends PickerQuickAccessProvider<IEditorQuickPickItem> {
private readonly pickState = new class {
scorerCache: FuzzyScorerCache = Object.create(null);
isQuickNavigating: boolean | undefined = undefined;
reset(isQuickNavigating: boolean): void {
// Caches
if (!isQuickNavigating) {
this.scorerCache = Object.create(null);
}
// Other
this.isQuickNavigating = isQuickNavigating;
}
};
constructor(
prefix: string,
@IEditorGroupsService protected readonly editorGroupService: IEditorGroupsService,
@IEditorService protected readonly editorService: IEditorService,
@IModelService private readonly modelService: IModelService,
@IModeService private readonly modeService: IModeService
) {
super(prefix,
{
canAcceptInBackground: true,
noResultsPick: {
label: localize('noViewResults', "No matching editors"),
groupId: -1
}
}
);
}
override provide(picker: IQuickPick<IEditorQuickPickItem>, token: CancellationToken): IDisposable {
// Reset the pick state for this run
this.pickState.reset(!!picker.quickNavigate);
// Start picker
return super.provide(picker, token);
}
protected _getPicks(filter: string): Array<IEditorQuickPickItem | IQuickPickSeparator> {
const query = prepareQuery(filter);
// Filtering
const filteredEditorEntries = this.doGetEditorPickItems().filter(entry => {
if (!query.normalized) {
return true;
}
// Score on label and description
const itemScore = scoreItemFuzzy(entry, query, true, quickPickItemScorerAccessor, this.pickState.scorerCache);
if (!itemScore.score) {
return false;
}
// Apply highlights
entry.highlights = { label: itemScore.labelMatch, description: itemScore.descriptionMatch };
return true;
});
// Sorting
if (query.normalized) {
const groups = this.editorGroupService.getGroups(GroupsOrder.GRID_APPEARANCE).map(group => group.id);
filteredEditorEntries.sort((entryA, entryB) => {
if (entryA.groupId !== entryB.groupId) {
return groups.indexOf(entryA.groupId) - groups.indexOf(entryB.groupId); // older groups first
}
return compareItemsByFuzzyScore(entryA, entryB, query, true, quickPickItemScorerAccessor, this.pickState.scorerCache);
});
}
// Grouping (for more than one group)
const filteredEditorEntriesWithSeparators: Array<IEditorQuickPickItem | IQuickPickSeparator> = [];
if (this.editorGroupService.count > 1) {
let lastGroupId: number | undefined = undefined;
for (const entry of filteredEditorEntries) {
if (typeof lastGroupId !== 'number' || lastGroupId !== entry.groupId) {
const group = this.editorGroupService.getGroup(entry.groupId);
if (group) {
filteredEditorEntriesWithSeparators.push({ type: 'separator', label: group.label });
}
lastGroupId = entry.groupId;
}
filteredEditorEntriesWithSeparators.push(entry);
}
} else {
filteredEditorEntriesWithSeparators.push(...filteredEditorEntries);
}
return filteredEditorEntriesWithSeparators;
}
private doGetEditorPickItems(): Array<IEditorQuickPickItem> {
const editors = this.doGetEditors();
const mapGroupIdToGroupAriaLabel = new Map<GroupIdentifier, string>();
for (const { groupId } of editors) {
if (!mapGroupIdToGroupAriaLabel.has(groupId)) {
const group = this.editorGroupService.getGroup(groupId);
if (group) {
mapGroupIdToGroupAriaLabel.set(groupId, group.ariaLabel);
}
}
}
return this.doGetEditors().map(({ editor, groupId }): IEditorQuickPickItem => {
const resource = EditorResourceAccessor.getOriginalUri(editor, { supportSideBySide: SideBySideEditor.PRIMARY });
const isDirty = editor.isDirty() && !editor.isSaving();
const description = editor.getDescription();
const nameAndDescription = description ? `${editor.getName()} ${description}` : editor.getName();
return {
groupId,
resource,
label: editor.getName(),
ariaLabel: (() => {
if (mapGroupIdToGroupAriaLabel.size > 1) {
return isDirty ?
localize('entryAriaLabelWithGroupDirty', "{0}, dirty, {1}", nameAndDescription, mapGroupIdToGroupAriaLabel.get(groupId)) :
localize('entryAriaLabelWithGroup', "{0}, {1}", nameAndDescription, mapGroupIdToGroupAriaLabel.get(groupId));
}
return isDirty ? localize('entryAriaLabelDirty', "{0}, dirty", nameAndDescription) : nameAndDescription;
})(),
description,
iconClasses: getIconClasses(this.modelService, this.modeService, resource).concat(editor.getLabelExtraClasses()),
italic: !this.editorGroupService.getGroup(groupId)?.isPinned(editor),
buttons: (() => {
return [
{
iconClass: isDirty ? ('dirty-editor ' + Codicon.closeDirty.classNames) : Codicon.close.classNames,
tooltip: localize('closeEditor', "Close Editor"),
alwaysVisible: isDirty
}
];
})(),
trigger: async () => {
const group = this.editorGroupService.getGroup(groupId);
if (group) {
await group.closeEditor(editor, { preserveFocus: true });
if (!group.contains(editor)) {
return TriggerAction.REMOVE_ITEM;
}
}
return TriggerAction.NO_ACTION;
},
accept: (keyMods, event) => this.editorGroupService.getGroup(groupId)?.openEditor(editor, { preserveFocus: event.inBackground }),
};
});
}
protected abstract doGetEditors(): IEditorIdentifier[];
}
//#region Active Editor Group Editors by Most Recently Used
export class ActiveGroupEditorsByMostRecentlyUsedQuickAccess extends BaseEditorQuickAccessProvider {
static PREFIX = 'edt active ';
constructor(
@IEditorGroupsService editorGroupService: IEditorGroupsService,
@IEditorService editorService: IEditorService,
@IModelService modelService: IModelService,
@IModeService modeService: IModeService
) {
super(ActiveGroupEditorsByMostRecentlyUsedQuickAccess.PREFIX, editorGroupService, editorService, modelService, modeService);
}
protected doGetEditors(): IEditorIdentifier[] {
const group = this.editorGroupService.activeGroup;
return group.getEditors(EditorsOrder.MOST_RECENTLY_ACTIVE).map(editor => ({ editor, groupId: group.id }));
}
}
//#endregion
//#region All Editors by Appearance
export class AllEditorsByAppearanceQuickAccess extends BaseEditorQuickAccessProvider {
static PREFIX = 'edt ';
constructor(
@IEditorGroupsService editorGroupService: IEditorGroupsService,
@IEditorService editorService: IEditorService,
@IModelService modelService: IModelService,
@IModeService modeService: IModeService
) {
super(AllEditorsByAppearanceQuickAccess.PREFIX, editorGroupService, editorService, modelService, modeService);
}
protected doGetEditors(): IEditorIdentifier[] {
const entries: IEditorIdentifier[] = [];
for (const group of this.editorGroupService.getGroups(GroupsOrder.GRID_APPEARANCE)) {
for (const editor of group.getEditors(EditorsOrder.SEQUENTIAL)) {
entries.push({ editor, groupId: group.id });
}
}
return entries;
}
}
//#endregion
//#region All Editors by Most Recently Used
export class AllEditorsByMostRecentlyUsedQuickAccess extends BaseEditorQuickAccessProvider {
static PREFIX = 'edt mru ';
constructor(
@IEditorGroupsService editorGroupService: IEditorGroupsService,
@IEditorService editorService: IEditorService,
@IModelService modelService: IModelService,
@IModeService modeService: IModeService
) {
super(AllEditorsByMostRecentlyUsedQuickAccess.PREFIX, editorGroupService, editorService, modelService, modeService);
}
protected doGetEditors(): IEditorIdentifier[] {
const entries: IEditorIdentifier[] = [];
for (const editor of this.editorService.getEditors(EditorsOrder.MOST_RECENTLY_ACTIVE)) {
entries.push(editor);
}
return entries;
}
}
//#endregion