Skip to content

Commit

Permalink
explore Hierarchy<T> idea, #34968
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed May 17, 2018
1 parent 72e6a7d commit 49323b3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { DocumentSymbolProvider, SymbolInformation, SymbolKind, TextDocument, CancellationToken, Uri, HierarchicalSymbolInformation } from 'vscode';
import { DocumentSymbolProvider, SymbolInformation, SymbolKind, TextDocument, CancellationToken, Uri, Hierarchy, SymbolInformation2 } from 'vscode';

import * as Proto from '../protocol';
import * as PConst from '../protocol.const';
Expand Down Expand Up @@ -31,7 +31,7 @@ export default class TypeScriptDocumentSymbolProvider implements DocumentSymbolP
public constructor(
private readonly client: ITypeScriptServiceClient) { }

public async provideDocumentSymbols(resource: TextDocument, token: CancellationToken): Promise<any[]> { // todo@joh `any[]` temporary hack to make typescript happy...
public async provideDocumentSymbols(resource: TextDocument, token: CancellationToken): Promise<any> { // todo@joh `any[]` temporary hack to make typescript happy...
const filepath = this.client.normalizePath(resource.uri);
if (!filepath) {
return [];
Expand All @@ -41,24 +41,27 @@ export default class TypeScriptDocumentSymbolProvider implements DocumentSymbolP
};

try {
const result: SymbolInformation[] | HierarchicalSymbolInformation[] = [];
if (this.client.apiVersion.has206Features()) {
const response = await this.client.execute('navtree', args, token);
if (response.body) {
// The root represents the file. Ignore this when showing in the UI
let tree = response.body;
if (tree.childItems) {
tree.childItems.forEach(item => TypeScriptDocumentSymbolProvider.convertNavTree(resource.uri, result as HierarchicalSymbolInformation[], item));
let result = new Array<Hierarchy<SymbolInformation2>>();
tree.childItems.forEach(item => TypeScriptDocumentSymbolProvider.convertNavTree(resource.uri, result, item));
return result;
}
}
} else {
const response = await this.client.execute('navbar', args, token);
if (response.body) {
let result = new Array<SymbolInformation>();
let foldingMap: ObjectMap<SymbolInformation> = Object.create(null);
response.body.forEach(item => TypeScriptDocumentSymbolProvider.convertNavBar(resource.uri, 0, foldingMap, result as SymbolInformation[], item));
return result;
}
}
return result;
return [];
} catch (e) {
return [];
}
Expand All @@ -82,24 +85,25 @@ export default class TypeScriptDocumentSymbolProvider implements DocumentSymbolP
}
}

