如何从文件类型调用WebView扩展

时间:2019-01-09 10:40:10

标签: visual-studio-code vscode-extensions

我正在创建一个VS Code WebView扩展,当我打开具有特定文件扩展名的文件时,我希望调用/触发该扩展。例如MyFile.abc

在myExt中,我将onFileSystem添加到了activationEvents中的package.json中:

{
    "name": "myext",
    "description": "A Webview API Sample",
    "version": "0.0.2",
    "publisher": "vscode-myext",
    "engines": {
        "vscode": "^1.25.0"
    },
    "categories": [
        "Other"
    ],
    "activationEvents": [
        "onWebviewPanel:myExt",
        "onFileSystem:abc",
        "*"     
    ],
    "main": "./out/extension.js",
    "contributes": {
        "commands": [
            {
                "command": "myExt.start",
                "title": "Start myExt ",
                "category": "My Ext"
            }
        ]
    },
    "scripts": {
        "vscode:prepublish": "tsc -p ./",
        "compile": "tsc -p ./",
        "watch": "tsc -w -p ./",
        "postinstall": "node ./node_modules/vscode/bin/install"
    },
    "dependencies": {
        "supports-color": "^6.0.0",
        "vscode": "^1.1.18"
    },
    "devDependencies": {
        "@types/node": "^10.5.2",
        "tslint": "^5.11.0",
        "typescript": "^2.9.2"
    }
}

当我在myExt中将"onFileSystem:abc"添加到activationEvents时,我希望打开带有扩展名.abc的文件时,我的webview扩展名都能打开,但是什么也没发生。

然后,我尝试了ActivationEvents设置"*",希望我的webview扩展名将在VSCode的开头打开,但也没有打开我的扩展名。

我可以按照正常方式通过 Ctrl + Shift + P 打开并运行扩展程序。

4 个答案:

答案 0 :(得分:0)

我认为打开具有特定名称或扩展名的文件时不会触发任何activation event。您尝试的onFileSystem事件具有不同的用途,并检查文件的方案

通常,您将为此使用onLanguage,并使用与.abc扩展名相关联的语言标识符。如果它不是受欢迎的文件扩展名,则可能需要在contributes.languages section中进行注册。

  

然后,我尝试使用activationEvents设置"*",期望我的webview扩展名将在VSCode的开头打开,但也没有打开我的扩展名。

如果激活事件为activate(),则应始终调用扩展程序的*方法。我假设通过“使用命令面板运行它”是指通过扩展开发主机调试扩展吗?除非您的扩展名位于您的<User>/.vscode/extensions目录中,否则它将不会包含在常规VSCode执行中。然后,它也应该在“扩展”面板中列出。

答案 1 :(得分:0)

我认为您需要使用

“ workspaceContains:*。abc”作为ActivationEvents

答案 2 :(得分:0)

我认为您必须在package.json中做以下事情

{
"activationEvents": [
        "onCommand:"**HERE WILL BE YOUR EXTENSION NAME WHICH YOU REGISTERED**"    
    ],
  "contributes": {

    "menus": {
      "explorer/context": [
        {
          "when": "resourceLangId == **abc**",  // this is the extension of the file where you want to execute your command
          "command": "**YOUR COMMAND NAME**",
          "title": "Anything relevent title",
          "group": "navigation"
        }
      ]
    }
  }
}

答案 3 :(得分:0)

根据Microsoft的custom-editor-sample示例,您需要在package.json中进行以下配置:

    "contributes": {
    "customEditors": [
        {
            "viewType": "catCustoms.catScratch",
            "displayName": "Cat Scratch",
            "selector": [
                {
                    "filenamePattern": "*.cscratch"
                }
            ]
        },