// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Collections.Generic; using System.Linq; using Mono.Collections.Generic; namespace Wox.Plugin { public class Query { internal Query() { } /// /// Initializes a new instance of the class. /// to allow unit tests for plug ins /// public Query(string query, string actionKeyword = "") { _query = query; ActionKeyword = actionKeyword; } private string _rawQuery; /// /// Gets 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 { if (_rawQuery == null) { _rawQuery = string.Join(Query.TermSeparator, _query.Split(new[] { TermSeparator }, StringSplitOptions.RemoveEmptyEntries)); } return _rawQuery; } } private string _search; /// /// Gets 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 { if (_search == null) { _search = RawQuery.Substring(ActionKeyword.Length).Trim(); } return _search; } } private ReadOnlyCollection _terms; /// /// Gets the raw query splited into a string array. /// public ReadOnlyCollection Terms { get { if (_terms == null) { var terms = _query .Trim() .Substring(ActionKeyword.Length) .Split(new[] { TermSeparator }, StringSplitOptions.RemoveEmptyEntries); _terms = new ReadOnlyCollection(terms); } return _terms; } } /// /// Query can be splited into multiple terms by whitespace /// public const string TermSeparator = " "; /// /// User can set multiple action keywords separated by ';' /// public const string ActionKeywordSeparator = ";"; /// /// '*' is used for System Plugin /// public const string GlobalPluginWildcardSign = "*"; public string ActionKeyword { get; set; } /// /// Gets return first search split by space if it has /// public string FirstSearch => SplitSearch(0); /// /// Gets strings from second search (including) to last search /// public string SecondToEndSearch { get { var index = string.IsNullOrEmpty(ActionKeyword) ? 1 : 2; return string.Join(TermSeparator, Terms.Skip(index).ToArray()); } } /// /// Gets return second search split by space if it has /// public string SecondSearch => SplitSearch(1); /// /// Gets return third search split by space if it has /// public string ThirdSearch => SplitSearch(2); private string SplitSearch(int index) { try { return string.IsNullOrEmpty(ActionKeyword) ? Terms[index] : Terms[index + 1]; } catch (IndexOutOfRangeException) { return string.Empty; } } private string _query; public override string ToString() => RawQuery; [Obsolete("Use Search instead, this method will be removed in v1.3.0")] public string GetAllRemainingParameter() => Search; } }