From 9c13416231074f78968640b0a4e87b4a00705651 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Mon, 6 Sep 2021 12:31:07 -0500 Subject: [PATCH 1/6] Fixes Typo TermSeparator & remove the actionkeyword in Terms --- Flow.Launcher.Core/Plugin/QueryBuilder.cs | 24 +++++------- Flow.Launcher.Plugin/Query.cs | 37 +++++++------------ Flow.Launcher/ActionKeywords.xaml.cs | 2 +- Flow.Launcher/ViewModel/PluginViewModel.cs | 2 +- .../Main.cs | 2 +- .../Main.cs | 2 +- Plugins/Flow.Launcher.Plugin.Shell/Main.cs | 2 +- 7 files changed, 28 insertions(+), 43 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/QueryBuilder.cs b/Flow.Launcher.Core/Plugin/QueryBuilder.cs index df336e14b..444335afd 100644 --- a/Flow.Launcher.Core/Plugin/QueryBuilder.cs +++ b/Flow.Launcher.Core/Plugin/QueryBuilder.cs @@ -10,35 +10,31 @@ namespace Flow.Launcher.Core.Plugin public static Query Build(string text, Dictionary nonGlobalPlugins) { // replace multiple white spaces with one white space - var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries); - if (terms.Length == 0) + var textSplit = text.Split(Query.TermSeparator, StringSplitOptions.RemoveEmptyEntries); + if (textSplit.Length == 0) { // nothing was typed return null; } - var rawQuery = string.Join(Query.TermSeperater, terms); + var rawQuery = string.Join(Query.TermSeparator, textSplit); string actionKeyword, search; - string possibleActionKeyword = terms[0]; - List actionParameters; + string possibleActionKeyword = textSplit[0]; + string[] terms; + if (nonGlobalPlugins.TryGetValue(possibleActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled) { // use non global plugin for query actionKeyword = possibleActionKeyword; - actionParameters = terms.Skip(1).ToList(); - search = actionParameters.Count > 0 ? rawQuery.Substring(actionKeyword.Length + 1) : string.Empty; + search = textSplit.Length > 1 ? rawQuery[(actionKeyword.Length + 1)..] : string.Empty; + terms = textSplit[1..]; } else { // non action keyword actionKeyword = string.Empty; search = rawQuery; + terms = textSplit; } - var query = new Query - { - Terms = terms, - RawQuery = rawQuery, - ActionKeyword = actionKeyword, - Search = search - }; + var query = new Query(rawQuery, search, terms, actionKeyword); return query; } diff --git a/Flow.Launcher.Plugin/Query.cs b/Flow.Launcher.Plugin/Query.cs index 1eb5c9c14..05e34b9d9 100644 --- a/Flow.Launcher.Plugin/Query.cs +++ b/Flow.Launcher.Plugin/Query.cs @@ -1,4 +1,5 @@ -using System; +using JetBrains.Annotations; +using System; using System.Collections.Generic; using System.Linq; @@ -23,7 +24,7 @@ namespace Flow.Launcher.Plugin /// Raw query, this includes action keyword if it has /// We didn't recommend use this property directly. You should always use Search property. /// - public string RawQuery { get; internal set; } + public string RawQuery { get; internal init; } /// /// Search part of a query. @@ -31,45 +32,40 @@ namespace Flow.Launcher.Plugin /// Since we allow user to switch a exclusive plugin to generic plugin, /// so this property will always give you the "real" query part of the query /// - public string Search { get; internal set; } + public string Search { get; internal init; } /// /// The raw query splited into a string array. /// - public string[] Terms { get; set; } + public string[] Terms { get; init; } /// /// Query can be splited into multiple terms by whitespace /// - public const string TermSeperater = " "; + public const string TermSeparator = " "; /// /// User can set multiple action keywords seperated by ';' /// - public const string ActionKeywordSeperater = ";"; + public const string ActionKeywordSeparator = ";"; /// /// '*' is used for System Plugin /// public const string GlobalPluginWildcardSign = "*"; - public string ActionKeyword { get; set; } + public string ActionKeyword { get; init; } /// /// Return first search split by space if it has /// public string FirstSearch => SplitSearch(0); + private string _secondToEndSearch; + /// /// strings from second search (including) to last search /// - public string SecondToEndSearch - { - get - { - var index = string.IsNullOrEmpty(ActionKeyword) ? 1 : 2; - return string.Join(TermSeperater, Terms.Skip(index).ToArray()); - } - } + public string SecondToEndSearch => _secondToEndSearch ??= string.Join(' ', Terms.AsMemory(2)); /// /// Return second search split by space if it has @@ -83,16 +79,9 @@ namespace Flow.Launcher.Plugin private string SplitSearch(int index) { - try - { - return string.IsNullOrEmpty(ActionKeyword) ? Terms[index] : Terms[index + 1]; - } - catch (IndexOutOfRangeException) - { - return string.Empty; - } + return index < Terms.Length ? Terms[index] : string.Empty; } public override string ToString() => RawQuery; } -} +} \ No newline at end of file diff --git a/Flow.Launcher/ActionKeywords.xaml.cs b/Flow.Launcher/ActionKeywords.xaml.cs index 4a2368347..281be6e52 100644 --- a/Flow.Launcher/ActionKeywords.xaml.cs +++ b/Flow.Launcher/ActionKeywords.xaml.cs @@ -30,7 +30,7 @@ namespace Flow.Launcher private void ActionKeyword_OnLoaded(object sender, RoutedEventArgs e) { - tbOldActionKeyword.Text = string.Join(Query.ActionKeywordSeperater, plugin.Metadata.ActionKeywords.ToArray()); + tbOldActionKeyword.Text = string.Join(Query.ActionKeywordSeparator, plugin.Metadata.ActionKeywords.ToArray()); tbAction.Focus(); } diff --git a/Flow.Launcher/ViewModel/PluginViewModel.cs b/Flow.Launcher/ViewModel/PluginViewModel.cs index 1ac320cf0..b0db8bca0 100644 --- a/Flow.Launcher/ViewModel/PluginViewModel.cs +++ b/Flow.Launcher/ViewModel/PluginViewModel.cs @@ -22,7 +22,7 @@ namespace Flow.Launcher.ViewModel public Visibility ActionKeywordsVisibility => PluginPair.Metadata.ActionKeywords.Count == 1 ? Visibility.Visible : Visibility.Collapsed; public string InitilizaTime => PluginPair.Metadata.InitTime.ToString() + "ms"; public string QueryTime => PluginPair.Metadata.AvgQueryTime + "ms"; - public string ActionKeywordsText => string.Join(Query.ActionKeywordSeperater, PluginPair.Metadata.ActionKeywords); + public string ActionKeywordsText => string.Join(Query.ActionKeywordSeparator, PluginPair.Metadata.ActionKeywords); public int Priority => PluginPair.Metadata.Priority; public void ChangeActionKeyword(string newActionKeyword, string oldActionKeyword) diff --git a/Plugins/Flow.Launcher.Plugin.PluginIndicator/Main.cs b/Plugins/Flow.Launcher.Plugin.PluginIndicator/Main.cs index 190f40a03..a27986e75 100644 --- a/Plugins/Flow.Launcher.Plugin.PluginIndicator/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.PluginIndicator/Main.cs @@ -27,7 +27,7 @@ namespace Flow.Launcher.Plugin.PluginIndicator IcoPath = metadata.IcoPath, Action = c => { - context.API.ChangeQuery($"{keyword}{Plugin.Query.TermSeperater}"); + context.API.ChangeQuery($"{keyword}{Plugin.Query.TermSeparator}"); return false; } }; diff --git a/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs b/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs index c3d9d1ab2..29d3511eb 100644 --- a/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs @@ -25,7 +25,7 @@ namespace Flow.Launcher.Plugin.ProcessKiller { var termToSearch = query.Terms.Length <= 1 ? null - : string.Join(Plugin.Query.TermSeperater, query.Terms.Skip(1)).ToLower(); + : string.Join(Plugin.Query.TermSeparator, query.Terms.Skip(1)).ToLower(); var processlist = processHelper.GetMatchingProcesses(termToSearch); diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs index 58f8538f0..9ccb600e5 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs @@ -297,7 +297,7 @@ namespace Flow.Launcher.Plugin.Shell private void OnWinRPressed() { - context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeperater}"); + context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}"); // show the main window and set focus to the query box Window mainWindow = Application.Current.MainWindow; From 812703766ab03693ea1366052f9acb1682f8de41 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Tue, 7 Sep 2021 17:07:21 -0500 Subject: [PATCH 2/6] Mark previous api as obsolete to preserve backward compatibility. --- Flow.Launcher.Core/Plugin/QueryBuilder.cs | 18 +++++++-------- Flow.Launcher.Plugin/Query.cs | 23 +++++++++++++++---- .../Main.cs | 4 ++-- .../Main.cs | 4 +--- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/QueryBuilder.cs b/Flow.Launcher.Core/Plugin/QueryBuilder.cs index 444335afd..ef387b693 100644 --- a/Flow.Launcher.Core/Plugin/QueryBuilder.cs +++ b/Flow.Launcher.Core/Plugin/QueryBuilder.cs @@ -10,31 +10,31 @@ namespace Flow.Launcher.Core.Plugin public static Query Build(string text, Dictionary nonGlobalPlugins) { // replace multiple white spaces with one white space - var textSplit = text.Split(Query.TermSeparator, StringSplitOptions.RemoveEmptyEntries); - if (textSplit.Length == 0) + var terms = text.Split(Query.TermSeparator, StringSplitOptions.RemoveEmptyEntries); + if (terms.Length == 0) { // nothing was typed return null; } - var rawQuery = string.Join(Query.TermSeparator, textSplit); + var rawQuery = string.Join(Query.TermSeparator, terms); string actionKeyword, search; - string possibleActionKeyword = textSplit[0]; - string[] terms; + string possibleActionKeyword = terms[0]; + string[] searchTerms; if (nonGlobalPlugins.TryGetValue(possibleActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled) { // use non global plugin for query actionKeyword = possibleActionKeyword; - search = textSplit.Length > 1 ? rawQuery[(actionKeyword.Length + 1)..] : string.Empty; - terms = textSplit[1..]; + search = terms.Length > 1 ? rawQuery[(actionKeyword.Length + 1)..] : string.Empty; + searchTerms = terms[1..]; } else { // non action keyword actionKeyword = string.Empty; search = rawQuery; - terms = textSplit; + searchTerms = terms; } - var query = new Query(rawQuery, search, terms, actionKeyword); + var query = new Query(rawQuery, search,terms, searchTerms, actionKeyword); return query; } diff --git a/Flow.Launcher.Plugin/Query.cs b/Flow.Launcher.Plugin/Query.cs index 05e34b9d9..12681388e 100644 --- a/Flow.Launcher.Plugin/Query.cs +++ b/Flow.Launcher.Plugin/Query.cs @@ -12,11 +12,11 @@ namespace Flow.Launcher.Plugin /// /// to allow unit tests for plug ins /// - public Query(string rawQuery, string search, string[] terms, string actionKeyword = "") + public Query(string rawQuery, string search, string[] terms, string[] searchTerms, string actionKeyword = "") { Search = search; RawQuery = rawQuery; - Terms = terms; + SearchTerms = searchTerms; ActionKeyword = actionKeyword; } @@ -35,18 +35,31 @@ namespace Flow.Launcher.Plugin public string Search { get; internal init; } /// - /// The raw query splited into a string array. + /// The search string split into a string array. /// + public string[] SearchTerms { get; init; } + + /// + /// The raw query split into a string array + /// + [Obsolete("It may or may not include action keyword, which can be confusing. Use SearchTerms instead")] public string[] Terms { get; init; } /// /// Query can be splited into multiple terms by whitespace /// public const string TermSeparator = " "; + + [Obsolete("Typo")] + public const string TermSeperater = TermSeparator; /// /// User can set multiple action keywords seperated by ';' /// public const string ActionKeywordSeparator = ";"; + + [Obsolete("Typo")] + public const string ActionKeywordSeperater = ActionKeywordSeparator; + /// /// '*' is used for System Plugin @@ -65,7 +78,7 @@ namespace Flow.Launcher.Plugin /// /// strings from second search (including) to last search /// - public string SecondToEndSearch => _secondToEndSearch ??= string.Join(' ', Terms.AsMemory(2)); + public string SecondToEndSearch => _secondToEndSearch ??= string.Join(' ', SearchTerms.AsMemory(2)); /// /// Return second search split by space if it has @@ -79,7 +92,7 @@ namespace Flow.Launcher.Plugin private string SplitSearch(int index) { - return index < Terms.Length ? Terms[index] : string.Empty; + return index < SearchTerms.Length ? SearchTerms[index] : string.Empty; } public override string ToString() => RawQuery; diff --git a/Plugins/Flow.Launcher.Plugin.PluginIndicator/Main.cs b/Plugins/Flow.Launcher.Plugin.PluginIndicator/Main.cs index a27986e75..b046b2beb 100644 --- a/Plugins/Flow.Launcher.Plugin.PluginIndicator/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.PluginIndicator/Main.cs @@ -12,11 +12,11 @@ namespace Flow.Launcher.Plugin.PluginIndicator { // if query contains more than one word, eg. github tips // user has decided to type something else rather than wanting to see the available action keywords - if (query.Terms.Length > 1) + if (query.SearchTerms.Length > 1) return new List(); var results = from keyword in PluginManager.NonGlobalPlugins.Keys - where keyword.StartsWith(query.Terms[0]) + where keyword.StartsWith(query.SearchTerms[0]) let metadata = PluginManager.NonGlobalPlugins[keyword].Metadata where !metadata.Disabled select new Result diff --git a/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs b/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs index 29d3511eb..31b4a67ed 100644 --- a/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs @@ -23,9 +23,7 @@ namespace Flow.Launcher.Plugin.ProcessKiller public List Query(Query query) { - var termToSearch = query.Terms.Length <= 1 - ? null - : string.Join(Plugin.Query.TermSeparator, query.Terms.Skip(1)).ToLower(); + var termToSearch = query.Search; var processlist = processHelper.GetMatchingProcesses(termToSearch); From 9b84f6c559a1ac46fed6fd41c1dc3558dd63d289 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Sat, 11 Sep 2021 09:41:39 -0500 Subject: [PATCH 3/6] fix an issue of joining string --- Flow.Launcher.Plugin/Query.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Plugin/Query.cs b/Flow.Launcher.Plugin/Query.cs index 12681388e..84dbb3b36 100644 --- a/Flow.Launcher.Plugin/Query.cs +++ b/Flow.Launcher.Plugin/Query.cs @@ -78,7 +78,7 @@ namespace Flow.Launcher.Plugin /// /// strings from second search (including) to last search /// - public string SecondToEndSearch => _secondToEndSearch ??= string.Join(' ', SearchTerms.AsMemory(2)); + public string SecondToEndSearch => _secondToEndSearch ??= string.Join(' ', SearchTerms[1..]); /// /// Return second search split by space if it has From 6a0b190120a37309573e710bdcfb07b8fde1f744 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Mon, 20 Sep 2021 21:51:07 -0500 Subject: [PATCH 4/6] Fix Unassigned Terms --- Flow.Launcher.Plugin/Query.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Plugin/Query.cs b/Flow.Launcher.Plugin/Query.cs index 84dbb3b36..a4a806a62 100644 --- a/Flow.Launcher.Plugin/Query.cs +++ b/Flow.Launcher.Plugin/Query.cs @@ -16,6 +16,9 @@ namespace Flow.Launcher.Plugin { Search = search; RawQuery = rawQuery; +#pragma warning disable 618 Legacy Support + Terms = terms; +#pragma warning restore 618 SearchTerms = searchTerms; ActionKeyword = actionKeyword; } @@ -38,7 +41,7 @@ namespace Flow.Launcher.Plugin /// The search string split into a string array. /// public string[] SearchTerms { get; init; } - + /// /// The raw query split into a string array /// @@ -56,7 +59,7 @@ namespace Flow.Launcher.Plugin /// User can set multiple action keywords seperated by ';' /// public const string ActionKeywordSeparator = ";"; - + [Obsolete("Typo")] public const string ActionKeywordSeperater = ActionKeywordSeparator; From 92c2281deff3940bdf838692a0bc303d4926e772 Mon Sep 17 00:00:00 2001 From: Kevin Zhang <45326534+taooceros@users.noreply.github.com> Date: Mon, 27 Sep 2021 09:02:24 -0500 Subject: [PATCH 5/6] fix secondToEndSearch error --- Flow.Launcher.Plugin/Query.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Plugin/Query.cs b/Flow.Launcher.Plugin/Query.cs index a4a806a62..dfa9e5df1 100644 --- a/Flow.Launcher.Plugin/Query.cs +++ b/Flow.Launcher.Plugin/Query.cs @@ -81,7 +81,7 @@ namespace Flow.Launcher.Plugin /// /// strings from second search (including) to last search /// - public string SecondToEndSearch => _secondToEndSearch ??= string.Join(' ', SearchTerms[1..]); + public string SecondToEndSearch => SearchTerms.Length > 1? _secondToEndSearch ??= string.Join(' ', SearchTerms[1..]]) : ""; /// /// Return second search split by space if it has From fdc8ae2842b0cf2764c4e240f04c06457a5ff9cb Mon Sep 17 00:00:00 2001 From: Kevin Zhang <45326534+taooceros@users.noreply.github.com> Date: Fri, 1 Oct 2021 17:04:02 -0500 Subject: [PATCH 6/6] remove warning suppression and fix a typo --- Flow.Launcher.Plugin/Query.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Flow.Launcher.Plugin/Query.cs b/Flow.Launcher.Plugin/Query.cs index dfa9e5df1..3fcf3c1d7 100644 --- a/Flow.Launcher.Plugin/Query.cs +++ b/Flow.Launcher.Plugin/Query.cs @@ -16,9 +16,7 @@ namespace Flow.Launcher.Plugin { Search = search; RawQuery = rawQuery; -#pragma warning disable 618 Legacy Support Terms = terms; -#pragma warning restore 618 SearchTerms = searchTerms; ActionKeyword = actionKeyword; } @@ -81,7 +79,7 @@ namespace Flow.Launcher.Plugin /// /// strings from second search (including) to last search /// - public string SecondToEndSearch => SearchTerms.Length > 1? _secondToEndSearch ??= string.Join(' ', SearchTerms[1..]]) : ""; + public string SecondToEndSearch => SearchTerms.Length > 1 ? (_secondToEndSearch ??= string.Join(' ', SearchTerms[1..])) : ""; /// /// Return second search split by space if it has