Skip to content

Commit d1c6a4a

Browse files
devvaannshabose
authored andcommitted
fix: show exact matches before partial matches
1 parent d42d14c commit d1c6a4a

File tree

1 file changed

+22
-2
lines changed
  • src/extensionsIntegrated/CustomSnippets

1 file changed

+22
-2
lines changed

src/extensionsIntegrated/CustomSnippets/helper.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,38 @@ define(function (require, exports, module) {
250250
* Gets all snippets that match the query (prefix matches)
251251
* @param {string} query - The search query
252252
* @param {Editor} editor - The editor instance
253-
* @returns {Array} - Array of matching snippets
253+
* @returns {Array} - an array of matching snippets, sorted with exact matches first
254254
*/
255255
function getMatchingSnippets(query, editor) {
256256
const queryLower = query.toLowerCase();
257257
const languageContext = getCurrentLanguageContext(editor);
258258

259-
return Global.SnippetHintsList.filter((snippet) => {
259+
const matchingSnippets = Global.SnippetHintsList.filter((snippet) => {
260260
if (snippet.abbreviation.toLowerCase().startsWith(queryLower)) {
261261
return isSnippetSupportedInLanguageContext(snippet, languageContext, editor);
262262
}
263263
return false;
264264
});
265+
266+
// sort snippets so that the exact matches will appear over the partial matches
267+
return matchingSnippets.sort((a, b) => {
268+
const aLower = a.abbreviation.toLowerCase();
269+
const bLower = b.abbreviation.toLowerCase();
270+
271+
// check if either is an exact match
272+
const aExact = aLower === queryLower;
273+
const bExact = bLower === queryLower;
274+
275+
// because exact matches appear first
276+
if (aExact && !bExact) {
277+
return -1;
278+
}
279+
if (bExact && !aExact) {
280+
return 1;
281+
}
282+
283+
return aLower.localeCompare(bLower);
284+
});
265285
}
266286

267287
/**

0 commit comments

Comments
 (0)