Refactoring Query

This commit is contained in:
bao-qian
2015-10-31 16:02:56 +00:00
parent 251834143c
commit a6f8eb28f2
3 changed files with 47 additions and 75 deletions

View File

@@ -22,6 +22,7 @@ namespace Wox.Core.Plugin
public static class PluginManager public static class PluginManager
{ {
public const string ActionKeywordWildcardSign = "*"; public const string ActionKeywordWildcardSign = "*";
private static List<PluginMetadata> pluginMetadatas; private static List<PluginMetadata> pluginMetadatas;
private static List<KeyValuePair<PluginPair, IInstantQuery>> instantSearches; private static List<KeyValuePair<PluginPair, IInstantQuery>> instantSearches;
private static List<KeyValuePair<PluginPair, IExclusiveQuery>> exclusiveSearchPlugins; private static List<KeyValuePair<PluginPair, IExclusiveQuery>> exclusiveSearchPlugins;
@@ -117,11 +118,18 @@ namespace Wox.Core.Plugin
public static void Query(Query query) 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; query.ActionKeyword = query.Terms[0];
QueryDispatcher.QueryDispatcher.Dispatch(query);
} }
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<PluginPair> AllPlugins public static List<PluginPair> AllPlugins
@@ -135,17 +143,10 @@ namespace Wox.Core.Plugin
/// <summary> /// <summary>
/// Check if a query contains valid action keyword /// Check if a query contains valid action keyword
/// </summary> /// </summary>
/// <param name="query"></param> /// <param name="actionKeyword"></param>
/// <returns></returns> /// <returns></returns>
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); PluginPair pair = plugins.FirstOrDefault(o => o.Metadata.ActionKeyword == actionKeyword);
if (pair != null) if (pair != null)
{ {
@@ -247,10 +248,10 @@ namespace Wox.Core.Plugin
internal static PluginPair GetActionKeywordPlugin(Query query) 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 a query doesn't contain a vaild action keyword, it should not be a action keword plugin query
if (!query.RawQuery.Contains(" ")) return null; 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) if (actionKeywordPluginPair != null)
{ {
var customizedPluginConfig = UserSettingStorage.Instance. var customizedPluginConfig = UserSettingStorage.Instance.

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Wox.Plugin namespace Wox.Plugin
{ {
@@ -9,42 +10,31 @@ 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; internal set; } public string RawQuery { get; }
/// <summary> /// <summary>
/// Search part of a query. /// Search part of a query.
/// This will not include action keyword if exclusive plugin gets it, otherwise it should be same as RawQuery. /// 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 /// Since we allow user to switch a exclusive plugin to generic plugin,
/// the query /// so this property will always give you the "real" query part of the query
/// </summary> /// </summary>
public string Search { get; internal set; } public string Search { get; internal set; }
internal string GetActionKeyword() /// <summary>
{ /// The raw query splited into a string array.
if (!string.IsNullOrEmpty(RawQuery)) /// </summary>
{ public string[] Terms { get; }
var strings = RawQuery.Split(' ');
if (strings.Length > 0)
{
return strings[0];
}
}
return string.Empty; public const string Seperater = " ";
}
internal string ActionKeyword { get; set; }
internal bool IsIntantQuery { get; set; } internal bool IsIntantQuery { get; set; }
/// <summary> /// <summary>
/// Return first search split by space if it has /// Return first search split by space if it has
/// </summary> /// </summary>
public string FirstSearch public string FirstSearch => SplitSearch(0);
{
get
{
return SplitSearch(0);
}
}
/// <summary> /// <summary>
/// strings from second search (including) to last search /// strings from second search (including) to last search
@@ -53,56 +43,34 @@ namespace Wox.Plugin
{ {
get get
{ {
if (string.IsNullOrEmpty(Search)) return string.Empty; var index = string.IsNullOrEmpty(ActionKeyword) ? 1 : 2;
return string.Join(Seperater, Terms.Skip(index).ToArray());
var strings = Search.Split(' ');
if (strings.Length > 1)
{
return Search.Substring(Search.IndexOf(' ') + 1);
}
return string.Empty;
} }
} }
/// <summary> /// <summary>
/// Return second search split by space if it has /// Return second search split by space if it has
/// </summary> /// </summary>
public string SecondSearch public string SecondSearch => SplitSearch(1);
{
get
{
return SplitSearch(1);
}
}
/// <summary> /// <summary>
/// Return third search split by space if it has /// Return third search split by space if it has
/// </summary> /// </summary>
public string ThirdSearch public string ThirdSearch => SplitSearch(2);
{
get
{
return SplitSearch(2);
}
}
private string SplitSearch(int index) private string SplitSearch(int index)
{ {
if (string.IsNullOrEmpty(Search)) return string.Empty; try
var strings = Search.Split(' ');
if (strings.Length > index)
{ {
return strings[index]; return string.IsNullOrEmpty(ActionKeyword) ? Terms[index] : Terms[index + 1];
}
catch (IndexOutOfRangeException)
{
return string.Empty;
} }
return string.Empty;
} }
public override string ToString() public override string ToString() => RawQuery;
{
return RawQuery;
}
[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")]
@@ -113,7 +81,10 @@ namespace Wox.Plugin
public Query(string rawQuery) 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<string>(); ActionParameters = new List<string>();
ParseQuery(); ParseQuery();
} }

View File

@@ -452,8 +452,11 @@ namespace Wox
return; return;
} }
lastQuery = tbQuery.Text; queryHasReturn = false;
Query query = new Query(tbQuery.Text);
lastQuery = query.RawQuery;
int searchDelay = GetSearchDelay(lastQuery); int searchDelay = GetSearchDelay(lastQuery);
query.IsIntantQuery = searchDelay == 0;
Dispatcher.DelayInvoke("UpdateSearch", Dispatcher.DelayInvoke("UpdateSearch",
() => () =>
@@ -466,9 +469,6 @@ namespace Wox
// didn't. // didn't.
if (pnlResult.Dirty) pnlResult.Clear(); if (pnlResult.Dirty) pnlResult.Clear();
}, TimeSpan.FromMilliseconds(100)); }, TimeSpan.FromMilliseconds(100));
queryHasReturn = false;
Query query = new Query(lastQuery);
query.IsIntantQuery = searchDelay == 0;
Query(query); Query(query);
Dispatcher.DelayInvoke("ShowProgressbar", () => Dispatcher.DelayInvoke("ShowProgressbar", () =>
{ {