telescope-frecency.nvim/lua/frecency/substr_sorter.lua
JINNOUCHI Yasushi c140e6ff9c
feat: make query() faster and more lazier (#241)
* refactor: simplify logic to load web_devicons

* refactor: make register() asynchronous

* fix: load lazily modules outside this plugin

* refactor: simplify logic to wait initialization

* refactor: use uv.hrtime() instead of os.clock()

* fix: avoid errors in calling plenary.log in async

* test: store elapsed time to check in tests

* test: fix module names

This becomes a problem only in Ubuntu because macOS and Windows does not
care cases in filenames.

* test: fix types and unused modules

* style: fix by stylua

* refactor: make recency / entry_maker loaded lazily
2024-08-25 19:28:52 +09:00

57 lines
1.5 KiB
Lua

-- TODO: use this module until telescope's release include this below.
-- https://github.com/nvim-telescope/telescope.nvim/pull/2950
local lazy_require = require "frecency.lazy_require"
local sorters = lazy_require "telescope.sorters"
local util = lazy_require "telescope.utils"
local substr_highlighter = function(make_display)
return function(_, prompt, display)
local highlights = {}
display = make_display(prompt, display)
local search_terms = util.max_split(prompt, "%s")
local hl_start, hl_end
for _, word in pairs(search_terms) do
hl_start, hl_end = display:find(word, 1, true)
if hl_start then
table.insert(highlights, { start = hl_start, finish = hl_end })
end
end
return highlights
end
end
return function()
local make_display = vim.o.smartcase
and function(prompt, display)
local has_upper_case = not not prompt:match "%u"
return has_upper_case and display or display:lower()
end
or function(_, display)
return display:lower()
end
return sorters.Sorter:new {
highlighter = substr_highlighter(make_display),
scoring_function = function(_, prompt, _, entry)
if #prompt == 0 then
return 1
end
local display = make_display(prompt, entry.ordinal)
local search_terms = util.max_split(prompt, "%s")
for _, word in pairs(search_terms) do
if not display:find(word, 1, true) then
return -1
end
end
return entry.index
end,
}
end