Move IsSubPathOf to SharedCommands

This commit is contained in:
Vic 2023-01-19 20:29:25 +08:00
parent 32268890b9
commit ac92b93f66
3 changed files with 20 additions and 25 deletions

View file

@ -241,5 +241,22 @@ namespace Flow.Launcher.Plugin.SharedCommands
return path;
}
/// <summary>
/// Returns if <paramref name="subPath"/> is a sub path of <paramref name="basePath"/>.
/// From https://stackoverflow.com/a/66877016
/// </summary>
/// <param name="subPath"></param>
/// <param name="basePath"></param>
/// <returns></returns>
public static bool IsSubPathOf(string subPath, string basePath)
{
var rel = Path.GetRelativePath(basePath, subPath);
return rel != "."
&& rel != ".."
&& !rel.StartsWith("../")
&& !rel.StartsWith(@"..\")
&& !Path.IsPathRooted(rel);
}
}
}

View file

@ -121,7 +121,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search
}
results.RemoveWhere(r => Settings.IndexSearchExcludedSubdirectoryPaths.Any(
excludedPath => IsSubPathOf(r.SubTitle, excludedPath.Path)));
excludedPath => FilesFolders.IsSubPathOf(r.SubTitle, excludedPath.Path)));
return results.ToList();
}
@ -256,16 +256,5 @@ namespace Flow.Launcher.Plugin.Explorer.Search
&& search != "%%"
&& !search.Contains('\\');
}
// https://stackoverflow.com/a/66877016
internal static bool IsSubPathOf(string subPath, string basePath)
{
var rel = Path.GetRelativePath(basePath, subPath);
return rel != "."
&& rel != ".."
&& !rel.StartsWith("../")
&& !rel.StartsWith(@"..\")
&& !Path.IsPathRooted(rel);
}
}
}

View file

@ -471,7 +471,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
var paths = pathEnv.Split(";", StringSplitOptions.RemoveEmptyEntries).DistinctBy(p => p.ToLowerInvariant());
var toFilter = paths.Where(x => commonParents.All(parent => !IsSubPathOf(x, parent)))
var toFilter = paths.Where(x => commonParents.All(parent => !FilesFolders.IsSubPathOf(x, parent)))
.AsParallel()
.SelectMany(p => EnumerateProgramsInDir(p, suffixes, recursive: false));
@ -763,17 +763,6 @@ namespace Flow.Launcher.Plugin.Program.Programs
}
}
// https://stackoverflow.com/a/66877016
private static bool IsSubPathOf(string subPath, string basePath)
{
var rel = Path.GetRelativePath(basePath, subPath);
return rel != "."
&& rel != ".."
&& !rel.StartsWith("../")
&& !rel.StartsWith(@"..\")
&& !Path.IsPathRooted(rel);
}
private static List<string> GetCommonParents(IEnumerable<ProgramSource> programSources)
{
// To avoid unnecessary io
@ -785,7 +774,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
HashSet<ProgramSource> parents = group.ToHashSet();
foreach (var source in group)
{
if (parents.Any(p => IsSubPathOf(source.Location, p.Location) &&
if (parents.Any(p => FilesFolders.IsSubPathOf(source.Location, p.Location) &&
source != p))
{
parents.Remove(source);