Skip to content

Commit 893a783

Browse files
committed
commands: executeDocumentSymbolProvider
1 parent 6708450 commit 893a783

5 files changed

Lines changed: 160 additions & 54 deletions

File tree

src/vs/editor/contrib/quickOpen/common/quickOpen.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55

66
'use strict';
77

8-
import {onUnexpectedError} from 'vs/base/common/errors';
8+
import URI from 'vs/base/common/uri';
9+
import {onUnexpectedError, illegalArgument} from 'vs/base/common/errors';
910
import {TPromise} from 'vs/base/common/winjs.base';
1011
import {Range} from 'vs/editor/common/core/range';
1112
import {IModel} from 'vs/editor/common/editorCommon';
1213
import {IOutlineEntry, IOutlineSupport} from 'vs/editor/common/modes';
1314
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
15+
import {IModelService} from 'vs/editor/common/services/modelService';
16+
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
1417

1518
const OutlineRegistry = new LanguageFeatureRegistry<IOutlineSupport>('outlineSupport');
1619

@@ -20,7 +23,12 @@ export {
2023
IOutlineSupport
2124
}
2225

23-
export function getOutlineEntries(model: IModel): TPromise<{ entries: IOutlineEntry[], outlineGroupLabel: { [n: string]: string;} }> {
26+
export interface IOutline {
27+
entries: IOutlineEntry[];
28+
outlineGroupLabel: { [n: string]: string; };
29+
}
30+
31+
export function getOutlineEntries(model: IModel): TPromise<IOutline> {
2432

2533
let groupLabels: { [n: string]: string } = Object.create(null);
2634
let entries: IOutlineEntry[] = [];
@@ -74,3 +82,16 @@ function flatten(bucket: IOutlineEntry[], entries: IOutlineEntry[], overrideCont
7482
}
7583
}
7684
}
85+
86+
87+
CommonEditorRegistry.registerLanguageCommand('_executeDocumentSymbolProvider', function(accessor, args) {
88+
const {resource} = args;
89+
if (!URI.isURI(resource)) {
90+
throw illegalArgument('resource');
91+
}
92+
const model = accessor.get(IModelService).getModel(resource);
93+
if (!model) {
94+
throw illegalArgument('resource');
95+
}
96+
return getOutlineEntries(model);
97+
});

src/vs/workbench/api/common/extHostLanguageFeatureCommands.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {ExtraInfoRegistry} from 'vs/editor/contrib/hover/common/hover';
2929
import {OccurrencesRegistry} from 'vs/editor/contrib/wordHighlighter/common/wordHighlighter';
3030
import {ReferenceRegistry} from 'vs/editor/contrib/referenceSearch/common/referenceSearch';
3131
import {QuickFixRegistry} from 'vs/editor/contrib/quickFix/common/quickFix';
32-
import {OutlineRegistry, IOutlineEntry, IOutlineSupport} from 'vs/editor/contrib/quickOpen/common/quickOpen';
32+
import {IOutline} from 'vs/editor/contrib/quickOpen/common/quickOpen';
3333
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
3434
import {NavigateTypesSupportRegistry, INavigateTypesSupport, ITypeBearing} from 'vs/workbench/parts/search/common/search'
3535
import {RenameRegistry} from 'vs/editor/contrib/rename/common/rename';
@@ -45,10 +45,10 @@ import {SuggestRegistry} from 'vs/editor/contrib/suggest/common/suggest';
4545
// vscode.executeReferenceProvider
4646
// vscode.executeDocumentRenameProvider
4747
// vscode.executeSignatureHelpProvider
48+
// vscode.executeDocumentSymbolProvider
4849

4950
// vscode.executeCodeActionProvider
5051
// vscode.executeCodeLensProvider
51-
// vscode.executeDocumentSymbolProvider
5252
// vscode.executeFormatDocumentProvider
5353
// vscode.executeFormatRangeProvider
5454
// vscode.executeFormatOnTypeProvider
@@ -69,6 +69,7 @@ export class ExtHostLanguageFeatureCommands {
6969
this._register('vscode.executeReferenceProvider', this._executeReferenceProvider);
7070
this._register('vscode.executeDocumentRenameProvider', this._executeDocumentRenameProvider);
7171
this._register('vscode.executeSignatureHelpProvider', this._executeSignatureHelpProvider);
72+
this._register('vscode.executeDocumentSymbolProvider', this._executeDocumentSymbolProvider);
7273
}
7374

