ToB企服应用市场:ToB评测及商务社交产业平台

标题: 实现一个VSCode插件(从创建到发布) [打印本页]

作者: 八卦阵    时间: 2025-1-13 08:50
标题: 实现一个VSCode插件(从创建到发布)
实现一个自己的VSCode 插件


1. 初始化项目

  1. npm install -g yo generator-code
复制代码
  1. yo code
复制代码

2. 项目结构

  1. case-converter/
  2. ├── package.json
  3. ├── src/
  4. │   └── extension.ts
  5. ├── .vscodeignore
  6. └── README.md
复制代码
3. 实现插件功能


  1. import * as vscode from 'vscode';
  2. export function activate(context: vscode.ExtensionContext) {
  3.     // 注册转换大写命令
  4.     let toUpperCommand = vscode.commands.registerCommand('case-converter.toUpperCase', () => {
  5.         const editor = vscode.window.activeTextEditor;
  6.         if (editor) {
  7.             const selection = editor.selection;
  8.             const text = editor.document.getText(selection);
  9.             editor.edit(editBuilder => {
  10.                 editBuilder.replace(selection, text.toUpperCase());
  11.             });
  12.         }
  13.     });
  14.     // 注册转换小写命令
  15.     let toLowerCommand = vscode.commands.registerCommand('case-converter.toLowerCase', () => {
  16.         const editor = vscode.window.activeTextEditor;
  17.         if (editor) {
  18.             const selection = editor.selection;
  19.             const text = editor.document.getText(selection);
  20.             editor.edit(editBuilder => {
  21.                 editBuilder.replace(selection, text.toLowerCase());
  22.             });
  23.         }
  24.     });
  25.     context.subscriptions.push(toUpperCommand, toLowerCommand);
  26. }
  27. export function deactivate() {}
复制代码
  1. {
  2.    // 省略其他配置 。。。
  3.   "activationEvents": [
  4.     "onCommand:case-converter.toUpperCase",
  5.     "onCommand:case-converter.toLowerCase"
  6.   ],
  7.   "contributes": {
  8.     "commands": [
  9.       {
  10.         "command": "case-converter.toUpperCase",
  11.         "title": "Convert to Uppercase"
  12.       },
  13.       {
  14.         "command": "case-converter.toLowerCase",
  15.         "title": "Convert to Lowercase"
  16.       }
  17.     ],
  18.     "keybindings": [
  19.       {
  20.         "command": "case-converter.toUpperCase",
  21.         "key": "ctrl+shift+u",
  22.         "mac": "cmd+shift+u"
  23.       },
  24.       {
  25.         "command": "case-converter.toLowerCase",
  26.         "key": "ctrl+shift+l",
  27.         "mac": "cmd+shift+l"
  28.       }
  29.     ],
  30.     "menus": {
  31.       "editor/context": [
  32.         {
  33.           "when": "editorHasSelection",
  34.           "command": "case-converter.toUpperCase",
  35.           "group": "1_modification"
  36.         },
  37.         {
  38.           "when": "editorHasSelection",
  39.           "command": "case-converter.toLowerCase",
  40.           "group": "1_modification"
  41.         }
  42.       ]
  43.     }
  44.   },
  45. }
复制代码
4. 测试和运行插件

5. 发布


6. 下载自己发布的插件



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4