【问题标题】:Explorer/Context Menus. Is it possible to change folder context depending on a file inside the folder?资源管理器/上下文菜单。是否可以根据文件夹内的文件更改文件夹上下文?
【发布时间】:2019-08-05 02:48:42
【问题描述】:

所以我目前有基于 VScode 文件夹的上下文菜单。

我想知道是否可以根据文件是否存在于文件夹中来做上下文。例如,如果文件夹中不存在 temp.exe,则不显示上下文?

在文档中似乎找不到任何地方。

谢谢,

特伦特

这是一个 Vscode 扩展。我只尝试了根据文件类型获取上下文的能力。

"menus": {
            "explorer/context":[
                {
                    "when": "explorerResourceIsFolder",
                    "command": "extension.runApp",
                    "group": "vm@1"
                },
                {
                    "when":"explorerResourceIsFolder",
                    "command": "extension.provisionApp",
                    "group": "vm@2"
                }
            ]
        }

【问题讨论】:

  • 您可以像这样检查哪些上下文键可用:stackoverflow.com/a/57245061/2631715 - 不过我认为它们中的任何一个都不适合您的情况。
  • 是的,不幸的是没有。我想我可能需要换个角度看。

标签: visual-studio-code vscode-extensions


【解决方案1】:

这个in operator for when clauses 看起来就是您要找的东西。来自 v1.49 发行说明:

我们为when 子句添加了一个新的in 运算符。这个新 运算符允许动态查找上下文键的值 另一个上下文键的值。例如,如果您想添加一个 包含特定类型文件的文件夹的上下文菜单命令 (或无法静态知道的东西),您现在可以使用 in操作符来实现吧。

vscode.executeCommand('setContext', 'ext:supportedFolders', [ 'test', 'foo', 'bar' ]);

// Note in this case (using an object), the value doesn't matter, it is based on the existence of the key in the object
vscode.executeCommand('setContext', 'ext:supportedFolders', { 'test': true, 'foo': 'anything', 'bar': false });

// Note, this assumes you have already defined a command called ext.doSpecial

"menus": {
  "explorer/context": [
    {
      "command": "ext.doSpecial",
      "when": "explorerResourceIsFolder && resourceFilename in ext:supportedFolders"
    }
  ]
}

在该示例中,我们采用 resourceFilename 的值(即 在这种情况下是文件夹的名称)并检查其是否存在 在ext:supportedFolders 的值中。如果存在,菜单将是 显示。


【讨论】:

    猜你喜欢
    • 2014-01-11
    • 2011-06-04
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多