support more external use in API

Signed-off-by: Shi Chen <chenshi@microsoft.com>
This commit is contained in:
Shi Chen 2022-06-14 10:03:55 +08:00
parent 33b1980f6f
commit 90e6cb4a40
3 changed files with 32 additions and 17 deletions

View file

@ -22,5 +22,9 @@ export function activate(context: vscode.ExtensionContext): SymbolTree {
tree.setInput(input); tree.setInput(input);
} }
return { setInput }; function getInput(): SymbolTreeInput<unknown> | undefined {
return tree.getInput();
}
return { setInput, getInput };
} }

View file

@ -6,32 +6,41 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
/** /**
* This interface describes the shape for the references viewlet API. It consists * This interface describes the shape for the references viewlet API. It includes
* of a single `setInput` function which must be called with a full implementation * 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: * 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 * ```ts
* // get references viewlet API * // get references viewlet API
* const api = await vscode.extensions.getExtension<SymbolTree>('ms-vscode.references-view').activate(); * const api = await vscode.extensions.getExtension<SymbolTree>('ms-vscode.references-view').activate();
* *
* // instantiate and set input which updates the view * // instantiate and set input which updates the view
* const myInput: SymbolTreeInput<MyItems> = ... * const myInput: SymbolTreeInput<MyItems> = ...
* api.setInput(myInput) * api.setInput(myInput);
* const currentInput = api.getInput();
* ``` * ```
*/ */
export interface SymbolTree { export interface SymbolTree {
/** /**
* Set the contents of the references viewlet. * Set the contents of the references viewlet.
* *
* @param input A symbol tree input object * @param input A symbol tree input object
*/ */
setInput(input: SymbolTreeInput<unknown>): void; setInput(input: SymbolTreeInput<unknown>): void;
/**
* Get the contents of the references viewlet.
*
* @returns The current symbol tree input object
*/
getInput(): SymbolTreeInput<unknown> | undefined;
} }
/** /**
* A symbol tree input is the entry point for populating the references viewlet. * 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. * must resolve to a model.
*/ */
export interface SymbolTreeInput<T> { export interface SymbolTreeInput<T> {
@ -54,17 +63,17 @@ export interface SymbolTreeInput<T> {
readonly location: vscode.Location; 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. * than `undefined` or `null` should be returned.
*/ */
resolve(): vscode.ProviderResult<SymbolTreeModel<T>>; resolve(): vscode.ProviderResult<SymbolTreeModel<T>>;
/** /**
* This function is called when re-running from history. The symbols tree has tracked * 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 * implementation of this function should return a clone where the `location`-property
* uses the provided `location` * uses the provided `location`
* *
* @param location The location at which the new input should be anchored. * @param location The location at which the new input should be anchored.
* @returns A new input which location is anchored at the position. * @returns A new input which location is anchored at the position.
*/ */
@ -94,7 +103,7 @@ export interface SymbolTreeModel<T> {
navigation?: SymbolItemNavigation<T>; navigation?: SymbolItemNavigation<T>;
/** /**
* 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. * symbol ranges in the source code.
*/ */
highlights?: SymbolItemEditorHighlights<T>; highlights?: SymbolItemEditorHighlights<T>;

View file

@ -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; direction.value = value;
let newInput: TypesTreeInput | undefined; let newInput: TypesTreeInput | undefined;
const oldInput = tree.getInput(); const oldInput = tree.getInput();
if (anchor instanceof TypeItem) { if (anchor instanceof TypeItem) {
newInput = new TypesTreeInput(new vscode.Location(anchor.item.uri, anchor.item.selectionRange.start), direction.value); 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) { } else if (oldInput instanceof TypesTreeInput) {
newInput = new TypesTreeInput(oldInput.location, direction.value); newInput = new TypesTreeInput(oldInput.location, direction.value);
} }
@ -36,8 +38,8 @@ export function register(tree: SymbolsTree, context: vscode.ExtensionContext): v
context.subscriptions.push( context.subscriptions.push(
vscode.commands.registerCommand('references-view.showTypeHierarchy', showTypeHierarchy), 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.showSupertypes', (item: TypeItem | vscode.Location | unknown) => setTypeHierarchyDirection(TypeHierarchyDirection.Supertypes, item)),
vscode.commands.registerCommand('references-view.showSubtypes', (item: TypeItem | unknown) => setTypeHierarchyDirection(TypeHierarchyDirection.Subtypes, item)), vscode.commands.registerCommand('references-view.showSubtypes', (item: TypeItem | vscode.Location | unknown) => setTypeHierarchyDirection(TypeHierarchyDirection.Subtypes, item)),
vscode.commands.registerCommand('references-view.removeTypeItem', removeTypeItem) vscode.commands.registerCommand('references-view.removeTypeItem', removeTypeItem)
); );
} }