内容简介:I’ve built twoVS Code does a great job to help with getting started on
I’ve built two VS Code extensions and thought it would be good to share my thoughts on the best way to kick start building your first extension. Key topics I will cover here are the basics about the build, unit testing and a publish/deployment pipeline.
Photo by Simon Abrams on Unsplash
Basics around building an extension
VS Code does a great job to help with getting started on your first extension . They use a Yeoman generator to set up a basic hello world project which is configured to debug. Debugging is essential and has helped me learn about the VS Code API. All the basics are covered here and I’d recommend reading the whole getting started section.
When you open the project main entry file extension.ts
you should see two functions: activate
and deactivate
. Activate is called when an event is fired in VS Code that your extension is bound to, for example calling a command via the command palette. Deactivate is called when the extension is disabled or uninstalled.
When things start to get tricky
This is not to scare you but to prepare yourself to be a little patient. The VS Code API is documented but not to the extent everything has example snippets, so there will be a bit of trial and error.
The key thing to understand is VS Code reads certain properties in your extension package.json
to bind the UI to your extension commands.
Activation Eventsis an array of strings that binds the event to a command id. Every command you define will have a string id. Event and command are split by a colon :
e.g. "onCommand:awesomeExt.helloWorld"
. An activation event in your package.json
might look like this:
{ "name": "Awesome Ext", "activationEvents":["onCommand:awesomeExt.helloWorld"] }
Next is Contribution Points which enable you to extend the VS Code UI. To help make the command above easy to find, you can give it a searchable title in the command palette. To do this use the commands
contribution point. See the package.json
example below:
{ "name": "Awesome Ext", "activationEvents":["onCommand:awesomeExt.helloWorld"], "contributes": { "commands": [ { "command": "awesomeExt.helloWorld", "title": "Hello World", } ] } }
All that is left is to use the VS Code API to register the command. Inside the activate
function use the following registerCommand
method, see below:
const vscode = require("vscode"); export function activate(){ vscode.commands.registerCommand("awesomeExt.helloWorld", () => { vscode.window.showInformationMessage("Hello World!"); }); }
This is all in the generated project, once you npm i
and press F5 to launch the extension. Then search the command palette to find your “hello world” command.
You are now well on your way to making your own VS Code extension.
How easy is it to unit test my extension?
The VS Code extension is essentially still a JavaScript project and so you can use any testing framework you like. To help with this I’ve written a post on unit tests and mocking VS Code API using Jest . It provides working unit test examples for mocking the API based off my Git Mob VS Code extension.
Publish your extension
To help you speed up the process of publishing to the Visual Studio Market Place I’ve created a post about deploy VS code extension via Azure pipeline . This covers how to build and deploy your extension but also from an automated pipeline viewpoint. To further improve the pipeline like running unit tests I’ve also published this: build a CD Azure pipeline .
More example code
To understand more about how the VS Code API fits together, here is a repo full of working examples you can try out, code extension examples . I recommend cloning the repo, running the extensions and placing some debug points to explore the examples.
Hope this helps you accelerate the development of your VS code extension and if you know of more resources please share here and put a link in the comments below.
以上所述就是小编给大家介绍的《Kick start building your first VS Code extension》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
CSS商业网站布局之道
朱印宏 / 清华大学出版社 / 2007-1 / 75.00元
本书是一本CSS技术专著。 主要从布局角度全面、系统和深入地讲解CSS在标准网站布局之中的应用。很多读者经过初步的学习之后就能够使用CSS设计出一些漂亮的网页样式,于是便乐在其中,踌躇满志,这是好事,但千万不要自我陶醉,因为你还未领略CSS的博大精深。用CSS容易,难的是全部都用CSS。CSS的精髓是布局,而不是样式,布局是需要缜密的逻辑思维和系统设计的,而样式只需要简单地编写代码或复制即可。本书......一起来看看 《CSS商业网站布局之道》 这本书的介绍吧!