From d31e705d96707deeabdcb73e92b9282d6332075b Mon Sep 17 00:00:00 2001 From: Nathan Craddock Date: Mon, 28 Feb 2022 15:31:21 -0700 Subject: [PATCH] feat(#3): prioritize on filename coverage This further improves the ranking algorithm by prioritizing a match by how much of the name it matched. As a concrete example, assume that ./nested/test.txt and ./my_test.txt exist. Previous to this commit, a search for "test" would rank my_test.txt over test.txt because the rank would be the same, so the shorter path would be shown. With this change, test.txt would have a better rank because the query "test" matches a greater portion of the filename. This prioritization is only applied to filename matching for now, but maybe it could be applied in general. Closes #3 --- src/filter.zig | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/filter.zig b/src/filter.zig index c1f2a80..da65d2d 100644 --- a/src/filter.zig +++ b/src/filter.zig @@ -203,11 +203,19 @@ pub fn rankToken( } else break; } - // retry on the full string if (best_rank != null) { // was a filename match, give priority best_rank.? /= 2.0; + + // how much of the token matched the filename? + if (token.len == name.?.len) { + best_rank.? /= 2.0; + } else { + const coverage = 1.0 - (@intToFloat(f64, token.len) / @intToFloat(f64, name.?.len)); + best_rank.? *= coverage; + } } else { + // retry on the full string it = IndexIterator.init(str, token[0], smart_case); while (it.next()) |start_index| { if (scanToEnd(str, token[1..], start_index, smart_case)) |match| {