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

64 lines
2.1 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 originalQuery, string trimmedQuery, Dictionary<string, PluginPair> nonGlobalPlugins)
{
2025-05-04 01:38:35 +00:00
// home query
if (string.IsNullOrEmpty(trimmedQuery))
2025-05-04 01:38:35 +00:00
{
return new Query()
{
Search = string.Empty,
OriginalQuery = string.Empty,
TrimmedQuery = string.Empty,
2025-05-04 01:38:35 +00:00
SearchTerms = Array.Empty<string>(),
ActionKeyword = string.Empty,
IsHomeQuery = true
2025-05-04 01:38:35 +00:00
};
}
// replace multiple white spaces with one white space
var terms = trimmedQuery.Split(Query.TermSeparator, StringSplitOptions.RemoveEmptyEntries);
if (terms.Length == 0)
2025-05-04 01:38:35 +00:00
{
// nothing was typed
return null;
}
string actionKeyword, search;
string possibleActionKeyword = terms[0];
string[] searchTerms;
if (nonGlobalPlugins.TryGetValue(possibleActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
2025-05-04 01:38:35 +00:00
{
// use non global plugin for query
actionKeyword = possibleActionKeyword;
search = terms.Length > 1 ? trimmedQuery[(actionKeyword.Length + 1)..].TrimStart() : string.Empty;
searchTerms = terms[1..];
}
else
2025-05-04 01:38:35 +00:00
{
// non action keyword
actionKeyword = string.Empty;
search = trimmedQuery.TrimStart();
searchTerms = terms;
}
return new Query()
{
Search = search,
OriginalQuery = originalQuery,
TrimmedQuery = trimmedQuery,
SearchTerms = searchTerms,
ActionKeyword = actionKeyword,
IsHomeQuery = false
};
}
}
}