【发布时间】:2018-09-07 02:42:35
【问题描述】:
我正在尝试创建一个 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
我觉得很奇怪,我的第一个命令可以正常工作,但第二个命令却不行,因为代码非常相似。任何想法为什么?
请注意,我显然更改了一些变量名,我仔细检查了真实代码中的拼写错误。
【问题讨论】:
-
将
registerTextEditorCommand更改为registerCommand时是否检查过它是否有效?我只是对此感到好奇。你的代码在我看来也不错。 -
我仍在调查究竟是什么导致了这个问题,但总而言之,应该将 TypeScript 编译成 JavaScript 的命令在我的机器上不起作用。我通过查看 JavaScript 生成的源代码发现了这一点。
-
您可以尝试为每个命令创建两个不同的扩展,看看是否可行?如果单个扩展中的多个命令没有引起任何问题,这将给出一个想法。
-
我可以通过手动编译 Typescript 源代码(通过在我的根文件夹中运行
tsc -p ./)来解决这个问题。调试时应该自动运行此命令,但是我仍然无法找到为什么在我的机器上不是这样。 -
@Eldy 我刚刚遇到了完全相同的问题(我的 extension.ts 文件在按下 F5 时没有被重建)并且我看到了一个错误,即找不到新注册的命令。运行
tsc -p ./也为我解决了这个问题!感谢您分享您的解决方案。
标签: typescript visual-studio-code vscode-extensions