简体   繁体   English

在VSCode扩展中注册多个命令

[英]Registration of multiple commands in VSCode extension

I'm trying to understand the extension activation routine and maybe someone can help me. 我正在尝试理解扩展激活例程,也许有人可以帮助我。

In the file package.json there is the activationEvents node, where I define when the extension will be activated (eg when executing a command via onCommand ). package.json文件中有activationEvents节点,我在其中定义何时激活扩展(例如,通过onCommand执行命令时)。 See help 见帮助

From code documentation of the sample extension, I understand that the extension will only be activated on the very first execution of the command. 从示例扩展的代码文档中,我了解扩展只会在第一次执行命令时激活。

What happens when I define multiple commands in the extension? 当我在扩展中定义多个命令时会发生什么? I would register all the commands in the activate function and add them to the activationEvents : 我会在activate函数中注册所有命令并将它们添加到activationEvents

"activationEvents": [
    "onCommand:extension.testCommand1",
    "onCommand:extension.testCommand2",
    "onCommand:extension.testCommand3"
],

Will the activate function only be called once when any of the commands was executed the very first time? 只有在第一次执行任何命令时,才会调用一次activate函数吗? Or will it call the function for the first execution of extension.testCommand1 , extension.testCommand2 and extension.testCommand3 (--> three times)? 或者它会调用函数来首次执行extension.testCommand1extension.testCommand2extension.testCommand3 ( - >三次)?

I'm also writing a language server and would like to put everything in the same extension. 我也在写一个语言服务器,并希望将所有内容放在同一个扩展名中。 Is this possible or do I need to create two additional extensions (client and server)? 这是可能的还是我需要创建两个额外的扩展(客户端和服务器)?

To answer your first question, it will be called exactly one time, only when the first activation event occurs. 要回答您的第一个问题,只有在第一个激活事件发生时,才会调用一次。 If you're interested in verifying this yourself, set the activation events in package.json to: 如果您有兴趣自己验证,请将package.json的激活事件设置为:

"activationEvents": [
    "onLanguage:js",
    "onLanguage:python",
    "onLanguage:ruby"
],

Then set a breakpoint in the activation method in your extension, press F5 to start debugging, and change the language mode via the command palette to js, python, and ruby. 然后在扩展中的激活方法中设置断点,按F5开始调试,并通过命令面板将语言模式更改为js,python和ruby。 You'll see that the break-point is only hit the first time. 你会看到断点只是第一次出现。

Regarding your second question, it probably depends on what your language service needs to do. 关于你的第二个问题,它可能取决于你的语言服务需要做什么。 If you want to do something as complex as the typescript language service, you might need both. 如果你想做一些像打字稿语言服务这样复杂的事情,你可能需要两者。 If you want to make something that lints, provides hover tips, or more, but isn't quite as complex as typescript, you can do it one extension. 如果你想制作一些lints,提供悬停技巧或更多内容,但不像打字稿那么复杂,你可以做一个扩展。

I'm personally following the example of the php service for my language extension. 我个人跟着我的语言扩展的php服务的例子。 The team did a good job with it and the features are well separated and easy to understand. 团队做得很好,功能分离且易于理解。 You can view the source of the php service here . 您可以在此处查看php服务的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM