Skip to content

Commit f32cbaf

Browse files
committed
Split name matching and desciption matching due to fuzzy change
1 parent 8fb2dad commit f32cbaf

File tree

2 files changed

+71
-33
lines changed

2 files changed

+71
-33
lines changed

Plugins/Flow.Launcher.Plugin.Program/Programs/UWP.cs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@ private static IEnumerable<Package> CurrentUserPackages()
206206
}
207207
catch (Exception e)
208208
{
209-
ProgramLogger.LogException("UWP" ,"CurrentUserPackages", $"id","An unexpected error occured and "
209+
ProgramLogger.LogException("UWP", "CurrentUserPackages", $"id", "An unexpected error occured and "
210210
+ $"unable to verify if package is valid", e);
211211
return false;
212212
}
213-
214-
213+
214+
215215
return valid;
216216
});
217217
return ps;
@@ -263,24 +263,42 @@ public class Application : IProgram
263263
public string LogoPath { get; set; }
264264
public UWP Package { get; set; }
265265

266-
public Application(){}
266+
public Application() { }
267267

268268

269269
public Result Result(string query, IPublicAPI api)
270270
{
271-
var title = (Name, Description) switch
272-
{
273-
(var n, null) => n,
274-
(var n, var d) when d.StartsWith(n) => d,
275-
(var n, var d) when n.StartsWith(d) => n,
276-
(var n, var d) when !string.IsNullOrEmpty(d) => $"{n}: {d}",
277-
_ => Name
278-
};
271+
string title;
272+
MatchResult matchResult;
279273

280-
var matchResult = StringMatcher.FuzzySearch(query, title);
274+
// We suppose Name won't be null
275+
if (Description == null || Name.StartsWith(Description))
276+
{
277+
title = Name;
278+
matchResult = StringMatcher.FuzzySearch(query, title);
279+
}
280+
else if (Description.StartsWith(Name))
281+
{
282+
title = Description;
283+
matchResult = StringMatcher.FuzzySearch(query, Description);
284+
}
285+
else
286+
{
287+
title = $"{Name}: {Description}";
288+
var nameMatch = StringMatcher.FuzzySearch(query, Name);
289+
var desciptionMatch = StringMatcher.FuzzySearch(query, Description);
290+
if (desciptionMatch.Score > nameMatch.Score)
291+
{
292+
for (int i = 0; i < desciptionMatch.MatchData.Count; i++)
293+
{
294+
desciptionMatch.MatchData[i] += Name.Length + 2; // 2 is ": "
295+
}
296+
matchResult = desciptionMatch;
297+
}
298+
else matchResult = nameMatch;
299+
}
281300

282-
if (!matchResult.Success)
283-
return null;
301+
if (!matchResult.Success) return null;
284302

285303
var result = new Result
286304
{
@@ -311,7 +329,7 @@ public List<Result> ContextMenus(IPublicAPI api)
311329

312330
Action = _ =>
313331
{
314-
Main.StartProcess(Process.Start,
332+
Main.StartProcess(Process.Start,
315333
new ProcessStartInfo(
316334
!string.IsNullOrEmpty(Main._settings.CustomizedExplorer)
317335
? Main._settings.CustomizedExplorer
@@ -403,14 +421,14 @@ internal string ResourceFromPri(string packageFullName, string packageName, stri
403421
public string FormattedPriReferenceValue(string packageName, string rawPriReferenceValue)
404422
{
405423
const string prefix = "ms-resource:";
406-
424+
407425
if (string.IsNullOrWhiteSpace(rawPriReferenceValue) || !rawPriReferenceValue.StartsWith(prefix))
408426
return rawPriReferenceValue;
409427

410428
string key = rawPriReferenceValue.Substring(prefix.Length);
411429
if (key.StartsWith("//"))
412430
return $"{prefix}{key}";
413-
431+
414432
if (!key.StartsWith("/"))
415433
{
416434
key = $"/{key}";

Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Flow.Launcher.Infrastructure;
1313
using Flow.Launcher.Plugin.Program.Logger;
1414
using Flow.Launcher.Plugin.SharedCommands;
15+
using Flow.Launcher.Plugin.SharedModels;
1516

1617
namespace Flow.Launcher.Plugin.Program.Programs
1718
{
@@ -36,19 +37,38 @@ public class Win32 : IProgram
3637

3738
public Result Result(string query, IPublicAPI api)
3839
{
39-
var title = (Name, Description) switch
40+
string title;
41+
MatchResult matchResult;
42+
43+
// We suppose Name won't be null
44+
if (Description == null || Name.StartsWith(Description))
4045
{
41-
(var n, null) => n,
42-
(var n, var d) when d.StartsWith(n) => d,
43-
(var n, var d) when n.StartsWith(d) => n,
44-
(var n, var d) when !string.IsNullOrEmpty(d) => $"{n}: {d}",
45-
_ => Name
46-
};
46+
title = Name;
47+
matchResult = StringMatcher.FuzzySearch(query, title);
48+
}
49+
else if (Description.StartsWith(Name))
50+
{
51+
title = Description;
52+
matchResult = StringMatcher.FuzzySearch(query, Description);
53+
}
54+
else
55+
{
56+
title = $"{Name}: {Description}";
57+
var nameMatch = StringMatcher.FuzzySearch(query, Name);
58+
var desciptionMatch = StringMatcher.FuzzySearch(query, Description);
59+
if (desciptionMatch.Score > nameMatch.Score)
60+
{
61+
for (int i = 0; i < desciptionMatch.MatchData.Count; i++)
62+
{
63+
desciptionMatch.MatchData[i] += Name.Length + 2; // 2 is ": "
64+
}
65+
matchResult = desciptionMatch;
66+
}
67+
else matchResult = nameMatch;
68+
}
4769

48-
var matchResult = StringMatcher.FuzzySearch(query, title);
70+
if (!matchResult.Success) return null;
4971

50-
if (!matchResult.Success)
51-
return null;
5272

5373
var result = new Result
5474
{
@@ -58,7 +78,7 @@ public Result Result(string query, IPublicAPI api)
5878
Score = matchResult.Score,
5979
TitleHighlightData = matchResult.MatchData,
6080
ContextData = this,
61-
Action = e =>
81+
Action = _ =>
6282
{
6383
var info = new ProcessStartInfo
6484
{
@@ -268,10 +288,10 @@ private static IEnumerable<string> ProgramPaths(string directory, string[] suffi
268288
try
269289
{
270290
var paths = Directory.EnumerateFiles(directory, "*", new EnumerationOptions
271-
{
272-
IgnoreInaccessible = true,
273-
RecurseSubdirectories = true
274-
})
291+
{
292+
IgnoreInaccessible = true,
293+
RecurseSubdirectories = true
294+
})
275295
.Where(x => suffixes.Contains(Extension(x)));
276296

277297
return paths;

0 commit comments

Comments
 (0)