feat: use improved telescope's matcher (#184)

ref #177
ref https://github.com/nvim-telescope/telescope.nvim/pull/2950

Now telescope has the same matcher as the one #177 has introduced. This
commit makes it use that.
This commit is contained in:
JINNOUCHI Yasushi 2024-03-20 18:15:20 +09:00 committed by GitHub
parent 8e3e5ba645
commit 747894efd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2 additions and 65 deletions

View File

@ -1,6 +1,6 @@
local State = require "frecency.state"
local Finder = require "frecency.finder"
local sorters = require "frecency.sorters"
local sorters = require "telescope.sorters"
local log = require "plenary.log"
local Path = require "plenary.path" --[[@as PlenaryPath]]
local actions = require "telescope.actions"
@ -110,7 +110,7 @@ function Picker:start(opts)
prompt_title = "Frecency",
finder = finder,
previewer = config_values.file_previewer(opts),
sorter = sorters.get_frecency_matcher(),
sorter = sorters.get_substr_matcher(),
on_input_filter_cb = self:on_input_filter_cb(opts),
attach_mappings = function(prompt_bufnr)
return self:attach_mappings(prompt_bufnr)

View File

@ -1,63 +0,0 @@
local sorters = require "telescope.sorters"
local util = require "telescope.utils"
local M = {}
---@param prompt string
---@return boolean
local function has_upper_case(prompt)
return not not prompt:match "%u"
end
---@param prompt string
---@param display string
---@return { start: integer, finish: integer }[]
local function highlighter(_, prompt, display)
---@type { start: integer, finish: integer }[]
local highlights = {}
display = has_upper_case(prompt) and display or display:lower()
local search_terms = util.max_split(prompt, "%s")
local hl_start, hl_end
for _, word in ipairs(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
---@param prompt string
---@param entry FrecencyEntry
---@return integer
local function scoring_function(_, prompt, _, entry)
if #prompt == 0 then
return 1
end
local display = has_upper_case(prompt) and entry.ordinal or entry.ordinal:lower()
local search_terms = util.max_split(prompt, "%s")
for _, word in ipairs(search_terms) do
if not display:find(word, 1, true) then
return -1
end
end
return entry.index
end
---This is a sorter similar to telescope.sorters.get_substr_matcher. telescope's
---one always ignore cases, but this sorter deal with them like 'smartcase' way.
function M.get_frecency_matcher()
return vim.o.smartcase
and sorters.Sorter:new {
highlighter = highlighter,
scoring_function = scoring_function,
}
or sorters.get_substr_matcher()
end
return M