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);
}
return { setInput };
function getInput(): SymbolTreeInput<unknown> | undefined {
return tree.getInput();
}
return { setInput, getInput };
}

View file

@ -6,9 +6,10 @@
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
@ -16,7 +17,8 @@ import * as vscode from 'vscode';
*
* // instantiate and set input which updates the view
* const myInput: SymbolTreeInput<MyItems> = ...
* api.setInput(myInput)
* api.setInput(myInput);
* const currentInput = api.getInput();
* ```
*/
export interface SymbolTree {
@ -27,6 +29,13 @@ export interface SymbolTree {
* @param input A symbol tree input object
*/
setInput(input: SymbolTreeInput<unknown>): void;
/**
* Get the contents of the references viewlet.
*
* @returns The current symbol tree input object
*/
getInput(): SymbolTreeInput<unknown> | undefined;
}
/**

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;
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)
);
}