实现 Shift+Alt+T 将选中文字转换为大写
This commit is contained in:
parent
8924b1cca3
commit
17953f6927
79
GPT.md
Normal file
79
GPT.md
Normal file
@ -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。
|
||||||
|
|
||||||
|
请注意,这只是一个基本的指南,您可能需要根据您的具体需求调整代码。如果您需要更详细的帮助或有任何问题,请随时告诉我!
|
11
package.json
11
package.json
@ -9,7 +9,9 @@
|
|||||||
"categories": [
|
"categories": [
|
||||||
"Other"
|
"Other"
|
||||||
],
|
],
|
||||||
"activationEvents": [],
|
"activationEvents": [
|
||||||
|
"onCommand:extension.convertCase"
|
||||||
|
],
|
||||||
"main": "./out/extension.js",
|
"main": "./out/extension.js",
|
||||||
"contributes": {
|
"contributes": {
|
||||||
"commands": [
|
"commands": [
|
||||||
@ -17,6 +19,13 @@
|
|||||||
"command": "text-conversion.helloWorld",
|
"command": "text-conversion.helloWorld",
|
||||||
"title": "Hello World"
|
"title": "Hello World"
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"keybindings": [
|
||||||
|
{
|
||||||
|
"command": "extension.convertCase",
|
||||||
|
"key": "shift+alt+t",
|
||||||
|
"when": "editorTextFocus"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -6,21 +6,51 @@ import * as vscode from 'vscode';
|
|||||||
// Your extension is activated the very first time the command is executed
|
// Your extension is activated the very first time the command is executed
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
|
|
||||||
// Use the console to output diagnostic information (console.log) and errors (console.error)
|
// // 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
|
// // This line of code will only be executed once when your extension is activated
|
||||||
console.log('Congratulations, your extension "text-conversion" is now active!');
|
// console.log('Congratulations, your extension "text-conversion" is now active!');
|
||||||
|
|
||||||
// The command has been defined in the package.json file
|
// // The command has been defined in the package.json file
|
||||||
// Now provide the implementation of the command with registerCommand
|
// // Now provide the implementation of the command with registerCommand
|
||||||
// The commandId parameter must match the command field in package.json
|
// // The commandId parameter must match the command field in package.json
|
||||||
let disposable = vscode.commands.registerCommand('text-conversion.helloWorld', () => {
|
// let disposable = vscode.commands.registerCommand('text-conversion.helloWorld', () => {
|
||||||
// The code you place here will be executed every time your command is executed
|
// // The code you place here will be executed every time your command is executed
|
||||||
// Display a message box to the user
|
// // Display a message box to the user
|
||||||
vscode.window.showInformationMessage('Hello World from text-conversion!');
|
// 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);
|
context.subscriptions.push(disposable);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method is called when your extension is deactivated
|
// This method is called when your extension is deactivated
|
||||||
export function deactivate() {}
|
export function deactivate() { }
|
||||||
|
|
||||||
|
function convertStringCase(str: string): string {
|
||||||
|
// 这里添加转换逻辑
|
||||||
|
return str.toUpperCase();
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user