0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TreeDataProviderのgetChildren()が呼び出されるタイミング

0
Last updated at Posted at 2023-03-28

VSCodeの拡張機能を開発していて、エクスプローラーにツリービューを表示する時、必ずvscode.TreeDataProviderインターフェースを実装してクラスを作成します。

この時、vscode.ProviderResult<T[]>を返すgetChildren()を実装することで、ツリービューに表示する要素を作成することができます。

しかし、このgetChildren()はどのタイミングで呼び出されているのでしょうか?

getChildren()が呼び出されるタイミングは次の通りとなっています。

  • ツリービューが最初に表示される時
  • ツリービュー内で項目が展開される時
  • onDidChangeTreeDataイベントがトリガーされる時

では、これらが正しく動作するためにはどのようにすれば良いのでしょうか?
それは、vscode.window.createTreeView()を呼び出す際に、treeDataProviderの値として、このクラスのインスタンスを表すthisをしていすることで、決まったタイミングでgetChildren()が呼び出されるようになっています。

vscode.TreeDataProviderを実装してクラスを作成するときは、コンストラクタの中で、必ずvscode.window.createTreeView()を呼び出すようにしましょう。

constructor(
    context: vscode.ExtensionContext,
) {
    const view = vscode.window.createTreeView("snippetsView", {
      treeDataProvider: this,
      showCollapseAll: true,
      canSelectMany: false,
      dragAndDropController: this,
    });
    context.subscriptions.push(view);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?