@@ -8,7 +8,7 @@ local function get_params()
88 return vim .lsp .util .make_position_params ()
99end
1010
11- M ._state = { winnr = nil , commands = nil }
11+ M ._state = { winnr = nil , parent_bufnr = nil , commands = nil }
1212local 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 )
146174end
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
149192function M .hover_actions ()
150193 utils .request (0 , " textDocument/hover" , get_params (), M .handler )
0 commit comments