From dc37ed8dd07cccdca50558902a71788e734b97dd Mon Sep 17 00:00:00 2001 From: Senghan Bright Date: Sun, 17 Jan 2021 12:59:20 +0100 Subject: [PATCH] improve substr matcher --- lua/telescope/_extensions/frecency.lua | 6 +----- lua/telescope/_extensions/frecency/sorter.lua | 19 ++++++++----------- lua/telescope/_extensions/frecency/util.lua | 2 +- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lua/telescope/_extensions/frecency.lua b/lua/telescope/_extensions/frecency.lua index acf472d..a7635c1 100644 --- a/lua/telescope/_extensions/frecency.lua +++ b/lua/telescope/_extensions/frecency.lua @@ -9,6 +9,7 @@ end local db_client = require("telescope._extensions.frecency.db_client") -- vim.defer_fn(db_client.init, 100) -- TODO: this is a crappy attempt to lessen loadtime impact, use VimEnter? db_client.init() +local os_path_sep = vim.loop.os_uname().sysname == "Windows" and "\\" or "/" -- finder code @@ -32,8 +33,6 @@ local frecency = function(opts) -- TODO: decide on how to handle cwd or lsp_workspace for pathname shorten? local results = db_client.get_file_scores(opts) -- TODO: pass `filter_workspace` option - local os_path_sep = vim.loop.os_uname().sysname == "Windows" and "\\" or "/" - local displayer = entry_display.create { separator = "", hl_chars = {[os_path_sep] = "TelescopePathSeparator"}, @@ -49,7 +48,6 @@ local frecency = function(opts) local buf_is_loaded = vim.api.nvim_buf_is_loaded local filename = entry.name - local hl_filename = buf_is_loaded(bufnr(filename)) and "TelescopeBufferLoaded" or "" if opts.tail_path then @@ -60,8 +58,6 @@ local frecency = function(opts) filename = path.make_relative(filename, cwd) - - -- TODO: remove score from display; only there for debug return displayer { {entry.score, "Directory"}, {filename, hl_filename}, diff --git a/lua/telescope/_extensions/frecency/sorter.lua b/lua/telescope/_extensions/frecency/sorter.lua index 706221e..1cee78d 100644 --- a/lua/telescope/_extensions/frecency/sorter.lua +++ b/lua/telescope/_extensions/frecency/sorter.lua @@ -36,20 +36,17 @@ my_sorters.get_substr_matcher = function(opts) substr.scoring_function = function(_, prompt, _, entry) local display = entry.name:lower() - local search_terms = util.split(prompt, " ") - local matched + local search_terms = util.split(prompt, "%s") + local matched = 0 + local total_search_terms = 0 for _, word in pairs(search_terms) do - matched = display:find(word, 1, true) and 1 or -1 - if matched == -1 then goto continue end + total_search_terms = total_search_terms + 1 + if display:find(word, 1, true) then + matched = matched + 1 + end end - ::continue:: - - if matched == -1 then - return -1 - else - return entry.index - end + return matched == total_search_terms and entry.index or -1 end return substr diff --git a/lua/telescope/_extensions/frecency/util.lua b/lua/telescope/_extensions/frecency/util.lua index bb3c4da..09df1db 100644 --- a/lua/telescope/_extensions/frecency/util.lua +++ b/lua/telescope/_extensions/frecency/util.lua @@ -34,7 +34,7 @@ end util.split = function(str, delimiter) local result = {} - for match in (str .. delimiter):gmatch("(.-)" .. delimiter) do + for match in str:gmatch("[^" .. delimiter .. "]+") do table.insert(result, match) end return result