Skip to content

Commit

Permalink
feat(lsp): add support of LSP items post-processing
Browse files Browse the repository at this point in the history
  • Loading branch information
sergluka committed Dec 12, 2024
1 parent 2eca9ba commit 0873b52
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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

0 comments on commit 0873b52

Please sign in to comment.