7475
private _register(id: string, callback: (...args: any[]) => any): void {
@@ -166,4 +167,15 @@ export class ExtHostLanguageFeatureCommands {
166167
}
167168
});
168169
}
170+
171+
private _executeDocumentSymbolProvider(resource: URI): Thenable<types.SymbolInformation[]> {
172+
const args = {
173+
resource
174+
};
175+
return this._commands.executeCommand<IOutline>('_executeDocumentSymbolProvider', args).then(value => {
176+
if (value && Array.isArray(value.entries)) {
177+
return value.entries.map(typeConverters.SymbolInformation.fromOutlineEntry);
178+
}
179+
});
180+
}
169181
}

src/vs/workbench/api/common/extHostLanguageFeatures.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,10 @@ class OutlineAdapter implements IOutlineSupport {
7272
let doc = this._documents.getDocument(resource);
7373
return asWinJsPromise(token => this._provider.provideDocumentSymbols(doc, token)).then(value => {
7474
if (Array.isArray(value)) {
75-
return value.map(OutlineAdapter._convertSymbolInfo);
75+
return value.map(TypeConverters.SymbolInformation.toOutlineEntry);
7676
}
7777
});
7878
}
79-
80-
private static _convertSymbolInfo(symbol: vscode.SymbolInformation): IOutlineEntry {
81-
return <IOutlineEntry>{
82-
type: TypeConverters.fromSymbolKind(symbol.kind),
83-
range: TypeConverters.fromRange(symbol.location.range),
84-
containerLabel: symbol.containerName,
85-
label: symbol.name,
86-
icon: undefined,
87-
};
88-
}
8979
}
9080

