Skip to content

Commit

Permalink
feat(lsp/codeAction): make float window keymaps configurable (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Jan 2, 2025
1 parent 37a2dce commit 17f8654
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
18 changes: 15 additions & 3 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,22 @@ rustaceanvim.code-action.Opts *rustaceanvim.code-action.Opts*

Fields: ~
{group_icon?} (string)
Text appended to a group action
Text appended to a group action
{ui_select_fallback?} (boolean)
Whether to fall back to `vim.ui.select` if there are no grouped code actions.
Default: `false`
Whether to fall back to `vim.ui.select` if there are no grouped code actions.
Default: `false`
{keys} (rustaceanvim.code-action.Keys)


rustaceanvim.code-action.Keys *rustaceanvim.code-action.Keys*

Fields: ~
{confirm?} (string|string[])
The key or keys with which to confirm a code action
Default: `"<CR>"`.
{quit?} (string)
The key or keys with which to close a code action window
Default: `{ "q", "<Esc>" }`.


rustaceanvim.lsp_server_health_status *rustaceanvim.lsp_server_health_status*
Expand Down
1 change: 1 addition & 0 deletions doc/tags
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rustaceanvim.FloatWinConfig rustaceanvim.txt /*rustaceanvim.FloatWinConfig*
rustaceanvim.LoadRASettingsOpts rustaceanvim.txt /*rustaceanvim.LoadRASettingsOpts*
rustaceanvim.Opts rustaceanvim.txt /*rustaceanvim.Opts*
rustaceanvim.RAInitializedStatus rustaceanvim.txt /*rustaceanvim.RAInitializedStatus*
rustaceanvim.code-action.Keys rustaceanvim.txt /*rustaceanvim.code-action.Keys*
rustaceanvim.code-action.Opts rustaceanvim.txt /*rustaceanvim.code-action.Opts*
rustaceanvim.config rustaceanvim.txt /*rustaceanvim.config*
rustaceanvim.config.server rustaceanvim.txt /*rustaceanvim.config.server*
Expand Down
25 changes: 17 additions & 8 deletions lua/rustaceanvim/commands/code_action_group.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ local config = require('rustaceanvim.config.internal')
local compat = require('rustaceanvim.compat')
local M = {}

local confirm_keys = config.tools.code_actions.keys.confirm
local quit_keys = config.tools.code_actions.keys.quit
confirm_keys = type(confirm_keys) == 'table' and confirm_keys or { confirm_keys }
quit_keys = type(quit_keys) == 'table' and quit_keys or { quit_keys }

---@class rustaceanvim.RACodeAction
---@field kind string
---@field group? string
Expand Down Expand Up @@ -194,10 +199,12 @@ local function on_code_action_results(results, ctx)

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

vim.keymap.set('n', '<CR>', on_primary_enter_press, { buffer = M.state.primary.bufnr, noremap = true, silent = true })

vim.keymap.set('n', 'q', on_primary_quit, { buffer = M.state.primary.bufnr, noremap = true, silent = true })
vim.keymap.set('n', '<Esc>', on_primary_quit, { buffer = M.state.primary.bufnr, noremap = true, silent = true })
vim.iter(confirm_keys):each(function(key)
vim.keymap.set('n', key, on_primary_enter_press, { buffer = M.state.primary.bufnr, noremap = true, silent = true })
end)
vim.iter(quit_keys):each(function(key)
vim.keymap.set('n', key, on_primary_quit, { buffer = M.state.primary.bufnr, noremap = true, silent = true })
end)

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

Expand Down Expand Up @@ -318,10 +325,12 @@ function M.on_cursor_move()
vim.api.nvim_buf_set_lines(M.state.secondary.bufnr, 0, 1, false, {})

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 })
vim.iter(confirm_keys):each(function(key)
vim.keymap.set('n', key, on_secondary_enter_press, { buffer = M.state.secondary.bufnr })
end)
vim.iter(quit_keys):each(function(key)
vim.keymap.set('n', key, on_secondary_quit, { buffer = M.state.secondary.bufnr })
end)

return
end
Expand Down
17 changes: 17 additions & 0 deletions lua/rustaceanvim/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ function M.validate(cfg)
if not ok then
return false, err
end
local code_actions = tools.code_actions
ok, err = validate('tools.code_actions', {
group_icon = { code_actions.group_icon, 'string' },
ui_select_fallback = { code_actions.ui_select_fallback, 'boolean' },
keys = { code_actions.keys, 'table' },
})
if not ok then
return false, err
end
local keys = code_actions.keys
ok, err = validate('tools.code_actions.keys', {
confirm = { keys.confirm, { 'table', 'string' } },
quit = { keys.quit, { 'table', 'string' } },
})
if not ok then
return false, err
end
local float_win_config = tools.float_win_config
ok, err = validate('tools.float_win_config', {
auto_focus = { float_win_config.auto_focus, 'boolean' },
Expand Down
12 changes: 12 additions & 0 deletions lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
---Whether to fall back to `vim.ui.select` if there are no grouped code actions.
---Default: `false`
---@field ui_select_fallback? boolean
---
---@field keys rustaceanvim.code-action.Keys

---@class rustaceanvim.code-action.Keys
---
---The key or keys with which to confirm a code action
---Default: `"<CR>"`.
---@field confirm? string | string[]
---
---The key or keys with which to close a code action window
---Default: `{ "q", "<Esc>" }`.
---@field quit? string

---@alias rustaceanvim.lsp_server_health_status 'ok' | 'warning' | 'error'

Expand Down
8 changes: 8 additions & 0 deletions lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ local RustaceanDefaultConfig = {
--- whether to fall back to `vim.ui.select` if there are no grouped code actions
---@type boolean
ui_select_fallback = false,

---@class rustaceanvim.internal.code_action.Keys
keys = {
---@type string | string[]
confirm = { '<CR>' },
---@type string | string[]
quit = { 'q', '<Esc>' },
},
},

--- options same as lsp hover
Expand Down

0 comments on commit 17f8654

Please sign in to comment.