Answer a question

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?

Answers

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"
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