diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 9b38913165..a8a9ddf463 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -22,6 +22,7 @@ namespace Wox.Core.Plugin public static class PluginManager { public const string ActionKeywordWildcardSign = "*"; + private static List pluginMetadatas; private static List> instantSearches; private static List> exclusiveSearchPlugins; @@ -117,11 +118,18 @@ namespace Wox.Core.Plugin public static void Query(Query query) { - if (!string.IsNullOrEmpty(query.RawQuery.Trim())) + query.ActionKeyword = string.Empty; + query.Search = query.RawQuery; + if (query.Terms.Length == 0) return; + if (IsVailldActionKeyword(query.Terms[0])) { - query.Search = IsActionKeywordQuery(query) ? query.RawQuery.Substring(query.RawQuery.IndexOf(' ') + 1) : query.RawQuery; - QueryDispatcher.QueryDispatcher.Dispatch(query); + query.ActionKeyword = query.Terms[0]; } + if (!string.IsNullOrEmpty(query.ActionKeyword)) + { + query.Search = string.Join(Wox.Plugin.Query.Seperater, query.Terms.Skip(1).ToArray()); + } + QueryDispatcher.QueryDispatcher.Dispatch(query); } public static List AllPlugins @@ -135,17 +143,10 @@ namespace Wox.Core.Plugin /// /// Check if a query contains valid action keyword /// - /// + /// /// - public static bool IsActionKeywordQuery(Query query) + private static bool IsVailldActionKeyword(string actionKeyword) { - if (string.IsNullOrEmpty(query.RawQuery)) return false; - var strings = query.RawQuery.Split(' '); - if (strings.Length == 1) return false; - - var actionKeyword = strings[0].Trim(); - if (string.IsNullOrEmpty(actionKeyword)) return false; - PluginPair pair = plugins.FirstOrDefault(o => o.Metadata.ActionKeyword == actionKeyword); if (pair != null) { @@ -247,10 +248,10 @@ namespace Wox.Core.Plugin internal static PluginPair GetActionKeywordPlugin(Query query) { - //if a query doesn't contain at least one space, it should not be a action keword plugin query - if (!query.RawQuery.Contains(" ")) return null; + //if a query doesn't contain a vaild action keyword, it should not be a action keword plugin query + if (string.IsNullOrEmpty(query.ActionKeyword)) return null; - PluginPair actionKeywordPluginPair = AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.GetActionKeyword()); + PluginPair actionKeywordPluginPair = AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionKeyword); if (actionKeywordPluginPair != null) { var customizedPluginConfig = UserSettingStorage.Instance. diff --git a/Wox.Plugin/Query.cs b/Wox.Plugin/Query.cs index 79fe259be4..0ec0882ea3 100644 --- a/Wox.Plugin/Query.cs +++ b/Wox.Plugin/Query.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Wox.Plugin { @@ -9,42 +10,31 @@ namespace Wox.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; } /// /// Search part of a query. /// This will not include action keyword if exclusive plugin gets it, otherwise it should be same as RawQuery. - /// 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 + /// 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; } - internal string GetActionKeyword() - { - if (!string.IsNullOrEmpty(RawQuery)) - { - var strings = RawQuery.Split(' '); - if (strings.Length > 0) - { - return strings[0]; - } - } + /// + /// The raw query splited into a string array. + /// + public string[] Terms { get; } - return string.Empty; - } + public const string Seperater = " "; + + internal string ActionKeyword { get; set; } internal bool IsIntantQuery { get; set; } /// /// Return first search split by space if it has /// - public string FirstSearch - { - get - { - return SplitSearch(0); - } - } + public string FirstSearch => SplitSearch(0); /// /// strings from second search (including) to last search @@ -53,56 +43,34 @@ namespace Wox.Plugin { get { - if (string.IsNullOrEmpty(Search)) return string.Empty; - - var strings = Search.Split(' '); - if (strings.Length > 1) - { - return Search.Substring(Search.IndexOf(' ') + 1); - } - return string.Empty; + var index = string.IsNullOrEmpty(ActionKeyword) ? 1 : 2; + return string.Join(Seperater, Terms.Skip(index).ToArray()); } } /// /// Return second search split by space if it has /// - public string SecondSearch - { - get - { - return SplitSearch(1); - } - } + public string SecondSearch => SplitSearch(1); /// /// Return third search split by space if it has /// - public string ThirdSearch - { - get - { - return SplitSearch(2); - } - } + public string ThirdSearch => SplitSearch(2); private string SplitSearch(int index) { - if (string.IsNullOrEmpty(Search)) return string.Empty; - - var strings = Search.Split(' '); - if (strings.Length > index) + try { - return strings[index]; + return string.IsNullOrEmpty(ActionKeyword) ? Terms[index] : Terms[index + 1]; + } + catch (IndexOutOfRangeException) + { + return string.Empty; } - - return string.Empty; } - public override string ToString() - { - return RawQuery; - } + public override string ToString() => RawQuery; [Obsolete("Use Search instead, A plugin developer shouldn't care about action name, as it may changed by users. " + "this property will be removed in v1.3.0")] @@ -113,7 +81,10 @@ namespace Wox.Plugin public Query(string rawQuery) { - RawQuery = rawQuery; + // replace multiple white spaces with one white space + Terms = rawQuery.Split(new[] { Seperater }, StringSplitOptions.RemoveEmptyEntries); + RawQuery = string.Join(Seperater, Terms.ToArray()); + ActionParameters = new List(); ParseQuery(); } diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 10e878eee3..bfb09795c7 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -452,8 +452,11 @@ namespace Wox return; } - lastQuery = tbQuery.Text; + queryHasReturn = false; + Query query = new Query(tbQuery.Text); + lastQuery = query.RawQuery; int searchDelay = GetSearchDelay(lastQuery); + query.IsIntantQuery = searchDelay == 0; Dispatcher.DelayInvoke("UpdateSearch", () => @@ -466,9 +469,6 @@ namespace Wox // didn't. if (pnlResult.Dirty) pnlResult.Clear(); }, TimeSpan.FromMilliseconds(100)); - queryHasReturn = false; - Query query = new Query(lastQuery); - query.IsIntantQuery = searchDelay == 0; Query(query); Dispatcher.DelayInvoke("ShowProgressbar", () => {