Skip to content

Commit

Permalink
feat(#3): prioritize on filename coverage
Browse files Browse the repository at this point in the history
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
  • Loading branch information
natecraddock committed Mar 4, 2022
1 parent ef63125 commit d31e705
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/filter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down

0 comments on commit d31e705

Please sign in to comment.