8000 mini.nvim/lua/mini/fuzzy.lua at main · tonyrt2/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
353 lines (313 loc) · 14 KB

fuzzy.lua

File metadata and controls

353 lines (313 loc) · 14 KB
--
-- Returns -1 if `positions` is `nil` or empty.
H.score_positions = function(positions)
if positions == nil or #positions == 0 then return -1 end
local first, last = positions[1], positions[#positions]
local cutoff = H.get_config().cutoff
return cutoff * math.min(last - first + 1, cutoff) + math.min(first, cutoff)
end
H.make_filter_indexes = function(word, candidate_array)
local res, letters = {}, H.string_to_letters(word)
for i, cand in ipairs(candidate_array) do
local positions = H.find_best_positions(letters, cand)
if positions ~= nil then table.insert(res, { index = i, score = H.score_positions(positions) }) end
end
return res
end
H.compare_filter_indexes = function(a, b) return a.score < b.score or (a.score == b.score and a.index < b.index) end
H.filter_by_indexes = function(candidate_array, ids)
local res, res_ids = {}, {}
for _, id in pairs(ids) do
table.insert(res, candidate_array[id.index])
table.insert(res_ids, id.index)
end
return res, res_ids
end
-- Utilities ------------------------------------------------------------------
H.error = function(msg) error('(mini.fuzzy) ' .. msg, 0) end
H.check_type = function(name, val, ref, allow_nil)
if type(val) == ref or (ref == 'callable' and vim.is_callable(val)) or (allow_nil and val == nil) then return end
H.error(string.format('`%s` should be %s, not %s', name, ref, type(val)))
end
H.string_to_letters = function(s) return vim.tbl_map(vim.pesc, vim.split(s, '')) end
return MiniFuzzy
0