mirror of
https://github.com/kristoferssolo/telescope-frecency.nvim.git
synced 2025-10-21 20:10:38 +00:00
support multiple search terms
This commit is contained in:
parent
deece31726
commit
c2d21f474a
@ -2,16 +2,27 @@ local sorters = require "telescope.sorters"
|
|||||||
|
|
||||||
local my_sorters = {}
|
local my_sorters = {}
|
||||||
|
|
||||||
|
local function split(s, delimiter)
|
||||||
|
local result = {}
|
||||||
|
for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
|
||||||
|
table.insert(result, match)
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
local substr_highlighter = function(_, prompt, display)
|
local substr_highlighter = function(_, prompt, display)
|
||||||
local highlights = {}
|
local highlights = {}
|
||||||
display = display:lower()
|
display = display:lower()
|
||||||
|
|
||||||
|
local search_terms = split(prompt, " ")
|
||||||
local hl_start, hl_end
|
local hl_start, hl_end
|
||||||
hl_start, hl_end = display:find(prompt, 1, true)
|
|
||||||
|
for _, word in pairs(search_terms) do
|
||||||
|
hl_start, hl_end = display:find(word, 1, true)
|
||||||
if hl_start then
|
if hl_start then
|
||||||
table.insert(highlights, {start = hl_start, finish = hl_end})
|
table.insert(highlights, {start = hl_start, finish = hl_end})
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return highlights
|
return highlights
|
||||||
end
|
end
|
||||||
@ -22,19 +33,19 @@ my_sorters.get_substr_matcher = function(opts)
|
|||||||
local substr = sorters:new()
|
local substr = sorters:new()
|
||||||
substr.highlighter = substr_highlighter
|
substr.highlighter = substr_highlighter
|
||||||
substr.scoring_function = function(_, prompt, _, entry)
|
substr.scoring_function = function(_, prompt, _, entry)
|
||||||
-- local base_score = frecency:score(prompt, entry)
|
|
||||||
local display = entry.name:lower()
|
local display = entry.name:lower()
|
||||||
local base_score
|
|
||||||
|
|
||||||
-- TODO: split the prompt into components
|
local search_terms = split(prompt, " ")
|
||||||
base_score = display:find(prompt, 1, true) and 1 or -1
|
local matched
|
||||||
|
for _, word in pairs(search_terms) do
|
||||||
if base_score == -1 then
|
matched = display:find(word, 1, true) and 1 or -1
|
||||||
return -1
|
if matched == -1 then goto continue end
|
||||||
end
|
end
|
||||||
|
|
||||||
if base_score == 0 then
|
::continue::
|
||||||
return entry.index
|
|
||||||
|
if matched == -1 then
|
||||||
|
return -1
|
||||||
else
|
else
|
||||||
return entry.index
|
return entry.index
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user