using System;
namespace Flow.Launcher.Plugin
{
public class Query
{
public Query() { }
///
/// to allow unit tests for plug ins
///
public Query(string rawQuery, string search, string[] terms, string[] searchTerms, string actionKeyword = "")
{
Search = search;
RawQuery = rawQuery;
#pragma warning disable CS0618
Terms = terms;
#pragma warning restore CS0618
SearchTerms = searchTerms;
ActionKeyword = actionKeyword;
}
///
/// 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; internal init; }
///
/// Determines whether the query was forced to execute again.
/// For example, the value will be true when the user presses Ctrl + R.
/// When this property is true, plugins handling this query should avoid serving cached results.
///
public bool IsReQuery { get; internal set; } = false;
///
/// 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
///
public string Search { get; internal init; }
///
/// The search string split into a string array.
///
public string[] SearchTerms { get; init; }
///
/// The raw query split into a string array
///
[Obsolete("It may or may not include action keyword, which can be confusing. Use SearchTerms instead")]
public string[] Terms { get; init; }
///
/// Query can be splited into multiple terms by whitespace
///
public const string TermSeparator = " ";
[Obsolete("Typo")]
public const string TermSeperater = TermSeparator;
///
/// User can set multiple action keywords seperated by ';'
///
public const string ActionKeywordSeparator = ";";
[Obsolete("Typo")]
public const string ActionKeywordSeperater = ActionKeywordSeparator;
///
/// '*' is used for System Plugin
///
public const string GlobalPluginWildcardSign = "*";
public string ActionKeyword { get; init; }
///
/// Return first search split by space if it has
///
public string FirstSearch => SplitSearch(0);
private string _secondToEndSearch;
///
/// strings from second search (including) to last search
///
public string SecondToEndSearch => SearchTerms.Length > 1 ? (_secondToEndSearch ??= string.Join(' ', SearchTerms[1..])) : "";
///
/// Return second search split by space if it has
///
public string SecondSearch => SplitSearch(1);
///
/// Return third search split by space if it has
///
public string ThirdSearch => SplitSearch(2);
private string SplitSearch(int index)
{
return index < SearchTerms.Length ? SearchTerms[index] : string.Empty;
}
public override string ToString() => RawQuery;
}
}