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

Activate command on TreeViewItem click, VSCode Extension

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

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

现在,"inline" 会在您必须单击 运行 命令的单词旁边放置一个图标,但我希望单击时命令 运行在节点本身上。

我要把 "group" 改成什么?还是我做一些完全不同的事情?

谢谢

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

command?: Command

The command that should be executed when the tree item is selected.

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

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[];
    }