简介
在开发小程序的时候,因为业务需要接触到了vscode插件开发。因为自己也是从零开始开发插件没有什么经验,所以就是一边查资料一边踩坑,经过一番折腾终于完成了一个简单的插件。插件功能虽然简单,但是开发插件的所有步骤都需要走一遍。所以对理解开发插件还是有很大帮助的。由于是第一次开发插件,难免在后面会忘记开发的流程,所以在这里以笔记的方式做个记录,方便大家参考和自己查阅。
什么是vscode插件
因为vscode是个轻量化的编辑器,在开发的时候自带的有利于开发的功能比较少。如果想更友好的开发和代码提示啥的就需要插件来实现。插件就是对vscode功能的一个扩展,来实现想要的功能。比如:代码高亮、代码提示、代码调试运行等。
Hello World
一切开始都是hello world。
安装官方脚手架
npm install -g yo generator-code
复制代码
执行命令生成插件文件夹
yo code
复制代码
文件目录
其中extension.ts
就是主要的插件文件,是插件的主入口
package.json
中关键的插件配置,其他配置和一般的npm包的配置基本相同
// vscode兼容的最低版本
"engines": {
"vscode": "^1.55.0"
},
// 插件激活事件配置
"activationEvents": [
"onCommand:vscode-plugin-demo.helloWorld"
],
// 主入口文件
"main": "./out/extension.js",
// 插件的配置
"contributes": {
"commands": [
{
"command": "vscode-plugin-demo.helloWorld",
"title": "Hello World"
}
]
},
复制代码
-
main
中定义了插件的入口文件,此处为extension.ts
-
contributes.commands
里面注册了一个名为vscode-plugin-demo.helloWorld
的命令,并在src/extension.js
中去实现了它(弹出一个Hello World
的提示); -
在
activationEvents
添加上onCommand:vscode-plugin-demo.helloWorld
当用户执行了这个命令操作时去执行前面定义的内容 -
除了
onCommand
之外,还有onView
、onUri
、onLanguage
等等,后面会遇到的
src/extension.ts
入口文件文件内容
import * as vscode from 'vscode';
// 当您的扩展激活时触发
export function activate(context: vscode.ExtensionContext) {
// 此代码行仅在激活扩展时执行一次
console.log('Congratulations, your extension "vscode-plugin-demo" is now active!');
// 命令Id参数必须与package.json中命令字段匹配
let disposable = vscode.commands.registerCommand('vscode-plugin-demo.helloWorld', () => {
// 每次执行命令时,您在这里放置的代码都会执行
// 向用户显示消息框
vscode.window.showInformationMessage('Hello World from vscode-plugin-demo!');
});
context.subscriptions.push(disposable);
}
// 当您的扩展被停用时触发
export function deactivate() {}
复制代码
运行调试
其实文件中已经给配置好了测试环境相关,在vscode/launch.json
文件中
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
},
{
"name": "Extension Tests",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
],
"outFiles": [
"${workspaceFolder}/out/test/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
}
]
}
复制代码
configurations.name
就是调试的环境的名字
打开调试
先点击1出现调试然后点击2处出现下拉选项,就是配置的调试环境选择配置好的Run Extension
点击前面的绿色的小按钮就会出现一个调试窗口。调试的快捷键是F5
调试窗口会有一个扩展开发宿主
的标识
调试窗口按下Ctrl+Shift+P
,输入HelloWorld
执行对应命令,当你发现右下角弹出了HelloWorld
的提示时,恭喜你,插件已经开发完成
添加右键菜单和快捷键
项目默认配置只是给添加了命令,不方便调试,为此给添加菜单和快捷键。
package.json
{
"contributes": {
// 注册命令
"commands": [
{
"command": "vscode-plugin-demo.helloWorld",
"title": "Hello World"
}
],
// 快捷键绑定
"keybindings": [
{
"command": "vscode-plugin-demo.helloWorld",
"key": "ctrl+f12",
"mac": "cmd+f12",
"when": "editorTextFocus"
}
],
// 设置菜单
"menus": {
"editor/context": [
{
"when": "editorFocus",
"command": "vscode-plugin-demo.helloWorld",
"group": "navigation"
}
]
}
}
}
复制代码
然后在打开的文件右键点击就有菜单了
Package.json文件常用配置讲解
{
// 插件的名字,应全部小写,不能有空格
"name": "wxapp-cli",
// 插件的友好显示名称,用于显示在应用市场,支持中文
"displayName": "VSCode插件wxapp-cli",
// 描述
"description": "一个创建小程序页面和组件的工具",
// 关键字,用于应用市场搜索
"keywords": ["vscode", "plugin", "wxapp", "cli"],
// 版本号
"version": "0.0.1",
// 发布者,如果要发布到应用市场的话,这个名字必须与发布者一致
"publisher": "water",
// 表示插件最低支持的vscode版本
"engines": {
"vscode": "^1.52.0"
},
// 插件应用市场分类,可选值: [Programming Languages, Snippets, Linters, Themes, Debuggers, Formatters, Keymaps, SCM Providers, Other, Extension Packs, Language Packs]
"categories": [
"Other"
],
// 插件图标,至少128x128像素 必须放在src/images文件夹下
"icon": "images/icon.png",
// 扩展的激活事件数组,可以被哪些事件激活扩展,后面有详细介绍
"activationEvents": [
"onCommand:extension.createPage",
"onCommand:extension.createComponent"
],
// 插件的主入口
"main": "./src/extension",
// 整个插件最重要最多的配置项
"contributes": {
// 插件配置项
"configuration": {
"type": "object",
// 配置项标题,会显示在vscode的设置页
"title": "wxTemp",
"properties": {
// 相关配置
"wxTemp.page.js": {
"type": "string",
"default": "",
"description": "小程序页面模版js"
},
"wxTemp.page.wxss": {
"type": "string",
"description": "小程序页面模版wxss"
},
}
},
// 命令
"commands": [
{
"command": "extension.createPage",
"title": "创建小程序页面"
},
{
"command": "extension.createComponent",
"title": "创建小程序组件"
}
],
// 快捷键绑定
"keybindings": [
{
"command": "extension.createPage",
"key": "ctrl+f10",
"mac": "cmd+f10",
"when": "editorTextFocus"
},
{
"command": "extension.createComponent",
"key": "ctrl+f11",
"mac": "cmd+f11",
"when": "editorTextFocus"
}
],
// 菜单
"menus": {
// 编辑器右键菜单
"editor/context": [
{
// 表示只有编辑器具有焦点时才会在菜单中出现
"when": "editorFocus",
"command": "extension.createPage",
// navigation是一个永远置顶的分组,后面的@6是人工进行组内排序
"group": "navigation@6"
},
{
"when": "editorFocus",
"command": "extension.createComponent",
"group": "navigation@5"
},
{
// 只有编辑器具有焦点,并且打开的是JS文件才会出现
"when": "editorFocus && resourceLangId == javascript",
"command": "extension.createComponentDemo",
"group": "z_commands"
},
{
"command": "extension.createComponentDemo2",
"group": "navigation"
}
],
// 编辑器右上角图标,不配置图片就显示文字
"editor/title": [
{
"when": "editorFocus && resourceLangId == javascript",
"command": "extension.createComponentDemo3",
"group": "navigation"
}
],
// 编辑器标题右键菜单
"editor/title/context": [
{
"when": "resourceLangId == javascript",
"command": "extension.createComponentDemo4",
"group": "navigation"
}
],
// 资源管理器右键菜单
"explorer/context": [
{
"command": "extension.createComponentDemo5",
"group": "navigation"
},
{
"command": "extension.createComponentDemo6",
"group": "navigation"
}
]
},
// 代码片段
"snippets": [
{
"language": "javascript",
"path": "./snippets/javascript.json"
},
{
"language": "html",
"path": "./snippets/html.json"
}
],
// 自定义新的activitybar图标,也就是左侧侧边栏大的图标
"viewsContainers": {
"activitybar": [
{
"id": "wxapp",
"title": "wxapp",
"icon": "images/icon.png"
}
]
},
// 自定义侧边栏内view的实现
"views": {
// 和 viewsContainers 的id对应
"wxapp": [
{
"id": "wxapp1",
"name": "天时"
},
{
"id": "wxapp2",
"name": "地利"
},
{
"id": "wxapp3",
"name": "人和"
}
]
},
// 图标主题
"iconThemes": [
{
"id": "IconTheme",
"label": "测试图标主题",
"path": "./theme/icon-theme.json"
}
]
},
// npm scripts
"scripts": {
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "node ./node_modules/vscode/bin/test"
},
// 开发依赖
"devDependencies": {
"@types/vscode": "^1.52.0",
"@types/glob": "^7.1.3",
"@types/mocha": "^8.0.0",
"@types/node": "^12.11.7",
"eslint": "^7.9.0",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/parser": "^4.1.1",
"glob": "^7.1.6",
"mocha": "^8.1.3",
"typescript": "^4.0.2",
"vscode-test": "^1.4.0",
"ts-loader": "^8.0.3",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.12"
},
"license": "SEE LICENSE IN LICENSE.txt",
"bugs": {
"url": "https://github.com/XXXX/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/XXXXX"
},
// 说明页
"homepage": "https://github.com/XXXXXXX/README.md"
}
复制代码
activationEvents
- onLanguage:${language}
- onCommand:${command}
- onDebug
- workspaceContains:${toplevelfilename}
- onFileSystem:${scheme}
- onView:${viewId}
- onUri
*
触发插件激活的事件配置
contributes
configuration
:设置commands
:命令menus
:菜单keybindings
:快捷键绑定languages
:新语言支持debuggers
:调试breakpoints
:断点grammars
themes
:主题snippets
:代码片段jsonValidation
:自定义JSON校验views
:左侧侧边栏视图viewsContainers
:自定义activitybarproblemMatchers
problemPatterns
taskDefinitions
colors
小结
这里简单以一个小例子来了解插件的开发步骤,罗列了一些常用到的插件配置,以后有机会在对插件配置部分的相关部分做详细的解释。希望对大家开发插件有所帮助