diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 2ba877c82f..1fb302882b 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -1,8 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Threading; +using System.Windows.Documents; using Wox.Core.Exception; using Wox.Core.i18n; using Wox.Core.UI; @@ -116,23 +118,33 @@ namespace Wox.Core.Plugin PluginInstaller.Install(path); } - public static void QueryForAllPlugins(Query query) + public static Query QueryInit(string text) //todo is that possible to move it into type Query? { - query.ActionKeyword = string.Empty; - query.Search = query.RawQuery; - if (query.Terms.Length == 0) return; - if (IsVailldActionKeyword(query.Terms[0])) + // replace multiple white spaces with one white space + var terms = text.Split(new[] { Query.Seperater }, StringSplitOptions.RemoveEmptyEntries); + var rawQuery = string.Join(Query.Seperater, terms.ToArray()); + var actionKeyword = string.Empty; + var search = rawQuery; + IEnumerable actionParameters = terms; + if (terms.Length == 0) return null; + if (IsVailldActionKeyword(terms[0])) { - query.ActionKeyword = query.Terms[0]; + actionKeyword = terms[0]; } - if (!string.IsNullOrEmpty(query.ActionKeyword)) + if (!string.IsNullOrEmpty(actionKeyword)) { - query.Search = string.Join(Query.Seperater, query.Terms.Skip(1).ToArray()); + actionParameters = terms.Skip(1); + search = string.Join(Query.Seperater, actionParameters.ToArray()); } - QueryDispatch(query); + return new Query + { + Terms = terms, RawQuery = rawQuery, ActionKeyword = actionKeyword, Search = search, + // Obsolete value initialisation + ActionName = actionKeyword, ActionParameters = actionParameters.ToList() + }; } - private static void QueryDispatch(Query query) + public static void QueryForAllPlugins(Query query) { var pluginPairs = GetNonSystemPlugin(query) != null ? new List { GetNonSystemPlugin(query) } : GetSystemPlugins(); diff --git a/Wox.Plugin/Query.cs b/Wox.Plugin/Query.cs index 86ee08e912..08422896f4 100644 --- a/Wox.Plugin/Query.cs +++ b/Wox.Plugin/Query.cs @@ -10,7 +10,7 @@ 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; } + public string RawQuery { get; internal set; } /// /// Search part of a query. @@ -23,7 +23,7 @@ namespace Wox.Plugin /// /// The raw query splited into a string array. /// - public string[] Terms { get; } + internal string[] Terms { private get; set; } public const string Seperater = " "; @@ -77,52 +77,12 @@ namespace Wox.Plugin [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")] - public string ActionName { get; private set; } + public string ActionName { get; internal set; } [Obsolete("Use Search instead, this property will be removed in v1.3.0")] - public List ActionParameters { get; private set; } - - public Query(string 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(); - } - - private void ParseQuery() - { - if (String.IsNullOrEmpty(RawQuery)) return; - - string[] strings = RawQuery.Split(' '); - //todo:not exactly correct. query that didn't containing a space should be a valid query - if (strings.Length == 1) return; //we consider a valid query must contain a space - - ActionName = strings[0]; - for (int i = 1; i < strings.Length; i++) - { - if (!String.IsNullOrEmpty(strings[i])) - { - ActionParameters.Add(strings[i]); - } - } - } + public List ActionParameters { get; internal set; } [Obsolete("Use Search instead, this method will be removed in v1.3.0")] - public string GetAllRemainingParameter() - { - - string[] strings = RawQuery.Split(new char[] { ' ' }, 2, StringSplitOptions.None); - if (strings.Length > 1) - { - return strings[1]; - } - - return String.Empty; - } - - + public string GetAllRemainingParameter() => Search; } } diff --git a/Wox.Test/QueryTest.cs b/Wox.Test/QueryTest.cs index 69aee10a11..fbe282b97b 100644 --- a/Wox.Test/QueryTest.cs +++ b/Wox.Test/QueryTest.cs @@ -1,4 +1,5 @@ using NUnit.Framework; +using Wox.Core.Plugin; using Wox.Plugin; namespace Wox.Test @@ -8,8 +9,7 @@ namespace Wox.Test [Test] public void ExclusivePluginQueryTest() { - Query q = new Query("f file.txt file2 file3"); - q.Search = "file.txt file2 file3"; + Query q = PluginManager.QueryInit("> file.txt file2 file3"); Assert.AreEqual(q.FirstSearch, "file.txt"); Assert.AreEqual(q.SecondSearch, "file2"); @@ -20,8 +20,7 @@ namespace Wox.Test [Test] public void GenericPluginQueryTest() { - Query q = new Query("file.txt file2 file3"); - q.Search = q.RawQuery; + Query q = PluginManager.QueryInit("file.txt file2 file3"); Assert.AreEqual(q.FirstSearch, "file.txt"); Assert.AreEqual(q.SecondSearch, "file2"); diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index e823b5d2f2..04f29a60b4 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -445,7 +445,6 @@ namespace Wox toolTip.IsOpen = false; pnlResult.Dirty = true; - if (IsInContextMenuMode) { QueryContextMenu(); @@ -453,8 +452,6 @@ namespace Wox } queryHasReturn = false; - Query query = new Query(tbQuery.Text); - lastQuery = query.RawQuery; Dispatcher.DelayInvoke("ClearResults", () => { @@ -464,7 +461,7 @@ namespace Wox // didn't. if (pnlResult.Dirty) pnlResult.Clear(); }, TimeSpan.FromMilliseconds(100)); - Query(query); + Query(tbQuery.Text); Dispatcher.DelayInvoke("ShowProgressbar", () => { if (!queryHasReturn && !string.IsNullOrEmpty(lastQuery)) @@ -480,9 +477,11 @@ namespace Wox { QueryHistoryStorage.Instance.Reset(); } - private void Query(Query q) + private void Query(string text) { - PluginManager.QueryForAllPlugins(q); + var query = PluginManager.QueryInit(text); + lastQuery = query?.RawQuery; + PluginManager.QueryForAllPlugins(query); StopProgress(); }