match: fzf: prioritize matches at word boundary, like fzf
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Mark Stosberg · committed by Mark Stosberg · parent 785c444 · Browse files · 4 files·+32 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Fixes: #695
Diff stats
4 files+32 added−1 removed
- tests:+3 added−0 removed
- other:+29 added−1 removed
Provenance
- Commit
6554dbe- Tree
8452588- Author
Mark Stosberg <mark@stosberg.com>- Committer
Mark Stosberg <mark@stosberg.com>- Parent
785c444
4 files changed+32 added−1 removed
modified:test/match-mode-fzf.fish+3 added
23 unmodified lines24 @test "shorter matches should match first" $got = long25 26 test_fzf_mode "\nbcdef\nabcd\n" cd27 @test "matched sub-string is closer to the beginning is sorted first." $got = bcdef28+29+test_fzf_mode "\nSteam\nMicrosoft Teams\n" tea30+@test "matched sub-string at word boundary is sorted first." $got = 'Microsoft Teams'modified:CHANGELOG.md+3 added-1 removed
58 unmodified lines59 if not specified at all ([#670][670]).60 * Improved scrolling with mouse wheel and touchpad ([#673][673]).61 * Incorrect touch coordinates with desktop scaling enabled62 ([#686][686]).63+* --match-mode=fzf now prioritizes matching at word boundaries,64+ matching fzf's behavior. ([#695][695])65 66 [658]: https://codeberg.org/dnkl/fuzzel/issues/65867 [670]: https://codeberg.org/dnkl/fuzzel/issues/67068 [673]: https://codeberg.org/dnkl/fuzzel/issues/67369 [686]: https://codeberg.org/dnkl/fuzzel/issues/68670+[695]: https://codeberg.org/dnkl/fuzzel/issues/69571 69-72 ### Security73 ### Contributors74 75 * Mark Stosberg1046 unmodified linesmodified:match.c+25 added
98 unmodified lines99 };100 101 static int match_thread(void *_ctx);102 103+static bool104+is_word_boundary(const char32_t *str, size_t pos)105+{106+ /* Position 0 is always a word boundary */107+ if (pos == 0)108+ return true;109+110+ /* Check if character before is a space/separator */111+ char32_t prev = str[pos - 1];112+ return isc32space(prev);113+}114+115 static char32_t *116 match_exact(const char32_t *haystack, size_t haystack_len,117 const char32_t *needle, size_t needle_len)118 {703 unmodified lines822 else823 return 1;824 }825 826+ /* Prioritize matches at word boundaries */827+ if (a->word_boundary != b->word_boundary) {828+ return a->word_boundary ? -1 : 1;829+ }830 else if (a->score > b->score)831 return -1;832 else if (a->score < b->score)833 return 1;509 unmodified lines1343 free(pos);1344 return;1345 }1346 1347+ /* Check if match starts at word boundary */1348+ bool word_boundary = false;1349+ if (match_name && pos_count > 0) {1350+ /* Check if first match position is at a word boundary in the title */1351+ word_boundary = is_word_boundary(app->title_lowercase, pos[0].start);1352+ }1353+1354 struct match m = {1355 .matched_type = app_match_type,1356 .application = app,1357 .pos = pos,1358 .pos_count = pos_count,1359 .score = score,1360+ .word_boundary = word_boundary,1361 };1362 1363 const size_t idx = matches->match_count++;1364 123 unmodified lines1488 .matched_type = MATCHED_NONE,1489 .application = matches->applications->v[i],1490 .pos = NULL,1491 .pos_count = 0,1492+ .word_boundary = false,1493 };1494 }1495 1496 /* Sort */250 unmodified linesmodified:match.h+1 added
23 unmodified lines24 struct application *application;25 struct match_substring *pos;26 size_t pos_count;27 size_t score;28+ bool word_boundary; /* True if match starts at word boundary */29 };30 31 struct wayland;32 struct matches;49 unmodified lines