From ab1d6770bd3ce80cf5c740722ccbae0cd63eaf00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Tue, 9 Apr 2024 10:17:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=B3=E9=94=AE=E8=8F=9C=E5=8D=95=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E9=80=89=E5=8C=BA=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor-submenu-handler.ts | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/extension-handler/editor-submenu-handler.ts b/src/extension-handler/editor-submenu-handler.ts index d190e01..b4df17f 100644 --- a/src/extension-handler/editor-submenu-handler.ts +++ b/src/extension-handler/editor-submenu-handler.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import { EOL } from '../type-definition/EOLType'; import { caseConversion } from '../main-code/conversion'; import { SupportCase } from '../type-definition/SupportCaseType'; +import { isStringArrayEqual } from '../main-code/utils'; /** * 编辑器右键菜单 @@ -16,33 +17,37 @@ const handleEditorReplace = (targetCase: SupportCase) => { 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)); // 转换文本 - const converted = caseConversion(targetCase, text, eol); - console.log('converted', converted); + const convertedList = textList.map(text => caseConversion(targetCase, text, eol)); + console.log('convertedList', convertedList); // 无法转换时,跳过转换 - if (converted === undefined) { + if (convertedList.filter(converted => converted !== undefined).length === 0) { console.log('converted text is undefined, skip replace contents.'); return; } // 当转换后文本与转换前相同时,跳过转换,避免形成 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); + } }); };