VSCode插件开发

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值