From 90e6cb4a4005789ec3d8c8492ef05cc56a040ab5 Mon Sep 17 00:00:00 2001 From: Shi Chen Date: Tue, 14 Jun 2022 10:03:55 +0800 Subject: [PATCH] support more external use in API Signed-off-by: Shi Chen --- extensions/references-view/src/extension.ts | 6 +++- .../references-view/src/references-view.d.ts | 35 ++++++++++++------- extensions/references-view/src/types/index.ts | 8 +++-- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/extensions/references-view/src/extension.ts b/extensions/references-view/src/extension.ts index 94c258cd73c..4cd47866fe2 100644 --- a/extensions/references-view/src/extension.ts +++ b/extensions/references-view/src/extension.ts @@ -22,5 +22,9 @@ export function activate(context: vscode.ExtensionContext): SymbolTree { tree.setInput(input); } - return { setInput }; + function getInput(): SymbolTreeInput | undefined { + return tree.getInput(); + } + + return { setInput, getInput }; } diff --git a/extensions/references-view/src/references-view.d.ts b/extensions/references-view/src/references-view.d.ts index 70fd932a19f..a2f2cb72049 100644 --- a/extensions/references-view/src/references-view.d.ts +++ b/extensions/references-view/src/references-view.d.ts @@ -6,32 +6,41 @@ import * as vscode from 'vscode'; /** - * This interface describes the shape for the references viewlet API. It consists - * of a single `setInput` function which must be called with a full implementation - * of the `SymbolTreeInput`-interface. To acquire this API use the default mechanics, e.g: - * + * This interface describes the shape for the references viewlet API. It includes + * a single `setInput` function which must be called with a full implementation + * of the `SymbolTreeInput`-interface. You can also use `getInput` function to + * get the current `SymbolTreeInput`. To acquire this API use the default mechanics, e.g: + * * ```ts * // get references viewlet API * const api = await vscode.extensions.getExtension('ms-vscode.references-view').activate(); - * + * * // instantiate and set input which updates the view * const myInput: SymbolTreeInput = ... - * api.setInput(myInput) + * api.setInput(myInput); + * const currentInput = api.getInput(); * ``` */ export interface SymbolTree { /** - * Set the contents of the references viewlet. - * + * Set the contents of the references viewlet. + * * @param input A symbol tree input object */ setInput(input: SymbolTreeInput): void; + + /** + * Get the contents of the references viewlet. + * + * @returns The current symbol tree input object + */ + getInput(): SymbolTreeInput | undefined; } /** * A symbol tree input is the entry point for populating the references viewlet. - * Inputs must be anchored at a code location, they must have a title, and they + * Inputs must be anchored at a code location, they must have a title, and they * must resolve to a model. */ export interface SymbolTreeInput { @@ -54,17 +63,17 @@ export interface SymbolTreeInput { readonly location: vscode.Location; /** - * Resolve this input to a model that contains the actual data. When there are no result + * Resolve this input to a model that contains the actual data. When there are no result * than `undefined` or `null` should be returned. */ resolve(): vscode.ProviderResult>; /** * This function is called when re-running from history. The symbols tree has tracked - * the original location of this input and that is now passed to this input. The + * the original location of this input and that is now passed to this input. The * implementation of this function should return a clone where the `location`-property * uses the provided `location` - * + * * @param location The location at which the new input should be anchored. * @returns A new input which location is anchored at the position. */ @@ -94,7 +103,7 @@ export interface SymbolTreeModel { navigation?: SymbolItemNavigation; /** - * Optional support for editor highlights. WHen implemented, the editor will highlight + * Optional support for editor highlights. WHen implemented, the editor will highlight * symbol ranges in the source code. */ highlights?: SymbolItemEditorHighlights; diff --git a/extensions/references-view/src/types/index.ts b/extensions/references-view/src/types/index.ts index ed2e4b872f6..5d1fc2f3318 100644 --- a/extensions/references-view/src/types/index.ts +++ b/extensions/references-view/src/types/index.ts @@ -19,13 +19,15 @@ export function register(tree: SymbolsTree, context: vscode.ExtensionContext): v } } - function setTypeHierarchyDirection(value: TypeHierarchyDirection, anchor: TypeItem | unknown) { + function setTypeHierarchyDirection(value: TypeHierarchyDirection, anchor: TypeItem | vscode.Location | unknown) { direction.value = value; let newInput: TypesTreeInput | undefined; const oldInput = tree.getInput(); if (anchor instanceof TypeItem) { newInput = new TypesTreeInput(new vscode.Location(anchor.item.uri, anchor.item.selectionRange.start), direction.value); + } else if (anchor instanceof vscode.Location) { + newInput = new TypesTreeInput(anchor, direction.value); } else if (oldInput instanceof TypesTreeInput) { newInput = new TypesTreeInput(oldInput.location, direction.value); } @@ -36,8 +38,8 @@ export function register(tree: SymbolsTree, context: vscode.ExtensionContext): v context.subscriptions.push( vscode.commands.registerCommand('references-view.showTypeHierarchy', showTypeHierarchy), - vscode.commands.registerCommand('references-view.showSupertypes', (item: TypeItem | unknown) => setTypeHierarchyDirection(TypeHierarchyDirection.Supertypes, item)), - vscode.commands.registerCommand('references-view.showSubtypes', (item: TypeItem | unknown) => setTypeHierarchyDirection(TypeHierarchyDirection.Subtypes, item)), + vscode.commands.registerCommand('references-view.showSupertypes', (item: TypeItem | vscode.Location | unknown) => setTypeHierarchyDirection(TypeHierarchyDirection.Supertypes, item)), + vscode.commands.registerCommand('references-view.showSubtypes', (item: TypeItem | vscode.Location | unknown) => setTypeHierarchyDirection(TypeHierarchyDirection.Subtypes, item)), vscode.commands.registerCommand('references-view.removeTypeItem', removeTypeItem) ); }