Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): add support of LSP items post-processing and deduplication #3381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions lua/telescope/builtin/__lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,47 @@ local function filter_file_ignore_patters(items, opts)
end, items)
end

---@param items vim.quickfix.entry[]
---@param opts table
---@return vim.quickfix.entry[]
local function remove_duplicates(items)
local seen = {}
local result = {}
for _, value in ipairs(items) do
key = string.format("%s:%d:%d:%s", value.filename, value.lnum, value.col, value.text)
if not seen[key] then
table.insert(result, value)
seen[key] = true
end
end
return result
end

---@param items vim.quickfix.entry[]
---@return vim.quickfix.entry[]
local function apply_post_process_handler(items, opts)
local post_process = vim.F.if_nil(opts.post_process, conf.post_process)
if type(post_process) == "function" then
items = post_process(items)
if items == nil then
utils.notify("buildin.post_process", {
msg = "'post_process' value is nil",
level = "ERROR",
})
items = {}
end
elseif post_process == "deduplicate" then
return remove_duplicates(items)
elseif post_process == nil then
else
utils.notify("buildin.post_process", {
msg = "Unexpected 'post_process' value: " .. post_process,
level = "WARN",
})
end
return items
end

---@param action telescope.lsp.list_or_jump_action
---@param title string prompt title
---@param funname string: name of the calling function
Expand Down Expand Up @@ -234,6 +275,7 @@ local function list_or_jump(action, title, funname, params, opts)

items = apply_action_handler(action, items, opts)
items = filter_file_ignore_patters(items, opts)
items = apply_post_process_handler(items, opts)

if vim.tbl_isempty(items) then
utils.notify(funname, {
Expand Down
13 changes: 13 additions & 0 deletions lua/telescope/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,19 @@ append(
Default: require("telescope.previewers").buffer_previewer_maker]]
)

append(
"post_process",
nil,
[[
LSP response items post processing.
Can be:
* nil - do nothing
* "deduplicate": Remove duplicates from the list. Duplicates are items
that have the same `filename`, `lnum` and `col`.
* function with signature: function(items) -> items
Default: nil]]
)

-- @param user_defaults table: a table where keys are the names of options,
-- and values are the ones the user wants
-- @param tele_defaults table: (optional) a table containing all of the defaults
Expand Down