Skip to content

Commit

Permalink
feat(lsp): most lsp sources now support params.include_current. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 6, 2024
1 parent 98d9ed7 commit 29d19d4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
18 changes: 18 additions & 0 deletions lua/trouble/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ local defaults = {
},
---@type table<string, trouble.Mode>
modes = {
-- sources define their own modes, which you can use directly,
-- or override like in the example below
lsp_references = {
-- some modes are configurable, see the source code for more details
params = {
include_declaration = true,
},
},
-- The LSP base mode for:
-- * lsp_definitions, lsp_references, lsp_implementations
-- * lsp_type_definitions, lsp_declarations, lsp_command
lsp_base = {
params = {
-- don't include the current location in the results
include_current = false,
},
},
-- more advanced example that extends the lsp_document_symbols
symbols = {
desc = "document symbols",
mode = "lsp_document_symbols",
Expand Down
51 changes: 33 additions & 18 deletions lua/trouble/sources/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ end

---@param method string
---@param cb trouble.Source.Callback
---@param ctx trouble.Source.ctx
---@param opts? {context?:any, params?:table<string,any>}
function M.get_locations(method, cb, opts)
function M.get_locations(method, cb, ctx, opts)
local win = vim.api.nvim_get_current_win()
local buf = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(win)
Expand All @@ -177,7 +178,7 @@ function M.get_locations(method, cb, opts)
function(results)
local items = {} ---@type trouble.Item[]
for _, resp in ipairs(results) do
vim.list_extend(items, M.get_items(resp.client, resp.result))
vim.list_extend(items, M.get_items(resp.client, resp.result, ctx.opts.params))
end
Cache.locations[id] = items
cb(items)
Expand Down Expand Up @@ -290,7 +291,9 @@ end

---@param client vim.lsp.Client
---@param locations? lsp.Location[]|lsp.LocationLink[]|lsp.Location
function M.get_items(client, locations)
---@param opts? {include_current?:boolean}
function M.get_items(client, locations, opts)
opts = opts or {}
locations = locations or {}
locations = Util.islist(locations) and locations or { locations }
---@cast locations (lsp.Location|lsp.LocationLink)[]
Expand All @@ -303,10 +306,12 @@ function M.get_items(client, locations)
local fname = vim.api.nvim_buf_get_name(0)
fname = vim.fs.normalize(fname)

---@param item trouble.Item
items = vim.tbl_filter(function(item)
return not (item.filename == fname and Filter.overlaps(cursor, item, { lines = true }))
end, items)
if not opts.include_current then
---@param item trouble.Item
items = vim.tbl_filter(function(item)
return not (item.filename == fname and Filter.overlaps(cursor, item, { lines = true }))
end, items)
end

-- Item.add_text(items, { mode = "full" })
return items
Expand Down Expand Up @@ -467,34 +472,44 @@ function M.get.command(cb, ctx)
if not params.command then
return Util.error(err)
end
M.get_locations("workspace/executeCommand", cb, { params = params })
M.get_locations("workspace/executeCommand", cb, ctx, { params = params })
end

---@param ctx trouble.Source.ctx
---@param cb trouble.Source.Callback
function M.get.references(cb)
M.get_locations("textDocument/references", cb, { context = { includeDeclaration = true } })
function M.get.references(cb, ctx)
local params = ctx.opts.params or {}
M.get_locations("textDocument/references", cb, ctx, {
context = {
includeDeclaration = params.include_declaration ~= false,
},
})
end

---@param cb trouble.Source.Callback
function M.get.definitions(cb)
M.get_locations("textDocument/definition", cb)
---@param ctx trouble.Source.ctx
function M.get.definitions(cb, ctx)
M.get_locations("textDocument/definition", cb, ctx)
end

---@param cb trouble.Source.Callback
function M.get.implementations(cb)
M.get_locations("textDocument/implementation", cb)
---@param ctx trouble.Source.ctx
function M.get.implementations(cb, ctx)
M.get_locations("textDocument/implementation", cb, ctx)
end

-- Type Definitions
---@param cb trouble.Source.Callback
function M.get.type_definitions(cb)
M.get_locations("textDocument/typeDefinition", cb)
---@param ctx trouble.Source.ctx
function M.get.type_definitions(cb, ctx)
M.get_locations("textDocument/typeDefinition", cb, ctx)
end

-- Declaration
---@param cb trouble.Source.Callback
function M.get.declarations(cb)
M.get_locations("textDocument/declaration", cb)
---@param ctx trouble.Source.ctx
function M.get.declarations(cb, ctx)
M.get_locations("textDocument/declaration", cb, ctx)
end

return M

0 comments on commit 29d19d4

Please sign in to comment.