Skip to content

Commit d597bd2

Browse files
authored
Hide commands with whitespace only names better (#38159)
This is a much tidier solution. Don't default _everything_ to a weight of 1 if the query is whitespace. Instead, do a simple string contains check (because FuzzySearch will beef it on just whitespace) Closes #38133 I originally based this off of #38157, so I know these two won't collide
1 parent 2623eb1 commit d597bd2

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs

+37-12
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,14 @@ private bool ActuallyLoading()
149149
// _always_ show up first.
150150
private int ScoreTopLevelItem(string query, IListItem topLevelOrAppItem)
151151
{
152-
if (string.IsNullOrWhiteSpace(query))
153-
{
154-
return 1;
155-
}
156-
157152
var title = topLevelOrAppItem.Title;
158-
if (string.IsNullOrEmpty(title))
153+
if (string.IsNullOrWhiteSpace(title))
159154
{
160155
return 0;
161156
}
162157

158+
var isWhiteSpace = string.IsNullOrWhiteSpace(query);
159+
163160
var isFallback = false;
164161
var isAliasSubstringMatch = false;
165162
var isAliasMatch = false;
@@ -179,17 +176,45 @@ private int ScoreTopLevelItem(string query, IListItem topLevelOrAppItem)
179176
extensionDisplayName = topLevel.ExtensionHost?.Extension?.PackageDisplayName ?? string.Empty;
180177
}
181178

182-
var nameMatch = StringMatcher.FuzzySearch(query, title);
183-
var descriptionMatch = StringMatcher.FuzzySearch(query, topLevelOrAppItem.Subtitle);
184-
var extensionTitleMatch = StringMatcher.FuzzySearch(query, extensionDisplayName);
179+
// StringMatcher.FuzzySearch will absolutely BEEF IT if you give it a
180+
// whitespace-only query.
181+
//
182+
// in that scenario, we'll just use a simple string contains for the
183+
// query. Maybe someone is really looking for things with a space in
184+
// them, I don't know.
185+
186+
// Title:
187+
// * whitespace query: 1 point
188+
// * otherwise full weight match
189+
var nameMatch = isWhiteSpace ?
190+
(title.Contains(query) ? 1 : 0) :
191+
StringMatcher.FuzzySearch(query, title).Score;
192+
193+
// Subtitle:
194+
// * whitespace query: 1/2 point
195+
// * otherwise ~half weight match. Minus a bit, because subtitles tend to be longer
196+
var descriptionMatch = isWhiteSpace ?
197+
(topLevelOrAppItem.Subtitle.Contains(query) ? .5 : 0) :
198+
(StringMatcher.FuzzySearch(query, topLevelOrAppItem.Subtitle).Score - 4) / 2.0;
199+
200+
// Extension title: despite not being visible, give the extension name itself some weight
201+
// * whitespace query: 0 points
202+
// * otherwise more weight than a subtitle, but not much
203+
var extensionTitleMatch = isWhiteSpace ? 0 : StringMatcher.FuzzySearch(query, extensionDisplayName).Score / 1.5;
204+
185205
var scores = new[]
186206
{
187-
nameMatch.Score,
188-
(descriptionMatch.Score - 4) / 2.0,
207+
nameMatch,
208+
descriptionMatch,
189209
isFallback ? 1 : 0, // Always give fallbacks a chance...
190210
};
191211
var max = scores.Max();
192-
max = max + (extensionTitleMatch.Score / 1.5);
212+
213+
// _Add_ the extension name. This will bubble items that match both
214+
// title and extension name up above ones that just match title.
215+
// e.g. "git" will up-weight "GitHub searches" from the GitHub extension
216+
// above "git" from "whatever"
217+
max = max + extensionTitleMatch;
193218

194219
// ... but downweight them
195220
var matchSomething = (max / (isFallback ? 3 : 1))

0 commit comments

Comments
 (0)