VSCode 编写插件
创建项目
先安装官方的脚手架,用来生成插件模板
# 安装脚手架
npm install -g yo generator-code
# 到自己工程目录下
yo code
# 再安装vscode模块
npm install vscode
按照需求创建
参考如下:
? What type of extension do you want to create? New Extension (JavaScript)
? What's the name of your extension? auto-time
? What's the identifier of your extension? UpdateTime
? What's the description of your extension? 用来自动更新Markdown顶部栏的时间,以及新建文档时自动生成顶部栏
? Enable JavaScript type checking in 'jsconfig.json'? Yes
? Initialize a git repository? Yes
? Which package manager to use? npm
调试项目
调试配置文件写法
.vscode/launch.json文件的写法
点击vscode左侧的debug按钮会在项目的.vscode目录下生成一个launch.json的文件,里面就是用来配置debug信息的
一般有专门的配置环境的插件(例如vscode插件的调试环境就自带了),点击

然后ctrl+shift+p输入helloWorld(contributes.commands里面注册的)就能看到前面的插件写的消息了
package配置信息
大量参考极客教程
生成的package.json有几个关键的配置信息
engines
它指定了运行这个插件需要的 VS Code 版本
"engines": {
"vscode": "^1.46.0"
},
activationEvents
这个属性指定了什么情况下这个插件应该被加载并且激活。在我们这个例子里,激活条件是,当用户想要运行 “UpdateTime.helloWorld” 这个命令时,就激活这个插件。 这个机制能够保证,当需要使用这个插件的时候,这个插件才被激活,尽可能地保证性能和内存使用的合理性。
// 注册命令
"activationEvents": [
"onCommand:UpdateTime.helloWorld"
],
其他的配置选项
// 配置目标语言 例如 onLanguage:javascript 只要打开了JS类型的文件,插件就会被激活。
// * 表示全部语言
onLanguage:${language}
// 启动命令
onCommand:${command}
contributes
"contributes": {
"commands": [
{
"command": "extension.sayHello",
"title": "Hello World"
}
]
},
这个属性指定了,这个插件给 VS Code 添加了一个 command,这个 command 的 id 是 “extension.sayHello”, 跟 extension.js 中写的一样。而这个命令的名字,叫做 Hello World。
如果不写这个属性的话,VS Code 是不会把这个命令注册到命令面板中的,也就没法找到这个命令并且执行了。
添加快捷键
"contributes": {
"commands": [
{
"command": "extension.sayHello",
"title": "Hello World"
}
],
"keybindings": [
{
"key": "ctrl+t",
"command": "extension.sayHello",
"when": "editorTextFocus"
}
]
},
加入菜单
// 菜单项
// "when": "editorFocus && resourceLangId == javascript" : 只有编辑器具有焦点,并且打开的是JS文件才会出现
// navigation是一个永远置顶的分组,后面的@6是人工进行组内排序
"contributes": {
"commands": [
{
"command": "extension.sayHello",
"title": "Hello World"
}
],
"keybindings": [
{
"key": "ctrl+t",
"command": "extension.sayHello",
"when": "editorTextFocus"
}
],
"menus": {
"editor/context": [
{
"when": "editorFocus && resourceLangId == javascript" ,
"command": "UpdateTime.helloWorld",
"group": "navigation@6"
}
]
}
},
插件默认配置
参考资料 contributes.configuration
添加默认配置
很多插件是需要一些额外配置才能工作的,设置默认配置同样在 package.json 里
// 例如下面添加两个配置项
"contributes": {
"configuration": {
"type": "object",
"title": "TypeScript configuration",
"properties": {
"typescript.useCodeSnippetsOnMethodSuggest": {
"type": "boolean",
"default": false,
"description": "Complete functions with their parameter signature."
},
"markdown.imgArr": {
"default": [
"https://image.quicktoolset.top/2020/06/19/Nu9RF1.jpg",
"https://image.quicktoolset.top/2020/06/19/Nu9goR.jpg",
"https://image.quicktoolset.top/2020/06/19/NuPfKO.jpg",
"https://image.quicktoolset.top/2020/06/19/NuP226.jpg",
"https://image.quicktoolset.top/2020/06/19/NuPsa9.jpg",
"https://image.quicktoolset.top/2020/06/19/NuPRxK.jpg",
"https://image.quicktoolset.top/2020/06/19/NuPrVJ.jpg",
"https://image.quicktoolset.top/2020/06/19/NuPcP1.jpg",
"https://image.quicktoolset.top/2020/06/19/NuPy5R.jpg",
"https://image.quicktoolset.top/2020/06/19/NuPg8x.jpg",
"https://image.quicktoolset.top/2020/06/19/NuPhrD.jpg"
],
"type": "array",
// 要加上这个才能在配置页看到 item
"items": {
"type": "string"
},
"markdownDescription": "在这里随机选择的头图"
}
}
}
}
小提示:配置 markdownDescription 比配置 description 更好,它能呈现 Markdown 格式的文档。
{
"gitMagic.blame.heatmap.enabled": {
"description": "Specifies whether to provide a heatmap indicator in the gutter blame annotations"
}
}
使用 markdownDescription 可以直接在提示使用 Markdown 的语法
{
"gitMagic.blame.dateFormat": {
"markdownDescription": "Specifies how to format absolute dates (e.g. using the `${date}` token) in gutter blame annotations. See the [Moment.js docs](https://momentjs.com/docs/#/displaying/format/) for valid formats"
}
}
读取配置文件
可以用 vscode.workspace.getConfiguration('myExtension') 读取配置值。
const config = vscode.workspace.getConfiguration();
const imgArr = config.get('markdown.imgArr'); // 获取指定配置项
console.log(imgArr);