Skip to content

Commit 0873b52

Browse files
committed
feat(lsp): add support of LSP items post-processing
1 parent 2eca9ba commit 0873b52

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lua/telescope/builtin/__lsp.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,47 @@ local function filter_file_ignore_patters(items, opts)
189189
end, items)
190190
end
191191

192+
---@param items vim.quickfix.entry[]
193+
---@param opts table
194+
---@return vim.quickfix.entry[]
195+
local function remove_duplicates(items)
196+
local seen = {}
197+
local result = {}
198+
for _, value in ipairs(items) do
199+
key = string.format("%s:%d:%d:%s", value.filename, value.lnum, value.col, value.text)
200+
if not seen[key] then
201+
table.insert(result, value)
202+
seen[key] = true
203+
end
204+
end
205+
return result
206+
end
207+
208+
---@param items vim.quickfix.entry[]
209+
---@return vim.quickfix.entry[]
210+
local function apply_post_process_handler(items, opts)
211+
local post_process = vim.F.if_nil(opts.post_process, conf.post_process)
212+
if type(post_process) == "function" then
213+
items = post_process(items)
214+
if items == nil then
215+
utils.notify("buildin.post_process", {
216+
msg = "'post_process' value is nil",
217+
level = "ERROR",
218+
})
219+
items = {}
220+
end
221+
elseif post_process == "deduplicate" then
222+
return remove_duplicates(items)
223+
elseif post_process == nil then
224+
else
225+
utils.notify("buildin.post_process", {
226+
msg = "Unexpected 'post_process' value: " .. post_process,
227+
level = "WARN",
228+
})
229+
end
230+
return items
231+
end
232+
192233
---@param action telescope.lsp.list_or_jump_action
193234
---@param title string prompt title
194235
---@param funname string: name of the calling function
@@ -234,6 +275,7 @@ local function list_or_jump(action, title, funname, params, opts)
234275

235276
items = apply_action_handler(action, items, opts)
236277
items = filter_file_ignore_patters(items, opts)
278+
items = apply_post_process_handler(items, opts)
237279

238280
if vim.tbl_isempty(items) then
239281
utils.notify(funname, {

lua/telescope/config.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,19 @@ append(
930930
Default: require("telescope.previewers").buffer_previewer_maker]]
931931
)
932932

933+
append(
934+
"post_process",
935+
nil,
936+
[[
937+
LSP response items post processing.
938+
Can be:
939+
* nil - do nothing
940+
* "deduplicate": Remove duplicates from the list. Duplicates are items
941+
that have the same `filename`, `lnum` and `col`.
942+
* function with signature: function(items) -> items
943+
Default: nil]]
944+
)
945+
933946
-- @param user_defaults table: a table where keys are the names of options,
934947
-- and values are the ones the user wants
935948
-- @param tele_defaults table: (optional) a table containing all of the defaults

0 commit comments

Comments
 (0)