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

@@ -57,7 +57,7 @@ Or right-click on the selected text -> Convert string to...
| 功能 Feature | 快捷键 shortcut key | | 功能 Feature | 快捷键 shortcut key |
| ------------------------------------------------ | ------------------- | | ------------------------------------------------ | ------------------- |
| 变量转换 快速选择 QuickPick | | | 变量转换 快速选择 QuickPick | Shift + Alt + T |
| 循环转换→上一个 Cyclic conversion → Previous one | Ctrl + Alt + [ | | 循环转换→上一个 Cyclic conversion → Previous one | Ctrl + Alt + [ |
| 循环转换→下一个 Cyclic conversion → Next one | Ctrl + Alt + ] | | 循环转换→下一个 Cyclic conversion → Next one | Ctrl + Alt + ] |

View File

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

View File

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

View File

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