当前位置: 首页 > news >正文

制作一个简单的vscode插件

当前环境情况

操作系统:Windows
项目类型:VS Code 插件(TypeScript 编写)
Node.js 版本:20.18.1
yarn 版本:1.22.22
npm 版本:10.8.2
npm registry:huawei ------- https://repo.huaweicloud.com/repository/npm/

安装官方的脚手架来初始化项目

  • 安装官方手脚架
    yarn global add yo generator-code
    
  • 初始化项目
    yo code
    
    然后根据提示,填写基本项目基本内容,我这里选择了 TypeScript

项目结构

hello-vscode-plugin/
├── package.json            # 项目元数据、依赖、脚本及 VS Code 插件配置
├── src/                    # TypeScript 源代码目录
│   └── extension.ts        # 插件主入口,核心功能实现
├── out/                    # 编译后的 JavaScript 文件目录
│   └── extension.js        # 编译后的插件主入口文件
├── node_modules/           # 项目依赖包目录,由 npm 自动生成
├── .vscode/                # VS Code 编辑器相关配置
│   └── settings.json       # 工作区级别的 VS Code 设置
├── tsconfig.json           # TypeScript 编译配置文件
├── .eslintrc.js            # ESLint 代码规范配置文件
├── README.md               # 项目说明文档,介绍插件功能和使用方法
├── LICENSE.md               # MIT 许可证,MIT 许可证是一种宽松、开放源代码的许可证,适合个人或企业自由使用和二次开发

重要文件内容

extension.ts

