Skip to content

Commit 804a5d5

Browse files
committed
simplify logic
1 parent ad25879 commit 804a5d5

File tree

1 file changed

+22
-35
lines changed
  • Plugins/Flow.Launcher.Plugin.Program/Programs

1 file changed

+22
-35
lines changed

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

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
2323
public class Win32 : IProgram, IEquatable<Win32>
2424
{
2525
public string Name { get; set; }
26-
public string UniqueIdentifier { get => _uid; set => _uid = value == null ? string.Empty : value.ToLowerInvariant(); } // For path comparison
26+
public string UniqueIdentifier { get => _uid; set => _uid = value == null ? string.Empty : value.ToLowerInvariant(); } // For path comparison
2727
public string IcoPath { get; set; }
2828
/// <summary>
2929
/// Path of the file. It's the path of .lnk and .url for .lnk and .url files.
@@ -69,28 +69,15 @@ public class Win32 : IProgram, IEquatable<Win32>
6969
Enabled = false
7070
};
7171

72-
private static MatchResult Match(string query, List<string> candidates)
72+
private static MatchResult Match(string query, IReadOnlyCollection<string> candidates)
7373
{
7474
if (candidates.Count == 0)
7575
return null;
7676

77-
List<MatchResult> matches = new List<MatchResult>();
78-
foreach(var candidate in candidates)
79-
{
80-
var match = StringMatcher.FuzzySearch(query, candidate);
81-
if (match.IsSearchPrecisionScoreMet())
82-
{
83-
matches.Add(match);
84-
}
85-
}
86-
if (matches.Count == 0)
87-
{
88-
return null;
89-
}
90-
else
91-
{
92-
return matches.MaxBy(match => match.Score);
93-
}
77+
var match = candidates.Select(candidate => StringMatcher.FuzzySearch(query, candidate))
78+
.MaxBy(match => match.Score);
79+
80+
return match?.IsSearchPrecisionScoreMet() ?? false ? match : null;
9481
}
9582

9683
public Result Result(string query, IPublicAPI api)
@@ -334,7 +321,7 @@ private static Win32 LnkProgram(string path)
334321
program.ExecutableName = Path.GetFileName(target);
335322

336323
var args = _helper.arguments;
337-
if(!string.IsNullOrEmpty(args))
324+
if (!string.IsNullOrEmpty(args))
338325
{
339326
program.LnkResolvedPath += " " + args;
340327
}
@@ -361,7 +348,7 @@ private static Win32 LnkProgram(string path)
361348
catch (FileNotFoundException e)
362349
{
363350
ProgramLogger.LogException($"|Win32|LnkProgram|{path}" +
364-
"|An unexpected error occurred in the calling method LnkProgram", e);
351+
"|An unexpected error occurred in the calling method LnkProgram", e);
365352

366353
return Default;
367354
}
@@ -428,7 +415,7 @@ private static Win32 ExeProgram(string path)
428415
catch (FileNotFoundException e)
429416
{
430417
ProgramLogger.LogException($"|Win32|ExeProgram|{path}" +
431-
$"|File not found when trying to load the program from {path}", e);
418+
$"|File not found when trying to load the program from {path}", e);
432419

433420
return Default;
434421
}
@@ -448,8 +435,7 @@ private static IEnumerable<string> EnumerateProgramsInDir(string directory, stri
448435

449436
return Directory.EnumerateFiles(directory, "*", new EnumerationOptions
450437
{
451-
IgnoreInaccessible = true,
452-
RecurseSubdirectories = recursive
438+
IgnoreInaccessible = true, RecurseSubdirectories = recursive
453439
}).Where(x => suffixes.Contains(Extension(x)));
454440
}
455441

@@ -458,7 +444,7 @@ private static string Extension(string path)
458444
var extension = Path.GetExtension(path)?.ToLowerInvariant();
459445
if (!string.IsNullOrEmpty(extension))
460446
{
461-
return extension.Substring(1); // remove dot
447+
return extension.Substring(1); // remove dot
462448
}
463449
else
464450
{
@@ -470,7 +456,7 @@ private static IEnumerable<Win32> UnregisteredPrograms(List<string> directories,
470456
{
471457
// Disabled custom sources are not in DisabledProgramSources
472458
var paths = directories.AsParallel()
473-
.SelectMany(s => EnumerateProgramsInDir(s, suffixes));
459+
.SelectMany(s => EnumerateProgramsInDir(s, suffixes));
474460

475461
// Remove disabled programs in DisabledProgramSources
476462
var programs = ExceptDisabledSource(paths).Select(x => GetProgramFromPath(x, protocols));
@@ -502,8 +488,8 @@ private static IEnumerable<Win32> PATHPrograms(string[] suffixes, string[] proto
502488
var paths = pathEnv.Split(";", StringSplitOptions.RemoveEmptyEntries).DistinctBy(p => p.ToLowerInvariant());
503489

504490
var toFilter = paths.Where(x => commonParents.All(parent => !IsSubPathOf(x, parent)))
505-
.AsParallel()
506-
.SelectMany(p => EnumerateProgramsInDir(p, suffixes, recursive: false));
491+
.AsParallel()
492+
.SelectMany(p => EnumerateProgramsInDir(p, suffixes, recursive: false));
507493

508494
var programs = ExceptDisabledSource(toFilter.Distinct())
509495
.Select(x => GetProgramFromPath(x, protocols));
@@ -533,7 +519,7 @@ private static IEnumerable<Win32> AppPathsPrograms(string[] suffixes, string[] p
533519
toFilter = toFilter.Distinct().Where(p => suffixes.Contains(Extension(p)));
534520

535521
var programs = ExceptDisabledSource(toFilter)
536-
.Select(x => GetProgramFromPath(x, protocols)).Where(x => x.Valid).ToList(); // ToList due to disposing issue
522+
.Select(x => GetProgramFromPath(x, protocols)).Where(x => x.Valid).ToList(); // ToList due to disposing issue
537523
return programs;
538524
}
539525

@@ -587,7 +573,8 @@ private static Win32 GetProgramFromPath(string path, string[] protocols)
587573
ExeExtension => ExeProgram(path),
588574
UrlExtension => UrlProgram(path, protocols),
589575
_ => Win32Program(path)
590-
}; ;
576+
};
577+
;
591578
}
592579

593580
public static IEnumerable<string> ExceptDisabledSource(IEnumerable<string> paths)
@@ -797,10 +784,10 @@ private static bool IsSubPathOf(string subPath, string basePath)
797784
{
798785
var rel = Path.GetRelativePath(basePath, subPath);
799786
return rel != "."
800-
&& rel != ".."
801-
&& !rel.StartsWith("../")
802-
&& !rel.StartsWith(@"..\")
803-
&& !Path.IsPathRooted(rel);
787+
&& rel != ".."
788+
&& !rel.StartsWith("../")
789+
&& !rel.StartsWith(@"..\")
790+
&& !Path.IsPathRooted(rel);
804791
}
805792

806793
private static List<string> GetCommonParents(IEnumerable<ProgramSource> programSources)
@@ -815,7 +802,7 @@ private static List<string> GetCommonParents(IEnumerable<ProgramSource> programS
815802
foreach (var source in group)
816803
{
817804
if (parents.Any(p => IsSubPathOf(source.Location, p.Location) &&
818-
source != p))
805+
source != p))
819806
{
820807
parents.Remove(source);
821808
}

0 commit comments

Comments
 (0)