Flow.Launcher/Flow.Launcher.Core/Plugin/QueryBuilder.cs

45 lines
1.5 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Plugin;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Core.Plugin
{
public static class QueryBuilder
{
public static Query Build(string text, Dictionary<string, PluginPair> nonGlobalPlugins)
{
// replace multiple white spaces with one white space
var terms = text.Split(Query.TermSeparator, StringSplitOptions.RemoveEmptyEntries);
if (terms.Length == 0)
{ // nothing was typed
return null;
}
2022-04-10 09:36:46 +00:00
var rawQuery = text;
string actionKeyword, search;
string possibleActionKeyword = terms[0];
string[] searchTerms;
if (nonGlobalPlugins.TryGetValue(possibleActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
{ // use non global plugin for query
actionKeyword = possibleActionKeyword;
2022-04-10 12:42:54 +00:00
search = terms.Length > 1 ? rawQuery[(actionKeyword.Length + 1)..].TrimStart() : string.Empty;
searchTerms = terms[1..];
}
else
{ // non action keyword
actionKeyword = string.Empty;
2022-04-10 12:42:54 +00:00
search = rawQuery.TrimStart();
searchTerms = terms;
}
return new Query ()
{
Search = search,
RawQuery = rawQuery,
SearchTerms = searchTerms,
ActionKeyword = actionKeyword
};
}
}
}