我使用以下代码来显示树项,
https://github.com/microsoft/vscode-extension-samples/tree/master/tree-view-sample
树中显示的项目与文件相关,如果文件更改,则应相应更改树项目的数量(使用 createFileSystemWatcher
工作正常),目前我已经添加了文件观察器的代码和当调试时它停止在那里(当我更改文件时在 getChildren
函数上)并且我看到我已经提供了 新更新 条目(在代码中),但是树 没有 用新数据刷新,知道我在这里遗漏了什么吗?不知何故,新更新的数据没有反射(reflect)在树中。当我刚接触这个话题时,我可能会错过一些东西。如果代码正常并且存在错误或不受支持,请告诉我。
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();
}
}
如果有什么遗漏,请告诉我,我补充说,我真的坚持下去了。
如果有另一种刷新树的方法,请告诉我
最佳答案
构造函数中的最后一行对我来说看起来很可疑。
fileWatcher.onDidChange(() => (this.eeake = this.getChildren()), this.refresh());
我相信你的意思是:
fileWatcher.onDidChange(() => {
this.eeake = this.getChildren();
this.refresh();
});
您的原始代码实际上会立即调用
this.refresh()
在构造函数中并将返回值作为第二个参数传递给 fileWatcher.onDidChange()
. this.refresh()
不是作为第一个参数传递给 onDidChange()
的监听器的一部分.
关于javascript - 添加新项目时 vsCode 刷新树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60849968/