|
|
|
@@ -2,14 +2,15 @@ import * as vscode from 'vscode';
|
|
|
|
|
import QuickPickItemEx from "../type-definition/QuickPickItemExType";
|
|
|
|
|
import { quickPickSupportCases } from '../type-definition/SupportCaseType';
|
|
|
|
|
import { TransformTextResult } from '../type-definition/TransformTextResultType';
|
|
|
|
|
import { transformMutliLineText } from '../main-code/transform';
|
|
|
|
|
import { transformMutliLineText, transformMutliSelectionText } from '../main-code/transform';
|
|
|
|
|
import { EOL } from '../type-definition/EOLType';
|
|
|
|
|
import { caseConversion } from '../main-code/conversion';
|
|
|
|
|
import { isStringArrayEqual } from '../main-code/utils';
|
|
|
|
|
|
|
|
|
|
const QuickPickLabelMaxLength = 60;
|
|
|
|
|
|
|
|
|
|
interface RecommendItem {
|
|
|
|
|
conversionText: string
|
|
|
|
|
conversionText: Array<string>
|
|
|
|
|
transforTo: string[]
|
|
|
|
|
keyword: string[]
|
|
|
|
|
}
|
|
|
|
@@ -17,18 +18,24 @@ interface RecommendItem {
|
|
|
|
|
/**
|
|
|
|
|
* 弹出的提示
|
|
|
|
|
*/
|
|
|
|
|
function generateOptionsBasedOnText(text: string, eol: EOL): Array<QuickPickItemEx> {
|
|
|
|
|
function generateOptionsBasedOnText(textList: string[], eol: EOL): Array<QuickPickItemEx> {
|
|
|
|
|
// Cut text 切割文本
|
|
|
|
|
const results: Array<TransformTextResult> = transformMutliLineText(text);
|
|
|
|
|
const resultsList: Array<TransformTextResult[]> = transformMutliSelectionText(textList);
|
|
|
|
|
|
|
|
|
|
const mergeResultList: Array<RecommendItem> = [];
|
|
|
|
|
for (const quickPick of quickPickSupportCases) {
|
|
|
|
|
const conversionResult: string = caseConversion(quickPick.type, text, eol, results);
|
|
|
|
|
const recommendItem: RecommendItem | undefined = mergeResultList.find(item => item.conversionText === conversionResult);
|
|
|
|
|
const conversionResults: Array<string> = [];
|
|
|
|
|
for (let i = 0; i < textList.length; i++) {
|
|
|
|
|
const text = textList[i];
|
|
|
|
|
const results = resultsList[i];
|
|
|
|
|
const conversionResult: string = caseConversion(quickPick.type, text, eol, results);
|
|
|
|
|
conversionResults.push(conversionResult);
|
|
|
|
|
}
|
|
|
|
|
const recommendItem: RecommendItem | undefined = mergeResultList.find(item => isStringArrayEqual(item.conversionText, conversionResults));
|
|
|
|
|
|
|
|
|
|
if (recommendItem === undefined) {
|
|
|
|
|
let item: RecommendItem = {
|
|
|
|
|
conversionText: conversionResult,
|
|
|
|
|
conversionText: conversionResults,
|
|
|
|
|
transforTo: [quickPick.shortName], // quickPick.name
|
|
|
|
|
keyword: quickPick.keyword,
|
|
|
|
|
};
|
|
|
|
@@ -52,10 +59,10 @@ function generateOptionsBasedOnText(text: string, eol: EOL): Array<QuickPickItem
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (const recommendItem of mergeResultList) {
|
|
|
|
|
if (text === recommendItem.conversionText) {
|
|
|
|
|
if (isStringArrayEqual(textList, recommendItem.conversionText)) {
|
|
|
|
|
continue; // 如果转换后与转换前相同,那么跳过这一项
|
|
|
|
|
}
|
|
|
|
|
const conversionTextForDisplay = recommendItem.conversionText.trim();
|
|
|
|
|
const conversionTextForDisplay = recommendItem.conversionText.join(' ').replace(/\s+/g, " "); // 友好展示 将连续空格 \n \t 等替换为单一空格
|
|
|
|
|
let quickPickItem: QuickPickItemEx = {
|
|
|
|
|
label: conversionTextForDisplay.length >= QuickPickLabelMaxLength
|
|
|
|
|
? (conversionTextForDisplay.substring(0, QuickPickLabelMaxLength - 3) + '...')
|
|
|
|
@@ -76,20 +83,20 @@ export function handleQuickPick() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let document = editor.document;
|
|
|
|
|
let selection = editor.selection;
|
|
|
|
|
let eol: EOL = document.eol === vscode.EndOfLine.CRLF ? '\r\n' : '\n';
|
|
|
|
|
const document = editor.document;
|
|
|
|
|
const selections = editor.selections;
|
|
|
|
|
const eol: EOL = document.eol === vscode.EndOfLine.CRLF ? '\r\n' : '\n';
|
|
|
|
|
|
|
|
|
|
// 获取选中的文本
|
|
|
|
|
let text = document.getText(selection);
|
|
|
|
|
const textList = selections.map(selection => document.getText(selection));
|
|
|
|
|
|
|
|
|
|
if (text.length === 0) {
|
|
|
|
|
if (textList.filter(text => text.length > 0).length === 0) {
|
|
|
|
|
vscode.window.showInformationMessage('请选择需要转换的变量后重试\nPlease select the variable you want to convert and try again.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 基于选中的文本生成选项
|
|
|
|
|
const options = generateOptionsBasedOnText(text, eol);
|
|
|
|
|
const options = generateOptionsBasedOnText(textList, eol);
|
|
|
|
|
// 显示推荐项列表
|
|
|
|
|
vscode.window.showQuickPick(options, {
|
|
|
|
|
matchOnDetail: true,
|
|
|
|
@@ -100,21 +107,22 @@ export function handleQuickPick() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理用户的选择
|
|
|
|
|
// vscode.window.showInformationMessage(`你选择了: ${JSON.stringify(selection)}`);
|
|
|
|
|
|
|
|
|
|
const converted = pickItem.value;
|
|
|
|
|
const convertedList = pickItem.value;
|
|
|
|
|
|
|
|
|
|
// 当转换后文本与转换前相同时,跳过转换,避免形成 Ctrl + Z 撤销历史记录
|
|
|
|
|
if (converted === text) {
|
|
|
|
|
if (isStringArrayEqual(convertedList, textList)) {
|
|
|
|
|
console.log('selection text is same to converted text, skip replace contents.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 替换文本
|
|
|
|
|
console.log('replace selection text', text, 'to', converted);
|
|
|
|
|
console.log('replace selection text', textList, 'to', convertedList);
|
|
|
|
|
editor.edit(editBuilder => {
|
|
|
|
|
editBuilder.replace(selection, converted);
|
|
|
|
|
for (let i = 0; i < selections.length; i++) {
|
|
|
|
|
const selection = selections[i];
|
|
|
|
|
const converted = convertedList[i];
|
|
|
|
|
editBuilder.replace(selection, converted);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|