mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge pull request #4151 from Flow-Launcher/fix3965
Fix incorrect text highlighting when using pinyin queries
This commit is contained in:
commit
c03c778efb
3 changed files with 40 additions and 23 deletions
|
|
@ -27,7 +27,7 @@ namespace Flow.Launcher.Infrastructure
|
||||||
{
|
{
|
||||||
switch (e.PropertyName)
|
switch (e.PropertyName)
|
||||||
{
|
{
|
||||||
case nameof (Settings.ShouldUsePinyin):
|
case nameof(Settings.ShouldUsePinyin):
|
||||||
if (_settings.ShouldUsePinyin)
|
if (_settings.ShouldUsePinyin)
|
||||||
{
|
{
|
||||||
Reload();
|
Reload();
|
||||||
|
|
@ -52,7 +52,7 @@ namespace Flow.Launcher.Infrastructure
|
||||||
|
|
||||||
private void CreateDoublePinyinTableFromStream(Stream jsonStream)
|
private void CreateDoublePinyinTableFromStream(Stream jsonStream)
|
||||||
{
|
{
|
||||||
var table = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonStream) ??
|
var table = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonStream) ??
|
||||||
throw new InvalidOperationException("Failed to deserialize double pinyin table: result is null");
|
throw new InvalidOperationException("Failed to deserialize double pinyin table: result is null");
|
||||||
|
|
||||||
var schemaKey = _settings.DoublePinyinSchema.ToString();
|
var schemaKey = _settings.DoublePinyinSchema.ToString();
|
||||||
|
|
@ -128,12 +128,12 @@ namespace Flow.Launcher.Infrastructure
|
||||||
if (IsChineseCharacter(content[i]))
|
if (IsChineseCharacter(content[i]))
|
||||||
{
|
{
|
||||||
var translated = _settings.UseDoublePinyin ? ToDoublePinyin(resultList[i]) : resultList[i];
|
var translated = _settings.UseDoublePinyin ? ToDoublePinyin(resultList[i]) : resultList[i];
|
||||||
|
|
||||||
if (i > 0)
|
if (i > 0 && content[i - 1] != ' ')
|
||||||
{
|
{
|
||||||
resultBuilder.Append(' ');
|
resultBuilder.Append(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
map.AddNewIndex(resultBuilder.Length, translated.Length);
|
map.AddNewIndex(resultBuilder.Length, translated.Length);
|
||||||
resultBuilder.Append(translated);
|
resultBuilder.Append(translated);
|
||||||
previousIsChinese = true;
|
previousIsChinese = true;
|
||||||
|
|
@ -144,11 +144,14 @@ namespace Flow.Launcher.Infrastructure
|
||||||
if (previousIsChinese)
|
if (previousIsChinese)
|
||||||
{
|
{
|
||||||
previousIsChinese = false;
|
previousIsChinese = false;
|
||||||
resultBuilder.Append(' ');
|
if (content[i] != ' ')
|
||||||
|
{
|
||||||
|
resultBuilder.Append(' ');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
map.AddNewIndex(resultBuilder.Length, resultList[i].Length);
|
map.AddNewIndex(resultBuilder.Length, 1);
|
||||||
resultBuilder.Append(resultList[i]);
|
resultBuilder.Append(content[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -156,7 +159,7 @@ namespace Flow.Launcher.Infrastructure
|
||||||
|
|
||||||
var translation = resultBuilder.ToString();
|
var translation = resultBuilder.ToString();
|
||||||
var result = (translation, map);
|
var result = (translation, map);
|
||||||
|
|
||||||
return _pinyinCache[content] = result;
|
return _pinyinCache[content] = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -185,8 +188,8 @@ namespace Flow.Launcher.Infrastructure
|
||||||
|
|
||||||
private string ToDoublePinyin(string fullPinyin)
|
private string ToDoublePinyin(string fullPinyin)
|
||||||
{
|
{
|
||||||
return currentDoublePinyinTable.TryGetValue(fullPinyin, out var doublePinyinValue)
|
return currentDoublePinyinTable.TryGetValue(fullPinyin, out var doublePinyinValue)
|
||||||
? doublePinyinValue
|
? doublePinyinValue
|
||||||
: fullPinyin;
|
: fullPinyin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ namespace Flow.Launcher.Infrastructure
|
||||||
public int MapToOriginalIndex(int translatedIndex)
|
public int MapToOriginalIndex(int translatedIndex)
|
||||||
{
|
{
|
||||||
var searchResult = _originalToTranslated.BinarySearch(translatedIndex);
|
var searchResult = _originalToTranslated.BinarySearch(translatedIndex);
|
||||||
return searchResult >= 0 ? searchResult : ~searchResult;
|
return searchResult >= 0 ? searchResult + 1 : ~searchResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void EndConstruct()
|
public void EndConstruct()
|
||||||
|
|
|
||||||
|
|
@ -22,19 +22,33 @@ namespace Flow.Launcher.Test
|
||||||
ClassicAssert.AreEqual(10, GetOriginalToTranslatedAt(mapping, 1));
|
ClassicAssert.AreEqual(10, GetOriginalToTranslatedAt(mapping, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(0, 0)]
|
|
||||||
[TestCase(2, 1)]
|
[TestCase(0, 0)] // "F" -> "F"
|
||||||
[TestCase(3, 1)]
|
[TestCase(1, 1)] // "l" -> "l"
|
||||||
[TestCase(5, 2)]
|
[TestCase(2, 2)] // "o" -> "o"
|
||||||
[TestCase(6, 2)]
|
[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
|
||||||
|
[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();
|
var mapping = new TranslationMapping();
|
||||||
// a测试
|
// Test case :
|
||||||
// a Ce Shi
|
// 0123456
|
||||||
mapping.AddNewIndex(0, 1);
|
// Flow 用户
|
||||||
mapping.AddNewIndex(2, 2);
|
// 012345678901
|
||||||
mapping.AddNewIndex(5, 3);
|
// 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(10, 2); // 户 -> Hu
|
||||||
|
|
||||||
var result = mapping.MapToOriginalIndex(translatedIndex);
|
var result = mapping.MapToOriginalIndex(translatedIndex);
|
||||||
ClassicAssert.AreEqual(expectedOriginalIndex, result);
|
ClassicAssert.AreEqual(expectedOriginalIndex, result);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue