Clean up provideDocumentSymbols

- Returned undefined instead of empty array
- Only execute server call in try catch
This commit is contained in:
Matt Bierner
2018-07-16 17:45:45 -07:00
parent 4c003dbbc1
commit 6c2818d42e

View File

@@ -34,30 +34,30 @@ class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider
private readonly client: ITypeScriptServiceClient private readonly client: ITypeScriptServiceClient
) { } ) { }
public async provideDocumentSymbols(resource: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.DocumentSymbol[] | vscode.SymbolInformation[]> { public async provideDocumentSymbols(resource: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.DocumentSymbol[] | undefined> {
const filepath = this.client.toPath(resource.uri); const file = this.client.toPath(resource.uri);
if (!filepath) { if (!file) {
return []; return undefined;
} }
const args: Proto.FileRequestArgs = {
file: filepath
};
let tree: Proto.NavigationTree | undefined;
try { try {
const args: Proto.FileRequestArgs = { file };
const response = await this.client.execute('navtree', args, token); const response = await this.client.execute('navtree', args, token);
if (response.body) { tree = response.body;
// The root represents the file. Ignore this when showing in the UI } catch {
const tree = response.body; return undefined;
if (tree.childItems) {
const result = new Array<vscode.DocumentSymbol>();
tree.childItems.forEach(item => TypeScriptDocumentSymbolProvider.convertNavTree(resource.uri, result, item));
return result;
}
}
return [];
} catch (e) {
return [];
} }
if (tree && tree.childItems) {
// The root represents the file. Ignore this when showing in the UI
const result: vscode.DocumentSymbol[] = [];
tree.childItems.forEach(item => TypeScriptDocumentSymbolProvider.convertNavTree(resource.uri, result, item));
return result;
}
return undefined;
} }
private static convertNavTree(resource: vscode.Uri, bucket: vscode.DocumentSymbol[], item: Proto.NavigationTree): boolean { private static convertNavTree(resource: vscode.Uri, bucket: vscode.DocumentSymbol[], item: Proto.NavigationTree): boolean {