Skip to content

Commit 3a91c95

Browse files
committed
feat: improve match goodness algorithm to show more useful hints
1 parent c0c1fc4 commit 3a91c95

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -908,12 +908,37 @@ define(function (require, exports, module) {
908908

909909
// filter folders using fuzzy matching
910910
const matches = folderList
911-
.map(folder => stringMatcher.match(folder, query))
912-
.filter(result => result !== null && result !== undefined)
913-
.sort((a, b) => b.matchGoodness - a.matchGoodness)
914-
.slice(0, 5);
911+
.map(folder => {
912+
const result = stringMatcher.match(folder, query);
913+
if (result) {
914+
// get the last folder name (e.g., "assets/images/" -> "images")
915+
const folderPath = result.label || folder;
916+
const segments = folderPath.split('/').filter(s => s.length > 0);
917+
const lastSegment = segments[segments.length - 1] || '';
918+
result.folderName = lastSegment.toLowerCase();
919+
920+
// we need to boost the score significantly if the last folder segment starts with the query
921+
// This ensures folders like "images/" rank higher than "testing/maps/google/" when typing "image"
922+
// note: here testing/maps/google has all the chars of 'image'
923+
if (lastSegment.toLowerCase().startsWith(query.toLowerCase())) {
924+
// Use a large positive boost (matchGoodness is negative, so we subtract a large negative number)
925+
result.matchGoodness -= 10000;
926+
}
927+
// Also boost (but less) if the last segment contains the query as a substring
928+
else if (lastSegment.toLowerCase().includes(query.toLowerCase())) {
929+
result.matchGoodness -= 1000;
930+
}
931+
}
932+
return result;
933+
})
934+
.filter(result => result !== null && result !== undefined);
935+
936+
// Sort by matchGoodness first (prefix matches will have best scores),
937+
// then alphabetically by folder name, then by full path
938+
StringMatch.multiFieldSort(matches, { matchGoodness: 0, folderName: 1, label: 2 });
915939

916-
_renderFolderSuggestions(matches, $suggestions, $input);
940+
const topMatches = matches.slice(0, 5);
941+
_renderFolderSuggestions(topMatches, $suggestions, $input);
917942
}
918943

919944
/**

src/htmlContent/image-folder-dialog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ <h1 class="dialog-title">Select Folder to Save Image</h1>
1515
<div id="folder-suggestions"></div>
1616

1717
<p class="folder-help-text">
18-
💡 Tip: Type to filter folders or create a new path
18+
💡 Type folder path or leave empty to download in project root
1919
</p>
2020
</div>
2121
<div class="modal-footer">

0 commit comments

Comments
 (0)