From 7d9de4b0d2b5fbaecc5f21004e989c6c53035a89 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 7 Dec 2025 18:16:09 +0800 Subject: [PATCH 1/6] Fix index lookup for boundary --- Flow.Launcher.Infrastructure/TranslationMapping.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/TranslationMapping.cs b/Flow.Launcher.Infrastructure/TranslationMapping.cs index b4c6764df1a..e7044307761 100644 --- a/Flow.Launcher.Infrastructure/TranslationMapping.cs +++ b/Flow.Launcher.Infrastructure/TranslationMapping.cs @@ -21,7 +21,7 @@ public void AddNewIndex(int translatedIndex, int length) public int MapToOriginalIndex(int translatedIndex) { var searchResult = _originalToTranslated.BinarySearch(translatedIndex); - return searchResult >= 0 ? searchResult : ~searchResult; + return searchResult >= 0 ? searchResult + 1 : ~searchResult; } public void EndConstruct() From b869eb8d1d2a6e0c342379fae579aa1747754cc4 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 7 Dec 2025 18:50:54 +0800 Subject: [PATCH 2/6] Avoid adding unnecessary space and improve unit test --- .../PinyinAlphabet.cs | 27 ++++++++------- Flow.Launcher.Test/TranslationMappingTest.cs | 33 +++++++++++++------ 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index 1c0cc6872ff..7f55d890992 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -27,7 +27,7 @@ public PinyinAlphabet() { switch (e.PropertyName) { - case nameof (Settings.ShouldUsePinyin): + case nameof(Settings.ShouldUsePinyin): if (_settings.ShouldUsePinyin) { Reload(); @@ -52,7 +52,7 @@ public void Reload() private void CreateDoublePinyinTableFromStream(Stream jsonStream) { - var table = JsonSerializer.Deserialize>>(jsonStream) ?? + var table = JsonSerializer.Deserialize>>(jsonStream) ?? throw new InvalidOperationException("Failed to deserialize double pinyin table: result is null"); var schemaKey = _settings.DoublePinyinSchema.ToString(); @@ -128,12 +128,12 @@ public bool ShouldTranslate(string stringToTranslate) if (IsChineseCharacter(content[i])) { var translated = _settings.UseDoublePinyin ? ToDoublePinyin(resultList[i]) : resultList[i]; - - if (i > 0) + + if (i > 0 && content[i - 1] != ' ') { resultBuilder.Append(' '); } - + map.AddNewIndex(resultBuilder.Length, translated.Length); resultBuilder.Append(translated); previousIsChinese = true; @@ -144,11 +144,14 @@ public bool ShouldTranslate(string stringToTranslate) if (previousIsChinese) { previousIsChinese = false; - resultBuilder.Append(' '); + if (content[i] != ' ') + { + resultBuilder.Append(' '); + } } - - map.AddNewIndex(resultBuilder.Length, resultList[i].Length); - resultBuilder.Append(resultList[i]); + + map.AddNewIndex(resultBuilder.Length, 1); + resultBuilder.Append(content[i]); } } @@ -156,7 +159,7 @@ public bool ShouldTranslate(string stringToTranslate) var translation = resultBuilder.ToString(); var result = (translation, map); - + return _pinyinCache[content] = result; } @@ -185,8 +188,8 @@ private static bool IsChineseCharacter(char c) private string ToDoublePinyin(string fullPinyin) { - return currentDoublePinyinTable.TryGetValue(fullPinyin, out var doublePinyinValue) - ? doublePinyinValue + return currentDoublePinyinTable.TryGetValue(fullPinyin, out var doublePinyinValue) + ? doublePinyinValue : fullPinyin; } } diff --git a/Flow.Launcher.Test/TranslationMappingTest.cs b/Flow.Launcher.Test/TranslationMappingTest.cs index bd3636f0ad8..ab02b23b2a8 100644 --- a/Flow.Launcher.Test/TranslationMappingTest.cs +++ b/Flow.Launcher.Test/TranslationMappingTest.cs @@ -22,19 +22,32 @@ public void AddNewIndex_ShouldAddTranslatedIndexPlusLength() ClassicAssert.AreEqual(10, GetOriginalToTranslatedAt(mapping, 1)); } - [TestCase(0, 0)] - [TestCase(2, 1)] - [TestCase(3, 1)] - [TestCase(5, 2)] - [TestCase(6, 2)] + + [TestCase(0, 0)] // "F" -> "F" + [TestCase(1, 1)] // "l" -> "l" + [TestCase(2, 2)] // "o" -> "o" + [TestCase(3, 3)] // "w" -> "w" + [TestCase(4, 4)] // " " -> " " + [TestCase(5, 5)] // "Y" (translated from "用") -> original index 5 + [TestCase(6, 5)] // "o" (translated from "用") -> original index 5 + [TestCase(7, 5)] // "n" (translated from "用") -> original index 5 + [TestCase(8, 5)] // "g" (translated from "用") -> original index 5 + [TestCase(10, 6)] // "H" (translated from "户") -> original index 6 public void MapToOriginalIndex_ShouldReturnExpectedIndex(int translatedIndex, int expectedOriginalIndex) { var mapping = new TranslationMapping(); - // a测试 - // a Ce Shi - mapping.AddNewIndex(0, 1); - mapping.AddNewIndex(2, 2); - mapping.AddNewIndex(5, 3); + // Test case : + // 0123456 + // Flow 用户 + // 0123456789012 + // Flow Yong Hu" + mapping.AddNewIndex(0, 1); // F + mapping.AddNewIndex(1, 1); // l + mapping.AddNewIndex(2, 1); // o + mapping.AddNewIndex(3, 1); // w + mapping.AddNewIndex(4, 1); // ' ' + mapping.AddNewIndex(5, 4); // 用 -> Yong + mapping.AddNewIndex(11, 2); // 户 -> Hu var result = mapping.MapToOriginalIndex(translatedIndex); ClassicAssert.AreEqual(expectedOriginalIndex, result); From a7c67bbb35b2ab49778b534867c196d46661fe09 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 7 Dec 2025 19:18:49 +0800 Subject: [PATCH 3/6] Fix incorrect index marker Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Flow.Launcher.Test/TranslationMappingTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Test/TranslationMappingTest.cs b/Flow.Launcher.Test/TranslationMappingTest.cs index ab02b23b2a8..be208d59961 100644 --- a/Flow.Launcher.Test/TranslationMappingTest.cs +++ b/Flow.Launcher.Test/TranslationMappingTest.cs @@ -39,8 +39,8 @@ public void MapToOriginalIndex_ShouldReturnExpectedIndex(int translatedIndex, in // Test case : // 0123456 // Flow 用户 - // 0123456789012 - // Flow Yong Hu" + // 012345678901 + // Flow Yong Hu mapping.AddNewIndex(0, 1); // F mapping.AddNewIndex(1, 1); // l mapping.AddNewIndex(2, 1); // o From 769179d04b48f06c3998942c83020a56fd5be6aa Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 7 Dec 2025 19:28:17 +0800 Subject: [PATCH 4/6] Fix test case Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Flow.Launcher.Test/TranslationMappingTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Test/TranslationMappingTest.cs b/Flow.Launcher.Test/TranslationMappingTest.cs index be208d59961..bcbbf8bf8a9 100644 --- a/Flow.Launcher.Test/TranslationMappingTest.cs +++ b/Flow.Launcher.Test/TranslationMappingTest.cs @@ -47,7 +47,7 @@ public void MapToOriginalIndex_ShouldReturnExpectedIndex(int translatedIndex, in mapping.AddNewIndex(3, 1); // w mapping.AddNewIndex(4, 1); // ' ' mapping.AddNewIndex(5, 4); // 用 -> Yong - mapping.AddNewIndex(11, 2); // 户 -> Hu + mapping.AddNewIndex(10, 2); // 户 -> Hu var result = mapping.MapToOriginalIndex(translatedIndex); ClassicAssert.AreEqual(expectedOriginalIndex, result); From 42e04d459b19d61f37d52831d0b7e9ead9b70289 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Mon, 8 Dec 2025 00:41:08 +0800 Subject: [PATCH 5/6] Cover all positions in unit test Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- Flow.Launcher.Test/TranslationMappingTest.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Flow.Launcher.Test/TranslationMappingTest.cs b/Flow.Launcher.Test/TranslationMappingTest.cs index bcbbf8bf8a9..53a394405ac 100644 --- a/Flow.Launcher.Test/TranslationMappingTest.cs +++ b/Flow.Launcher.Test/TranslationMappingTest.cs @@ -32,7 +32,10 @@ public void AddNewIndex_ShouldAddTranslatedIndexPlusLength() [TestCase(6, 5)] // "o" (translated from "用") -> original index 5 [TestCase(7, 5)] // "n" (translated from "用") -> original index 5 [TestCase(8, 5)] // "g" (translated from "用") -> original index 5 + [TestCase(9, 6)] // " " (space between "Yong" and "Hu") -> original index 6 [TestCase(10, 6)] // "H" (translated from "户") -> original index 6 + [TestCase(11, 6)] // "u" (translated from "户") -> original index 6 + public void MapToOriginalIndex_ShouldReturnExpectedIndex(int translatedIndex, int expectedOriginalIndex) public void MapToOriginalIndex_ShouldReturnExpectedIndex(int translatedIndex, int expectedOriginalIndex) { var mapping = new TranslationMapping(); From b6339d38fa48955984b6ba6884fabb6a1fd88657 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Mon, 8 Dec 2025 13:06:14 +0800 Subject: [PATCH 6/6] Fix compile error introduced by coderabbit --- Flow.Launcher.Test/TranslationMappingTest.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Flow.Launcher.Test/TranslationMappingTest.cs b/Flow.Launcher.Test/TranslationMappingTest.cs index 53a394405ac..a3c0026c03d 100644 --- a/Flow.Launcher.Test/TranslationMappingTest.cs +++ b/Flow.Launcher.Test/TranslationMappingTest.cs @@ -32,11 +32,9 @@ public void AddNewIndex_ShouldAddTranslatedIndexPlusLength() [TestCase(6, 5)] // "o" (translated from "用") -> original index 5 [TestCase(7, 5)] // "n" (translated from "用") -> original index 5 [TestCase(8, 5)] // "g" (translated from "用") -> original index 5 - [TestCase(9, 6)] // " " (space between "Yong" and "Hu") -> original index 6 [TestCase(10, 6)] // "H" (translated from "户") -> original index 6 [TestCase(11, 6)] // "u" (translated from "户") -> original index 6 public void MapToOriginalIndex_ShouldReturnExpectedIndex(int translatedIndex, int expectedOriginalIndex) - public void MapToOriginalIndex_ShouldReturnExpectedIndex(int translatedIndex, int expectedOriginalIndex) { var mapping = new TranslationMapping(); // Test case :