Skip to content

Commit a8e4c50

Browse files
committed
Move MatchResult to Flow.Launcher.Plugin so that plugins can utilize main method
1 parent b67f5de commit a8e4c50

File tree

9 files changed

+129
-118
lines changed

9 files changed

+129
-118
lines changed

Flow.Launcher.Infrastructure/StringMatcher.cs

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Flow.Launcher.Plugin.SharedModel;
12
using System;
23
using System.Collections.Generic;
34
using System.ComponentModel;
@@ -239,75 +240,9 @@ private static int CalculateSearchScore(string query, string stringToCompare, in
239240

240241
return score;
241242
}
242-
243-
public enum SearchPrecisionScore
244-
{
245-
Regular = 50,
246-
Low = 20,
247-
None = 0
248-
}
249243
}
250244

251-
public class MatchResult
252-
{
253-
public MatchResult(bool success, SearchPrecisionScore searchPrecision)
254-
{
255-
Success = success;
256-
SearchPrecision = searchPrecision;
257-
}
258-
259-
public MatchResult(bool success, SearchPrecisionScore searchPrecision, List<int> matchData, int rawScore)
260-
{
261-
Success = success;
262-
SearchPrecision = searchPrecision;
263-
MatchData = matchData;
264-
RawScore = rawScore;
265-
}
266-
267-
public bool Success { get; set; }
268-
269-
/// <summary>
270-
/// The final score of the match result with search precision filters applied.
271-
/// </summary>
272-
public int Score { get; private set; }
273-
274-
/// <summary>
275-
/// The raw calculated search score without any search precision filtering applied.
276-
/// </summary>
277-
private int _rawScore;
278-
279-
public int RawScore
280-
{
281-
get { return _rawScore; }
282-
set
283-
{
284-
_rawScore = value;
285-
Score = ScoreAfterSearchPrecisionFilter(_rawScore);
286-
}
287-
}
288-
289-
/// <summary>
290-
/// Matched data to highlight.
291-
/// </summary>
292-
public List<int> MatchData { get; set; }
293-
294-
public SearchPrecisionScore SearchPrecision { get; set; }
295-
296-
public bool IsSearchPrecisionScoreMet()
297-
{
298-
return IsSearchPrecisionScoreMet(_rawScore);
299-
}
300-
301-
private bool IsSearchPrecisionScoreMet(int rawScore)
302-
{
303-
return rawScore >= (int)SearchPrecision;
304-
}
305-
306-
private int ScoreAfterSearchPrecisionFilter(int rawScore)
307-
{
308-
return IsSearchPrecisionScoreMet(rawScore) ? rawScore : 0;
309-
}
310-
}
245+
311246

