Skip to content

Commit

Permalink
docs(vimdoc): add missing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Dec 18, 2024
1 parent 30889ef commit 95315b0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 39 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,6 @@ vim.keymap.set(
vim.cmd.RustLsp { 'hover', 'actions' }
```

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 `<CR>`
on the respective line, or with a keymap for the `<Plug>RustHoverAction` mapping,
which accepts a `<count>` prefix as the (1-based) index of the hover action to invoke.
Expand Down
6 changes: 6 additions & 0 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ It accepts the following subcommands:
You can modify these by defining `<Plug>rustaceanvim.code_action.confirm` or
`<Plug>rustaceanvim.code_action.quit` mappings.
'hover {actions|range}' - Hover actions, or hover over visually selected range.
You can invoke a hover action by switching to the hover window and entering `<CR>`
on the respective line, or with a keymap for the `<Plug>RustHoverAction` mapping,
which accepts a `<count>` prefix as the (1-based) index of the hover action to invoke.

For example, if you set the keymap: `vim.keymap.set('n', '<space>a', '<Plug>RustHoverAction')`,
you can invoke the third hover action with `3<space>a`.
'explainError {cycle?|current?}' - Display a hover window with explanations form the Rust error index.
- If called with |cycle| or no args:
Like |vim.diagnostic.goto_next|,
Expand Down
104 changes: 68 additions & 36 deletions lua/rustaceanvim/commands/code_action_group.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ function M.apply_action(action, client, ctx)
end
end

---@class rustaceanvim.code_action_group.set_user_keymaps
---@field confirm boolean
---@field quit boolean

---@class rustaceanvim.api.keyset.keymap: vim.api.keyset.keymap
---@field rhs string

---@return rustaceanvim.code_action_group.set_user_keymaps
local function search_for_user_keymaps()
return vim
.iter(vim.api.nvim_get_keymap('n'))
:map(function(keymap)
return keymap.rhs
end)
:filter(function(rhs)
return type(rhs) == 'string' and vim.startswith(rhs, '<Plug>')
end)
:fold(
{},
---@param acc rustaceanvim.code_action_group.set_user_keymaps
---@param rhs string
function(acc, rhs)
if type(rhs) ~= 'string' then
return acc
end
return {
confirm = acc.confirm or rhs:find('rustaceanvim%.code_action%.confirm') ~= nil,
quit = acc.quit or rhs:find('rustaceanvim%.code_action%.quit') ~= nil,
}
end
)
end

---@alias action_tuple { [1]: number, [2]: rustaceanvim.RACodeAction|rustaceanvim.RACommand }

---@param action_tuple action_tuple | nil
Expand Down Expand Up @@ -194,38 +227,7 @@ local function on_code_action_results(results, ctx)

vim.api.nvim_buf_set_lines(M.state.primary.bufnr, 0, 1, false, {})

-- Search for user keymaps

---@class rustaceanvim.code_action_group.set_user_keymaps
---@field confirm boolean
---@field quit boolean

---@class rustaceanvim.api.keyset.keymap: vim.api.keyset.keymap
---@field rhs string

---@type rustaceanvim.code_action_group.set_user_keymaps
local user_keymaps = vim
.iter(vim.api.nvim_get_keymap('n'))
:map(function(keymap)
return keymap.rhs
end)
:filter(function(rhs)
return type(rhs) == 'string' and vim.startswith(rhs, '<Plug>')
end)
:fold(
{},
---@param acc rustaceanvim.code_action_group.set_user_keymaps
---@param rhs string
function(acc, rhs)
if type(rhs) ~= 'string' then
return acc
end
return {
confirm = acc.confirm or rhs:find('rustaceanvim%.code_action%.confirm') ~= nil,
quit = acc.quit or rhs:find('rustaceanvim%.code_action%.quit') ~= nil,
}
end
)
local user_keymaps = search_for_user_keymaps()

if user_keymaps.confirm then
vim.keymap.set(
Expand Down Expand Up @@ -281,7 +283,7 @@ function M.codeactionify_window_buffer(winnr, bufnr)
vim.wo[winnr].cul = true
end

local function on_secondary_enter_press()
local function on_secondary_confirm()
local line = vim.api.nvim_win_get_cursor(M.state.secondary.winnr)[1]
local active_group = nil

Expand Down Expand Up @@ -370,10 +372,40 @@ function M.on_cursor_move()

M.codeactionify_window_buffer(M.state.secondary.winnr, M.state.secondary.bufnr)

vim.keymap.set('n', '<CR>', on_secondary_enter_press, { buffer = M.state.secondary.bufnr })

vim.keymap.set('n', 'q', on_secondary_quit, { buffer = M.state.secondary.bufnr })
local user_keymaps = search_for_user_keymaps()

if user_keymaps.confirm then
vim.keymap.set(
'n',
'<Plug>rustaceanvim.code_action.confirm',
on_secondary_confirm,
{ buffer = M.state.secondary.bufnr, noremap = true, silent = true }
)
else
vim.keymap.set(
'n',
'<CR>',
on_secondary_confirm,
{ buffer = M.state.secondary.bufnr, noremap = true, silent = true }
)
end

if user_keymaps.quit then
vim.keymap.set(
'n',
'<Plug>rustaceanvim.code_action.quit',
on_secondary_quit,
{ buffer = M.state.secondary.bufnr, noremap = true, silent = true }
)
else
vim.keymap.set('n', 'q', on_secondary_quit, { buffer = M.state.secondary.bufnr, noremap = true, silent = true })
vim.keymap.set(
'n',
'<Esc>',
on_secondary_quit,
{ buffer = M.state.secondary.bufnr, noremap = true, silent = true }
)
end
return
end

Expand Down
6 changes: 6 additions & 0 deletions lua/rustaceanvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
--- You can modify these by defining `<Plug>rustaceanvim.code_action.confirm` or
--- `<Plug>rustaceanvim.code_action.quit` mappings.
--- 'hover {actions|range}' - Hover actions, or hover over visually selected range.
--- You can invoke a hover action by switching to the hover window and entering `<CR>`
--- on the respective line, or with a keymap for the `<Plug>RustHoverAction` mapping,
--- which accepts a `<count>` prefix as the (1-based) index of the hover action to invoke.
---
--- For example, if you set the keymap: `vim.keymap.set('n', '<space>a', '<Plug>RustHoverAction')`,
--- you can invoke the third hover action with `3<space>a`.
--- 'explainError {cycle?|current?}' - Display a hover window with explanations form the Rust error index.
--- - If called with |cycle| or no args:
--- Like |vim.diagnostic.goto_next|,
Expand Down

0 comments on commit 95315b0

Please sign in to comment.