我正在创建一个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
时,我希望我的webview扩展能够在我打开扩展名为.abc
的文件的时候打开,但是没有任何反应。
然后我尝试了activationEvents设置"*"
,期望我的webview扩展在VSCode的开头打开但是也没有打开我的扩展。
我可以按正常方式通过Ctrl + Shift + P打开并运行我的扩展程序。
我不认为当打开具有特定名称或扩展名的文件时会触发任何activation event。您尝试的onFileSystem
事件具有不同的目的并检查文件的方案。
通常你会使用onLanguage
,并使用与你的.abc
扩展相关联的语言标识符。如果它不是一个流行的文件扩展名,您可能需要在contributes.languages
section中注册它。
然后我尝试了
activationEvents
设置"*"
,期望我的webview扩展将在VSCode的开头打开但是也没有打开我的扩展。
如果激活事件是activate()
,则应始终调用扩展的*
方法。我假设通过“使用命令面板运行它”你的意思是通过扩展开发主机调试扩展?除非您的扩展名在<User>/.vscode/extensions
目录中,否则它不会包含在常规VSCode执行中。然后它也应该在Extensions面板中列出。
我认为你需要使用
“workspaceContains:*。abc”作为activationEvents
我认为你必须在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"
}
]
}
}
}