private static convertNavTree(resource: Uri, bucket: HierarchicalSymbolInformation[], item: Proto.NavigationTree): void {
const result = new HierarchicalSymbolInformation(
private static convertNavTree(resource: Uri, bucket: Hierarchy<SymbolInformation>[], item: Proto.NavigationTree): void {
if (!TypeScriptDocumentSymbolProvider.shouldInclueEntry(item.text)) {
return;
}
const symbolInfo = new SymbolInformation2(
item.text,
'', // detail, e.g. signature etc
'', // todo@joh detail
outlineTypeTable[item.kind as string] || SymbolKind.Variable,
typeConverters.Range.fromTextSpan(item.spans[0]),
typeConverters.Location.fromTextSpan(resource, item.spans[0]),
typeConverters.Range.fromTextSpan(item.spans[0])
);
const hierarchy = new Hierarchy(symbolInfo);
if (item.childItems && item.childItems.length > 0) {
result.children = [];
for (const child of item.childItems) {
TypeScriptDocumentSymbolProvider.convertNavTree(resource, result.children, child);
TypeScriptDocumentSymbolProvider.convertNavTree(resource, hierarchy.children, child);
}
}
bucket.push(hierarchy);

if (TypeScriptDocumentSymbolProvider.shouldInclueEntry(result.name)) {
bucket.push(result);
}
}

private static shouldInclueEntry(name: string): boolean {
Expand Down
17 changes: 9 additions & 8 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,19 +571,20 @@ declare module 'vscode' {

//#region Joh: hierarchical document symbols, https:/Microsoft/vscode/issues/34968

export class HierarchicalSymbolInformation {
name: string;
kind: SymbolKind;
export class Hierarchy<T> {
parent: T;
children: Hierarchy<T>[];
constructor(element: T);
}

export class SymbolInformation2 extends SymbolInformation {
detail: string;
location: Location;
range: Range;
children: HierarchicalSymbolInformation[];

constructor(name: string, detail: string, kind: SymbolKind, location: Location, range: Range);
constructor(name: string, detail: string, kind: SymbolKind, range: Range, location: Location);
}

export interface DocumentSymbolProvider {
provideDocumentSymbols(document: TextDocument, token: CancellationToken): ProviderResult<HierarchicalSymbolInformation[] | SymbolInformation[]>;
provideDocumentSymbols(document: TextDocument, token: CancellationToken): ProviderResult<SymbolInformation[] | Hierarchy<SymbolInformation>[]>;
}

//#endregion
Expand Down
12 changes: 9 additions & 3 deletions src/vs/workbench/api/node/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,16 @@ export function createApiFactory(
SourceBreakpoint: extHostTypes.SourceBreakpoint,
StatusBarAlignment: extHostTypes.StatusBarAlignment,
SymbolInformation: extHostTypes.SymbolInformation,
HierarchicalSymbolInformation: class extends extHostTypes.HierarchicalSymbolInformation {
constructor(name, detail, kind, keyof, range) {
SymbolInformation2: class extends extHostTypes.SymbolInformation2 {
constructor(name, detail, kind, range, location) {
checkProposedApiEnabled(extension);
super(name, detail, kind, keyof, range);
super(name, detail, kind, range, location);
}
},
Hierarchy: class <T> extends extHostTypes.Hierarchy<T> {
constructor(parent: T) {
checkProposedApiEnabled(extension);
super(parent);
}
},
SymbolKind: extHostTypes.SymbolKind,
Expand Down
18 changes: 9 additions & 9 deletions src/vs/workbench/api/node/extHostLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
import { mixin } from 'vs/base/common/objects';
import * as vscode from 'vscode';
import * as typeConvert from 'vs/workbench/api/node/extHostTypeConverters';
import { Range, Disposable, CompletionList, SnippetString, CodeActionKind, HierarchicalSymbolInformation, SymbolInformation } from 'vs/workbench/api/node/extHostTypes';
import { Range, Disposable, CompletionList, SnippetString, CodeActionKind, SymbolInformation, Hierarchy, SymbolInformation2 } from 'vs/workbench/api/node/extHostTypes';
import { ISingleEditOperation } from 'vs/editor/common/model';
import * as modes from 'vs/editor/common/modes';
import { ExtHostHeapService } from 'vs/workbench/api/node/extHostHeapService';
Expand Down Expand Up @@ -44,14 +44,14 @@ class OutlineAdapter {
return undefined;
}
let [probe] = value;
if (!(probe instanceof HierarchicalSymbolInformation)) {
value = OutlineAdapter._asSymbolTree(<SymbolInformation[]>value);
if (!(probe instanceof Hierarchy)) {
value = OutlineAdapter._asSymbolHierarchy(<SymbolInformation[]>value);
}
return (<HierarchicalSymbolInformation[]>value).map(typeConvert.HierarchicalSymbolInformation.from);
return (<Hierarchy<SymbolInformation2>[]>value).map(typeConvert.HierarchicalSymbolInformation.from);
});
}

private static _asSymbolTree(info: SymbolInformation[]): vscode.HierarchicalSymbolInformation[] {
private static _asSymbolHierarchy(info: SymbolInformation[]): vscode.Hierarchy<SymbolInformation2>[] {
// first sort by start (and end) and then loop over all elements
// and build a tree based on containment.
info = info.slice(0).sort((a, b) => {
Expand All @@ -61,18 +61,18 @@ class OutlineAdapter {
}
return res;
});
let res: HierarchicalSymbolInformation[] = [];
let parentStack: HierarchicalSymbolInformation[] = [];
let res: Hierarchy<SymbolInformation2>[] = [];
let parentStack: Hierarchy<SymbolInformation2>[] = [];
for (let i = 0; i < info.length; i++) {
let element = new HierarchicalSymbolInformation(info[i].name, '', info[i].kind, info[i].location, info[i].location.range);
let element = new Hierarchy(new SymbolInformation2(info[i].name, '', info[i].kind, info[i].location.range, info[i].location));
while (true) {
if (parentStack.length === 0) {
parentStack.push(element);
res.push(element);
break;
}
let parent = parentStack[parentStack.length - 1];
if (parent.range.contains(element.range)) {
if (parent.parent.range.contains(element.parent.range)) {
parent.children.push(element);
parentStack.push(element);
break;
Expand Down
20 changes: 10 additions & 10 deletions src/vs/workbench/api/node/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,27 +364,27 @@ export namespace SymbolInformation {
}

export namespace HierarchicalSymbolInformation {
export function from(info: vscode.HierarchicalSymbolInformation): modes.SymbolInformation {
export function from(info: vscode.Hierarchy<vscode.SymbolInformation2>): modes.SymbolInformation {
let result: modes.SymbolInformation = {
name: info.name,
detail: info.detail,
location: location.from(info.location),
definingRange: Range.from(info.range),
kind: SymbolKind.from(info.kind)
name: info.parent.name,
detail: info.parent.detail,
location: location.from(info.parent.location),
definingRange: Range.from(info.parent.range),
kind: SymbolKind.from(info.parent.kind)
};
if (info.children) {
result.children = info.children.map(from);
}
return result;
}
export function to(info: modes.SymbolInformation): types.HierarchicalSymbolInformation {
let result = new types.HierarchicalSymbolInformation(
export function to(info: modes.SymbolInformation): types.Hierarchy<vscode.SymbolInformation2> {
let result = new types.Hierarchy<vscode.SymbolInformation2>(new types.SymbolInformation2(
info.name,
info.detail,
SymbolKind.to(info.kind),
Range.to(info.definingRange),
location.to(info.location),
Range.to(info.definingRange)
);
));
if (info.children) {
result.children = info.children.map(to);
}
Expand Down
23 changes: 14 additions & 9 deletions src/vs/workbench/api/node/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,19 +876,24 @@ export class SymbolInformation {
}
}

export class HierarchicalSymbolInformation {
name: string;
location: Location;
export class SymbolInformation2 extends SymbolInformation {

detail: string;
kind: SymbolKind;
range: Range;
children: HierarchicalSymbolInformation[];

constructor(name: string, detail: string, kind: SymbolKind, location: Location, range: Range) {
this.name = name;
this.kind = kind;
this.location = location;
constructor(name: string, detail: string, kind: SymbolKind, range: Range, location: Location) {
super(name, kind, undefined, location);
this.detail = detail;
this.range = range;
}
}

export class Hierarchy<T> {
parent: T;
children: Hierarchy<T>[];

constructor(parent: T) {
this.parent = parent;
this.children = [];
}
}
Expand Down

0 comments on commit 49323b3

Please sign in to comment.