From b52bbc4bb0e50bf7da65b2695e1d602344877858 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Fri, 20 Sep 2024 12:33:18 +0200 Subject: [PATCH] feat(lsp): `` mapping for hover actions (#510) --- README.md | 12 ++++++++++++ lua/rustaceanvim/hover_actions.lua | 13 ++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3f125923..0603833e 100644 --- a/README.md +++ b/README.md @@ -403,6 +403,18 @@ vim.keymap.set( By default, this plugin replaces Neovim's built-in hover handler with hover actions, so you can also use `vim.lsp.buf.hover()`. + You can invoke a hover action by switching to the hover window and entering `` + on the respective line, or with a keymap for the `RustHoverAction` mapping, + which accepts a `` prefix as the (1-based) index of the hover action to invoke. + + For example, if you set the following keymap: + + ```lua + vim.keymap.set('n', 'a', 'RustHoverAction') + ``` + + you can invoke the third hover action with `3a`. + ![](https://github.com/mrcjkb/rustaceanvim/assets/12857160/c7b6c730-4439-47b0-9a75-7ea4e6831f7a) diff --git a/lua/rustaceanvim/hover_actions.lua b/lua/rustaceanvim/hover_actions.lua index cfcd0e70..c9b09726 100644 --- a/lua/rustaceanvim/hover_actions.lua +++ b/lua/rustaceanvim/hover_actions.lua @@ -30,10 +30,8 @@ end -- run the command under the cursor, if the thing under the cursor is not the -- command then do nothing ---@param ctx table -local function run_command(ctx) - local winnr = vim.api.nvim_get_current_win() - local line = vim.api.nvim_win_get_cursor(winnr)[1] - +---@param line integer +local function run_command(ctx, line) if line > #_state.commands then return end @@ -132,8 +130,13 @@ function M.handler(_, result, ctx) -- run the command under the cursor vim.keymap.set('n', '', function() - run_command(ctx) + local line = vim.api.nvim_win_get_cursor(winnr)[1] + run_command(ctx, line) end, { buffer = bufnr, noremap = true, silent = true }) + vim.keymap.set('n', 'RustHoverAction', function() + local line = math.max(vim.v.count, 1) + run_command(ctx, line) + end, { buffer = vim.api.nvim_get_current_buf(), noremap = true, silent = true }) end local rl = require('rustaceanvim.rust_analyzer')