Skip to content

Commit

Permalink
Merge pull request #152008 from CsCherrYY/cs-reference-view-api
Browse files Browse the repository at this point in the history
Support switching to/from custom views in reference-view API
  • Loading branch information
jrieken committed Aug 19, 2022
2 parents 53e89be + 11b6d00 commit a3f5d24
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
6 changes: 5 additions & 1 deletion extensions/references-view/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
17 changes: 13 additions & 4 deletions extensions/references-view/src/references-view.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
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<SymbolTree>('vscode.references-view').activate();
*
* // 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 {
Expand All @@ -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;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions extensions/references-view/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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)
);
}
Expand Down

0 comments on commit a3f5d24

Please sign in to comment.