From cecfdcde228ae7dfecbecbe985da54a16dcce52e Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Tue, 12 Jun 2018 12:47:15 +0200 Subject: [PATCH] update proposed API to DocumentSymbol-type, #34968 --- .../src/features/documentSymbol.ts | 10 ++--- src/vs/vscode.proposed.d.ts | 42 +++++++++++++++++-- src/vs/workbench/api/node/extHost.api.impl.ts | 7 +--- .../workbench/api/node/extHostApiCommands.ts | 2 +- .../api/node/extHostLanguageFeatures.ts | 32 +++++++------- .../api/node/extHostTypeConverters.ts | 17 ++++---- src/vs/workbench/api/node/extHostTypes.ts | 19 +++++---- 7 files changed, 83 insertions(+), 46 deletions(-) diff --git a/extensions/typescript-language-features/src/features/documentSymbol.ts b/extensions/typescript-language-features/src/features/documentSymbol.ts index 8df78a13761..b07bc9b47a0 100644 --- a/extensions/typescript-language-features/src/features/documentSymbol.ts +++ b/extensions/typescript-language-features/src/features/documentSymbol.ts @@ -51,7 +51,7 @@ class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider // The root represents the file. Ignore this when showing in the UI const tree = response.body; if (tree.childItems) { - const result = new Array(); + const result = new Array(); tree.childItems.forEach(item => TypeScriptDocumentSymbolProvider.convertNavTree(resource.uri, result, item)); return result; } @@ -89,12 +89,12 @@ class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider } } - private static convertNavTree(resource: vscode.Uri, bucket: vscode.SymbolInformation[], item: Proto.NavigationTree): boolean { - const symbolInfo = new vscode.SymbolInformation2( + private static convertNavTree(resource: vscode.Uri, bucket: vscode.DocumentSymbol[], item: Proto.NavigationTree): boolean { + const symbolInfo = new vscode.DocumentSymbol( item.text, getSymbolKind(item.kind), - '', // no container name - typeConverters.Location.fromTextSpan(resource, item.spans[0]), + typeConverters.Range.fromTextSpan(item.spans[0]), + typeConverters.Range.fromTextSpan(item.spans[0]), ); let shouldInclude = TypeScriptDocumentSymbolProvider.shouldInclueEntry(item); diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 7f063470703..0553ae06499 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -377,13 +377,47 @@ declare module 'vscode' { //#region Joh: hierarchical document symbols, https://github.com/Microsoft/vscode/issues/34968 - export class SymbolInformation2 extends SymbolInformation { - definingRange: Range; - children: SymbolInformation2[]; + export class DocumentSymbol { + + /** + * The name of this symbol. + */ + name: string; + + /** + * The kind of this symbol. + */ + kind: SymbolKind; + + /** + * The full range of this symbol not including leading/trailing whitespace but everything else. + */ + fullRange: Range; + + /** + * The range that should be revealed when this symbol is being selected, e.g the name of a function. + * Must be contained by the [`fullRange`](#DocumentSymbol.fullRange). + */ + gotoRange: Range; + + /** + * Children of this symbol, e.g. properties of a class. + */ + children: DocumentSymbol[]; + + /** + * Creates a new document symbol. + * + * @param name The name of the symbol. + * @param kind The kind of the symbol. + * @param fullRange The full range of the symbol. + * @param gotoRange The range that should be reveal. + */ + constructor(name: string, kind: SymbolKind, fullRange: Range, gotoRange: Range); } export interface DocumentSymbolProvider { - provideDocumentSymbols(document: TextDocument, token: CancellationToken): ProviderResult; + provideDocumentSymbols(document: TextDocument, token: CancellationToken): ProviderResult; } //#endregion diff --git a/src/vs/workbench/api/node/extHost.api.impl.ts b/src/vs/workbench/api/node/extHost.api.impl.ts index 52309306e47..afb97a5f1ac 100644 --- a/src/vs/workbench/api/node/extHost.api.impl.ts +++ b/src/vs/workbench/api/node/extHost.api.impl.ts @@ -692,12 +692,7 @@ export function createApiFactory( SourceBreakpoint: extHostTypes.SourceBreakpoint, StatusBarAlignment: extHostTypes.StatusBarAlignment, SymbolInformation: extHostTypes.SymbolInformation, - SymbolInformation2: class extends extHostTypes.SymbolInformation2 { - constructor(name, kind, containerName, location) { - checkProposedApiEnabled(extension); - super(name, kind, containerName, location); - } - }, + DocumentSymbol: extHostTypes.DocumentSymbol, SymbolKind: extHostTypes.SymbolKind, SourceControlInputBoxValidationType: extHostTypes.SourceControlInputBoxValidationType, TextDocumentSaveReason: extHostTypes.TextDocumentSaveReason, diff --git a/src/vs/workbench/api/node/extHostApiCommands.ts b/src/vs/workbench/api/node/extHostApiCommands.ts index 73899b8f066..0a62b6f8031 100644 --- a/src/vs/workbench/api/node/extHostApiCommands.ts +++ b/src/vs/workbench/api/node/extHostApiCommands.ts @@ -403,7 +403,7 @@ export class ExtHostApiCommands { }); } - private _executeDocumentSymbolProvider(resource: URI): Thenable { + private _executeDocumentSymbolProvider(resource: URI): Thenable { const args = { resource }; diff --git a/src/vs/workbench/api/node/extHostLanguageFeatures.ts b/src/vs/workbench/api/node/extHostLanguageFeatures.ts index 08ac7552855..053f23442c2 100644 --- a/src/vs/workbench/api/node/extHostLanguageFeatures.ts +++ b/src/vs/workbench/api/node/extHostLanguageFeatures.ts @@ -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, SymbolInformation, SymbolInformation2 } from 'vs/workbench/api/node/extHostTypes'; +import { Range, Disposable, CompletionList, SnippetString, CodeActionKind, SymbolInformation, DocumentSymbol } 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'; @@ -20,7 +20,7 @@ import { asWinJsPromise } from 'vs/base/common/async'; import { MainContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesShape, ObjectIdentifier, IRawColorInfo, IMainContext, IdObject, ISerializedRegExp, ISerializedIndentationRule, ISerializedOnEnterRule, ISerializedLanguageConfiguration, WorkspaceSymbolDto, SuggestResultDto, WorkspaceSymbolsDto, SuggestionDto, CodeActionDto, ISerializedDocumentFilter } from './extHost.protocol'; import { regExpLeadsToEndlessLoop } from 'vs/base/common/strings'; import { IPosition } from 'vs/editor/common/core/position'; -import { IRange } from 'vs/editor/common/core/range'; +import { IRange, Range as EditorRange } from 'vs/editor/common/core/range'; import { isFalsyOrEmpty } from 'vs/base/common/arrays'; import { isObject } from 'vs/base/common/types'; import { ISelection, Selection } from 'vs/editor/common/core/selection'; @@ -43,15 +43,15 @@ class OutlineAdapter { if (isFalsyOrEmpty(value)) { return undefined; } - let [probe] = value; - if (!(probe instanceof SymbolInformation2)) { - value = OutlineAdapter._asSymbolHierarchy(resource, value); + if (value[0] instanceof DocumentSymbol) { + return (value).map(typeConvert.DocumentSymbol.from); + } else { + return OutlineAdapter._asDocumentSymbolTree(resource, value); } - return (value).map(typeConvert.DocumentSymbol.from); }); } - private static _asSymbolHierarchy(resource: URI, info: SymbolInformation[]): vscode.SymbolInformation2[] { + private static _asDocumentSymbolTree(resource: URI, info: SymbolInformation[]): modes.DocumentSymbol[] { // 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) => { @@ -61,13 +61,17 @@ class OutlineAdapter { } return res; }); - let res: SymbolInformation2[] = []; - let parentStack: SymbolInformation2[] = []; + let res: modes.DocumentSymbol[] = []; + let parentStack: modes.DocumentSymbol[] = []; for (let i = 0; i < info.length; i++) { - let element = new SymbolInformation2(info[i].name, info[i].kind, '', info[i].location); - element.containerName = info[i].containerName; - element.location = info[i].location; // todo@joh make this proper - element.location.uri = element.location.uri || resource; + let element = { + name: info[i].name, + kind: typeConvert.SymbolKind.from(info[i].kind), + containerName: info[i].containerName, + fullRange: typeConvert.Range.from(info[i].location.range), + identifierRange: typeConvert.Range.from(info[i].location.range), + children: [] + }; while (true) { if (parentStack.length === 0) { @@ -76,7 +80,7 @@ class OutlineAdapter { break; } let parent = parentStack[parentStack.length - 1]; - if (parent.definingRange.contains(element.definingRange) && !parent.definingRange.isEqual(element.definingRange)) { + if (EditorRange.containsRange(parent.fullRange, element.fullRange) && !EditorRange.equalsRange(parent.fullRange, element.fullRange)) { parent.children.push(element); parentStack.push(element); break; diff --git a/src/vs/workbench/api/node/extHostTypeConverters.ts b/src/vs/workbench/api/node/extHostTypeConverters.ts index 0a62f953867..e16843c2412 100644 --- a/src/vs/workbench/api/node/extHostTypeConverters.ts +++ b/src/vs/workbench/api/node/extHostTypeConverters.ts @@ -376,25 +376,24 @@ export namespace WorkspaceSymbol { } export namespace DocumentSymbol { - export function from(info: vscode.SymbolInformation2): modes.DocumentSymbol { + export function from(info: vscode.DocumentSymbol): modes.DocumentSymbol { let result: modes.DocumentSymbol = { name: info.name, - fullRange: Range.from(info.definingRange), - identifierRange: Range.from(info.location.range), - kind: SymbolKind.from(info.kind), - containerName: info.containerName + fullRange: Range.from(info.fullRange), + identifierRange: Range.from(info.gotoRange), + kind: SymbolKind.from(info.kind) }; if (info.children) { result.children = info.children.map(from); } return result; } - export function to(info: modes.DocumentSymbol): vscode.SymbolInformation2 { - let result = new types.SymbolInformation2( + export function to(info: modes.DocumentSymbol): vscode.DocumentSymbol { + let result = new types.DocumentSymbol( info.name, SymbolKind.to(info.kind), - info.containerName, - new types.Location(undefined, Range.to(info.identifierRange)) + Range.to(info.fullRange), + Range.to(info.identifierRange), ); if (info.children) { result.children = info.children.map(to) as any; diff --git a/src/vs/workbench/api/node/extHostTypes.ts b/src/vs/workbench/api/node/extHostTypes.ts index 916de5d9fbd..5f4e216580a 100644 --- a/src/vs/workbench/api/node/extHostTypes.ts +++ b/src/vs/workbench/api/node/extHostTypes.ts @@ -881,18 +881,23 @@ export class SymbolInformation { } } -export class SymbolInformation2 extends SymbolInformation { - definingRange: Range; - children: SymbolInformation2[]; - constructor(name: string, kind: SymbolKind, containerName: string, location: Location) { - super(name, kind, containerName, location); +export class DocumentSymbol { + name: string; + kind: SymbolKind; + fullRange: Range; + gotoRange: Range; + children: DocumentSymbol[]; + constructor(name: string, kind: SymbolKind, fullRange: Range, gotoRange: Range) { + this.name = name; + this.kind = kind; + this.fullRange = fullRange; + this.gotoRange = gotoRange; this.children = []; - this.definingRange = location.range; } - } + export enum CodeActionTrigger { Automatic = 1, Manual = 2,