三分钟带你开发VsCode插件 (单纯开发一个是真的很简单)

一、创建vscode插件的项目和配置

通过 npm 全局安装yo(Yeoman 生成器)和generator-code

npm install -g yo generator-code

创建插件的项目

yo code

extension.js入口文件实现 插入一段字的功能

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
  // 注册命令 这里的3224是你的插件名字 . 后面的helloWorld是你的启动命令
  let disposable = vscode.commands.registerCommand('3224.helloWorld', () => {
    //这个是信息提示框,输出这个信息,也就是在用了这个命令后,插件生效后输出这些
    vscode.window.showInformationMessage('我真的启动了吗,不可思议');

    //接下来就是功能实现了
    const editor = vscode.window.activeTextEditor; //获取当前活动编辑器(官方话)
    // 这个就是编辑的函数了
    editor.edit((build)=>{
    // 插入一段文字 selection.end  意思就是光标后面插入 “佳丽犬”这三个字
	build.insert(editor.selection.end, "佳丽犬") 
    })
  });
}

export function deactivate() {} // 插件停用逻辑

 实现右键能够使用命令 Package.json 文件下

{
   // 这个你们创建肯定没有,这个是发布的适合添加的你们的id
  "publisher": "chen-wen-plugin-publisher", 
  "name": "3224",
  "displayName": "3224",
  "description": "22223",
  "version": "0.0.1",
  "engines": {
    "vscode": "^1.100.0"
  },
  "categories": [
    "Other"
  ],
  "activationEvents": [],
  "main": "./extension.js",
  "contributes": {
    "commands": [{
      "command": "3224.helloWorld",
      "title": "Hello World"
    }],
  //============================================ 就这一块代码添加进去就好了
     "menus": {
      "editor/context": [  
        {
          "command": "3224.helloWorld", // 这个一定要和你注册的命令一样
          "when": "editorTextFocus",  // 这个就是那个右键是否显示命令了
          "group": "1_modification"
        }
      ]
    }
  //============================================
  },
  "scripts": {
    "lint": "eslint .",
    "pretest": "npm run lint",
    "test": "vscode-test"
  },
  "devDependencies": {
    "@types/vscode": "^1.100.0",
    "@types/mocha": "^10.0.10",
    "@types/node": "20.x",
    "eslint": "^9.25.1",
    "@vscode/test-cli": "^0.0.10",
    "@vscode/test-electron": "^2.5.2"
  }
}

二、测试插件

1.按下F5 启动调试模式,他会弹出一个新的vscode

2.ctrl+shift+p / 右键  helloword 就可以启用命令,但是我们那个插入需要又一个文件来选中有了光标后才能插入所以,先随便打开一个文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值