Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh treeview on underlying data change

I have written a very simple TreeView Extension for Visual Studio Code that displays a basic list of VSCode Tasks and executes the task on selection.

I would like the treeview to auto update when new tasks are added (via the tasks.json file, or autodetected tasks from things like the package.json files scripts) but can't figure out how to do so.

In my activate method I register my TreeDataProvider and execution command:

    vscode.window.registerTreeDataProvider('vscodeTasks', treeProvider);
    let disposable = vscode.commands.registerCommand('task.runTask', task => {
      vscode.tasks.executeTask(task);
    });
    vscode.context.subscriptions.push(disposable);

And in my implementation of the TreeDataProvider in the getChildren function I fetch the tasks using:

vscode.tasks.fetchTasks()

And turn each into a TreeItem

Any help with the auto refresh would be appreciated.

like image 650
Dave Hart Avatar asked Sep 20 '18 09:09

Dave Hart


1 Answers

I was able to make my treeview reload by adding the following to the implementation

private _onDidChangeTreeData: EventEmitter<
    myItem | undefined
  > = new EventEmitter<myItem | undefined>();

  readonly onDidChangeTreeData: Event<myItem | undefined> = this
    ._onDidChangeTreeData.event;

  refresh(): void {
    this._onDidChangeTreeData.fire(undefined);
  }

And then registering this as a command

commands.registerCommand("******.refresh", () =>
          treeDataProvider.refresh()
        );

By doing this, you can reload your treeview on using the command refresh I followed the following documents - https://code.visualstudio.com/api/extension-guides/tree-view

Now all you have to do is trigger this command when there is a change in your data, which can be done in different ways

Hope it helps! Happy Coding.

like image 107
Shardul Saiya Avatar answered Oct 20 '22 04:10

Shardul Saiya