单击TreeViewItem项上的命令,VSCode扩展

时间:2019-07-02 20:51:45

标签: visual-studio-code treeview vscode-extensions treeviewitem

上下文:与在TreeView的任何项目上构建可单击的编辑按钮有关的问题。

当单击TreeViewItem前面的编辑​​按钮时,如何调用回调函数?

编辑按钮的图像: https://code.visualstudio.com/api/extension-guides/tree-view#view-actions

我注意到TreeItem实例的command属性,并浏览了文档,但不明白如何使用命令调用回调函数。

任何帮助将不胜感激。 谢谢

文档:https://code.visualstudio.com/docs/extensionAPI/vscode-api#TreeItem

            "view/item/context": [
                {
                    "command": "issuesList.deleteEntry",
                    "when": "view == issuesList && viewItem == dependency",
                    "group": "inline"
                },
                {
                    "command": "issuesList.viewInVisualizer",
                    "when": "view == issuesList && viewItem == dependency"
                }
            ]

2 个答案:

答案 0 :(得分:0)

似乎您应该通过vscode.commands.registerCommand API将函数绑定到命令。 代码看起来像

import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
    // other code
    vscode.commands.registerCommand('issuesList.deleteEntry', (node: Dependency) => node.deleteEntry());
    vscode.commands.registerCommand('issuesList.viewInVisualizer', (node: Dependency) => viewInVisualizer(node));
}

可以在https://code.visualstudio.com/api/references/vscode-api#commands找到更详细的用法,还可以在https://github.com/microsoft/vscode-extension-samples/tree/master/tree-view-sample找到TreeView的完整示例

答案 1 :(得分:0)

    const tree = vscode.window.createTreeView('myview', {treeDataProvider: dataProvider, showCollapseAll: true });
    tree.onDidChangeSelection( e => click(e.selection));