diff --git a/lua/telescope/builtin/__lsp.lua b/lua/telescope/builtin/__lsp.lua index aeb99575ac..71765a7fab 100644 --- a/lua/telescope/builtin/__lsp.lua +++ b/lua/telescope/builtin/__lsp.lua @@ -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 @@ -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, { diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua index 716141664c..7760fa1037 100644 --- a/lua/telescope/config.lua +++ b/lua/telescope/config.lua @@ -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