Skip to content

Commit e5cc2cc

Browse files
authored
Merge pull request #145 from jjw24/pluginInitFail
plugin init fail continue - create query builder and re-enable tests.
2 parents cecb65c + e1e7387 commit e5cc2cc

File tree

11 files changed

+120
-92
lines changed

11 files changed

+120
-92
lines changed

Plugins/Wox.Plugin.PluginIndicator/Main.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public List<Result> Query(Query query)
1313
var results = from keyword in PluginManager.NonGlobalPlugins.Keys
1414
where keyword.StartsWith(query.Terms[0])
1515
let metadata = PluginManager.NonGlobalPlugins[keyword].Metadata
16-
let disabled = PluginManager.Settings.Plugins[metadata.ID].Disabled
17-
where !disabled
16+
where !metadata.Disabled
1817
select new Result
1918
{
2019
Title = keyword,

Wox.Core/Plugin/PluginManager.cs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static void InitializePlugins(IPublicAPI api)
120120
catch (Exception e)
121121
{
122122
Log.Exception(nameof(PluginManager), $"Fail to Init plugin: {pair.Metadata.Name}", e);
123-
pair.Metadata.Disabled = true; // TODO: not sure this really disable it later on
123+
pair.Metadata.Disabled = true;
124124
failedPlugins.Enqueue(pair);
125125
}
126126
});
@@ -149,35 +149,6 @@ public static void InstallPlugin(string path)
149149
PluginInstaller.Install(path);
150150
}
151151

