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

@@ -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();
}