Reference: https://liiked.github.io/VS-Code-Extension-Doc-ZH
I'm working on improve the performance of code completion tools. Recently, I turned to node.js completion. Hence, I have to learning somethings about extension development in visual studio code, in order to develop a demo to validate and test the model.
Get Started
First, install Yeoman and VS Code Extension Generator
|
|
Then, run yo code and fill out the necessary information, Yeoman will generate a source folder for VSC extension automatically.
Then, open VSC and enter the plugin folder, press F5, a test VSC window will be opened. Open VSC command window(shift + command(ctrl) + p) and enter Hello World, the VSC will show a notification which says "Hello World" in the right-bottom of the test window:
Congrats! You've successfully run your first extension!
But how does it work?
We check the created source folder of the VSC extension first:
.
├── .vscode
│ ├── launch.json // Config for launching and debugging the extension
│ └── tasks.json // Config for build task that compiles TypeScript
├── .gitignore // Ignore build output and node_modules
├── README.md // Readable description of your extension's functionality
├── src
│ └── extension.ts // Extension source code
├── package.json // Extension manifest
├── tsconfig.json // TypeScript configuration
Each VSC extension must have a package.json, in which we stores information about the extension, such as name, publisher, activationEvents, etc. Some important keywords are listed here:
nameandpublisher: VSC usespublisher.nameas the identifier of the extensionmain: entrance file of the extensionactivationEvents: the event used to activate the extensioncontributes: configurations, including user defined configurations
In the generated source folder, the entry file is extension.js, now let's have a look:
|
|
The entry file exports two functions: activate and deactivate. activate will be executed when the activationEvents are triggered, while deactivate is used to clear in workspace before the extension is closed.
Our first extension contains three parts actually:
-
onCommandactivationEventsThis is defined in
package.json'sactivationEventsfield. After defining this, the user can enterHello Worldto activate the extension.1 2 3"activationEvents": [ "onCommand:extension.helloWorld" ] -
contributes.commandsThis is also defined in
package.json. This field is used to bind the command IDextension.helloWorldwith keywordHello World. With this we can useHello Worldcommand in VSC's command window.1 2 3 4 5 6 7 8"contributes": { "commands": [ { "command": "extension.helloWorld", "title": "Hello World" } ] } -
vscode.commands.registerCommandThis is the VSC API which binds a function with the command ID
extension.helloWorld.In the example above, the command
extension.helloWorldis registered with a function of lambda expression