Activate command on TreeViewItem click, VSCode Extension
Asked Answered
L

2

6

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

Lamellibranch answered 25/7, 2018 at 18:50 Comment(0)
T
8

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

Truncated answered 25/7, 2018 at 19:16 Comment(4)
what about doubleclick?Torosian
You can / have to implement that yourself.Truncated
Any example would be greatTorosian
github.com/vshaxe/vshaxe/blob/master/src/vshaxe/view/…Truncated
H
2

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[];
    }
Hudson answered 10/1, 2021 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.