内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
计算机程序设计艺术(第3卷 英文版·第2版)
Donald E.Knuth / 人民邮电出版社 / 2010-10 / 119.00元
《计算机程序设计艺术》系列被公认为计算机科学领域的权威之作,深入阐述了程序设计理论,对计算机领域的发展有着极为深远的影响。本书是该系列的第3卷,扩展了第1卷中信息结构的内容,主要讲排序和查找。书中对排序和查找算法进行了详细的介绍,并对各种算法的效率做了大量的分析。 本书适合从事计算机科学、计算数学等各方面工作的人员阅读,也适合高等院校相关专业的师生作为教学参考书,对于想深入理解计算机算法的读......一起来看看 《计算机程序设计艺术(第3卷 英文版·第2版)》 这本书的介绍吧!