Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate command on TreeViewItem click, VSCode Extension

I would like to run a command on the click of a tree view item, rather than in a menu that comes up. Right now in my package.json, I have this:

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

Right now, "inline" will put an icon next to the words which you have to click to run the command, but I would like the command to run when I click on the node itself.

What do I change "group" to? Or do I do something different entirely?

Thanks

like image 585
Justin Kalloor Avatar asked Jul 25 '18 18:07

Justin Kalloor


2 Answers

You have to set the command property on the TreeItem instance.

command?: Command

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

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

like image 100
Gama11 Avatar answered Sep 29 '22 22:09

Gama11


Pass a vscode.Command object to your TreeItem like in the snippet below. Make sure you have defined your command in package.json and extension.ts.

class TreeItem extends vscode.TreeItem {

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

Here's the complete type definition for 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[];
    }
like image 39
secretshardul Avatar answered Sep 29 '22 20:09

secretshardul