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

右键菜单支持多选区转换

This commit is contained in:
程序员小墨 2024-04-09 10:17:10 +08:00
parent d45c3a5f41
commit ab1d6770bd

View File

@ -2,6 +2,7 @@ import * as vscode from 'vscode';
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 { SupportCase } from '../type-definition/SupportCaseType'; import { SupportCase } from '../type-definition/SupportCaseType';
import { isStringArrayEqual } from '../main-code/utils';
/** /**
* *
@ -16,33 +17,37 @@ const handleEditorReplace = (targetCase: SupportCase) => {
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));
// 转换文本 // 转换文本
const converted = caseConversion(targetCase, text, eol); const convertedList = textList.map(text => caseConversion(targetCase, text, eol));
console.log('converted', converted); console.log('convertedList', convertedList);
// 无法转换时,跳过转换 // 无法转换时,跳过转换
if (converted === undefined) { if (convertedList.filter(converted => converted !== undefined).length === 0) {
console.log('converted text is undefined, skip replace contents.'); console.log('converted text is undefined, skip replace contents.');
return; return;
} }
// 当转换后文本与转换前相同时,跳过转换,避免形成 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);
}
}); });
}; };