Jenny M
Jenny M

Reputation: 1023

vsCode refresh tree when adding new Item

I use the following code to show tree items,

https://github.com/microsoft/vscode-extension-samples/tree/master/tree-view-sample

The items which is shown in the tree is related to a file, if the file changed the number of the tree items should be changed accordingly(using createFileSystemWatcher which works ok), currently I’ve add the code of the file watcher and when debug it stops there (on the getChildren function when I'm changing the file) and I see that I’ve provided the new updated entries (in the code),however the tree doesn’t refreshed with the new data, any idea what am I missing here ? somehow the new updated data is not reflected in the tree. as I new to this topic maybe I miss something. if the code is OK and there is a bug or this is not supported please let me know.

export class TaskTreeDataProvider implements vscode.TreeDataProvider<TreeItem> {

private _onDidChangeTreeData: vscode.EventEmitter<TreeItem | null> = new vscode.EventEmitter<TreeItem | null>();
  readonly onDidChangeTreeData: vscode.Event<TreeItem | null> = this
    ._onDidChangeTreeData.event;

  private eeake: Promise<TreeItem[]> | undefined = undefined;
  private autoRefresh: boolean = true;

  constructor(private context: vscode.ExtensionContext) {
    this.autoRefresh = vscode.workspace
      .getConfiguration(“sView")
      .get("autorefresh");

    let filePath = this.fileName;
    let fileWatcher = vscode.workspace.createFileSystemWatcher(filePath);
    fileWatcher.onDidChange(() => (this.eeake = this.getChildren()), this.refresh());
  }

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

  public async getChildren(task?: TreeItem): Promise<TreeItem[]> {
    let tasks = await vscode.tasks
      .fetchTasks({ type: “run” })
      .then(function (value) {
        return value;
      });

    let entry: TreeItem[] = [];
    if (tasks.length !== 0) {
      for (var i = 0; i < tasks.length; i++) {
        entry[i] = new TreeItem(
          tasks[i].definition.type,
          tasks[i].name,
          {
            command: “sView.executeTask",
            title: "Execute",
            arguments: [tasks[i]]
          }
        );
      }
    }
    return entry;
  }

  getTreeItem(task: TreeItem): vscode.TreeItem {
    return task;
  }
}



class TreeItem extends vscode.TreeItem {
  type: string;
  constructor(
    type: string,
    label: string,
    collapsibleState: vscode.TreeItemCollapsibleState,
    command?: vscode.Command
  ) {
    super(label, collapsibleState);
    this.type = type;
    this.command = command;
    this.iconPath = getIcon();
  }

}

If there is something missing please let me know and I add, I'm really stuck with it.

If there is another way to refresh the tree please let me know

Upvotes: 4

Views: 2074

Answers (1)

hackape
hackape

Reputation: 19987

Last line in constructor looks suspicious to me.

fileWatcher.onDidChange(() => (this.eeake = this.getChildren()), this.refresh());

I believe you mean:

fileWatcher.onDidChange(() => {
  this.eeake = this.getChildren();
  this.refresh();
});

Your original code actually immediately invokes this.refresh() in constructor and pass the return value as the 2nd arg to fileWatcher.onDidChange(). this.refresh() wasn't part of the listener passed as 1st arg to onDidChange().

Upvotes: 1

Related Questions