Skip to content

Commit d2283a1

Browse files
committed
add Sublime-like fuzzy search to file browser
1 parent 20ff4ac commit d2283a1

File tree

4 files changed

+96
-10
lines changed

4 files changed

+96
-10
lines changed

src/base/base_string.c

+79
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,85 @@ fuzzy_match_find(Arena *arena, String8 needle, String8 haystack)
16991699
scratch_end(scratch);
17001700
return result;
17011701
}
1702+
1703+
internal ScoredFuzzyMatchRangeList
1704+
scored_fuzzy_match_find(Arena *arena, String8 needle, String8 haystack)
1705+
{
1706+
Temp scratch = scratch_begin(0, 0);
1707+
// We're going to implement a very simple scoring mechanism similar to that described in
1708+
// https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/.
1709+
#define scored_fuzzy_match_unmatched -1
1710+
#define scored_fuzzy_match_consecutive 5
1711+
#define scored_fuzzy_match_unmatched_leading -3
1712+
ScoredFuzzyMatchRangeList invalid = {0};
1713+
ScoredFuzzyMatchRangeList result = {0};
1714+
// Simplify to a single needle which has common delimiters removed.
1715+
String8List needles = str8_split(scratch.arena, needle, (U8*)" ", 1, 0);
1716+
needle = str8_list_join(scratch.arena, &needles, 0);
1717+
if (needle.size == 0)
1718+
{
1719+
scratch_end(scratch);
1720+
return invalid;
1721+
}
1722+
String8 tmp_str = str8(needle.str, 1);
1723+
U64 find_pos = 0;
1724+
find_pos = str8_find_needle(haystack, find_pos, tmp_str, StringMatchFlag_CaseInsensitive);
1725+
if (find_pos >= haystack.size)
1726+
{
1727+
scratch_end(scratch);
1728+
return invalid;
1729+
}
1730+
// Leading character penalty.
1731+
// Only go to a max of 3 based on the article.
1732+
result.score += Min(find_pos, 3) * scored_fuzzy_match_unmatched_leading;
1733+
// We also want to deduct for additional unmatched characters between start and find_pos.
1734+
if (find_pos > 3)
1735+
{
1736+
result.score += (find_pos - 3) * scored_fuzzy_match_unmatched;
1737+
}
1738+
Rng1U64 range = r1u64(find_pos, find_pos + 1);
1739+
FuzzyMatchRangeNode *n = push_array(arena, FuzzyMatchRangeNode, 1);
1740+
n->range = range;
1741+
SLLQueuePush(result.list.first, result.list.last, n);
1742+
result.list.count += 1;
1743+
// Match the rest.
1744+
U64 prev_found = find_pos;
1745+
U64 search_start = 0;
1746+
find_pos += 1;
1747+
for (U64 idx = 1; idx < needle.size; ++idx)
1748+
{
1749+
tmp_str = str8(needle.str + idx, 1);
1750+
search_start = find_pos;
1751+
find_pos = str8_find_needle(haystack, find_pos, tmp_str, StringMatchFlag_CaseInsensitive);
1752+
if (find_pos >= haystack.size)
1753+
{
1754+
scratch_end(scratch);
1755+
return invalid;
1756+
}
1757+
// Compute consecutive bonus.
1758+
if (prev_found + 1 == find_pos)
1759+
{
1760+
result.score += scored_fuzzy_match_consecutive;
1761+
// We can reuse the existing node and simply extend it.
1762+
result.list.last->range.max = find_pos + 1;
1763+
}
1764+
else
1765+
{
1766+
result.score += (find_pos - search_start) * scored_fuzzy_match_unmatched;
1767+
Rng1U64 range = r1u64(find_pos, find_pos + 1);
1768+
FuzzyMatchRangeNode *n = push_array(arena, FuzzyMatchRangeNode, 1);
1769+
n->range = range;
1770+
SLLQueuePush(result.list.first, result.list.last, n);
1771+
result.list.count += 1;
1772+
}
1773+
prev_found = find_pos;
1774+
find_pos += 1;
1775+
}
1776+
// Compute final unmatched characters.
1777+
result.score += (haystack.size - find_pos) * scored_fuzzy_match_unmatched;
1778+
scratch_end(scratch);
1779+
return result;
1780+
}
17021781
////////////////////////////////
17031782
//~ NOTE(allen): Serialization Helpers
17041783

