Skip to content

Commit 617183b

Browse files
Refactor result matching logic
1 parent e6b8a0d commit 617183b

File tree

1 file changed

+74
-22
lines changed
  • Plugins/Flow.Launcher.Plugin.Program/Programs

1 file changed

+74
-22
lines changed

Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,51 +69,103 @@ public class Win32 : IProgram, IEquatable<Win32>
6969
Enabled = false
7070
};
7171

72+
private static MatchResult Match(string query, List<string> candidates)
73+
{
74+
if (candidates.Count == 0)
75+
return null;
76+
77+
List<MatchResult> matches = new List<MatchResult>();
78+
foreach(var candidate in candidates)
79+
{
80+
var match = StringMatcher.FuzzySearch(query, candidate);
81+
if (match.IsSearchPrecisionScoreMet())
82+
{
83+
matches.Add(match);
84+
}
85+
}
86+
if (matches.Count == 0)
87+
{
88+
return null;
89+
}
90+
else
91+
{
92+
return matches.MaxBy(match => match.Score);
93+
}
94+
}
7295

7396
public Result Result(string query, IPublicAPI api)
7497
{
7598
string title;
7699
MatchResult matchResult;
77100

78101
// Name of the result
79-
string resultName = string.IsNullOrEmpty(LocalizedName) ? Name : LocalizedName;
102+
// Check equality to avoid matching again in candidates
103+
bool useLocalizedName = !string.IsNullOrEmpty(LocalizedName) && !Name.Equals(LocalizedName);
104+
string resultName = useLocalizedName ? LocalizedName : Name;
80105

81-
// We suppose Name won't be null
82-
if (!Main._settings.EnableDescription || Description == null || resultName.StartsWith(Description))
106+
if (!Main._settings.EnableDescription)
83107
{
84108
title = resultName;
85-
matchResult = StringMatcher.FuzzySearch(query, title);
86-
}
87-
else if (Description.StartsWith(resultName))
88-
{
89-
title = Description;
90-
matchResult = StringMatcher.FuzzySearch(query, Description);
109+
matchResult = StringMatcher.FuzzySearch(query, resultName);
91110
}
92111
else
93112
{
94-
title = $"{resultName}: {Description}";
95-
var nameMatch = StringMatcher.FuzzySearch(query, resultName);
96-
var desciptionMatch = StringMatcher.FuzzySearch(query, Description);
97-
if (desciptionMatch.Score > nameMatch.Score)
113+
if (string.IsNullOrEmpty(Description) || resultName.StartsWith(Description))
98114
{
99-
for (int i = 0; i < desciptionMatch.MatchData.Count; i++)
115+
// Description is invalid or included in resultName
116+
// Description is always localized, so Name.StartsWith(Description) is generally useless
117+
title = resultName;
118+
matchResult = StringMatcher.FuzzySearch(query, resultName);
119+
}
120+
else if (Description.StartsWith(resultName))
121+
{
122+
// resultName included in Description
123+
title = Description;
124+
matchResult = StringMatcher.FuzzySearch(query, Description);
125+
}
126+
else
127+
{
128+
// Search in both
129+
title = $"{resultName}: {Description}";
130+
var nameMatch = StringMatcher.FuzzySearch(query, resultName);
131+
var descriptionMatch = StringMatcher.FuzzySearch(query, Description);
132+
if (descriptionMatch.Score > nameMatch.Score)
133+
{
134+
for (int i = 0; i < descriptionMatch.MatchData.Count; i++)
135+
{
136+
descriptionMatch.MatchData[i] += resultName.Length + 2; // 2 is ": "
137+
}
138+
matchResult = descriptionMatch;
139+
}
140+
else
100141
{
101-
desciptionMatch.MatchData[i] += resultName.Length + 2; // 2 is ": "
142+
matchResult = nameMatch;
102143
}
103-
matchResult = desciptionMatch;
104144
}
105-
else matchResult = nameMatch;
106145
}
107146

147+
List<string> candidates = new List<string>();
148+
108149
if (!matchResult.IsSearchPrecisionScoreMet())
109150
{
110151
if (ExecutableName != null) // only lnk program will need this one
111-
matchResult = StringMatcher.FuzzySearch(query, ExecutableName);
112-
113-
if (!matchResult.IsSearchPrecisionScoreMet())
152+
{
153+
candidates.Add(ExecutableName);
154+
}
155+
if (useLocalizedName)
156+
{
157+
candidates.Add(Name);
158+
}
159+
matchResult = Match(query, candidates);
160+
if (matchResult == null)
161+
{
114162
return null;
115-
116-
matchResult.MatchData = new List<int>();
163+
}
164+
else
165+
{
166+
// Nothing to highlight in title in this case
167+
matchResult.MatchData.Clear();
168+
}
117169
}
118170

119171
string subtitle = string.Empty;

0 commit comments

Comments
 (0)