-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
Copy pathopenSymbolHandler.ts
232 lines (188 loc) · 7.79 KB
/
openSymbolHandler.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import { URI } from 'vs/base/common/uri';
import { TPromise } from 'vs/base/common/winjs.base';
import { onUnexpectedError } from 'vs/base/common/errors';
import { ThrottledDelayer } from 'vs/base/common/async';
import { QuickOpenHandler, EditorQuickOpenEntry } from 'vs/workbench/browser/quickopen';
import { QuickOpenModel, QuickOpenEntry, compareEntries } from 'vs/base/parts/quickopen/browser/quickOpenModel';
import { IAutoFocus, Mode, IEntryRunContext } from 'vs/base/parts/quickopen/common/quickOpen';
import * as filters from 'vs/base/common/filters';
import * as strings from 'vs/base/common/strings';
import { Range } from 'vs/editor/common/core/range';
import { IWorkbenchEditorConfiguration } from 'vs/workbench/common/editor';
import { symbolKindToCssClass } from 'vs/editor/common/modes';
import { IResourceInput } from 'vs/platform/editor/common/editor';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IWorkspaceSymbolProvider, getWorkspaceSymbols, IWorkspaceSymbol } from 'vs/workbench/parts/search/common/search';
import { basename } from 'vs/base/common/paths';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { ILabelService } from 'vs/platform/label/common/label';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Schemas } from 'vs/base/common/network';
import { IOpenerService } from 'vs/platform/opener/common/opener';
class SymbolEntry extends EditorQuickOpenEntry {
private bearingResolve: Thenable<this>;
constructor(
private bearing: IWorkspaceSymbol,
private provider: IWorkspaceSymbolProvider,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IEditorService editorService: IEditorService,
@ILabelService private labelService: ILabelService,
@IOpenerService private openerService: IOpenerService
) {
super(editorService);
}
getLabel(): string {
return this.bearing.name;
}
getAriaLabel(): string {
return nls.localize('entryAriaLabel', "{0}, symbols picker", this.getLabel());
}
getDescription(): string {
const containerName = this.bearing.containerName;
if (this.bearing.location.uri) {
if (containerName) {
return `${containerName} — ${basename(this.bearing.location.uri.fsPath)}`;
}
return this.labelService.getUriLabel(this.bearing.location.uri, { relative: true });
}
return containerName;
}
getIcon(): string {
return symbolKindToCssClass(this.bearing.kind);
}
getResource(): URI {
return this.bearing.location.uri;
}
run(mode: Mode, context: IEntryRunContext): boolean {
// resolve this type bearing if neccessary
if (!this.bearingResolve && typeof this.provider.resolveWorkspaceSymbol === 'function' && !this.bearing.location.range) {
this.bearingResolve = Promise.resolve(this.provider.resolveWorkspaceSymbol(this.bearing, CancellationToken.None)).then(result => {
this.bearing = result || this.bearing;
return this;
}, onUnexpectedError);
}
// open after resolving
TPromise.as(this.bearingResolve).then(() => {
const scheme = this.bearing.location.uri ? this.bearing.location.uri.scheme : void 0;
if (scheme === Schemas.http || scheme === Schemas.https) {
if (mode === Mode.OPEN || mode === Mode.OPEN_IN_BACKGROUND) {
this.openerService.open(this.bearing.location.uri); // support http/https resources (https://github.com/Microsoft/vscode/issues/58924))
}
} else {
super.run(mode, context);
}
});
// hide if OPEN
return mode === Mode.OPEN;
}
getInput(): IResourceInput {
const input: IResourceInput = {
resource: this.bearing.location.uri,
options: {
pinned: !this.configurationService.getValue<IWorkbenchEditorConfiguration>().workbench.editor.enablePreviewFromQuickOpen
}
};
if (this.bearing.location.range) {
input.options.selection = Range.collapseToStart(this.bearing.location.range);
}
return input;
}
static compare(elementA: SymbolEntry, elementB: SymbolEntry, searchValue: string): number {
// Sort by Type if name is identical
const elementAName = elementA.getLabel().toLowerCase();
const elementBName = elementB.getLabel().toLowerCase();
if (elementAName === elementBName) {
let elementAType = symbolKindToCssClass(elementA.bearing.kind);
let elementBType = symbolKindToCssClass(elementB.bearing.kind);
return elementAType.localeCompare(elementBType);
}
return compareEntries(elementA, elementB, searchValue);
}
}
export interface IOpenSymbolOptions {
skipSorting: boolean;
skipLocalSymbols: boolean;
skipDelay: boolean;
}
export class OpenSymbolHandler extends QuickOpenHandler {
static readonly ID = 'workbench.picker.symbols';
private static readonly TYPING_SEARCH_DELAY = 200; // This delay accommodates for the user typing a word and then stops typing to start searching
private delayer: ThrottledDelayer<QuickOpenEntry[]>;
private options: IOpenSymbolOptions;
constructor(@IInstantiationService private instantiationService: IInstantiationService) {
super();
this.delayer = new ThrottledDelayer<QuickOpenEntry[]>(OpenSymbolHandler.TYPING_SEARCH_DELAY);
this.options = Object.create(null);
}
setOptions(options: IOpenSymbolOptions) {
this.options = options;
}
canRun(): boolean | string {
return true;
}
getResults(searchValue: string, token: CancellationToken): TPromise<QuickOpenModel> {
searchValue = searchValue.trim();
let promise: TPromise<QuickOpenEntry[]>;
if (!this.options.skipDelay) {
promise = this.delayer.trigger(() => {
if (token.isCancellationRequested) {
return TPromise.wrap([]);
}
return this.doGetResults(searchValue, token);
});
} else {
promise = this.doGetResults(searchValue, token);
}
return promise.then(e => new QuickOpenModel(e));
}
private doGetResults(searchValue: string, token: CancellationToken): TPromise<SymbolEntry[]> {
return getWorkspaceSymbols(searchValue, token).then(tuples => {
if (token.isCancellationRequested) {
return [];
}
const result: SymbolEntry[] = [];
for (let tuple of tuples) {
const [provider, bearings] = tuple;
this.fillInSymbolEntries(result, provider, bearings, searchValue);
}
// Sort (Standalone only)
if (!this.options.skipSorting) {
searchValue = searchValue ? strings.stripWildcards(searchValue.toLowerCase()) : searchValue;
return result.sort((a, b) => SymbolEntry.compare(a, b, searchValue));
}
return result;
});
}
private fillInSymbolEntries(bucket: SymbolEntry[], provider: IWorkspaceSymbolProvider, types: IWorkspaceSymbol[], searchValue: string): void {
// Convert to Entries
for (let element of types) {
if (this.options.skipLocalSymbols && !!element.containerName) {
continue; // ignore local symbols if we are told so
}
const entry = this.instantiationService.createInstance(SymbolEntry, element, provider);
entry.setHighlights(filters.matchesFuzzy(searchValue, entry.getLabel()));
bucket.push(entry);
}
}
getGroupLabel(): string {
return nls.localize('symbols', "symbol results");
}
getEmptyLabel(searchString: string): string {
if (searchString.length > 0) {
return nls.localize('noSymbolsMatching', "No symbols matching");
}
return nls.localize('noSymbolsWithoutInput', "Type to search for symbols");
}
getAutoFocus(searchValue: string): IAutoFocus {
return {
autoFocusFirstEntry: true,
autoFocusPrefixMatch: searchValue.trim()
};
}
}