diff options
| author | fdschmidt93 <39233597+fdschmidt93@users.noreply.github.com> | 2021-03-03 22:33:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-03 22:33:03 +0100 |
| commit | db7615578b43586ef6526c56ba397f86b13fbed9 (patch) | |
| tree | 0b1644a0269b96f96370962b77babe8486403058 /lua/telescope/sorters.lua | |
| parent | 390e5fa9f50a2946e2c1d080f114313d984bc4e1 (diff) | |
feat: lsp diagnostics and prefiltering by entry attribute (#543)
prefiltering for lsp_document_symbols and lsp_workspace_symbols.
example: type `:var:` to show all vars
Diffstat (limited to 'lua/telescope/sorters.lua')
| -rw-r--r-- | lua/telescope/sorters.lua | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/lua/telescope/sorters.lua b/lua/telescope/sorters.lua index a85a75b..6ae404d 100644 --- a/lua/telescope/sorters.lua +++ b/lua/telescope/sorters.lua @@ -39,6 +39,7 @@ function Sorter:new(opts) return setmetatable({ state = {}, + filter_function = opts.filter_function, scoring_function = opts.scoring_function, highlighter = opts.highlighter, discard = opts.discard, @@ -84,7 +85,13 @@ function Sorter:score(prompt, entry) return FILTERED end - local score = self:scoring_function(prompt or "", ordinal, entry) + local filter_score + if self.filter_function ~= nil then + filter_score, prompt = self:filter_function(prompt, entry) + end + + local score = (filter_score == FILTERED and FILTERED or + self:scoring_function(prompt or "", ordinal, entry)) if score == FILTERED then self:_mark_discarded(prompt, ordinal) @@ -467,4 +474,45 @@ sorters.get_substr_matcher = function() } end +local substr_matcher = function(_, prompt, line, _) + local display = line:lower() + 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 + end + end + + return matched == total_search_terms and 0 or FILTERED +end + +local filter_function = function(opts) + local scoring_function = vim.F.if_nil(opts.filter_function, substr_matcher) + local tag = vim.F.if_nil(opts.tag, "ordinal") + local delimiter = vim.F.if_nil(opts.delimiter, ":") + + return function(_, prompt, entry) + local filter = "^(" .. delimiter .. "(%S+)" .. "[" .. delimiter .. "%s]" .. ")" + local matched = prompt:match(filter) + + if matched == nil then + return 0, prompt + end + -- clear prompt of tag + prompt = prompt:sub(#matched + 1, -1) + local query = vim.trim(matched:gsub(delimiter, "")) + return scoring_function(_, query, entry[tag], _), prompt + end +end + +sorters.prefilter = function(opts) + local sorter = opts.sorter + sorter.filter_function = filter_function(opts) + sorter._was_discarded = function() return false end + return sorter +end + return sorters |
