create query builder and re-enable tests.

make the plugin manager plugin data the source for disabled plugins
This commit is contained in:
clueless 2020-02-22 11:02:07 +02:00
parent cb9e045c7f
commit e1e7387c73
11 changed files with 120 additions and 92 deletions

View File

@ -13,8 +13,7 @@ namespace Wox.Plugin.PluginIndicator
var results = from keyword in PluginManager.NonGlobalPlugins.Keys
where keyword.StartsWith(query.Terms[0])
let metadata = PluginManager.NonGlobalPlugins[keyword].Metadata
let disabled = PluginManager.Settings.Plugins[metadata.ID].Disabled
where !disabled
where !metadata.Disabled
select new Result
{
Title = keyword,

View File

@ -120,7 +120,7 @@ namespace Wox.Core.Plugin
catch (Exception e)
{
Log.Exception(nameof(PluginManager), $"Fail to Init plugin: {pair.Metadata.Name}", e);
pair.Metadata.Disabled = true; // TODO: not sure this really disable it later on
pair.Metadata.Disabled = true;
failedPlugins.Enqueue(pair);
}
});
@ -149,35 +149,6 @@ namespace Wox.Core.Plugin
PluginInstaller.Install(path);
}
public static Query QueryInit(string text) //todo is that possible to move it into type Query?
{
// replace multiple white spaces with one white space
var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
var rawQuery = string.Join(Query.TermSeperater, terms);
var actionKeyword = string.Empty;
var search = rawQuery;
var actionParameters = terms.ToList();
if (terms.Length == 0) return null;
if (NonGlobalPlugins.ContainsKey(terms[0]) &&
!Settings.Plugins[NonGlobalPlugins[terms[0]].Metadata.ID].Disabled)
{
actionKeyword = terms[0];
actionParameters = terms.Skip(1).ToList();
search = string.Join(Query.TermSeperater, actionParameters.ToArray());
}
var query = new Query
{
Terms = terms,
RawQuery = rawQuery,
ActionKeyword = actionKeyword,
Search = search,
// Obsolete value initialisation
ActionName = actionKeyword,
ActionParameters = actionParameters
};
return query;
}
public static List<PluginPair> ValidPluginsForQuery(Query query)
{
if (NonGlobalPlugins.ContainsKey(query.ActionKeyword))

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Wox.Plugin;
namespace Wox.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(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
if (terms.Length == 0)
{ // nothing was typed
return null;
}
var rawQuery = string.Join(Query.TermSeperater, terms);
string actionKeyword, search;
string possibleActionKeyword = terms[0];
List<string> actionParameters;
if (nonGlobalPlugins.TryGetValue(possibleActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
{ // use non global plugin for query
actionKeyword = possibleActionKeyword;
actionParameters = terms.Skip(1).ToList();
search = rawQuery.Substring(actionKeyword.Length + 1);
}
else
{ // non action keyword
actionKeyword = string.Empty;
actionParameters = terms.ToList();
search = rawQuery;
}
var query = new Query
{
Terms = terms,
RawQuery = rawQuery,
ActionKeyword = actionKeyword,
Search = search,
// Obsolete value initialisation
ActionName = actionKeyword,
ActionParameters = actionParameters
};
return query;
}
}
}

View File

@ -53,6 +53,7 @@
</Compile>
<Compile Include="Plugin\ExecutablePlugin.cs" />
<Compile Include="Plugin\PluginsLoader.cs" />
<Compile Include="Plugin\QueryBuilder.cs" />
<Compile Include="Updater.cs" />
<Compile Include="Resource\AvailableLanguages.cs" />
<Compile Include="Resource\Internationalization.cs" />

View File

@ -28,7 +28,7 @@ namespace Wox.Infrastructure.UserSettings
{
ID = metadata.ID,
Name = metadata.Name,
ActionKeywords = metadata.ActionKeywords,
ActionKeywords = metadata.ActionKeywords,
Disabled = metadata.Disabled
};
}
@ -39,7 +39,11 @@ namespace Wox.Infrastructure.UserSettings
{
public string ID { get; set; }
public string Name { get; set; }
public List<string> ActionKeywords { get; set; }
public List<string> ActionKeywords { get; set; } // a reference of the action keywords from plugin manager
/// <summary>
/// Used only to save the state of the plugin in settings
/// </summary>
public bool Disabled { get; set; }
}
}

View File

