【VSCode】插件编写

有些功能使用终端工具不是很方便,就打算使用内置的插件功能实现

本章介绍了如何开发一个插件

脚手架的安装

  1. 安装 nodejs

  2. 使用 npm 安装 Yeoman 和 VS Code 拓展生成器

    1
    npm install -g yo generator-code

工程创建与调试

  1. 运行 Yo Code 生成工程

    1
    yo code Hello -t=js -q
  2. 使用 VSCode 打开工程文件夹 Hello

  3. 点击左侧的图标 运行和调试,切换到调试选项卡

  4. 点击 Run Extension 启动调试,这会打开一个新的 VSCode

菜单及快捷键

package.json 详细参考

  1. 打开 package.json,找到 contributes

  2. commands 下面添加 menuskeybindingscontributes 代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    "contributes": {
    "commands": [{
    "command": "vsc-ext-test.helloWorld",
    "title": "Hello World"

    }],
    "menus": {
    "editor/context": [
    {
    "when": "editorFocus",
    "command": "vsc-ext-test.helloWorld",
    "group": "navigation"
    }
    ]
    },
    "keybindings": [
    {
    "command": "vsc-ext-test.helloWorld",
    "key": "Ctrl+F7",
    "mac": "CMD+F7",
    "when": "editorTextFocus"
    }
    ]
    },

插件打包(.vsix)

  1. 安装 vsce

    1
    npm install -g vsce
  2. 打包

    需要先修改 Readme 才可以打包

    1
    vsce package
  3. 安装

    将打包生成的 .vsix 文件拖到 VSCode 的窗口中的应用市场上完成安装

【VSCode】插件编写

https://biteax.com/7f6cd23b.html

作者

石志超

发布于

2022-01-17

更新于

2023-09-27

许可协议