Skip to content

Commit

Permalink
fix: avoid falling back to fallback unnecessarily
Browse files Browse the repository at this point in the history
As noted here:

- #420 (comment)

It's possible to hit the max file limit (coming soon to a repo near you)
but have all the results hidden because they're in dot directories. As
such, `#results` can be 0 even though the scanner found stuff.

Avoid this by also returning candidate count, and not falling back
unless that is 0 as well.
  • Loading branch information
wincent committed Oct 14, 2023
1 parent 8e5e827 commit f883d00
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions bin/benchmarks/matcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ benchmark({
local input = ''
for letter in query:gmatch('.') do
local matches = lib.matcher_run(matcher, input)
for k = 0, matches.count - 1 do
for k = 0, matches.match_count - 1 do
local str = matches.matches[k]
ffi.string(str.contents, str.length)
end
input = input .. letter
end
local matches = lib.matcher_run(matcher, input)
for k = 0, matches.count - 1 do
for k = 0, matches.match_count - 1 do
local str = matches.matches[k]
ffi.string(str.contents, str.length)
end
Expand Down
7 changes: 4 additions & 3 deletions lua/wincent/commandt/lib/matcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,12 @@ result_t *commandt_matcher_run(matcher_t *matcher, const char *needle) {
result_t *results = xmalloc(sizeof(result_t));
unsigned count = matches_count > limit ? limit : matches_count;
results->matches = xmalloc(count * sizeof(const char *));
results->count = 0;
results->match_count = 0;
results->candidate_count = candidate_count;

for (long i = 0; i < count && results->count <= limit; i++) {
for (long i = 0; i < count && results->match_count <= limit; i++) {
if (matches[i]->score > 0.0f) {
results->matches[results->count++] = matches[i]->candidate;
results->matches[results->match_count++] = matches[i]->candidate;
}
}

Expand Down
3 changes: 2 additions & 1 deletion lua/wincent/commandt/lib/matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
// TODO: may later want to return highlight positions as well
typedef struct {
str_t **matches;
unsigned count;
unsigned match_count;
unsigned candidate_count;
} result_t;

/**
Expand Down
4 changes: 2 additions & 2 deletions lua/wincent/commandt/private/finders/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ return function(directory, command, options)
finder.run = function(query)
local results = lib.matcher_run(finder.matcher, query)
local strings = {}
for i = 0, results.count - 1 do
for i = 0, results.match_count - 1 do
local str = results.matches[i]
table.insert(strings, ffi.string(str.contents, str.length))
end
return strings
return strings, results.candidate_count
end
finder.open = options.open
return finder
Expand Down
4 changes: 2 additions & 2 deletions lua/wincent/commandt/private/finders/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ return function(directory, options)
finder.run = function(query)
local results = lib.matcher_run(finder.matcher, query)
local strings = {}
for i = 0, results.count - 1 do
for i = 0, results.match_count - 1 do
local str = results.matches[i]
table.insert(strings, ffi.string(str.contents, str.length))
end
return strings
return strings, results.candidate_count
end
finder.open = options.open
return finder
Expand Down
4 changes: 2 additions & 2 deletions lua/wincent/commandt/private/finders/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ return function(directory, candidates, options)
finder.run = function(query)
local results = lib.matcher_run(finder.matcher, query)
local strings = {}
for i = 0, results.count - 1 do
for i = 0, results.match_count - 1 do
local str = results.matches[i]
table.insert(strings, ffi.string(str.contents, str.length))
end
return strings
return strings, results.candidate_count
end
finder.open = options.open
return finder
Expand Down
4 changes: 2 additions & 2 deletions lua/wincent/commandt/private/finders/watchman.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ return function(directory, options)
finder.run = function(query)
local results = lib.matcher_run(finder.matcher, query)
local strings = {}
for i = 0, results.count - 1 do
for i = 0, results.match_count - 1 do
local str = results.matches[i]
table.insert(strings, ffi.string(str.contents, str.length))
end
return strings
return strings, results.candidate_count
end
finder.open = options.open
return finder
Expand Down
3 changes: 2 additions & 1 deletion lua/wincent/commandt/private/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ setmetatable(c, {
typedef struct {
str_t **matches;
unsigned count;
unsigned match_count;
unsigned candidate_count;
} result_t;
typedef struct {
Expand Down
5 changes: 3 additions & 2 deletions lua/wincent/commandt/private/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local ui = {}
local MatchListing = require('wincent.commandt.private.match_listing').MatchListing
local Prompt = require('wincent.commandt.private.prompt').Prompt

local candidate_count = nil
local cmdline_enter_autocmd = nil
local current_finder = nil -- Reference to avoid premature garbage collection.
local current_window = nil
Expand Down Expand Up @@ -94,8 +95,8 @@ ui.show = function(finder, options)
margin = options.margin,
name = options.name,
on_change = function(query)
results = current_finder.run(query)
if #results > 0 then
results, candidate_count = current_finder.run(query)
if #results > 0 or candidate_count > 0 then
-- Once we've proved a finder works, we don't ever want to use fallback.
current_finder.fallback = nil
elseif current_finder.fallback then
Expand Down
2 changes: 1 addition & 1 deletion lua/wincent/commandt/test/matcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('matcher.c', function()
match = function(query)
local results = lib.matcher_run(matcher, query)
local strings = {}
for k = 0, results.count - 1 do
for k = 0, results.match_count - 1 do
local str = results.matches[k]
table.insert(strings, ffi.string(str.contents, str.length))
end
Expand Down

0 comments on commit f883d00

Please sign in to comment.