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 const string ActionKeywordWildcardSign = "*";
private static List<PluginMetadata> pluginMetadatas;
private static List<KeyValuePair<PluginPair, IInstantQuery>> instantSearches;
private static List<KeyValuePair<PluginPair, IExclusiveQuery>> 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<PluginPair> AllPlugins
@ -135,17 +143,10 @@ namespace Wox.Core.Plugin
/// <summary>
/// Check if a query contains valid action keyword
/// </summary>
/// <param name="query"></param>
/// <param name="actionKeyword"></param>
/// <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);
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.

View File

@ -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.
/// </summary>
public string RawQuery { get; internal set; }
public string RawQuery { get; }
/// <summary>
/// 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
/// </summary>
public string Search { get; internal set; }
internal string GetActionKeyword()
{
if (!string.IsNullOrEmpty(RawQuery))
{
var strings = RawQuery.Split(' ');
if (strings.Length > 0)
{
return strings[0];
}
}
/// <summary>
/// The raw query splited into a string array.
/// </summary>
public string[] Terms { get; }
return string.Empty;
}
public const string Seperater = " ";
internal string ActionKeyword { get; set; }
internal bool IsIntantQuery { get; set; }
/// <summary>
/// Return first search split by space if it has
/// </summary>
public string FirstSearch
{
get
{
return SplitSearch(0);
}
}
public string FirstSearch => SplitSearch(0);
/// <summary>
/// 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());
}
}
/// <summary>
/// Return second search split by space if it has
/// </summary>
public string SecondSearch
{
get
{
return SplitSearch(1);
}
}
public string SecondSearch => SplitSearch(1);
/// <summary>
/// Return third search split by space if it has
/// </summary>
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<string>();
ParseQuery();
}

View File

@ -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", () =>
{