1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

QuickPick 弹窗支持多选区分词

This commit is contained in:
2024-04-09 14:09:56 +08:00
parent c3199faa65
commit 47efcf56f5
5 changed files with 61 additions and 29 deletions

View File

@@ -24,6 +24,11 @@ const handleEditorReplace = (targetCase: SupportCase) => {
// 获取选中的文本
const textList = selections.map(selection => document.getText(selection));
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 convertedList = textList.map(text => caseConversion(targetCase, text, eol));
console.log('convertedList', convertedList);

View File

@@ -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);
}
});
});
}
}

View File

@@ -2,17 +2,36 @@ import { TransformTextResult } from "../type-definition/TransformTextResultType"
const logDebugInfo = false;
export function transformMutliLineText(multilineInput: string): Array<TransformTextResult> {
const results: Array<TransformTextResult> = [];
const lines = multilineInput.split(/\r?\n/);
/**
* 多选区分词
*
* @param multiSelectionInputs
* @returns
* @since 2024-04-03
*/
export function transformMutliSelectionText(selectionInputs: string[]): Array<TransformTextResult[]> {
return selectionInputs.map(selectionInput => transformMutliLineText(selectionInput));
}
/**
* 多行内容分词(单一选区)
*
* @param multiLineInput
* @returns
* @since 2024-04-03
*/
export function transformMutliLineText(multiLineInput: string): TransformTextResult[] {
const results: TransformTextResult[] = [];
const lines = multiLineInput.split(/\r?\n/);
for (const line of lines) {
// console.log('line', '->' + line + '<-');
results.push(transformText(line));
}
return results;
}
/**
* 分词
* 独立段落单元分词 (不包含换行)
*
* @param str
* @since 2024-04-02

View File

@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
interface ExtendedQuickPickItem extends vscode.QuickPickItem {
value: string;
value: string[];
}
// type QuickPickItemEx = ExtendedQuickPickItem | vscode.QuickPickItem;