Files
PowerToys/src/modules/launcher/PowerLauncher/Plugin/QueryBuilder.cs
Heiko 979d3c6011 [PT Run] Results only for plugin with longest matching activation phrase (#15132)
* first try to fix with new idea

* small changes

* [PT Run] Updated QuryBuilder unit tests

* [PT Run] Updated QueryBuilder test

* [PT Run] Updated QueryBuilder test name

* Improvements

* Improve readability of test

* Improve Query-Tests for spellcheck

* clean expect.txt

Co-authored-by: cyberrex5 <ghassanjad2004@gmail.com>
2021-12-27 17:00:07 +01:00

72 lines
2.7 KiB
C#

// 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 Wox.Plugin;
namespace PowerLauncher.Plugin
{
public static class QueryBuilder
{
public static Dictionary<PluginPair, Query> Build(string text)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
text = text.Trim();
int longestActionKeywordLength = 0;
// This Dictionary contains the corresponding query for each plugin
Dictionary<PluginPair, Query> pluginQueryPair = new Dictionary<PluginPair, Query>();
foreach (var nonGlobalPlugin in PluginManager.NonGlobalPlugins)
{
var pluginActionKeyword = nonGlobalPlugin.Metadata.ActionKeyword;
if (nonGlobalPlugin.Metadata.Disabled || !text.StartsWith(pluginActionKeyword, StringComparison.Ordinal))
{
continue;
}
// Save the length of the longest matching keyword for later use
if (pluginActionKeyword.Length > longestActionKeywordLength)
{
longestActionKeywordLength = pluginActionKeyword.Length;
}
// A new query is constructed for each plugin
var query = new Query(text, pluginActionKeyword);
pluginQueryPair.TryAdd(nonGlobalPlugin, query);
}
// If we have plugin action keywords that start with the same char we get false positives (Example: ? and ??)
// Here we remove each query pair that has a shorter keyword than the longest matching one
foreach (PluginPair plugin in pluginQueryPair.Keys)
{
if (plugin.Metadata.ActionKeyword.Length < longestActionKeywordLength)
{
pluginQueryPair.Remove(plugin);
}
}
// If the user has specified a matching action keyword, then do not
// add the global plugins to the list.
if (pluginQueryPair.Count == 0)
{
foreach (PluginPair globalPlugin in PluginManager.GlobalPlugins)
{
if (!globalPlugin.Metadata.Disabled && !pluginQueryPair.ContainsKey(globalPlugin))
{
var query = new Query(text);
pluginQueryPair.Add(globalPlugin, query);
}
}
}
return pluginQueryPair;
}
}
}