1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
zhangbk1
2024-03-28 19:03:31 +08:00
parent 17953f6927
commit be9952f6bc
6 changed files with 226 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as TextConversion from './text-conversion';
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
@@ -19,7 +20,7 @@ export function activate(context: vscode.ExtensionContext) {
// vscode.window.showInformationMessage('Hello World from text-conversion!');
// });
let disposable = vscode.commands.registerCommand('extension.convertCase', () => {
const handleEditorReplace = (convertFunction: (selectionText: string) => string) => {
// 获取当前编辑器
let editor = vscode.window.activeTextEditor;
if (editor) {
@@ -29,7 +30,7 @@ export function activate(context: vscode.ExtensionContext) {
// 获取选中的文本
let text = document.getText(selection);
// 转换文本
let converted = convertStringCase(text);
let converted = convertFunction(text);
// 当转换后文本与转换前相同时,跳过转换
if (text === converted) {
@@ -38,19 +39,34 @@ export function activate(context: vscode.ExtensionContext) {
}
// 替换文本
console.log('replace selection text', text, 'to', converted);
editor.edit(editBuilder => {
editBuilder.replace(selection, converted);
});
}
});
};
let disposableCamelCase = vscode.commands.registerCommand('extension.toCamelCase', () => {
handleEditorReplace(TextConversion.toCamelCase);
});
context.subscriptions.push(disposableCamelCase);
let disposablePascalCase = vscode.commands.registerCommand('extension.toPascalCase', () => {
handleEditorReplace(TextConversion.toPascalCase);
});
context.subscriptions.push(disposablePascalCase);
let disposableUpperCase = vscode.commands.registerCommand('extension.toUpperCase', () => {
handleEditorReplace(TextConversion.toUpperCase);
});
context.subscriptions.push(disposableUpperCase);
let disposableLowerCase = vscode.commands.registerCommand('extension.toLowerCase', () => {
handleEditorReplace(TextConversion.toLowerCase);
});
context.subscriptions.push(disposableLowerCase);
context.subscriptions.push(disposable);
}
// This method is called when your extension is deactivated
export function deactivate() { }
function convertStringCase(str: string): string {
// 这里添加转换逻辑
return str.toUpperCase();
}