暂存
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
@@ -5,6 +5,36 @@ import * as assert from 'assert';
|
||||
import * as vscode from 'vscode';
|
||||
// import * as myExtension from '../../extension';
|
||||
|
||||
enum TextFormat {
|
||||
/** 独立单词 */
|
||||
SINGLE_WORD,
|
||||
/** 小驼峰 */
|
||||
CAMEL_CASE,
|
||||
/** 大驼峰 */
|
||||
PASCAL_CASE,
|
||||
/** 大写 */
|
||||
UPPER_CASE,
|
||||
/** 小写 */
|
||||
LOWER_CASE,
|
||||
}
|
||||
|
||||
interface TestCase {
|
||||
input: string
|
||||
inputType: TextFormat | Array<TextFormat>
|
||||
output: {
|
||||
camelCase: string
|
||||
}
|
||||
}
|
||||
const testCase: Array<TestCase> = [
|
||||
{
|
||||
input: '',
|
||||
inputType: [],
|
||||
output: {
|
||||
camelCase: '',
|
||||
}
|
||||
},
|
||||
];
|
||||
|
||||
suite('Extension Test Suite', () => {
|
||||
vscode.window.showInformationMessage('Start all tests.');
|
||||
|
||||
|
39
src/text-conversion.ts
Normal file
39
src/text-conversion.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 转小驼峰 to Camel Case
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
*/
|
||||
export function toCamelCase(str: string): string {
|
||||
return str.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转大驼峰 to Pascal Case
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
*/
|
||||
export function toPascalCase(str: string): string {
|
||||
return str.replace(/(^\w|_\w)/g, (g) => g.toUpperCase().replace('_', ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* 转大写 to Upper Case
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
*/
|
||||
export function toUpperCase(str: string): string {
|
||||
return str.toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转小写 to Lower Case
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
*/
|
||||
export function toLowerCase(str: string): string {
|
||||
return str.toLowerCase();
|
||||
}
|
Reference in New Issue
Block a user