From 992b72de0214aeb30d58b3821edb748b1f451e90 Mon Sep 17 00:00:00 2001 From: Senghan Bright Date: Thu, 14 Jan 2021 20:56:56 +0100 Subject: [PATCH] switch to substring matcher (WIP) --- lua/telescope/_extensions/frecency.lua | 5 +-- lua/telescope/_extensions/frecency/sorter.lua | 33 ++++++++++++++----- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lua/telescope/_extensions/frecency.lua b/lua/telescope/_extensions/frecency.lua index c143c05..68c2da2 100644 --- a/lua/telescope/_extensions/frecency.lua +++ b/lua/telescope/_extensions/frecency.lua @@ -71,8 +71,9 @@ local frecency = function(opts) } end, }, - previewer = conf.file_previewer(opts), - sorter = sorters.get_frecency_sorter(opts), + -- previewer = conf.file_previewer(opts), + sorter = sorters.get_substr_matcher(opts), + -- sorter = conf.file_sorter(opts) }):find() end diff --git a/lua/telescope/_extensions/frecency/sorter.lua b/lua/telescope/_extensions/frecency/sorter.lua index 1821cb0..39b38a3 100644 --- a/lua/telescope/_extensions/frecency/sorter.lua +++ b/lua/telescope/_extensions/frecency/sorter.lua @@ -2,16 +2,31 @@ local sorters = require "telescope.sorters" local my_sorters = {} -my_sorters.get_frecency_sorter = function(opts) + +local substr_highlighter = function(_, prompt, display) + local highlights = {} + display = display:lower() + + local hl_start, hl_end + hl_start, hl_end = display:find(prompt, 1, true) + if hl_start then + table.insert(highlights, {start = hl_start, finish = hl_end}) + end + + return highlights +end + +my_sorters.get_substr_matcher = function(opts) opts = opts or {} - opts.ngram_len = 2 - local fuzzy_sorter = sorters.get_generic_fuzzy_sorter(opts) + local substr = sorters:new() + substr.highlighter = substr_highlighter + substr.scoring_function = function(_, prompt, _, entry) + -- local base_score = frecency:score(prompt, entry) + local base_score - local frecency = sorters:new() - frecency.highlighter = fuzzy_sorter.highlighter - frecency.scoring_function = function(_, prompt, _, entry) - local base_score = fuzzy_sorter:score(prompt, entry) + -- TODO: split the prompt into components + base_score = entry.name:find(prompt, 1, true) and 1 or -1 if base_score == -1 then return -1 @@ -20,11 +35,11 @@ my_sorters.get_frecency_sorter = function(opts) if base_score == 0 then return entry.index else - return math.min(math.pow(entry.index, 0.25), 2) * base_score + return entry.index end end - return frecency + return substr end return my_sorters