Skip to content

Commit 84cc86e

Browse files
Vinzent03mawkler
authored andcommitted
feat: set keymap only for buffers with lsp that support it
1 parent 95da9e8 commit 84cc86e

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lua/refjump/keymaps.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,24 @@ function M.create_keymaps(opts)
2727
and repeatable_jump_map
2828
or jump_map
2929

30-
vim.keymap.set(nxo, opts.keymaps.next, jump({ forward = true }), { desc = 'Next reference' })
31-
vim.keymap.set(nxo, opts.keymaps.prev, jump({ forward = false }), { desc = 'Previous reference' })
30+
-- Create keymaps only for buffers with lsp that supports documentHighlight
31+
vim.api.nvim_create_autocmd("LspAttach", {
32+
group = vim.api.nvim_create_augroup('refjump_lsp_attach', {}),
33+
---@param event { buf: number, data: { client_id: number } }
34+
callback = function(event)
35+
local client_id = event.data.client_id
36+
local client = vim.lsp.get_client_by_id(client_id)
37+
38+
if client and client.supports_method('textDocument/documentHighlight', { bufnr = event.buf }) then
39+
vim.keymap.set(nxo, opts.keymaps.next, jump({ forward = true }),
40+
{ desc = 'Next reference', buffer = event.buf })
41+
42+
vim.keymap.set(nxo, opts.keymaps.prev, jump({ forward = false }),
43+
{ desc = 'Previous reference', buffer = event.buf })
44+
return
45+
end
46+
end
47+
})
3248
end
3349

3450
return M

0 commit comments

Comments
 (0)