VSCode plug in
First Extension
Requirement
install generator
- Yeoman, VS Code Extension Generator
npm install -g yo generator-code
Generate scaffolds
yo code
# ? What's the identifier of your extension? helloworld
code ./helloworld
Run
F5
- Command Palette
β§βP
Anatomy
onCommand
Activation Event: onCommand:extension.helloWorld
contributes.commands
Contribution Point: Hello World
in Command Palette
commands.registerCommand
VS Code API: extension.helloWorld
bind command and function
File Structure
.
βββ .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
Extension Manifest package.json
<publisher>.<name>
as a unique ID for the extension.
main
: The extension entry point.
activationEvents
and contributes
engines.vscode
minimum version of VS Code API
{
"name": "helloworld-sample",
"displayName": "helloworld-sample",
"description": "HelloWorld example for VS Code",
"version": "0.0.1",
"publisher": "vscode-samples",
"repository": "https://github.com/microsoft/vscode-extension-samples/helloworld-sample",
"engines": {
"vscode": "^1.51.0"
},
"categories": ["Other"],
"activationEvents": ["onCommand:extension.helloWorld"],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "extension.helloWorld",
"title": "Hello World"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./"
},
"devDependencies": {
"@types/node": "^8.10.25",
"@types/vscode": "^1.51.0",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
}
}
Extension Entry File
activate()
and deactivate()
// 'vscode' λͺ¨λμ VS Code extensibility APIλ₯Ό ν¬ν¨
// μλ μ½λμμ λͺ¨λμ κ°μ Έμ€κ³ μ½λμμ vscode λ³μΉμ ν΅ν΄ μ°Έμ‘° κ°λ₯
import * as vscode from 'vscode';
// νμ₯ λͺ¨λμ΄ νμ±νλλ©΄ μ€νλλ λ©μλ
// νμ±νλλ©΄ μ μΌ λ¨Όμ μ€νλλ λΆλΆ
export function activate(context: vscode.ExtensionContext) {
// μ§λ¨ μ 보λ₯Ό μΆλ ₯νκΈ° μν΄ console.logμ console.errorλ₯Ό μ¬μ©
// νμ₯ λͺ¨λμ΄ νμ±ν λλ©΄ ν λ²λ§ μ€ν
console.log('Congratulations, your extension "helloworld-sample" is now active!');
// package.json νμΌμ μ μλ λͺ
λ Ή
// registerCommandλ‘ λͺ
λ Ήμ ꡬν체 μ 곡
// commandId νλΌλ―Έν°λ package.jsonμ μλ command νλμ 맀μΉλμ΄μΌ ν¨
let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {
// μ¬λ¬λΆμ΄ μ μν λͺ
λ Ήμ΄ μ€νλ λλ§λ€ μ€νλλ μ½λ
// μ¬μ©μμκ² λ©μμ§ λ°μ€ νμ
vscode.window.showInformationMessage('Hello World!');
});
context.subscriptions.push(disposable);
}
// μ΄ λ©μλλ μ¬λ¬λΆμ νμ₯ λͺ¨λμ΄ λΉνμ±νλ λ μ€νλ¨
export function deactivate() {}
ref