Skip to content
This repository was archived by the owner on Jan 3, 2024. It is now read-only.

Commit 9408691

Browse files
committed
feat: add keymaps option for scrolling the hover window
1 parent fe900a3 commit 9408691

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ local opts = {
213213
-- whether the hover action window gets automatically focused
214214
-- default: false
215215
auto_focus = false,
216+
-- keymaps for scrolling the hover window
217+
keymaps = {
218+
-- enable the keymaps
219+
-- default: false
220+
enable = false,
221+
-- scroll up
222+
-- default: "<c-j>"
223+
scroll_up = "<c-j>",
224+
-- scroll down
225+
-- default: "<c-k>"
226+
scroll_down = "<c-k>",
227+
},
216228
},
217229

218230
-- settings for showing the crate graph based on graphviz and the dot

lua/rust-tools/config.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ local defaults = {
9292
-- whether the hover action window gets automatically focused
9393
-- default: false
9494
auto_focus = false,
95+
96+
-- keymaps for scrolling the hover window
97+
keymaps = {
98+
-- enable the keymaps
99+
-- default: false
100+
enable = false,
101+
-- scroll up
102+
-- default: "<c-j>"
103+
scroll_up = "<c-j>",
104+
-- scroll down
105+
-- default: "<c-k>"
106+
scroll_down = "<c-k>",
107+
},
95108
},
96109

97110
-- settings for showing the crate graph based on graphviz and the dot

lua/rust-tools/hover_actions.lua

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local function get_params()
88
return vim.lsp.util.make_position_params()
99
end
1010

11-
M._state = { winnr = nil, commands = nil }
11+
M._state = { winnr = nil, parent_bufnr = nil, commands = nil }
1212
local set_keymap_opt = { noremap = true, silent = true }
1313

1414
-- run the command under the cursor, if the thing under the cursor is not the
@@ -84,6 +84,10 @@ function M.handler(_, result)
8484
return
8585
end
8686

87+
-- update parent_bufnr before focus on hover buf
88+
local parent_bufnr = vim.api.nvim_get_current_buf()
89+
M._state.parent_bufnr = parent_bufnr
90+
8791
local bufnr, winnr = util.open_floating_preview(
8892
markdown_lines,
8993
"markdown",
@@ -113,9 +117,33 @@ function M.handler(_, result)
113117
set_keymap_opt
114118
)
115119

120+
-- set keymaps for scrolling the popup
121+
local keymaps = config.options.tools.hover_actions.keymaps
122+
if keymaps.enable then
123+
vim.api.nvim_buf_set_keymap(
124+
M._state.parent_bufnr, -- set for parent buf, not the hover window buf
125+
"n",
126+
keymaps.scroll_up,
127+
":lua require'rust-tools.hover_actions'.scroll_hover(1)<CR>",
128+
{ silent = true }
129+
)
130+
131+
vim.api.nvim_buf_set_keymap(
132+
M._state.parent_bufnr, -- set for parent buf, not the hover window buf
133+
"n",
134+
keymaps.scroll_down,
135+
":lua require'rust-tools.hover_actions'.scroll_hover(-1)<CR>",
136+
{ silent = true }
137+
)
138+
end
139+
116140
vim.api.nvim_buf_attach(bufnr, false, {
117141
on_detach = function()
118142
M._state.winnr = nil
143+
if keymaps.enable then
144+
vim.api.nvim_buf_del_keymap(M._state.parent_bufnr, "n", keymaps.scroll_up)
145+
vim.api.nvim_buf_del_keymap(M._state.parent_bufnr, "n", keymaps.scroll_down)
146+
end
119147
end,
120148
})
121149

@@ -145,6 +173,21 @@ function M.handler(_, result)
145173
)
146174
end
147175

176+
---Scroll the hover window
177+
---@param offset number, scroll up if offset > 0 else scroll down
178+
function M.scroll_hover(offset)
179+
if M._state.winnr ~= nil then
180+
local cmd = [[exec "norm! \<c-d>"]]
181+
if offset < 0 then
182+
cmd = [[exec "norm! \<c-u>"]]
183+
end
184+
vim.api.nvim_win_call(
185+
M._state.winnr,
186+
function() vim.cmd(cmd) end
187+
)
188+
end
189+
end
190+
148191
-- Sends the request to rust-analyzer to get hover actions and handle it
149192
function M.hover_actions()
150193
utils.request(0, "textDocument/hover", get_params(), M.handler)

0 commit comments

Comments
 (0)