9181
class CodeLensAdapter implements modes.ICodeLensSupport {

src/vs/workbench/api/common/pluginHostTypeConverters.ts

Lines changed: 97 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,43 +68,6 @@ export function fromPosition(position: types.Position):IPosition {
6868
return { lineNumber: position.line + 1, column: position.character + 1};
6969
}
7070

71-
export function fromSymbolKind(kind: number | types.SymbolKind): string {
72-
switch (kind) {
73-
case types.SymbolKind.Method:
74-
return 'method';
75-
case types.SymbolKind.Function:
76-
return 'function';
77-
case types.SymbolKind.Constructor:
78-
return 'constructor';
79-
case types.SymbolKind.Variable:
80-
return 'variable';
81-
case types.SymbolKind.Class:
82-
return 'class';
83-
case types.SymbolKind.Interface:
84-
return 'interface';
85-
case types.SymbolKind.Module:
86-
case types.SymbolKind.Namespace:
87-
case types.SymbolKind.Package:
88-
return 'module';
89-
case types.SymbolKind.Property:
90-
return 'property';
91-
case types.SymbolKind.Enum:
92-
return 'enum';
93-
case types.SymbolKind.String:
94-
return 'string';
95-
case types.SymbolKind.File:
96-
return 'file';
97-
case types.SymbolKind.Array:
98-
return 'array';
99-
case types.SymbolKind.Number:
100-
return 'number';
101-
case types.SymbolKind.Boolean:
102-
return 'boolean';
103-
}
104-
105-
return 'property';
106-
}
107-
10871
export function fromDiagnosticSeverity(value: number): Severity {
10972
switch (value) {
11073
case types.DiagnosticSeverity.Error:
@@ -211,6 +174,103 @@ export function fromTextEdit(edit: vscode.TextEdit) {
211174
}
212175
}
213176

177+
export namespace SymbolKind {
178+
179+
export function from(kind: number | types.SymbolKind): string {
180+
switch (kind) {
181+
case types.SymbolKind.Method:
182+
return 'method';
183+
case types.SymbolKind.Function:
184+
return 'function';
185+
case types.SymbolKind.Constructor:
186+
return 'constructor';
187+
case types.SymbolKind.Variable:
188+
return 'variable';
189+
case types.SymbolKind.Class:
190+
return 'class';
191+
case types.SymbolKind.Interface:
192+
return 'interface';
193+
case types.SymbolKind.Module:
194+
case types.SymbolKind.Namespace:
195+
case types.SymbolKind.Package:
196+
return 'module';
197+
case types.SymbolKind.Property:
198+
return 'property';
199+
case types.SymbolKind.Enum:
200+
return 'enum';
201+
case types.SymbolKind.String:
202+
return 'string';
203+
case types.SymbolKind.File:
204+
return 'file';
205+
case types.SymbolKind.Array:
206+
return 'array';
207+
case types.SymbolKind.Number:
208+
return 'number';
209+
case types.SymbolKind.Boolean:
210+
return 'boolean';
211+
}
212+
return 'property';
213+
}
214+
215+
export function to(type: string): types.SymbolKind {
216+
switch (type) {
217+
case 'method':
218+
return types.SymbolKind.Method;
219+
case 'function':
220+
return types.SymbolKind.Function;
221+
case 'constructor':
222+
return types.SymbolKind.Constructor;
223+
case 'variable':
224+
return types.SymbolKind.Variable;
225+
case 'class':
226+
return types.SymbolKind.Class;
227+
case 'interface':
228+
return types.SymbolKind.Interface;
229+
case 'module':
230+
// case types.SymbolKind.Namespace:
231+
// case types.SymbolKind.Package:
232+
return types.SymbolKind.Module;
233+
case 'property':
234+
return types.SymbolKind.Property;
235+
case 'enum':
236+
return types.SymbolKind.Enum;
237+
case 'string':
238+
return types.SymbolKind.String;
239+
case 'file':
240+
return types.SymbolKind.File;
241+
case 'array':
242+
return types.SymbolKind.Array;
243+
case 'number':
244+
return types.SymbolKind.Number;
245+
case 'boolean':
246+
return types.SymbolKind.Boolean;
247+
}
248+
return types.SymbolKind.Property
249+
}
250+
}
251+
252+
export namespace SymbolInformation {
253+
254+
export function fromOutlineEntry(entry: modes.IOutlineEntry): types.SymbolInformation {
255+
return new types.SymbolInformation(entry.label,
256+
SymbolKind.to(entry.type),
257+
toRange(entry.range),
258+
undefined,
259+
entry.containerLabel)
260+
}
261+
262+
export function toOutlineEntry(symbol: vscode.SymbolInformation): modes.IOutlineEntry {
263+
return <modes.IOutlineEntry>{
264+
type: SymbolKind.from(symbol.kind),
265+
range: fromRange(symbol.location.range),
266+
containerLabel: symbol.containerName,
267+
label: symbol.name,
268+
icon: undefined,
269+
};
270+
}
271+
272+
}
273+
214274
export function fromSymbolInformation(info: vscode.SymbolInformation): ITypeBearing {
215275
return <ITypeBearing>{
216276
name: info.name,

src/vs/workbench/test/common/api/extHostLanguageFeatureCommands.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ suite('ExtHostLanguageFeatureCommands', function() {
188188
test('Definition, ⇔ back and forth', function(done) {
189189

190190
disposables.push(extHost.registerDefinitionProvider(defaultSelector, <vscode.DefinitionProvider>{
191-
provideDefinition(doc:any): any {
191+
provideDefinition(doc: any): any {
192192
return new types.Location(doc.uri, new types.Range(0, 0, 0, 0));
193193
}
194194
}));
@@ -208,5 +208,28 @@ suite('ExtHostLanguageFeatureCommands', function() {
208208
done();
209209
});
210210
});
211-
})
211+
});
212+
213+
// --- outline
214+
215+
test('Outline, back and forth', function(done) {
216+
disposables.push(extHost.registerDocumentSymbolProvider(defaultSelector, <vscode.DocumentSymbolProvider>{
217+
provideDocumentSymbols(): any {
218+
return [
219+
new types.SymbolInformation('testing1', types.SymbolKind.Enum, new types.Range(1, 0, 1, 0)),
220+
new types.SymbolInformation('testing2', types.SymbolKind.Enum, new types.Range(0, 1, 0, 3)),
221+
]
222+
}
223+
}));
224+
225+
threadService.sync().then(() => {
226+
commands.executeCommand<vscode.SymbolInformation[]>('vscode.executeDocumentSymbolProvider', model.getAssociatedResource()).then(values => {
227+
assert.equal(values.length, 2);
228+
let [first, second] = values;
229+
assert.equal(first.name, 'testing2');
230+
assert.equal(second.name, 'testing1');
231+
done();
232+
});
233+
});
234+
});
212235
});

0 commit comments

Comments
 (0)