-
-
Notifications
You must be signed in to change notification settings - Fork 465
[FEATURE] Folder and File Action Keywords #4093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
c979609
531e8c7
dbe7d52
e0a99c5
9209b93
d2dd16b
fbb26e9
c90de6f
20959dc
4107b87
a4d3c80
30dbdb8
0a0cd3e
4e5378b
dbf4491
945a3d6
b67a608
dcf4b46
1d60cb7
558967d
24d212f
592f60a
ed5debf
fc573bd
1cdbdd0
69e2031
8b7cbf7
438e709
bd8179d
13dad55
6da9b80
1150f19
8e34897
b601b00
1f2f474
bb490c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,15 @@ | ||
| using Flow.Launcher.Plugin.Explorer.Search.DirectoryInfo; | ||
| using Flow.Launcher.Plugin.Explorer.Search.Everything; | ||
| using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks; | ||
| using Flow.Launcher.Plugin.SharedCommands; | ||
| using System; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Windows.Documents; | ||
| using Flow.Launcher.Plugin.Explorer.Exceptions; | ||
| using Flow.Launcher.Plugin.Explorer.Search.DirectoryInfo; | ||
| using Flow.Launcher.Plugin.Explorer.Search.Everything; | ||
| using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks; | ||
| using Flow.Launcher.Plugin.SharedCommands; | ||
| using static Flow.Launcher.Plugin.Explorer.Settings; | ||
| using Path = System.IO.Path; | ||
|
|
||
| namespace Flow.Launcher.Plugin.Explorer.Search | ||
|
|
@@ -18,6 +20,13 @@ | |
|
|
||
| internal Settings Settings; | ||
|
|
||
| private readonly Dictionary<ActionKeyword, List<ResultType>> _typesToFilterByActionKeyword = new() | ||
| { | ||
| { ActionKeyword.FileSearchActionKeyword, [ResultType.Folder, ResultType.Volume] }, | ||
| { ActionKeyword.FolderSearchActionKeyword, [ResultType.File] }, | ||
| }; | ||
|
|
||
|
|
||
| public SearchManager(Settings settings, PluginInitContext context) | ||
| { | ||
| Context = context; | ||
|
|
@@ -31,7 +40,7 @@ | |
| { | ||
| private static PathEqualityComparator instance; | ||
| public static PathEqualityComparator Instance => instance ??= new PathEqualityComparator(); | ||
|
|
||
| public bool Equals(Result x, Result y) | ||
| { | ||
| return x.Title.Equals(y.Title, StringComparison.OrdinalIgnoreCase) | ||
|
|
@@ -48,82 +57,94 @@ | |
| { | ||
| var results = new HashSet<Result>(PathEqualityComparator.Instance); | ||
|
|
||
| // This allows the user to type the below action keywords and see/search the list of quick folder links | ||
| if (ActionKeywordMatch(query, Settings.ActionKeyword.SearchActionKeyword) | ||
| || ActionKeywordMatch(query, Settings.ActionKeyword.QuickAccessActionKeyword) | ||
| || ActionKeywordMatch(query, Settings.ActionKeyword.PathSearchActionKeyword) | ||
| || ActionKeywordMatch(query, Settings.ActionKeyword.IndexSearchActionKeyword) | ||
| || ActionKeywordMatch(query, Settings.ActionKeyword.FileContentSearchActionKeyword)) | ||
| var keyword = query.ActionKeyword.Length == 0 ? Query.GlobalPluginWildcardSign : query.ActionKeyword; | ||
|
|
||
| // No action keyword matched - plugin should not handle this query, return empty results. | ||
jjw24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var activeActionKeywords = Settings.GetActiveActionKeywords(keyword); | ||
| if (activeActionKeywords.Count == 0) | ||
| { | ||
| if (string.IsNullOrEmpty(query.Search) && ActionKeywordMatch(query, Settings.ActionKeyword.QuickAccessActionKeyword)) | ||
| return QuickAccess.AccessLinkListAll(query, Settings.QuickAccessLinks); | ||
| return []; | ||
| } | ||
| else | ||
|
|
||
| var isPathSearch = query.Search.IsLocationPathString() | ||
jjw24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| || EnvironmentVariables.IsEnvironmentVariableSearch(query.Search) | ||
| || EnvironmentVariables.HasEnvironmentVar(query.Search); | ||
|
|
||
| var queryIsEmpty = string.IsNullOrEmpty(query.Search); | ||
|
|
||
| if (queryIsEmpty && activeActionKeywords.ContainsKey(ActionKeyword.QuickAccessActionKeyword)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it's still running the query and the error is specific to my environment, but we should have it returned empty list if no query input with the action keyword. |
||
| { | ||
| // No action keyword matched- plugin should not handle this query, return empty results. | ||
| return new List<Result>(); | ||
| return QuickAccess.AccessLinkListAll(query, Settings.QuickAccessLinks); | ||
| } | ||
|
|
||
| IAsyncEnumerable<SearchResult> searchResults; | ||
| // If query is empty and active keyword is folder or search, return empty results. | ||
| if (queryIsEmpty && (activeActionKeywords.ContainsKey(ActionKeyword.FolderSearchActionKeyword) | ||
| || activeActionKeywords.ContainsKey(ActionKeyword.FileSearchActionKeyword))) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| bool isPathSearch = query.Search.IsLocationPathString() | ||
| || EnvironmentVariables.IsEnvironmentVariableSearch(query.Search) | ||
| || EnvironmentVariables.HasEnvironmentVar(query.Search); | ||
| // When file search is active, do not include path search in the active keywords. | ||
| // This prevents unwanted PathSearch results (e.g., drives or raw volume paths). | ||
| if (isPathSearch && !activeActionKeywords.ContainsKey(ActionKeyword.PathSearchActionKeyword) | ||
| && !activeActionKeywords.ContainsKey(ActionKeyword.FileSearchActionKeyword)) | ||
| { | ||
| activeActionKeywords.Add(ActionKeyword.PathSearchActionKeyword, keyword); | ||
| } | ||
|
|
||
| IAsyncEnumerable<SearchResult> searchResults; | ||
|
|
||
| string engineName; | ||
|
|
||
| switch (isPathSearch) | ||
| { | ||
| case true | ||
| when ActionKeywordMatch(query, Settings.ActionKeyword.PathSearchActionKeyword) | ||
| || ActionKeywordMatch(query, Settings.ActionKeyword.SearchActionKeyword): | ||
|
|
||
| when activeActionKeywords.ContainsKey(ActionKeyword.PathSearchActionKeyword) | ||
| || activeActionKeywords.ContainsKey(ActionKeyword.SearchActionKeyword): | ||
| results.UnionWith(await PathSearchAsync(query, token).ConfigureAwait(false)); | ||
|
|
||
| return results.ToList(); | ||
| return [.. results]; | ||
|
|
||
| case false | ||
| when ActionKeywordMatch(query, Settings.ActionKeyword.FileContentSearchActionKeyword): | ||
|
|
||
| // Intentionally require enabling of Everything's content search due to its slowness | ||
| when activeActionKeywords.ContainsKey(ActionKeyword.FileContentSearchActionKeyword): | ||
| if (Settings.ContentIndexProvider is EverythingSearchManager && !Settings.EnableEverythingContentSearch) | ||
| return EverythingContentSearchResult(query); | ||
|
|
||
| searchResults = Settings.ContentIndexProvider.ContentSearchAsync("", query.Search, token); | ||
| engineName = Enum.GetName(Settings.ContentSearchEngine); | ||
| break; | ||
|
|
||
| case false | ||
| when ActionKeywordMatch(query, Settings.ActionKeyword.IndexSearchActionKeyword) | ||
| || ActionKeywordMatch(query, Settings.ActionKeyword.SearchActionKeyword): | ||
| case true or false | ||
| when activeActionKeywords.ContainsKey(ActionKeyword.QuickAccessActionKeyword): | ||
| return QuickAccess.AccessLinkListMatched(query, Settings.QuickAccessLinks); | ||
|
|
||
|
|
||
| case false | ||
| when CanUseIndexSearchByActionKeywords(activeActionKeywords): | ||
| searchResults = Settings.IndexProvider.SearchAsync(query.Search, token); | ||
| engineName = Enum.GetName(Settings.IndexSearchEngine); | ||
| break; | ||
|
|
||
| case true or false | ||
| when ActionKeywordMatch(query, Settings.ActionKeyword.QuickAccessActionKeyword): | ||
| return QuickAccess.AccessLinkListMatched(query, Settings.QuickAccessLinks); | ||
|
|
||
| default: | ||
| return results.ToList(); | ||
| } | ||
| return [..results]; | ||
|
|
||
| // Merge Quick Access Link results for non-path searches. | ||
| results.UnionWith(QuickAccess.AccessLinkListMatched(query, Settings.QuickAccessLinks)); | ||
| } | ||
|
|
||
| //Merge Quick Access Link results for non-path searches. | ||
| results.UnionWith(GetQuickAccessResultsFilteredByActionKeyword(query, activeActionKeywords)); | ||
| try | ||
| { | ||
| await foreach (var search in searchResults.WithCancellation(token).ConfigureAwait(false)) | ||
| if (search.Type == ResultType.File && IsExcludedFile(search)) { | ||
| { | ||
| if (ShouldSkipResultByTypeAndActionKeyword(activeActionKeywords, search)) | ||
| { | ||
| continue; | ||
| } else { | ||
| results.Add(ResultManager.CreateResult(query, search)); | ||
| } | ||
| results.Add(ResultManager.CreateResult(query, search)); | ||
| } | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| return new List<Result>(); | ||
| return [.. results]; | ||
| } | ||
| catch (EngineNotAvailableException) | ||
| { | ||
|
|
@@ -137,33 +158,13 @@ | |
| results.RemoveWhere(r => Settings.IndexSearchExcludedSubdirectoryPaths.Any( | ||
| excludedPath => FilesFolders.PathContains(excludedPath.Path, r.SubTitle, allowEqual: true))); | ||
|
|
||
| return results.ToList(); | ||
| } | ||
|
|
||
| private bool ActionKeywordMatch(Query query, Settings.ActionKeyword allowedActionKeyword) | ||
| { | ||
| var keyword = query.ActionKeyword.Length == 0 ? Query.GlobalPluginWildcardSign : query.ActionKeyword; | ||
|
|
||
| return allowedActionKeyword switch | ||
| { | ||
| Settings.ActionKeyword.SearchActionKeyword => Settings.SearchActionKeywordEnabled && | ||
| keyword == Settings.SearchActionKeyword, | ||
| Settings.ActionKeyword.PathSearchActionKeyword => Settings.PathSearchKeywordEnabled && | ||
| keyword == Settings.PathSearchActionKeyword, | ||
| Settings.ActionKeyword.FileContentSearchActionKeyword => Settings.FileContentSearchKeywordEnabled && | ||
| keyword == Settings.FileContentSearchActionKeyword, | ||
| Settings.ActionKeyword.IndexSearchActionKeyword => Settings.IndexSearchKeywordEnabled && | ||
| keyword == Settings.IndexSearchActionKeyword, | ||
| Settings.ActionKeyword.QuickAccessActionKeyword => Settings.QuickAccessKeywordEnabled && | ||
| keyword == Settings.QuickAccessActionKeyword, | ||
| _ => throw new ArgumentOutOfRangeException(nameof(allowedActionKeyword), allowedActionKeyword, "actionKeyword out of range") | ||
| }; | ||
| return [.. results]; | ||
| } | ||
|
|
||
| private List<Result> EverythingContentSearchResult(Query query) | ||
| { | ||
| return new List<Result>() | ||
| { | ||
| return | ||
| [ | ||
| new() | ||
| { | ||
| Title = Localize.flowlauncher_plugin_everything_enable_content_search(), | ||
|
|
@@ -176,7 +177,7 @@ | |
| return false; | ||
| } | ||
| } | ||
| }; | ||
| ]; | ||
| } | ||
|
|
||
| private async Task<List<Result>> PathSearchAsync(Query query, CancellationToken token = default) | ||
|
|
@@ -188,7 +189,7 @@ | |
| if (EnvironmentVariables.IsEnvironmentVariableSearch(querySearch)) | ||
| return EnvironmentVariables.GetEnvironmentStringPathSuggestions(querySearch, query, Context); | ||
|
|
||
| // Query is a location path with a full environment variable, eg. %appdata%\somefolder\, c:\users\%USERNAME%\downloads | ||
| var needToExpand = EnvironmentVariables.HasEnvironmentVar(querySearch); | ||
| var path = needToExpand ? Environment.ExpandEnvironmentVariables(querySearch) : querySearch; | ||
|
|
||
|
|
@@ -197,7 +198,7 @@ | |
|
|
||
| // Check that actual location exists, otherwise directory search will throw directory not found exception | ||
| if (!FilesFolders.ReturnPreviousDirectoryIfIncompleteString(path).LocationExists()) | ||
| return results.ToList(); | ||
| return [.. results]; | ||
|
|
||
| var useIndexSearch = Settings.IndexSearchEngine is Settings.IndexSearchEngineOption.WindowsIndex | ||
| && UseWindowsIndexForDirectorySearch(path); | ||
|
|
@@ -209,7 +210,7 @@ | |
| : ResultManager.CreateOpenCurrentFolderResult(retrievedDirectoryPath, query.ActionKeyword, useIndexSearch)); | ||
|
|
||
| if (token.IsCancellationRequested) | ||
| return new List<Result>(); | ||
| return [.. results]; | ||
|
|
||
| IAsyncEnumerable<SearchResult> directoryResult; | ||
|
|
||
|
|
@@ -231,7 +232,7 @@ | |
| } | ||
|
|
||
| if (token.IsCancellationRequested) | ||
| return new List<Result>(); | ||
| return [.. results]; | ||
|
|
||
| try | ||
| { | ||
|
|
@@ -246,14 +247,14 @@ | |
| } | ||
|
|
||
|
|
||
| return results.ToList(); | ||
| return [.. results]; | ||
| } | ||
|
|
||
| public bool IsFileContentSearch(string actionKeyword) => actionKeyword == Settings.FileContentSearchActionKeyword; | ||
|
|
||
| public static bool UseIndexSearch(string path) | ||
| { | ||
| if (Main.Settings.IndexSearchEngine is not Settings.IndexSearchEngineOption.WindowsIndex) | ||
| if (Main.Settings.IndexSearchEngine is not IndexSearchEngineOption.WindowsIndex) | ||
| return false; | ||
|
|
||
| // Check if the path is using windows index search | ||
|
|
@@ -275,10 +276,67 @@ | |
|
|
||
| private bool IsExcludedFile(SearchResult result) | ||
| { | ||
| string[] excludedFileTypes = Settings.ExcludedFileTypes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); | ||
| string[] excludedFileTypes = Settings.ExcludedFileTypes.Split([','], StringSplitOptions.RemoveEmptyEntries); | ||
| string fileExtension = Path.GetExtension(result.FullPath).TrimStart('.'); | ||
|
|
||
| return excludedFileTypes.Contains(fileExtension, StringComparer.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| private bool ShouldSkipResultByTypeAndActionKeyword(Dictionary<ActionKeyword, string> actions, SearchResult search) | ||
| { | ||
| // Is excluded file type | ||
| if (search.Type == ResultType.File && IsExcludedFile(search)) | ||
| { | ||
| return true; | ||
| } | ||
| return IsResultTypeFilteredByActionKeyword(search.Type, actions); | ||
|
|
||
| } | ||
|
|
||
| private List<Result> GetQuickAccessResultsFilteredByActionKeyword(Query query, Dictionary<ActionKeyword, string> actions) | ||
| { | ||
| var results = QuickAccess.AccessLinkListMatched(query, Settings.QuickAccessLinks) ?? []; | ||
| if (results.Count == 0) | ||
| { | ||
| return results; | ||
| } | ||
|
|
||
| return results | ||
| .Where(r => r.ContextData is SearchResult result | ||
| && !IsResultTypeFilteredByActionKeyword(result.Type, actions)) | ||
| .ToList(); | ||
| } | ||
| private bool IsResultTypeFilteredByActionKeyword(ResultType type, Dictionary<ActionKeyword, string> actions) | ||
| { | ||
| foreach (var action in actions.Keys) | ||
| { | ||
| if (_typesToFilterByActionKeyword.TryGetValue(action, out var typesToFilter)) | ||
| { | ||
| return typesToFilter.Contains(type); | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private bool CanUseIndexSearchByActionKeywords(Dictionary<ActionKeyword, string> actions) | ||
| { | ||
| List<ActionKeyword> keysToUseSearch = | ||
| [ | ||
| ActionKeyword.FileSearchActionKeyword, | ||
| ActionKeyword.FolderSearchActionKeyword, | ||
| ActionKeyword.IndexSearchActionKeyword, | ||
| ActionKeyword.SearchActionKeyword | ||
| ]; | ||
| foreach (var key in keysToUseSearch) | ||
| { | ||
| var contains = actions.ContainsKey(key); | ||
| if (!contains) continue; | ||
| return contains; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| } | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.