From 7e006b06c4db1464be9c4e2826774fe6f0a4f880 Mon Sep 17 00:00:00 2001 From: Ashley Hauck <953151+khyperia@users.noreply.github.com> Date: Thu, 23 Apr 2026 14:20:58 +0200 Subject: [PATCH] fix(lsp): callHierarchy/outgoingCalls ranges are relative to caller, not callee #39336 Problem: The fromRanges field of the result of callHierarchy/outgoingCalls is documented as being relative to the caller. Using vim.lsp.buf.outgoing_calls() opened the qflist with an entry with the callee's filename, but the caller's line number. Solution: Open the qflist with the callers file (the bufnr from the request), rather than the callees (the uri from the resulting CallHierarchyItem) --- runtime/lua/vim/lsp/handlers.lua | 13 +++++++++++-- test/functional/plugin/lsp/buf_spec.lua | 5 +++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua index f8c1db9607..f4765183af 100644 --- a/runtime/lua/vim/lsp/handlers.lua +++ b/runtime/lua/vim/lsp/handlers.lua @@ -519,7 +519,8 @@ end --- @overload fun(direction:'to'): fun(_, result: lsp.CallHierarchyOutgoingCall[]?) local function make_call_hierarchy_handler(direction) --- @param result lsp.CallHierarchyIncomingCall[]|lsp.CallHierarchyOutgoingCall[] - return function(_, result) + --- @param ctx lsp.HandlerContext + return function(_, result, ctx) if not result then return end @@ -527,9 +528,17 @@ local function make_call_hierarchy_handler(direction) for _, call_hierarchy_call in pairs(result) do --- @type lsp.CallHierarchyItem local call_hierarchy_item = call_hierarchy_call[direction] + local filename = nil + local bufnr = nil + if direction == 'from' then + filename = assert(vim.uri_to_fname(call_hierarchy_item.uri)) + else + bufnr = ctx.bufnr + end for _, range in pairs(call_hierarchy_call.fromRanges) do table.insert(items, { - filename = assert(vim.uri_to_fname(call_hierarchy_item.uri)), + filename = filename, + bufnr = bufnr, text = call_hierarchy_item.name, lnum = range.start.line + 1, col = range.start.character + 1, diff --git a/test/functional/plugin/lsp/buf_spec.lua b/test/functional/plugin/lsp/buf_spec.lua index 4f88108419..f5fc498ef6 100644 --- a/test/functional/plugin/lsp/buf_spec.lua +++ b/test/functional/plugin/lsp/buf_spec.lua @@ -106,13 +106,14 @@ describe('vim.lsp.buf', function() }, } local handler = require 'vim.lsp.handlers'['callHierarchy/outgoingCalls'] - handler(nil, rust_analyzer_response, {}) + local bufnr = vim.api.nvim_get_current_buf() + handler(nil, rust_analyzer_response, { bufnr = bufnr }) return vim.fn.getqflist() end) local expected = { { - bufnr = 2, + bufnr = 1, col = 5, end_col = 0, lnum = 4,