Skip to content

Commit e26ab34

Browse files
committed
feat(lsp): Jump between symbol references
1 parent bd455bc commit e26ab34

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

plugin/autocmds/lsp-attach.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,53 @@ local function keymaps(bufnr, client)
9494
k("<leader>dd", snacks.picker.diagnostics_buffer, "Diagnostics: File")
9595
k("K", hover, "Hover")
9696

97+
-- Jump to type implementation
9798
if client:supports_method(methods.textDocument_typeDefinition) then
9899
k("grt", vim.lsp.buf.type_definition, "Jump to type definition")
99100
end
101+
102+
-- Jump to symbol references
103+
if client:supports_method(methods.textDocument_references) then
104+
local function jump_to_reference(direction)
105+
return function()
106+
-- Make sure we're at the beginning of the current word
107+
vim.cmd("normal! eb")
108+
109+
vim.lsp.buf.references(nil, {
110+
on_list = function(options)
111+
if not options or not options.items or #options.items == 0 then
112+
vim.notify("No references found", vim.log.levels.WARN)
113+
return
114+
end
115+
116+
-- Find the current reference based on cursor position
117+
local current_ref = 1
118+
local lnum = vim.fn.line(".")
119+
local col = vim.fn.col(".")
120+
for i, item in ipairs(options.items) do
121+
if item.lnum == lnum and item.col == col then
122+
current_ref = i
123+
break
124+
end
125+
end
126+
127+
-- Calculate the adjacent reference based on direction
128+
local delta = direction == "next" and 1 or -1
129+
local adjacent_ref = math.min(#options.items, current_ref + delta)
130+
if adjacent_ref < 1 then
131+
adjacent_ref = 1
132+
end
133+
134+
vim.fn.setqflist({}, "r", { items = options.items })
135+
vim.cmd(adjacent_ref .. "cc")
136+
end,
137+
})
138+
end
139+
end
140+
141+
k("[r", jump_to_reference("prev"), "Jump to previous reference")
142+
k("]r", jump_to_reference("next"), "Jump to next reference")
143+
end
100144
end
101145

102146
-- Highlight all references to symbol under cursor

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ Random features added.
313313
- `<c-w>]` jump to definition (vsplit)
314314
- `<leader>dd` open diagnostics (buffer)
315315
- `<leader>dD` open diagnostics (workspace)
316+
- `[r` jump to previous symbol reference
317+
- `]r` jump to next symbol reference
316318
- `[x` jump to previous error
317319
- `]x` jump to next error
318320

0 commit comments

Comments
 (0)