Refactoring Query initialisation

This commit is contained in:
bao-qian
2015-11-03 05:09:54 +00:00
parent f5d3df65b0
commit 288ac62448
4 changed files with 36 additions and 66 deletions

View File

@@ -1,8 +1,10 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
using System.Windows.Documents;
using Wox.Core.Exception; using Wox.Core.Exception;
using Wox.Core.i18n; using Wox.Core.i18n;
using Wox.Core.UI; using Wox.Core.UI;
@@ -116,23 +118,33 @@ namespace Wox.Core.Plugin
PluginInstaller.Install(path); 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; // replace multiple white spaces with one white space
query.Search = query.RawQuery; var terms = text.Split(new[] { Query.Seperater }, StringSplitOptions.RemoveEmptyEntries);
if (query.Terms.Length == 0) return; var rawQuery = string.Join(Query.Seperater, terms.ToArray());
if (IsVailldActionKeyword(query.Terms[0])) var actionKeyword = string.Empty;
var search = rawQuery;
IEnumerable<string> 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 ? var pluginPairs = GetNonSystemPlugin(query) != null ?
new List<PluginPair> { GetNonSystemPlugin(query) } : GetSystemPlugins(); new List<PluginPair> { GetNonSystemPlugin(query) } : GetSystemPlugins();

View File

@@ -10,7 +10,7 @@ namespace Wox.Plugin
/// Raw query, this includes action keyword if it has /// Raw query, this includes action keyword if it has
/// We didn't recommend use this property directly. You should always use Search property. /// We didn't recommend use this property directly. You should always use Search property.
/// </summary> /// </summary>
public string RawQuery { get; } public string RawQuery { get; internal set; }
/// <summary> /// <summary>
/// Search part of a query. /// Search part of a query.
@@ -23,7 +23,7 @@ namespace Wox.Plugin
/// <summary> /// <summary>
/// The raw query splited into a string array. /// The raw query splited into a string array.
/// </summary> /// </summary>
public string[] Terms { get; } internal string[] Terms { private get; set; }
public const string Seperater = " "; 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. " + [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")] "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")] [Obsolete("Use Search instead, this property will be removed in v1.3.0")]
public List<string> ActionParameters { get; private set; } public List<string> ActionParameters { get; internal 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<string>();
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]);
}
}
}
[Obsolete("Use Search instead, this method will be removed in v1.3.0")] [Obsolete("Use Search instead, this method will be removed in v1.3.0")]
public string GetAllRemainingParameter() public string GetAllRemainingParameter() => Search;
{
string[] strings = RawQuery.Split(new char[] { ' ' }, 2, StringSplitOptions.None);
if (strings.Length > 1)
{
return strings[1];
}
return String.Empty;
}
} }
} }

View File

@@ -1,4 +1,5 @@
using NUnit.Framework; using NUnit.Framework;
using Wox.Core.Plugin;
using Wox.Plugin; using Wox.Plugin;
namespace Wox.Test namespace Wox.Test
@@ -8,8 +9,7 @@ namespace Wox.Test
[Test] [Test]
public void ExclusivePluginQueryTest() public void ExclusivePluginQueryTest()
{ {
Query q = new Query("f file.txt file2 file3"); Query q = PluginManager.QueryInit("> file.txt file2 file3");
q.Search = "file.txt file2 file3";
Assert.AreEqual(q.FirstSearch, "file.txt"); Assert.AreEqual(q.FirstSearch, "file.txt");
Assert.AreEqual(q.SecondSearch, "file2"); Assert.AreEqual(q.SecondSearch, "file2");
@@ -20,8 +20,7 @@ namespace Wox.Test
[Test] [Test]
public void GenericPluginQueryTest() public void GenericPluginQueryTest()
{ {
Query q = new Query("file.txt file2 file3"); Query q = PluginManager.QueryInit("file.txt file2 file3");
q.Search = q.RawQuery;
Assert.AreEqual(q.FirstSearch, "file.txt"); Assert.AreEqual(q.FirstSearch, "file.txt");
Assert.AreEqual(q.SecondSearch, "file2"); Assert.AreEqual(q.SecondSearch, "file2");

View File

@@ -445,7 +445,6 @@ namespace Wox
toolTip.IsOpen = false; toolTip.IsOpen = false;
pnlResult.Dirty = true; pnlResult.Dirty = true;
if (IsInContextMenuMode) if (IsInContextMenuMode)
{ {
QueryContextMenu(); QueryContextMenu();
@@ -453,8 +452,6 @@ namespace Wox
} }
queryHasReturn = false; queryHasReturn = false;
Query query = new Query(tbQuery.Text);
lastQuery = query.RawQuery;
Dispatcher.DelayInvoke("ClearResults", () => Dispatcher.DelayInvoke("ClearResults", () =>
{ {
@@ -464,7 +461,7 @@ namespace Wox
// didn't. // didn't.
if (pnlResult.Dirty) pnlResult.Clear(); if (pnlResult.Dirty) pnlResult.Clear();
}, TimeSpan.FromMilliseconds(100)); }, TimeSpan.FromMilliseconds(100));
Query(query); Query(tbQuery.Text);
Dispatcher.DelayInvoke("ShowProgressbar", () => Dispatcher.DelayInvoke("ShowProgressbar", () =>
{ {
if (!queryHasReturn && !string.IsNullOrEmpty(lastQuery)) if (!queryHasReturn && !string.IsNullOrEmpty(lastQuery))
@@ -480,9 +477,11 @@ namespace Wox
{ {
QueryHistoryStorage.Instance.Reset(); 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(); StopProgress();
} }