feat: detect upper case letters with get_substr_matcher (#2950)

* feat: detect upper case letters by substr matcher

* test: add tests for get_substr_matcher
This commit is contained in:
JINNOUCHI Yasushi
2024-03-20 14:24:31 +09:00
committed by GitHub
parent 3b8399c273
commit f06eeb79e1
2 changed files with 111 additions and 19 deletions

View File

@@ -511,44 +511,52 @@ sorters.get_levenshtein_sorter = function()
}
end
local substr_highlighter = function(_, prompt, display)
local highlights = {}
display = display:lower()
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
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 })
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
end
return highlights
return highlights
end
end
sorters.get_substr_matcher = 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 Sorter:new {
highlighter = substr_highlighter,
highlighter = substr_highlighter(make_display),
scoring_function = function(_, prompt, _, entry)
if #prompt == 0 then
return 1
end
local display = entry.ordinal:lower()
local display = make_display(prompt, entry.ordinal)
local search_terms = util.max_split(prompt, "%s")
local matched = 0
local total_search_terms = 0
for _, word in pairs(search_terms) do
total_search_terms = total_search_terms + 1
if display:find(word, 1, true) then
matched = matched + 1
if not display:find(word, 1, true) then
return -1
end
end
return matched == total_search_terms and entry.index or -1
return entry.index
end,
}
end