在TreeViewItem单击上激活命令,VSCode扩展

时间:2018-07-25 18:50:56

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

我想在单击树状视图项目时而不是在出现的菜单中运行命令。现在在我的package.json中,我有这个:

    {
      "command": "test.view.showError",
      "when": "view == test.view && viewItem == test",
      "group": "inline"
    }

现在,“ inline”将在您必须单击以运行命令的单词旁边放置一个图标,但是我希望在单击节点本身时运行该命令。

我如何将“组”更改为?还是我做完全不同的事情?

谢谢

2 个答案:

答案 0 :(得分:2)

您必须在command实例上设置TreeItem属性。

  

命令?:Command

     

选择树项时应执行的command

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

答案 1 :(得分:1)

vscode.Command 对象传递给您的 TreeItem,如下面的代码片段所示。确保您已在 package.jsonextension.ts 中定义您的命令。

class TreeItem extends vscode.TreeItem {

    command = {
        "title": "Show error",
        "command": "test.view.showError",
    }
    constructor(label: string) {
        super(label);
    }
}

以下是 vscode.Command 的完整类型定义:

    /**
     * Represents a reference to a command. Provides a title which
     * will be used to represent a command in the UI and, optionally,
     * an array of arguments which will be passed to the command handler
     * function when invoked.
     */
    export interface Command {
        /**
         * Title of the command, like `save`.
         */
        title: string;

        /**
         * The identifier of the actual command handler.
         * @see [commands.registerCommand](#commands.registerCommand).
         */
        command: string;

        /**
         * A tooltip for the command, when represented in the UI.
         */
        tooltip?: string;

        /**
         * Arguments that the command handler should be
         * invoked with.
         */
        arguments?: any[];
    }
相关问题