Skip to content

Commit 02b321c

Browse files
committed
add scoring based on index and small optimization
1 parent 28c86af commit 02b321c

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public static async ValueTask<bool> IsEverythingRunningAsync(CancellationToken t
5959
{
6060
EverythingApiDllImport.Everything_GetMajorVersion();
6161
var result = EverythingApiDllImport.Everything_GetLastError() != StateCode.IPCError;
62+
6263
return result;
6364
}
6465
finally
@@ -67,6 +68,8 @@ public static async ValueTask<bool> IsEverythingRunningAsync(CancellationToken t
6768
}
6869
}
6970

71+
const int ScoreScaleFactor = 5;
72+
7073
/// <summary>
7174
/// Searches the specified key word and reset the everything API afterwards
7275
/// </summary>
@@ -115,7 +118,7 @@ public static async IAsyncEnumerable<SearchResult> SearchAsync(EverythingSearchO
115118

116119
EverythingApiDllImport.Everything_SetSort(option.SortOption);
117120
EverythingApiDllImport.Everything_SetMatchPath(option.IsFullPathSearch);
118-
121+
119122
if (option.SortOption == SortOption.RUN_COUNT_DESCENDING)
120123
{
121124
EverythingApiDllImport.Everything_SetRequestFlags(EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME | EVERYTHING_REQUEST_RUN_COUNT);
@@ -132,10 +135,13 @@ public static async IAsyncEnumerable<SearchResult> SearchAsync(EverythingSearchO
132135
if (!EverythingApiDllImport.Everything_QueryW(true))
133136
{
134137
CheckAndThrowExceptionOnError();
138+
135139
yield break;
136140
}
137141

138-
for (var idx = 0; idx < EverythingApiDllImport.Everything_GetNumResults(); ++idx)
142+
var numResults = EverythingApiDllImport.Everything_GetNumResults();
143+
144+
for (var idx = 0; idx < numResults; ++idx)
139145
{
140146
if (token.IsCancellationRequested)
141147
{
@@ -151,7 +157,8 @@ public static async IAsyncEnumerable<SearchResult> SearchAsync(EverythingSearchO
151157
Type = EverythingApiDllImport.Everything_IsFolderResult(idx) ? ResultType.Folder :
152158
EverythingApiDllImport.Everything_IsFileResult(idx) ? ResultType.File :
153159
ResultType.Volume,
154-
Score = (int)EverythingApiDllImport.Everything_GetResultRunCount( (uint)idx)
160+
Score = (option.SortOption is SortOption.RUN_COUNT_DESCENDING ? (int)EverythingApiDllImport.Everything_GetResultRunCount((uint)idx) : 0) * ScoreScaleFactor,
161+
WindowsIndexed = false
155162
};
156163

157164
yield return result;
@@ -192,6 +199,7 @@ private static void CheckAndThrowExceptionOnError()
192199
public static async Task IncrementRunCounterAsync(string fileOrFolder)
193200
{
194201
await _semaphore.WaitAsync(TimeSpan.FromSeconds(1));
202+
195203
try
196204
{
197205
_ = EverythingApiDllImport.Everything_IncRunCountFromFileName(fileOrFolder);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ public SearchManager(Settings settings, PluginInitContext context)
2929
public class PathEqualityComparator : IEqualityComparer<Result>
3030
{
3131
private static PathEqualityComparator instance;
32+
3233
public static PathEqualityComparator Instance => instance ??= new PathEqualityComparator();
3334

3435
public bool Equals(Result x, Result y)
3536
{
3637
return x.Title.Equals(y.Title, StringComparison.OrdinalIgnoreCase)
37-
&& string.Equals(x.SubTitle, y.SubTitle, StringComparison.OrdinalIgnoreCase);
38+
&& string.Equals(x.SubTitle, y.SubTitle, StringComparison.OrdinalIgnoreCase);
3839
}
3940

4041
public int GetHashCode(Result obj)
@@ -106,7 +107,10 @@ when ActionKeywordMatch(query, Settings.ActionKeyword.IndexSearchActionKeyword)
106107

107108
try
108109
{
109-
await foreach (var search in searchResults.WithCancellation(token).ConfigureAwait(false))
110+
await foreach (var search in searchResults.Select((r, i) => r with
111+
{
112+
Score = -i + 50
113+
}).WithCancellation(token).ConfigureAwait(false))
110114
results.Add(ResultManager.CreateResult(query, search));
111115
}
112116
catch (OperationCanceledException)

0 commit comments

Comments
 (0)