VSCode插件开发
创建项目
npm -g i yo generator-code
yo code
? What type of extension do you want to create? New Extension (TypeScript)
? What's the name of your extension? demo
? What's the identifier of your extension? demo
? What's the description of your extension?
? Initialize a git repository? No
? Bundle the source code with webpack? No
? Which package manager to use? npm
Extension.ts
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
console.log("插件已激活!");
// 注册一个命令
let disposable = vscode.commands.registerCommand('demo.sayHello', () => {
setTimeout(() => {
vscode.window.showInformationMessage('调用命令成功!');
}, 2000);
});
context.subscriptions.push(disposable);
}
export function deactivate() {/* 插件被销毁前 */}
Package.json
// ...
"activationEvents": [
"onCommand:demo.sayHello"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "demo.sayHello",
"title": "hello"
}
]
},
// ...
Contribution Points
用于注册一些信息。
比如"commands": [ {"command": "demo.sayHello", "title": "hello"} ]
表示当按下Ctrl + Shift + P
输入hello
时调用demo.sayHello
方法。
Activation Events
出于性能考虑,插件并不总是处于激活的状。activationEvents
用于设置何时激活插件。"onCommand:demo.sayHello"
表示当调用命令demo.sayHello
时启用插件。
除onCommand
外,插件还有以下激活方式:
onLanguage
onCommand
onDebug
onDebugInitialConfigurations
onDebugResolve
workspaceContains
onFileSystem
onView
onUri
onWebviewPanel
onCustomEditor
*
onStartupFinished