阅读用于创建VS代码扩展的官方文档,提供一个命令(例如:扩展入口文件、VS代码Api -命令等),给出的示例使用以下模式:
activate()函数中)为了更清楚起见,我在这里给出了activate()函数的示例代码:
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "helloworld-sample" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('helloworld.helloWorld', () => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode.window.showInformationMessage('Hello World!');
});
context.subscriptions.push(disposable);
}现在,除了这不是我从“直觉”的角度对扩展贡献命令(可能是“定义新命令的扩展应该在VS Code开始时激活,以便它的命令从那里开始可用等等”)的模式之外,我还有几个问题,这些问题显然只是要求澄清,因为事情是这样工作的,甚至是以“官方”的形式出现的:
activate()函数之后检查命令的处理程序;onCommand激活事件的文档实际上声明在命令被调用时任何时候都调用扩展;该语句也将与我所理解的模式相反,即在每次调用时“原子地”激活扩展/注册命令;disposable取消注册命令,以便处理程序在每次调用时都与命令新关联(文档不清楚这一点);谢谢你的澄清和抱歉,如果我没有正确理解模式的第一位。
发布于 2021-12-18 14:17:04
您读过package.json文件了吗,它在那里声明何时应该激活扩展名:activationEvents
在启动时,VSC为在load_extension中定义的每个扩展命令在命令表中放置一个package.json函数句柄。
当您第一次调用命令时,将调用load_extension函数,加载扩展,并使用实际的命令函数句柄更新命令表。然后再次调用命令函数,现在使用正确的函数。
这是延迟扩展激活,如果您不使用此会话的扩展,则不需要进行某些工作。
https://stackoverflow.com/questions/70402682
复制相似问题