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
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user