src/base/base_string.h

+8
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ struct FuzzyMatchRangeList
148148
U64 total_dim;
149149
};
150150

151+
typedef struct ScoredFuzzyMatchRangeList ScoredFuzzyMatchRangeList;
152+
struct ScoredFuzzyMatchRangeList
153+
{
154+
FuzzyMatchRangeList list;
155+
S32 score;
156+
};
157+
151158
////////////////////////////////
152159
//~ rjf: Character Classification & Conversion Functions
153160

@@ -342,6 +349,7 @@ internal Vec4F32 rgba_from_hex_string_4f32(String8 hex_string);
342349
//~ rjf: String Fuzzy Matching
343350

344351
internal FuzzyMatchRangeList fuzzy_match_find(Arena *arena, String8 needle, String8 haystack);
352+
internal ScoredFuzzyMatchRangeList scored_fuzzy_match_find(Arena *arena, String8 needles, String8 haystack);
345353

346354
////////////////////////////////
347355
//~ NOTE(allen): Serialization Helpers

src/df/gfx/df_views.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ df_qsort_compare_file_info__default(DF_FileInfo *a, DF_FileInfo *b)
2626
internal int
2727
df_qsort_compare_file_info__default_filtered(DF_FileInfo *a, DF_FileInfo *b)
2828
{
29-
int result = 0;
30-
if(a->filename.size < b->filename.size)
29+
if (a->match_ranges.score > b->match_ranges.score)
3130
{
32-
result = -1;
31+
return -1;
3332
}
34-
else if(a->filename.size > b->filename.size)
33+
if (a->match_ranges.score < b->match_ranges.score)
3534
{
36-
result = +1;
35+
return 1;
3736
}
38-
return result;
37+
return 0;
3938
}
4039

4140
internal int
@@ -2242,8 +2241,8 @@ DF_VIEW_UI_FUNCTION_DEF(FileSystem)
22422241
OS_FileIter *it = os_file_iter_begin(scratch.arena, path_query.path, 0);
22432242
for(OS_FileInfo info = {0}; os_file_iter_next(scratch.arena, it, &info);)
22442243
{
2245-
FuzzyMatchRangeList match_ranges = fuzzy_match_find(fs->cached_files_arena, path_query.search, info.name);
2246-
B32 fits_search = (path_query.search.size == 0 || match_ranges.count == match_ranges.needle_part_count);
2244+
ScoredFuzzyMatchRangeList match_ranges = scored_fuzzy_match_find(fs->cached_files_arena, path_query.search, info.name);
2245+
B32 fits_search = (path_query.search.size == 0 || match_ranges.list.count != 0);
22472246
B32 fits_dir_only = !!(info.props.flags & FilePropertyFlag_IsFolder) || !dir_selection;
22482247
if(fits_search && fits_dir_only)
22492248
{
@@ -2542,7 +2541,7 @@ DF_VIEW_UI_FUNCTION_DEF(FileSystem)
25422541
UI_PrefWidth(ui_pct(1, 0))
25432542
{
25442543
UI_Box *box = ui_build_box_from_stringf(UI_BoxFlag_DrawText, "%S##%p", file->filename, view);
2545-
df_box_equip_fuzzy_match_range_list_vis(box, file->match_ranges);
2544+
df_box_equip_fuzzy_match_range_list_vis(box, file->match_ranges.list);
25462545
}
25472546
}
25482547

src/df/gfx/df_views.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct DF_FileInfo
2222
{
2323
String8 filename;
2424
FileProperties props;
25-
FuzzyMatchRangeList match_ranges;
25+
ScoredFuzzyMatchRangeList match_ranges;
2626
};
2727

2828
typedef struct DF_FileInfoNode DF_FileInfoNode;

0 commit comments

Comments
 (0)