From 17953f6927a42e96c82c39da15f4e1a604559b48 Mon Sep 17 00:00:00 2001 From: zhangbk1 Date: Thu, 28 Mar 2024 14:45:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20Shift+Alt+T=20=E5=B0=86?= =?UTF-8?q?=E9=80=89=E4=B8=AD=E6=96=87=E5=AD=97=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E5=A4=A7=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GPT.md | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 11 ++++++- src/extension.ts | 52 ++++++++++++++++++++++++------- 3 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 GPT.md diff --git a/GPT.md b/GPT.md new file mode 100644 index 0000000..743797e --- /dev/null +++ b/GPT.md @@ -0,0 +1,79 @@ +请帮我用js写一个小驼峰,大驼峰,下划线写法互相转换的VSCode插件。实现按shift+alt+t自动在这几种模式下切换。我完全没有写过VSCode插件,所以你可能需要详细告诉我每一步该怎么做,谢谢你 + +当然可以帮助您!创建一个VSCode插件涉及几个步骤,包括设置项目结构、编写代码以及打包和发布插件。以下是创建您所需插件的基本步骤: + +1. **安装必要的软件**: + - 安装最新版本的[Node.js](https://nodejs.org/)。 + - 安装[Visual Studio Code](https://code.visualstudio.com/)。 + +2. **设置插件项目**: + - 打开VSCode,安装`Yeoman`和`VS Code Extension Generator`工具,通过运行以下命令: + ```bash + npm install -g yo generator-code + ``` + - 运行生成器来创建新的插件项目: + ```bash + yo code + ``` + - 按照提示操作,选择**TypeScript**作为项目语言,填写插件的名称和其他信息。 + +3. **编写插件代码**: + - 在生成的项目中,打开`src/extension.ts`文件,这是插件的主要代码文件。 + - 编写一个函数来转换字符串格式: + ```typescript + function convertStringCase(str: string): string { + // 这里添加转换逻辑 + return str; + } + ``` + - 注册一个命令,当用户按下`shift+alt+t`时触发字符串转换: + ```typescript + import * as vscode from 'vscode'; + + export function activate(context: vscode.ExtensionContext) { + let disposable = vscode.commands.registerCommand('extension.convertCase', () => { + // 获取当前编辑器 + let editor = vscode.window.activeTextEditor; + if (editor) { + let document = editor.document; + let selection = editor.selection; + + // 获取选中的文本 + let text = document.getText(selection); + // 转换文本 + let converted = convertStringCase(text); + + // 替换文本 + editor.edit(editBuilder => { + editBuilder.replace(selection, converted); + }); + } + }); + + context.subscriptions.push(disposable); + } + ``` + - 在`package.json`中添加键绑定: + ```json + "contributes": { + "keybindings": [ + { + "command": "extension.convertCase", + "key": "shift+alt+t", + "when": "editorTextFocus" + } + ] + } + ``` + +4. **测试插件**: + - 在VSCode中按下`F5`来运行和测试您的插件。 + +5. **打包和发布**: + - 使用`vsce`工具来打包您的插件: + ```bash + vsce package + ``` + - 按照[官方文档](https://code.visualstudio.com/api/working-with-extensions/publishing-extension)发布到VSCode Marketplace。 + +请注意,这只是一个基本的指南,您可能需要根据您的具体需求调整代码。如果您需要更详细的帮助或有任何问题,请随时告诉我! \ No newline at end of file diff --git a/package.json b/package.json index 28e1cb1..e5c36cb 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,9 @@ "categories": [ "Other" ], - "activationEvents": [], + "activationEvents": [ + "onCommand:extension.convertCase" + ], "main": "./out/extension.js", "contributes": { "commands": [ @@ -17,6 +19,13 @@ "command": "text-conversion.helloWorld", "title": "Hello World" } + ], + "keybindings": [ + { + "command": "extension.convertCase", + "key": "shift+alt+t", + "when": "editorTextFocus" + } ] }, "scripts": { diff --git a/src/extension.ts b/src/extension.ts index efe771a..03104db 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,21 +6,51 @@ import * as vscode from 'vscode'; // Your extension is activated the very first time the command is executed export function activate(context: vscode.ExtensionContext) { - // Use the console to output diagnostic information (console.log) and errors (console.error) - // This line of code will only be executed once when your extension is activated - console.log('Congratulations, your extension "text-conversion" is now active!'); + // // Use the console to output diagnostic information (console.log) and errors (console.error) + // // This line of code will only be executed once when your extension is activated + // console.log('Congratulations, your extension "text-conversion" is now active!'); - // The command has been defined in the package.json file - // Now provide the implementation of the command with registerCommand - // The commandId parameter must match the command field in package.json - let disposable = vscode.commands.registerCommand('text-conversion.helloWorld', () => { - // The code you place here will be executed every time your command is executed - // Display a message box to the user - vscode.window.showInformationMessage('Hello World from text-conversion!'); + // // The command has been defined in the package.json file + // // Now provide the implementation of the command with registerCommand + // // The commandId parameter must match the command field in package.json + // let disposable = vscode.commands.registerCommand('text-conversion.helloWorld', () => { + // // The code you place here will be executed every time your command is executed + // // Display a message box to the user + // vscode.window.showInformationMessage('Hello World from text-conversion!'); + // }); + + let disposable = vscode.commands.registerCommand('extension.convertCase', () => { + // 获取当前编辑器 + let editor = vscode.window.activeTextEditor; + if (editor) { + let document = editor.document; + let selection = editor.selection; + + // 获取选中的文本 + let text = document.getText(selection); + // 转换文本 + let converted = convertStringCase(text); + + // 当转换后文本与转换前相同时,跳过转换 + if (text === converted) { + console.log('selection text is same to converted text, skip replace contents.'); + return; + } + + // 替换文本 + editor.edit(editBuilder => { + editBuilder.replace(selection, converted); + }); + } }); context.subscriptions.push(disposable); } // This method is called when your extension is deactivated -export function deactivate() {} +export function deactivate() { } + +function convertStringCase(str: string): string { + // 这里添加转换逻辑 + return str.toUpperCase(); +}