Skip to content

Commit

Permalink
fix(lsp): use caller text for call locations in incoming lsp calls. F…
Browse files Browse the repository at this point in the history
…ixes #529
  • Loading branch information
folke committed Jul 4, 2024
1 parent 95568c6 commit 12dc19a
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions lua/trouble/sources/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ for _, mode in ipairs({ "incoming_calls", "outgoing_calls" }) do
title = "{hl:Title}" .. Util.camel(mode, " ") .. "{hl} {count}",
desc = Util.camel(mode, " "),
source = "lsp." .. mode,
format = "{kind_icon} {chi.name} {text:ts} {pos} {hl:Title}{item.client:Title}{hl}",
format = "{kind_icon} {text:ts} {pos} {hl:Title}{item.client:Title}{hl}",
}
end

Expand Down Expand Up @@ -251,28 +251,48 @@ function M.call_hierarchy(cb, incoming)
---@param responses trouble.lsp.Response<(lsp.CallHierarchyIncomingCall|lsp.CallHierarchyOutgoingCall)[]>[][]
function(responses)
local items = {} ---@type trouble.Item[]

for _, results in ipairs(responses) do
for _, res in ipairs(results) do
local client = res.client
local calls = res.result
local todo = {} ---@type lsp.ResultItem[]
local chi = res.params.item

for _, call in ipairs(calls) do
if incoming then
todo[#todo + 1] = call.to or call.from
end
vim.list_extend(items, M.results_to_items(client, todo))
end
end
Item.add_text(items, { mode = "after" })

if incoming then
-- for incoming calls, we actually want the call locations, not just the caller
-- but we use the caller's item text as the call location text
local texts = {} ---@type table<lsp.CallHierarchyItem, string>
for _, item in ipairs(items) do
texts[item.item.symbol] = item.item.text
end

items = {}
for _, results in ipairs(responses) do
for _, res in ipairs(results) do
local client = res.client
local calls = res.result
local todo = {} ---@type lsp.ResultItem[]

for _, call in ipairs(calls) do
for _, r in ipairs(call.fromRanges or {}) do
local t = vim.deepcopy(chi) --[[@as lsp.ResultItem]]
local t = vim.deepcopy(call.from) --[[@as lsp.ResultItem]]
t.location = { range = r or call.from.selectionRange or call.from.range, uri = call.from.uri }
t.text = texts[call.from]
todo[#todo + 1] = t
end
else
todo[#todo + 1] = call.to
end
vim.list_extend(items, M.results_to_items(client, todo))
end
vim.list_extend(items, M.results_to_items(client, todo))
end
end
Item.add_text(items, { mode = "after" })
cb(items)
end
)
Expand Down Expand Up @@ -348,7 +368,7 @@ function M.range_to_item(client, range)
})
end

---@alias lsp.ResultItem lsp.Symbol|lsp.CallHierarchyItem
---@alias lsp.ResultItem lsp.Symbol|lsp.CallHierarchyItem|{text?:string}
---@param client vim.lsp.Client
---@param results lsp.ResultItem[]
---@param default_uri? string
Expand Down Expand Up @@ -395,6 +415,7 @@ function M.results_to_items(client, results, default_uri)
item.range = range and ranges[range] or nil
item.item.kind = vim.lsp.protocol.SymbolKind[result.kind] or tostring(result.kind)
item.item.symbol = result
item.item.text = result.text
items[#items + 1] = item
for _, child in ipairs(result.children or {}) do
item:add_child(add(child))
Expand Down

0 comments on commit 12dc19a

Please sign in to comment.