VSCode 带有树视图和自定义上下文菜单的扩展
VSCode extension with a tree view and custom context menu
我正在实施 Visual Studio 代码扩展,提供自定义树视图,在树视图中,我使用以下 contributes
设置在上下文菜单中显示自定义命令:
"contributes": {
...
"menus": {
"view/item/context": [
{
"command": "myExtension.uploadFile",
"when": "view == myBucketExplorer"
}
]
}
...
}
现在,有没有办法在树视图中只为 根节点 显示此命令?是否有一个 when
子句可以帮助解决这个问题,或者我是否需要在实际调用菜单时以某种方式以编程方式禁用该命令?
您可以为您的TreeItem
设置contextValue
。
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
}
}
然后使用
"when": "view == myBucketExplorer && viewItem == YOUR_CONTEXT"
我正在实施 Visual Studio 代码扩展,提供自定义树视图,在树视图中,我使用以下 contributes
设置在上下文菜单中显示自定义命令:
"contributes": {
...
"menus": {
"view/item/context": [
{
"command": "myExtension.uploadFile",
"when": "view == myBucketExplorer"
}
]
}
...
}
现在,有没有办法在树视图中只为 根节点 显示此命令?是否有一个 when
子句可以帮助解决这个问题,或者我是否需要在实际调用菜单时以某种方式以编程方式禁用该命令?
您可以为您的TreeItem
设置contextValue
。
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
}
}
然后使用
"when": "view == myBucketExplorer && viewItem == YOUR_CONTEXT"