Petr Broz
Petr Broz

Reputation: 9942

VSCode extension with a tree view and custom context menu

I'm implementing a Visual Studio Code extension that provides a custom tree view, and in the tree view I'm showing custom commands in context menu using the following contributes setup:

"contributes": {
    ...
    "menus": {
        "view/item/context": [
            {
                "command": "myExtension.uploadFile",
                "when": "view == myBucketExplorer"
            }
        ]
    }
    ...
}

Now, is there a way to only show this command for root nodes in the tree view? Is there perhaps a when clause that could help with that, or would I need to somehow disable the command programatically when the menu is actually invoked?

Upvotes: 7

Views: 4441

Answers (1)

Alex
Alex

Reputation: 67879

You can set contextValue for your TreeItem.

export class Something extends vscode.TreeItem {
    // ...
    constructor(
        isRoot: boolean
    ) {
        this.contextValue = isRoot ? 'YOUR_CONTEXT' : undefined;
    }

}

async getChildren(element?: Something): Promise<Something[]> {
    if (element) {
        // NOT root
    } else {
        // ROOT -- Use different context for items
    }
}

And then use

"when": "view == myBucketExplorer && viewItem == YOUR_CONTEXT"

Upvotes: 9

Related Questions