Skip to content

Commit de36eaf

Browse files
VictoriousRaptorJack251970Copilot
authored
Use Highlight matches from Everything (#4196)
* Use Highlight matches from Everything To solve inaccurate highlight when facing Chinese Pinyin searches * Fix typos * Update comments Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Remove unnecessary constructor Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Use camel case for parameters * Use ToInt32 instead of explicit converting Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Revert "Remove unnecessary constructor" This reverts commit 7e9dc18. * Make SearchResult readonly and init HighlightData to empty list Changed SearchResult to a readonly record struct for better immutability and performance. Updated HighlightData to initialize as an empty list using a collection expression instead of null. Retained the default constructor with a clarifying comment. * Fix incorrect argument type after merging --------- Co-authored-by: Jack251970 <1160210343@qq.com> Co-authored-by: Jack Ye <jack1160210343@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 24b4e03 commit de36eaf

4 files changed

Lines changed: 68 additions & 9 deletions

File tree

Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ public static async IAsyncEnumerable<SearchResult> SearchAsync(EverythingSearchO
157157
Type = EverythingApiDllImport.Everything_IsFolderResult(idx) ? ResultType.Folder :
158158
EverythingApiDllImport.Everything_IsFileResult(idx) ? ResultType.File :
159159
ResultType.Volume,
160-
Score = (int)EverythingApiDllImport.Everything_GetResultRunCount( (uint)idx)
160+
Score = Convert.ToInt32(EverythingApiDllImport.Everything_GetResultRunCount((uint)idx)),
161+
HighlightData = EverythingHighlightStringToHighlightList(EverythingApiDllImport.Everything_GetResultHighlightedFileName((uint)idx))
161162
};
162163

163164
yield return result;
@@ -208,5 +209,55 @@ public static async Task IncrementRunCounterAsync(string fileOrFolder)
208209
}
209210
finally { _semaphore.Release(); }
210211
}
212+
213+
/// <summary>
214+
/// Convert the highlighted string from Everything API to a list of highlight indexes for our Result.
215+
/// </summary>
216+
/// <param name="highlightString">Text inside a * quote is highlighted, two consecutive *'s is a single literal *. For example, in the highlighted text: abc*123* the 123 part is highlighted.</param>
217+
/// <returns>A list of zero-based character indices that should be highlighted.</returns>
218+
public static List<int> EverythingHighlightStringToHighlightList(string highlightString)
219+
{
220+
var highlightData = new List<int>();
221+
222+
if (string.IsNullOrEmpty(highlightString))
223+
return highlightData;
224+
225+
var isHighlighted = false;
226+
var actualIndex = 0; // Index in the actual string (without * markers)
227+
var length = highlightString.Length;
228+
229+
for (var i = 0; i < length; i++)
230+
{
231+
if (highlightString[i] == '*')
232+
{
233+
// Check if it's a literal * (two consecutive *)
234+
if (i + 1 < length && highlightString[i + 1] == '*')
235+
{
236+
// Two consecutive *'s represent a single literal *
237+
if (isHighlighted)
238+
{
239+
highlightData.Add(actualIndex);
240+
}
241+
actualIndex++;
242+
i++; // Skip the next *
243+
}
244+
else
245+
{
246+
isHighlighted = !isHighlighted;
247+
}
248+
}
249+
else
250+
{
251+
// Regular character
252+
if (isHighlighted)
253+
{
254+
highlightData.Add(actualIndex);
255+
}
256+
actualIndex++;
257+
}
258+
}
259+
260+
return highlightData;
261+
}
211262
}
212263
}

Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingApiDllImport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static void Load(string directory)
147147
[DllImport(DLL)]
148148
public static extern bool Everything_GetResultDateRecentlyChanged(uint nIndex, out long lpFileTime);
149149
[DllImport(DLL, CharSet = CharSet.Unicode)]
150-
public static extern IntPtr Everything_GetResultHighlightedFileName(uint nIndex);
150+
public static extern string Everything_GetResultHighlightedFileName(uint nIndex);
151151
[DllImport(DLL, CharSet = CharSet.Unicode)]
152152
public static extern IntPtr Everything_GetResultHighlightedPath(uint nIndex);
153153
[DllImport(DLL, CharSet = CharSet.Unicode)]

Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ public static Result CreateResult(Query query, SearchResult result)
106106
return result.Type switch
107107
{
108108
ResultType.Folder or ResultType.Volume =>
109-
CreateFolderResult(Path.GetFileName(result.FullPath), result.FullPath, result.FullPath, query, result.Score, result.WindowsIndexed),
109+
CreateFolderResult(Path.GetFileName(result.FullPath), result.FullPath, result.FullPath, query, result.Score, result.WindowsIndexed, result.HighlightData),
110110
ResultType.File =>
111-
CreateFileResult(result.FullPath, query, result.Score, result.WindowsIndexed),
111+
CreateFileResult(result.FullPath, query, result.Score, result.WindowsIndexed, name: null, highlightData: result.HighlightData),
112112
_ => throw new ArgumentOutOfRangeException(null)
113113
};
114114
}
@@ -134,7 +134,7 @@ internal static void ShowNativeContextMenu(string path, ResultType type)
134134
}
135135
}
136136