312247
public class MatchOption
313248
{

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Newtonsoft.Json;
55
using Newtonsoft.Json.Converters;
66
using Flow.Launcher.Plugin;
7+
using Flow.Launcher.Plugin.SharedModel;
78

89
namespace Flow.Launcher.Infrastructure.UserSettings
910
{
@@ -30,7 +31,7 @@ public class Settings : BaseModel
3031
/// </summary>
3132
public bool ShouldUsePinyin { get; set; } = false;
3233

33-
internal StringMatcher.SearchPrecisionScore QuerySearchPrecision { get; private set; } = StringMatcher.SearchPrecisionScore.Regular;
34+
internal SearchPrecisionScore QuerySearchPrecision { get; private set; } = SearchPrecisionScore.Regular;
3435

3536
[JsonIgnore]
3637
public string QuerySearchPrecisionString
@@ -40,8 +41,8 @@ public string QuerySearchPrecisionString
4041
{
4142
try
4243
{
43-
var precisionScore = (StringMatcher.SearchPrecisionScore)Enum
44-
.Parse(typeof(StringMatcher.SearchPrecisionScore), value);
44+
var precisionScore = (SearchPrecisionScore)Enum
45+
.Parse(typeof(SearchPrecisionScore), value);
4546

4647
QuerySearchPrecision = precisionScore;
4748
StringMatcher.Instance.UserSettingSearchPrecision = precisionScore;
@@ -50,8 +51,8 @@ public string QuerySearchPrecisionString
5051
{
5152
Logger.Log.Exception(nameof(Settings), "Failed to load QuerySearchPrecisionString value from Settings file", e);
5253

53-
QuerySearchPrecision = StringMatcher.SearchPrecisionScore.Regular;
54-
StringMatcher.Instance.UserSettingSearchPrecision = StringMatcher.SearchPrecisionScore.Regular;
54+
QuerySearchPrecision = SearchPrecisionScore.Regular;
55+
StringMatcher.Instance.UserSettingSearchPrecision = SearchPrecisionScore.Regular;
5556

5657
throw;
5758
}

Flow.Launcher.Plugin/Flow.Launcher.Plugin.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
1+
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.1</TargetFramework>

Flow.Launcher.Plugin/IPublicAPI.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Flow.Launcher.Plugin.SharedModel;
2+
using System;
23
using System.Collections.Generic;
34

45
namespace Flow.Launcher.Plugin
@@ -89,6 +90,6 @@ public interface IPublicAPI
8990
/// </summary>
9091
event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent;
9192

92-
public (List<int> MatchedData, int Score, bool Success) MatchString(string query, string stringToCompare);
93+
public MatchResult FuzzySearch(string query, string stringToCompare);
9394
}
9495
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Flow.Launcher.Plugin.SharedModel
6+
{
7+
public class MatchResult
8+
{
9+
public MatchResult(bool success, SearchPrecisionScore searchPrecision)
10+
{
11+
Success = success;
12+
SearchPrecision = searchPrecision;
13+
}
14+
15+
public MatchResult(bool success, SearchPrecisionScore searchPrecision, List<int> matchData, int rawScore)
16+
{
17+
Success = success;
18+
SearchPrecision = searchPrecision;
19+
MatchData = matchData;
20+
RawScore = rawScore;
21+
}
22+
23+
public bool Success { get; set; }
24+
25+
/// <summary>
26+
/// The final score of the match result with search precision filters applied.
27+
/// </summary>
28+
public int Score { get; private set; }
29+
30+
/// <summary>
31+
/// The raw calculated search score without any search precision filtering applied.
32+
/// </summary>
33+
private int _rawScore;
34+
35+
public int RawScore
36+
{
37+
get { return _rawScore; }
38+
set
39+
{
40+
_rawScore = value;
41+
Score = ScoreAfterSearchPrecisionFilter(_rawScore);
42+
}
43+
}
44+
45+
/// <summary>
46+
/// Matched data to highlight.
47+
/// </summary>
48+
public List<int> MatchData { get; set; }
49+
50+
public SearchPrecisionScore SearchPrecision { get; set; }
51+
52+
public bool IsSearchPrecisionScoreMet()
53+
{
54+
return IsSearchPrecisionScoreMet(_rawScore);
55+
}
56+
57+
private bool IsSearchPrecisionScoreMet(int rawScore)
58+
{
59+
return rawScore >= (int)SearchPrecision;
60+
}
61+
62+
private int ScoreAfterSearchPrecisionFilter(int rawScore)
63+
{
64+
return IsSearchPrecisionScoreMet(rawScore) ? rawScore : 0;
65+
}
66+
}
67+
68+
public enum SearchPrecisionScore
69+
{
70+
Regular = 50,
71+
Low = 20,
72+
None = 0
73+
}
74+
}

Flow.Launcher.Test/FuzzyMatcherTest.cs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using NUnit.Framework;
66
using Flow.Launcher.Infrastructure;
77
using Flow.Launcher.Plugin;
8+
using Flow.Launcher.Plugin.SharedModel;
89

910
namespace Flow.Launcher.Test
1011
{
@@ -37,8 +38,8 @@ public List<int> GetPrecisionScores()
3738
{
3839
var listToReturn = new List<int>();
3940

40-
Enum.GetValues(typeof(StringMatcher.SearchPrecisionScore))
41-
.Cast<StringMatcher.SearchPrecisionScore>()
41+
Enum.GetValues(typeof(SearchPrecisionScore))
42+
.Cast<SearchPrecisionScore>()
4243
.ToList()
4344
.ForEach(x => listToReturn.Add((int)x));
4445

@@ -145,20 +146,20 @@ public void WhenGivenQueryString_ThenShouldReturn_TheDesiredScoring(
145146
$"Expected score for compare string '{compareString}': {expectedScore}, Actual: {rawScore}");
146147
}
147148

148-
[TestCase("goo", "Google Chrome", StringMatcher.SearchPrecisionScore.Regular, true)]
149-
[TestCase("chr", "Google Chrome", StringMatcher.SearchPrecisionScore.Low, true)]
150-
[TestCase("chr", "Chrome", StringMatcher.SearchPrecisionScore.Regular, true)]
151-
[TestCase("chr", "Help cure hope raise on mind entity Chrome", StringMatcher.SearchPrecisionScore.Regular, false)]
152-
[TestCase("chr", "Help cure hope raise on mind entity Chrome", StringMatcher.SearchPrecisionScore.Low, true)]
153-
[TestCase("chr", "Candy Crush Saga from King", StringMatcher.SearchPrecisionScore.Regular, false)]
154-
[TestCase("chr", "Candy Crush Saga from King", StringMatcher.SearchPrecisionScore.None, true)]
155-
[TestCase("ccs", "Candy Crush Saga from King", StringMatcher.SearchPrecisionScore.Low, true)]
156-
[TestCase("cand", "Candy Crush Saga from King",StringMatcher.SearchPrecisionScore.Regular, true)]
157-
[TestCase("cand", "Help cure hope raise on mind entity Chrome", StringMatcher.SearchPrecisionScore.Regular, false)]
149+
[TestCase("goo", "Google Chrome", SearchPrecisionScore.Regular, true)]
150+
[TestCase("chr", "Google Chrome", SearchPrecisionScore.Low, true)]
151+
[TestCase("chr", "Chrome", SearchPrecisionScore.Regular, true)]
152+
[TestCase("chr", "Help cure hope raise on mind entity Chrome", SearchPrecisionScore.Regular, false)]
153+
[TestCase("chr", "Help cure hope raise on mind entity Chrome", SearchPrecisionScore.Low, true)]
154+
[TestCase("chr", "Candy Crush Saga from King", SearchPrecisionScore.Regular, false)]
155+
[TestCase("chr", "Candy Crush Saga from King", SearchPrecisionScore.None, true)]
156+
[TestCase("ccs", "Candy Crush Saga from King", SearchPrecisionScore.Low, true)]
157+
[TestCase("cand", "Candy Crush Saga from King",SearchPrecisionScore.Regular, true)]
158+
[TestCase("cand", "Help cure hope raise on mind entity Chrome", SearchPrecisionScore.Regular, false)]
158159
public void WhenGivenDesiredPrecision_ThenShouldReturn_AllResultsGreaterOrEqual(
159160
string queryString,
160161
string compareString,
161-
StringMatcher.SearchPrecisionScore expectedPrecisionScore,
162+
SearchPrecisionScore expectedPrecisionScore,
162163
bool expectedPrecisionResult)
163164
{
164165
// When
@@ -182,32 +183,32 @@ public void WhenGivenDesiredPrecision_ThenShouldReturn_AllResultsGreaterOrEqual(
182183
$"Precision Score: {(int)expectedPrecisionScore}");
183184
}
184185

185-
[TestCase("exce", "OverLeaf-Latex: An online LaTeX editor", StringMatcher.SearchPrecisionScore.Regular, false)]
186-
[TestCase("term", "Windows Terminal (Preview)", StringMatcher.SearchPrecisionScore.Regular, true)]
187-
[TestCase("sql s managa", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
188-
[TestCase("sql' s manag", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
189-
[TestCase("sql s manag", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
190-
[TestCase("sql manag", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
191-
[TestCase("sql", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
192-
[TestCase("sql serv", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
193-
[TestCase("servez", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
194-
[TestCase("sql servz", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
195-
[TestCase("sql serv man", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
196-
[TestCase("sql studio", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
197-
[TestCase("mic", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
198-
[TestCase("chr", "Shutdown", StringMatcher.SearchPrecisionScore.Regular, false)]
199-
[TestCase("mssms", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
200-
[TestCase("chr", "Change settings for text-to-speech and for speech recognition (if installed).", StringMatcher.SearchPrecisionScore.Regular, false)]
201-
[TestCase("ch r", "Change settings for text-to-speech and for speech recognition (if installed).", StringMatcher.SearchPrecisionScore.Regular, true)]
202-
[TestCase("a test", "This is a test", StringMatcher.SearchPrecisionScore.Regular, true)]
203-
[TestCase("test", "This is a test", StringMatcher.SearchPrecisionScore.Regular, true)]
204-
[TestCase("cod", VisualStudioCode, StringMatcher.SearchPrecisionScore.Regular, true)]
205-
[TestCase("code", VisualStudioCode, StringMatcher.SearchPrecisionScore.Regular, true)]
206-
[TestCase("codes", "Visual Studio Codes", StringMatcher.SearchPrecisionScore.Regular, true)]
186+
[TestCase("exce", "OverLeaf-Latex: An online LaTeX editor", SearchPrecisionScore.Regular, false)]
187+
[TestCase("term", "Windows Terminal (Preview)", SearchPrecisionScore.Regular, true)]
188+
[TestCase("sql s managa", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, false)]
189+
[TestCase("sql' s manag", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, false)]
190+
[TestCase("sql s manag", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, true)]
191+
[TestCase("sql manag", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, true)]
192+
[TestCase("sql", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, true)]
193+
[TestCase("sql serv", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, true)]
194+
[TestCase("servez", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, false)]
195+
[TestCase("sql servz", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, false)]
196+
[TestCase("sql serv man", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, true)]
197+
[TestCase("sql studio", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, true)]
198+
[TestCase("mic", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, true)]
199+
[TestCase("chr", "Shutdown", SearchPrecisionScore.Regular, false)]
200+
[TestCase("mssms", MicrosoftSqlServerManagementStudio, SearchPrecisionScore.Regular, false)]
201+
[TestCase("chr", "Change settings for text-to-speech and for speech recognition (if installed).", SearchPrecisionScore.Regular, false)]
202+
[TestCase("ch r", "Change settings for text-to-speech and for speech recognition (if installed).", SearchPrecisionScore.Regular, true)]
203+
[TestCase("a test", "This is a test", SearchPrecisionScore.Regular, true)]
204+
[TestCase("test", "This is a test", SearchPrecisionScore.Regular, true)]
205+
[TestCase("cod", VisualStudioCode, SearchPrecisionScore.Regular, true)]
206+
[TestCase("code", VisualStudioCode, SearchPrecisionScore.Regular, true)]
207+
[TestCase("codes", "Visual Studio Codes", SearchPrecisionScore.Regular, true)]
207208
public void WhenGivenQuery_ShouldReturnResults_ContainingAllQuerySubstrings(
208209
string queryString,
209210
string compareString,
210-
StringMatcher.SearchPrecisionScore expectedPrecisionScore,
211+
SearchPrecisionScore expectedPrecisionScore,
211212
bool expectedPrecisionResult)
212213
{
213214
// When
@@ -238,7 +239,7 @@ public void WhenGivenAQuery_Scoring_ShouldGiveMoreWeightToStartOfNewWord(
238239
string queryString, string compareString1, string compareString2)
239240
{
240241
// When
241-
var matcher = new StringMatcher { UserSettingSearchPrecision = StringMatcher.SearchPrecisionScore.Regular };
242+
var matcher = new StringMatcher { UserSettingSearchPrecision = SearchPrecisionScore.Regular };
242243

243244
// Given
244245
var compareString1Result = matcher.FuzzyMatch(queryString, compareString1);

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Flow.Launcher.Infrastructure.Image;
1515
using Flow.Launcher.Plugin;
1616
using Flow.Launcher.ViewModel;
17+
using Flow.Launcher.Plugin.SharedModel;
1718

1819
namespace Flow.Launcher
1920
{
@@ -132,11 +133,7 @@ public List<PluginPair> GetAllPlugins()
132133

133134
public event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent;
134135

135-
public (List<int> MatchedData, int Score, bool Success) MatchString(string query, string stringToCompare)
136-
{
137-
var result = StringMatcher.FuzzySearch(query, stringToCompare);
138-
return (result.MatchData, result.Score, result.Success);
139-
}
136+
public MatchResult FuzzySearch(string query, string stringToCompare) => StringMatcher.FuzzySearch(query, stringToCompare);
140137

141138
#endregion
142139

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Flow.Launcher.Infrastructure.Storage;
1818
using Flow.Launcher.Infrastructure.UserSettings;
1919
using Flow.Launcher.Plugin;
20+
using Flow.Launcher.Plugin.SharedModel;
2021

2122
namespace Flow.Launcher.ViewModel
2223
{
@@ -152,7 +153,7 @@ public List<string> QuerySearchPrecisionStrings
152153
{
153154
var precisionStrings = new List<string>();
154155

155-
var enumList = Enum.GetValues(typeof(StringMatcher.SearchPrecisionScore)).Cast<StringMatcher.SearchPrecisionScore>().ToList();
156+
var enumList = Enum.GetValues(typeof(SearchPrecisionScore)).Cast<SearchPrecisionScore>().ToList();
156157

157158
enumList.ForEach(x => precisionStrings.Add(x.ToString()));
158159

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using Flow.Launcher.Infrastructure;
4+
using Flow.Launcher.Plugin.SharedModel;
45

56
namespace Flow.Launcher.Plugin.BrowserBookmark.Commands
67
{

0 commit comments

Comments
 (0)