【问题标题】:VSCode extension with a tree view and custom context menu带有树视图和自定义上下文菜单的 VSCode 扩展
【发布时间】:2019-06-13 08:31:53
【问题描述】:

我正在实现一个提供自定义树视图的 Visual Studio Code 扩展,并在树视图中使用以下 contributes 设置在上下文菜单中显示自定义命令:

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

现在,有没有办法只在树视图中为 根节点 显示此命令?是否有可能有一个when 子句可以帮助解决这个问题,或者我需要在实际调用菜单时以某种方式以编程方式禁用该命令?

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    您可以为您的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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2012-12-30
      • 2022-07-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多