137-
internal static Result CreateFolderResult(string title, string subtitle, string path, Query query, int score = 0, bool windowsIndexed = false)
137+
internal static Result CreateFolderResult(string title, string subtitle, string path, Query query, int score = 0, bool windowsIndexed = false, List<int> highlightData = null)
138138
{
139139
if (Settings.BoostHomeFolderScore && IsHomeFolderPath(path))
140140
score += HomeFolderScoreBoost;
@@ -145,7 +145,7 @@ internal static Result CreateFolderResult(string title, string subtitle, string
145145
IcoPath = path,
146146
SubTitle = subtitle,
147147
AutoCompleteText = GetAutoCompleteText(title, query, path, ResultType.Folder),
148-
TitleHighlightData = Context.API.FuzzySearch(query.Search, title).MatchData,
148+
TitleHighlightData = highlightData ?? Context.API.FuzzySearch(query.Search, title).MatchData,
149149
CopyText = path,
150150
Preview = new Result.PreviewInfo
151151
{
@@ -327,7 +327,7 @@ internal static Result CreateOpenCurrentFolderResult(string path, string actionK
327327
};
328328
}
329329

330-
internal static Result CreateFileResult(string filePath, Query query, int score = 0, bool windowsIndexed = false, string name = null)
330+
internal static Result CreateFileResult(string filePath, Query query, int score = 0, bool windowsIndexed = false, string name = null, List<int> highlightData = null)
331331
{
332332
var isMedia = IsMedia(Path.GetExtension(filePath));
333333
var title = name ?? Path.GetFileName(filePath) ?? string.Empty;
@@ -347,7 +347,7 @@ internal static Result CreateFileResult(string filePath, Query query, int score
347347
FilePath = filePath,
348348
},
349349
AutoCompleteText = GetAutoCompleteText(title, query, filePath, ResultType.File),
350-
TitleHighlightData = Context.API.FuzzySearch(query.Search, title).MatchData,
350+
TitleHighlightData = highlightData ?? Context.API.FuzzySearch(query.Search, title).MatchData,
351351
Score = score,
352352
CopyText = filePath,
353353
PreviewPanel = new Lazy<UserControl>(() => new PreviewPanel(Settings, filePath, ResultType.File)),
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
using System.Collections.Generic;
2+
13
namespace Flow.Launcher.Plugin.Explorer.Search
24
{
3-
public record struct SearchResult
5+
public readonly record struct SearchResult
46
{
7+
// Constructor is necesssary for record struct
8+
public SearchResult()
9+
{
10+
}
11+
512
public string FullPath { get; init; }
613
public ResultType Type { get; init; }
714
public int Score { get; init; }
815

916
public bool WindowsIndexed { get; init; }
17+
public List<int> HighlightData { get; init; } = [];
1018
}
1119
}

0 commit comments

Comments
 (0)