我正在尝试创建一个 VSCode 扩展。这个扩展提供了两个命令,不用管它们的实现:
export function activate(context: ExtensionContext) {
const provider = new ContentProvider();
const providerRegistrations = Disposable.from(
workspace.registerTextDocumentContentProvider(ContentProvider.scheme, provider)
);
// Open the dynamic document, and shows it in the next editor
const openMyExtensionCommandRegistration = commands.registerTextEditorCommand('extension.openMyExtension', editor => {
// Activate the extension and do something
});
const useMyExtensionCommandRegistration = commands.registerTextEditorCommand('extension.useMyExtension', editor => {
// Do something
});
context.subscriptions.push(
provider,
openMyExtensionCommandRegistration,
useMyExtensionCommandRegistration,
providerRegistrations
);
}
这是我的 package.json
文件的一部分:
"activationEvents": [
"onCommand:extension.openMyExtension"
],
"main": "./out/extension",
"contributes": {
"commands": [
{
"command": "extension.openMyExtension",
"title": "Open my extension",
"category": "MyExtension"
},
{
"command": "extension.useMyExtension",
"title": "Do something with my extension",
"category": "MyExtension"
}
],
应该激活我的扩展程序的第一个命令有效。它出现在命令面板中,并在调用时实际执行它应该执行的操作。
然而,尽管第二个命令出现在命令面板中,但在调用时会引发以下错误消息:
command 'extension.useMyExtension' not found
我觉得很奇怪,我的第一个命令运行良好,但第二个却不行,因为代码非常相似。有什么想法吗?
请注意,我显然更改了一些变量名,我仔细检查了真实代码中的拼写错误。
最佳答案
您需要将所有已注册的命令添加到 package.json 中的 activationEvents
列表中,以便它们在调用时可用。像这样更新你的 package.json:
{
...
"activationEvents": [
"onCommand:extension.openMyExtension",
"onCommand:extension.useMyExtension"
]
...
}
您可以在官方 VSCode Documentation 中找到有关激活事件的更多详细信息.
关于typescript - 在 VSCode 扩展中找不到命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49534068/