@ -0,0 +1,51 @@
using System.Collections.Generic;
using NUnit.Framework;
using Wox.Core.Plugin;
using Wox.Plugin;
namespace Wox.Test
{
public class QueryBuilderTest
{
[Test]
public void ExclusivePluginQueryTest()
{
var nonGlobalPlugins = new Dictionary<string, PluginPair>
{
{">", new PluginPair {Metadata = new PluginMetadata {ActionKeywords = new List<string> {">"}}}}
};
Query q = QueryBuilder.Build("> file.txt file2 file3", nonGlobalPlugins);
Assert.AreEqual("file.txt file2 file3", q.Search);
Assert.AreEqual(">", q.ActionKeyword);
}
[Test]
public void ExclusivePluginQueryIgnoreDisabledTest()
{
var nonGlobalPlugins = new Dictionary<string, PluginPair>
{
{">", new PluginPair {Metadata = new PluginMetadata {ActionKeywords = new List<string> {">"}, Disabled = true}}}
};
Query q = QueryBuilder.Build("> file.txt file2 file3", nonGlobalPlugins);
Assert.AreEqual("> file.txt file2 file3", q.Search);
}
[Test]
public void GenericPluginQueryTest()
{
Query q = QueryBuilder.Build("file.txt file2 file3", new Dictionary<string, PluginPair>());
Assert.AreEqual("file.txt file2 file3", q.Search);
Assert.AreEqual("", q.ActionKeyword);
Assert.AreEqual("file.txt", q.FirstSearch);
Assert.AreEqual("file2", q.SecondSearch);
Assert.AreEqual("file3", q.ThirdSearch);
Assert.AreEqual("file2 file3", q.SecondToEndSearch);
}
}
}

View File

@ -1,33 +0,0 @@
using NUnit.Framework;
using Wox.Core.Plugin;
using Wox.Plugin;
namespace Wox.Test
{
public class QueryTest
{
[Test]
[Ignore("Current query is tightly integrated with GUI, can't be tested.")]
public void ExclusivePluginQueryTest()
{
Query q = PluginManager.QueryInit("> file.txt file2 file3");
Assert.AreEqual(q.FirstSearch, "file.txt");
Assert.AreEqual(q.SecondSearch, "file2");
Assert.AreEqual(q.ThirdSearch, "file3");
Assert.AreEqual(q.SecondToEndSearch, "file2 file3");
}
[Test]
[Ignore("Current query is tightly integrated with GUI, can't be tested.")]
public void GenericPluginQueryTest()
{
Query q = PluginManager.QueryInit("file.txt file2 file3");
Assert.AreEqual(q.FirstSearch, "file.txt");
Assert.AreEqual(q.SecondSearch, "file2");
Assert.AreEqual(q.ThirdSearch, "file3");
Assert.AreEqual(q.SecondToEndSearch, "file2 file3");
}
}
}

View File

@ -45,7 +45,7 @@
</Compile>
<Compile Include="FuzzyMatcherTest.cs" />
<Compile Include="Plugins\PluginInitTest.cs" />
<Compile Include="QueryTest.cs" />
<Compile Include="QueryBuilderTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UrlPluginTest.cs" />
</ItemGroup>

View File

@ -215,7 +215,8 @@ namespace Wox
private void OnPluginToggled(object sender, RoutedEventArgs e)
{
var id = _viewModel.SelectedPlugin.PluginPair.Metadata.ID;
_settings.PluginSettings.Plugins[id].Disabled = _viewModel.SelectedPlugin.PluginPair.Metadata.Disabled;
// used to sync the current status from the plugin manager into the setting to keep consistency after save
_settings.PluginSettings.Plugins[id].Disabled = _viewModel.SelectedPlugin.PluginPair.Metadata.Disabled;
}
private void OnPluginActionKeywordsClick(object sender, MouseButtonEventArgs e)

View File

@ -377,7 +377,7 @@ namespace Wox.ViewModel
ProgressBarVisibility = Visibility.Hidden;
_isQueryRunning = true;
var query = PluginManager.QueryInit(QueryText.Trim());
var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
if (query != null)
{
// handle the exclusiveness of plugin using action keyword
@ -401,8 +401,7 @@ namespace Wox.ViewModel
{
Parallel.ForEach(plugins, parallelOptions, plugin =>
{
var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID];
if (!config.Disabled)
if (!plugin.Metadata.Disabled)
{
var results = PluginManager.QueryForPlugin(plugin, query);
UpdateResultView(results, plugin.Metadata, query);

View File

@ -150,26 +150,11 @@ namespace Wox.ViewModel
{
get
{
var plugins = PluginManager.AllPlugins;
var settings = Settings.PluginSettings.Plugins;
plugins.Sort((a, b) =>
{
var d1 = settings[a.Metadata.ID].Disabled;
var d2 = settings[b.Metadata.ID].Disabled;
if (d1 == d2)
{
return string.Compare(a.Metadata.Name, b.Metadata.Name, StringComparison.CurrentCulture);
}
else
{
return d1.CompareTo(d2);
}
});
var metadatas = plugins.Select(p => new PluginViewModel
{
PluginPair = p,
}).ToList();
var metadatas = PluginManager.AllPlugins
.OrderBy(x => x.Metadata.Disabled)
.ThenBy(y => y.Metadata.Name)
.Select(p => new PluginViewModel { PluginPair = p})
.ToList();
return metadatas;
}
}