152-
public static Query QueryInit(string text) //todo is that possible to move it into type Query?
153-
{
154-
// replace multiple white spaces with one white space
155-
var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
156-
var rawQuery = string.Join(Query.TermSeperater, terms);
157-
var actionKeyword = string.Empty;
158-
var search = rawQuery;
159-
var actionParameters = terms.ToList();
160-
if (terms.Length == 0) return null;
161-
if (NonGlobalPlugins.ContainsKey(terms[0]) &&
162-
!Settings.Plugins[NonGlobalPlugins[terms[0]].Metadata.ID].Disabled)
163-
{
164-
actionKeyword = terms[0];
165-
actionParameters = terms.Skip(1).ToList();
166-
search = string.Join(Query.TermSeperater, actionParameters.ToArray());
167-
}
168-
var query = new Query
169-
{
170-
Terms = terms,
171-
RawQuery = rawQuery,
172-
ActionKeyword = actionKeyword,
173-
Search = search,
174-
// Obsolete value initialisation
175-
ActionName = actionKeyword,
176-
ActionParameters = actionParameters
177-
};
178-
return query;
179-
}
180-
181152
public static List<PluginPair> ValidPluginsForQuery(Query query)
182153
{
183154
if (NonGlobalPlugins.ContainsKey(query.ActionKeyword))

Wox.Core/Plugin/QueryBuilder.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Wox.Plugin;
5+
6+
namespace Wox.Core.Plugin
7+
{
8+
public static class QueryBuilder
9+
{
10+
public static Query Build(string text, Dictionary<string, PluginPair> nonGlobalPlugins)
11+
{
12+
// replace multiple white spaces with one white space
13+
var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
14+
if (terms.Length == 0)
15+
{ // nothing was typed
16+
return null;
17+
}
18+
19+
var rawQuery = string.Join(Query.TermSeperater, terms);
20+
string actionKeyword, search;
21+
string possibleActionKeyword = terms[0];
22+
List<string> actionParameters;
23+
if (nonGlobalPlugins.TryGetValue(possibleActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
24+
{ // use non global plugin for query
25+
actionKeyword = possibleActionKeyword;
26+
actionParameters = terms.Skip(1).ToList();
27+
search = rawQuery.Substring(actionKeyword.Length + 1);
28+
}
29+
else
30+
{ // non action keyword
31+
actionKeyword = string.Empty;
32+
actionParameters = terms.ToList();
33+
search = rawQuery;
34+
}
35+
36+
var query = new Query
37+
{
38+
Terms = terms,
39+
RawQuery = rawQuery,
40+
ActionKeyword = actionKeyword,
41+
Search = search,
42+
// Obsolete value initialisation
43+
ActionName = actionKeyword,
44+
ActionParameters = actionParameters
45+
};
46+
47+
return query;
48+
}
49+
}
50+
}

Wox.Core/Wox.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
</Compile>
5454
<Compile Include="Plugin\ExecutablePlugin.cs" />
5555
<Compile Include="Plugin\PluginsLoader.cs" />
56+
<Compile Include="Plugin\QueryBuilder.cs" />
5657
<Compile Include="Updater.cs" />
5758
<Compile Include="Resource\AvailableLanguages.cs" />
5859
<Compile Include="Resource\Internationalization.cs" />

Wox.Infrastructure/UserSettings/PluginSettings.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void UpdatePluginSettings(List<PluginMetadata> metadatas)
2828
{
2929
ID = metadata.ID,
3030
Name = metadata.Name,
31-
ActionKeywords = metadata.ActionKeywords,
31+
ActionKeywords = metadata.ActionKeywords,
3232
Disabled = metadata.Disabled
3333
};
3434
}
@@ -39,7 +39,11 @@ public class Plugin
3939
{
4040
public string ID { get; set; }
4141
public string Name { get; set; }
42-
public List<string> ActionKeywords { get; set; }
42+
public List<string> ActionKeywords { get; set; } // a reference of the action keywords from plugin manager
43+
44+
/// <summary>
45+
/// Used only to save the state of the plugin in settings
46+
/// </summary>
4347
public bool Disabled { get; set; }
4448
}
4549
}

Wox.Test/QueryBuilderTest.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Collections.Generic;
2+
using NUnit.Framework;
3+
using Wox.Core.Plugin;
4+
using Wox.Plugin;
5+
6+
namespace Wox.Test
7+
{
8+
public class QueryBuilderTest
9+
{
10+
[Test]
11+
public void ExclusivePluginQueryTest()
12+
{
13+
var nonGlobalPlugins = new Dictionary<string, PluginPair>
14+
{
15+
{">", new PluginPair {Metadata = new PluginMetadata {ActionKeywords = new List<string> {">"}}}}
16+
};
17+
18+
Query q = QueryBuilder.Build("> file.txt file2 file3", nonGlobalPlugins);
19+
20+
Assert.AreEqual("file.txt file2 file3", q.Search);
21+
Assert.AreEqual(">", q.ActionKeyword);
22+
}
23+
24+
[Test]
25+
public void ExclusivePluginQueryIgnoreDisabledTest()
26+
{
27+
var nonGlobalPlugins = new Dictionary<string, PluginPair>
28+
{
29+
{">", new PluginPair {Metadata = new PluginMetadata {ActionKeywords = new List<string> {">"}, Disabled = true}}}
30+
};
31+
32+
Query q = QueryBuilder.Build("> file.txt file2 file3", nonGlobalPlugins);
33+
34+
Assert.AreEqual("> file.txt file2 file3", q.Search);
35+
}
36+
37+
[Test]
38+
public void GenericPluginQueryTest()
39+
{
40+
Query q = QueryBuilder.Build("file.txt file2 file3", new Dictionary<string, PluginPair>());
41+
42+
Assert.AreEqual("file.txt file2 file3", q.Search);
43+
Assert.AreEqual("", q.ActionKeyword);
44+
45+
Assert.AreEqual("file.txt", q.FirstSearch);
46+
Assert.AreEqual("file2", q.SecondSearch);
47+
Assert.AreEqual("file3", q.ThirdSearch);
48+
Assert.AreEqual("file2 file3", q.SecondToEndSearch);
49+
}
50+
}
51+
}

Wox.Test/QueryTest.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

Wox.Test/Wox.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
</Compile>
4646
<Compile Include="FuzzyMatcherTest.cs" />
4747
<Compile Include="Plugins\PluginInitTest.cs" />
48-
<Compile Include="QueryTest.cs" />
48+
<Compile Include="QueryBuilderTest.cs" />
4949
<Compile Include="Properties\AssemblyInfo.cs" />
5050
<Compile Include="UrlPluginTest.cs" />
5151
</ItemGroup>

Wox/SettingWindow.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ private void OnAddCustomeHotkeyClick(object sender, RoutedEventArgs e)
215215
private void OnPluginToggled(object sender, RoutedEventArgs e)
216216
{
217217
var id = _viewModel.SelectedPlugin.PluginPair.Metadata.ID;
218-
_settings.PluginSettings.Plugins[id].Disabled = _viewModel.SelectedPlugin.PluginPair.Metadata.Disabled;
218+
// used to sync the current status from the plugin manager into the setting to keep consistency after save
219+
_settings.PluginSettings.Plugins[id].Disabled = _viewModel.SelectedPlugin.PluginPair.Metadata.Disabled;
219220
}
220221

221222
private void OnPluginActionKeywordsClick(object sender, MouseButtonEventArgs e)

Wox/ViewModel/MainViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ private void QueryResults()
377377

378378
ProgressBarVisibility = Visibility.Hidden;
379379
_isQueryRunning = true;
380-
var query = PluginManager.QueryInit(QueryText.Trim());
380+
var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
381381
if (query != null)
382382
{
383383
// handle the exclusiveness of plugin using action keyword
@@ -401,8 +401,7 @@ private void QueryResults()
401401
{
402402
Parallel.ForEach(plugins, parallelOptions, plugin =>
403403
{
404-
var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID];
405-
if (!config.Disabled)
404+
if (!plugin.Metadata.Disabled)
406405
{
407406
var results = PluginManager.QueryForPlugin(plugin, query);
408407
UpdateResultView(results, plugin.Metadata, query);

0 commit comments

Comments
 (0)