import * as vscode from "vscode";
import { toLowerCamelCase } from "./utils";export function activate(context: vscode.ExtensionContext) {console.log('Congratulations, your extension "extensionproject" is now active!');const disposable = vscode.commands.registerTextEditorCommand("hello-vscode-plugin.toLowerCamelCase",(textEditor, edit) => {if (textEditor.selection.isEmpty) {// 未选中文本直接返回return;}const textRange = new vscode.Range(textEditor.selection.start,textEditor.selection.end);const text = textEditor.document.getText(textRange);edit.replace(textRange, toLowerCamelCase(text));});context.subscriptions.push(disposable);
}export function deactivate() {}

utils/index.ts

export const toLowerCamelCase = (str: string) => {if (str.length < 2) {return str.toLowerCase();}const [firstWords, ...otherWords] = str.split("_");return (firstWords.toLowerCase() +otherWords.filter((word) => !!word).map((word) =>word[0].toUpperCase() + word.slice(1, word.length).toLowerCase()).join(""));
};

package.json

{"name": "hello-vscode-plugin","displayName": "hello-vscode-plugin","description": "将枚举等文本快速转换为 lowerCamelCase 格式的 VS Code 扩展。","publisher": "Jacky","version": "0.0.2","license": "MIT","repository": {"type": "git","url": "https://git.com/jacky/hello-vscode-plugin"},"engines": {"vscode": "^1.103.0"},"categories": ["Other"],"activationEvents": ["onCommand:hello-vscode-plugin.toLowerCamelCase"],"main": "./out/extension.js","contributes": {"menus": {"editor/context": [{"when": "editorFocus","command": "hello-vscode-plugin.toLowerCamelCase"}]},"commands": [{"command": "hello-vscode-plugin.toLowerCamelCase","title": "toLowerCamelCase","category": "hello-vscode-plugin"}]},"scripts": {"vscode:prepublish": "npm run compile","compile": "tsc -p ./","watch": "tsc -watch -p ./","pretest": "npm run compile && npm run lint","lint": "eslint src","test": "vscode-test"},"devDependencies": {"@types/vscode": "^1.103.0","@types/mocha": "^10.0.10","@types/node": "22.x","@typescript-eslint/eslint-plugin": "^8.39.0","@typescript-eslint/parser": "^8.39.0","eslint": "^9.32.0","typescript": "^5.9.2","@vscode/test-cli": "^0.0.11","@vscode/test-electron": "^2.5.2"}
}

LICENSE.md

# MIT LicenseCopyright (c) 2025 JackyPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

README.md

# hello-vscode-plugin将枚举等文本快速转换为 lowerCamelCase 格式的 VS Code 扩展。## Features- 支持将选中的文本转换为 lowerCamelCase 格式
- 通过右键菜单快速访问转换功能
- 适用于枚举值、变量名等需要转换为驼峰命名的场景## Usage1. 在编辑器中选择需要转换的文本
2. 右键点击选中的文本
3. 选择 "toLowerCamelCase" 选项
4. 文本将自动转换为 lowerCamelCase 格式## Release Notes### 0.0.1Initial release of hello-vscode-plugin extension.---## Following extension guidelinesEnsure that you've read through the extensions guidelines and follow the best practices for creating your extension.* [Extension Guidelines](https://code.visualstudio.com/api/references/extension-guidelines)## Working with MarkdownYou can author your README using Visual Studio Code. Here are some useful editor keyboard shortcuts:* Split the editor (`Cmd+\` on macOS or `Ctrl+\` on Windows and Linux).
* Toggle preview (`Shift+Cmd+V` on macOS or `Shift+Ctrl+V` on Windows and Linux).
* Press `Ctrl+Space` (Windows, Linux, macOS) to see a list of Markdown snippets.## For more information* [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown)
* [Markdown Syntax Reference](https://help.github.com/articles/markdown-basics/)**Enjoy!**

调试

  • 在当前项目中,按F5进入调试模式
  • 然后在其他项目中,进行大写转小驼峰,选中大写内容:
  • 在命令面板(cmd + P)(或在工作台顶部上面进行搜索)中输入 >toLowerCamelCase 点击下方出现的命令后就可以在右下角看到弹出框了
    在这里插入图片描述
    ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/52b60dbcde9f43c784a27f1a0b218137.png
    正常转换就行
    在这里插入图片描述

打包 vsix

1. 安装打包工具

首先需要安装 VS Code 官方提供的打包工具 vsce(Visual Studio Code Extension Manager):

npm install -g @vscode/vsce

2. 准备打包

在打包前,确保插件项目根目录下的 package.json 文件配置正确,特别是以下字段:

name:插件名称(必须唯一)
version:版本号(每次发布需递增)
engines.vscode:支持的 VS Code 版本范围

3. 执行打包命令

在插件项目的根目录(package.json 所在目录)执行:

vsce package

4. 获取 VSIX 文件

打包成功后,会在当前目录生成一个 .vsix 文件,文件名格式为:
[插件名称]-[版本号].vsix(例如 my-extension-0.0.1.vsix)

5. 安装测试(可选)

可以手动安装生成的 VSIX 文件验证:

  1. 在 VS Code 中打开「扩展」面板(Ctrl+Shift+X)
  2. 点击右上角的「…」菜单,选择「从 VSIX 安装」
  3. 选择生成的 .vsix 文件完成安装

注意事项

  • 如果插件依赖外部资源,需确保在 package.json 的 files 字段中声明需要包含的文件 / 目录
  • 若出现权限错误,在 macOS/Linux 系统可尝试添加 sudo,Windows 系统建议以管理员身份运行命令行
  • 如需发布到 VS Code 市场,需先注册 Azure DevOps 账号 并获取发布令牌

文章转载自:

http://5a7mmnog.rbtny.cn
http://0kPiei2R.rbtny.cn
http://5LDamVDY.rbtny.cn
http://sDrYYkgX.rbtny.cn
http://6R5uLjkf.rbtny.cn
http://bA851dLV.rbtny.cn
http://qAqmPnDJ.rbtny.cn
http://TRWW7lId.rbtny.cn
http://zDjghtUx.rbtny.cn
http://zjmL3oUF.rbtny.cn
http://KjrMrSvj.rbtny.cn
http://erqHC0XL.rbtny.cn
http://WneC53qg.rbtny.cn
http://VDJ01VwU.rbtny.cn
http://SAuf8zhl.rbtny.cn
http://iQ8KgZhB.rbtny.cn
http://q8DObZZg.rbtny.cn
http://k6oWdLt2.rbtny.cn
http://Q8Sm8rFI.rbtny.cn
http://cqzczg2e.rbtny.cn
http://WTcDeFK2.rbtny.cn
http://tkVRSsqt.rbtny.cn
http://2PlQW6jW.rbtny.cn
http://2I9KPQGo.rbtny.cn
http://HOJSKCQw.rbtny.cn
http://045xXNBm.rbtny.cn
http://AGMvroTz.rbtny.cn
http://9szxbIH4.rbtny.cn
http://VnUBJ7iM.rbtny.cn
http://bWpRD1q2.rbtny.cn
http://www.dtcms.com/a/381467.html

相关文章:

  • 【算法详解】:从 模拟 开始打开算法密匙
  • kubeadm搭建生产环境的单master多node的k8s集群
  • RocketMQ存储核心:MappedFile解析
  • 7.k8s四层代理service
  • Stable Virtual Camera:Stability AI等推出的AI模型 ,2D图像轻松转3D视频
  • Golang并发编程及其高级特性
  • 给AI配一台手机+电脑?智谱AutoGLM上线!
  • 怎么在手机上选择一款好用的桌面待办清单工具
  • 傲琪人工合成石墨片:破解智能手机散热困境的创新解决方案
  • LeetCode 刷题【74. 搜索二维矩阵、75. 颜色分类、76. 最小覆盖子串】
  • 【Linux】【实战向】Linux 进程替换避坑指南:从理解 bash 阻塞等待,到亲手实现能执行 ls/cd 的 Shell
  • SRE 系列(七)| 从技术架构到团队组织
  • 网络安全-vulnhub-Web developer 1
  • 国产延时芯片EH3B05上电延时3秒开关机芯片方案超低功耗
  • vivado下载程序后不弹出ila窗口
  • 【VC】 error MSB8041: 此项目需要 MFC 库
  • S7-200 SMART PLC 安全全指南:配置、漏洞解析与复现防护
  • 点可云进销存商城如何部署在微信小程序
  • 安卓学习 之 界面切换
  • 从 IDE 到 CLI:AI 编程代理工具全景与落地指南(附对比矩阵与脚本化示例)
  • 王道数据结构 学习笔记
  • 畅阅读小程序|畅阅读系统|基于java的畅阅读系统小程序设计与实现(源码+数据库+文档)
  • 在springboot中使用mock做controller层单元测试,请求示例包括GET(带参数)、POST(带请求头)、下载文件、上传文件等
  • Kafka 线上问题排查完整手册
  • 数据结构中的排序秘籍:从基础到进阶的全面解析
  • NFS 服务器 使用
  • Zookeeper:分布式协调服务
  • 在 R 语言里,`$` 只有一个作用 按名字提取“列表型”对象里的单个元素 对象 $ 名字
  • 【pure-admin】项目登录模块分析
  • 关于Redis不同序列化压缩性能的对比