Better later than never :)
Implement Updateable, Interactive vscode.TreeView
- Contribute
TreeView
Update Command
- Contribute
View
Object
- Contribute
View
Update Button
- Create & Register the
TreeView
class
- Activate
Contribute TreeView
Update Command
Add command
contribution to package.json
{
"contributes": {
"commands": [
{
"command": "Update-TreeView",
"category": "Custom",
"title": "Actively updates the tree view",
"icon": "$(extensions-refresh)"
}
]
}
}
Contribute View
Object
Add view
contribution to package.json
{
"contributes": {
"views": {
"explorer": [
{
"id": "customView",
"name": "Custom View"
}
]
}
}
}
Contribute View
Update Button
Add button contribution for customView
to package.json
. Please note that we assigned the update command we have contribute earlier.
{
"contributes": {
"menus": {
"view/title": [
{
"command": "Update-TreeView",
"when": "view == customView",
"group": "navigation"
}
]
}
}
}
Create & Register the TreeView
class
Code Breakdown
refresh
- fires when the refresh
button is clicked on the view. We achieved that by registering the update command and fire the update event when invoking it.
getChildren
- this is the logic by which you will build your tree.
register
- a wrapper method for registering the tree provider and the update command.
withProgress
- will show a progress bar while your tree is loaded/updated.
TreeItem
- a class for creating the tree items.
Important!
Please check the events under register
method. They will fire when you perform actions on your tree. In order to get the TreeItem(s)
on which the action was preformed, call e.selection
or e.element
.
Create a new file custom-tree.ts
and paste the following code inside
import * as vscode from 'vscode';
export class CustomTreeDataProvider implements vscode.TreeDataProvider<TreeItem> {
private _onDidChangeTreeData: vscode.EventEmitter<TreeItem | undefined | null | void> = new vscode.EventEmitter<TreeItem | undefined | null | void>();
readonly onDidChangeTreeData: vscode.Event<TreeItem | undefined | null | void> = this._onDidChangeTreeData.event;
refresh(): void {
this._onDidChangeTreeData.fire();
}
getTreeItem(element: TreeItem): vscode.TreeItem | Thenable<vscode.TreeItem> {
return element;
}
getChildren(element?: TreeItem | undefined): vscode.ProviderResult<TreeItem[]> {
return vscode.window.withProgress({ location: { viewId: "customView" } }, () => {
return new Promise<TreeItem[]>((resolve) => {
let file = new TreeItem('file.txt', vscode.ThemeIcon.File, undefined);
let folder = new TreeItem('Parent', vscode.ThemeIcon.Folder, [file]);
resolve([folder]);
});
});
}
public register(context: vscode.ExtensionContext): any {
// setup
const options = {
treeDataProvider: this,
showCollapseAll: true
};
// build
vscode.window.registerTreeDataProvider('customView', this);
vscode.commands.registerCommand('Update-TreeView', () => {
this.refresh();
});
// create
const tree = vscode.window.createTreeView('customView', options);
// setup: events
tree.onDidChangeSelection(e => {
console.log(e); // breakpoint here for debug
});
tree.onDidCollapseElement(e => {
console.log(e); // breakpoint here for debug
});
tree.onDidChangeVisibility(e => {
console.log(e); // breakpoint here for debug
});
tree.onDidExpandElement(e => {
console.log(e); // breakpoint here for debug
});
// subscribe
context.subscriptions.push(tree);
}
}
class TreeItem extends vscode.TreeItem {
children: TreeItem[] | undefined;
command?: vscode.Command | undefined;
constructor(
label: string,
iconPath?: string | vscode.Uri | { light: string | vscode.Uri; dark: string | vscode.Uri } | vscode.ThemeIcon,
children?: TreeItem[] | undefined,
command?: vscode.Command) {
super(
label,
children === undefined
? vscode.TreeItemCollapsibleState.None
: vscode.TreeItemCollapsibleState.Collapsed);
this.command = command;
this.iconPath = iconPath;
this.children = children;
}
}
Activate
Add the following code to your activate
method under extension.ts
.
import * as vscode from 'vscode';
import { CustomTreeDataProvider } from './custom-tree';
export function activate(context: vscode.ExtensionContext) {
new CustomTreeDataProvider().register(context);
}