Skip to content

Commit 17f8654

Browse files
authored
feat(lsp/codeAction): make float window keymaps configurable (#644)
1 parent 37a2dce commit 17f8654

File tree

6 files changed

+70
-11
lines changed

6 files changed

+70
-11
lines changed

doc/rustaceanvim.txt

+15-3
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,22 @@ rustaceanvim.code-action.Opts *rustaceanvim.code-action.Opts*
250250

251251
Fields: ~
252252
{group_icon?} (string)
253-
Text appended to a group action
253+
Text appended to a group action
254254
{ui_select_fallback?} (boolean)
255-
Whether to fall back to `vim.ui.select` if there are no grouped code actions.
256-
Default: `false`
255+
Whether to fall back to `vim.ui.select` if there are no grouped code actions.
256+
Default: `false`
257+
{keys} (rustaceanvim.code-action.Keys)
258+
259+
260+
rustaceanvim.code-action.Keys *rustaceanvim.code-action.Keys*
261+
262+
Fields: ~
263+
{confirm?} (string|string[])
264+
The key or keys with which to confirm a code action
265+
Default: `"<CR>"`.
266+
{quit?} (string)
267+
The key or keys with which to close a code action window
268+
Default: `{ "q", "<Esc>" }`.
257269

258270

259271
rustaceanvim.lsp_server_health_status *rustaceanvim.lsp_server_health_status*

doc/tags

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ rustaceanvim.FloatWinConfig rustaceanvim.txt /*rustaceanvim.FloatWinConfig*
77
rustaceanvim.LoadRASettingsOpts rustaceanvim.txt /*rustaceanvim.LoadRASettingsOpts*
88
rustaceanvim.Opts rustaceanvim.txt /*rustaceanvim.Opts*
99
rustaceanvim.RAInitializedStatus rustaceanvim.txt /*rustaceanvim.RAInitializedStatus*
10+
rustaceanvim.code-action.Keys rustaceanvim.txt /*rustaceanvim.code-action.Keys*
1011
rustaceanvim.code-action.Opts rustaceanvim.txt /*rustaceanvim.code-action.Opts*
1112
rustaceanvim.config rustaceanvim.txt /*rustaceanvim.config*
1213
rustaceanvim.config.server rustaceanvim.txt /*rustaceanvim.config.server*

lua/rustaceanvim/commands/code_action_group.lua

+17-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ local config = require('rustaceanvim.config.internal')
33
local compat = require('rustaceanvim.compat')
44
local M = {}
55

6+
local confirm_keys = config.tools.code_actions.keys.confirm
7+
local quit_keys = config.tools.code_actions.keys.quit
8+
confirm_keys = type(confirm_keys) == 'table' and confirm_keys or { confirm_keys }
9+
quit_keys = type(quit_keys) == 'table' and quit_keys or { quit_keys }
10+
611
---@class rustaceanvim.RACodeAction
712
---@field kind string
813
---@field group? string
@@ -194,10 +199,12 @@ local function on_code_action_results(results, ctx)
194199

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

197-
vim.keymap.set('n', '<CR>', on_primary_enter_press, { buffer = M.state.primary.bufnr, noremap = true, silent = true })
198-
199-
vim.keymap.set('n', 'q', on_primary_quit, { buffer = M.state.primary.bufnr, noremap = true, silent = true })
200-
vim.keymap.set('n', '<Esc>', on_primary_quit, { buffer = M.state.primary.bufnr, noremap = true, silent = true })
202+
vim.iter(confirm_keys):each(function(key)
203+
vim.keymap.set('n', key, on_primary_enter_press, { buffer = M.state.primary.bufnr, noremap = true, silent = true })
204+
end)
205+
vim.iter(quit_keys):each(function(key)
206+
vim.keymap.set('n', key, on_primary_quit, { buffer = M.state.primary.bufnr, noremap = true, silent = true })
207+
end)
201208

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

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

320327
M.codeactionify_window_buffer(M.state.secondary.winnr, M.state.secondary.bufnr)
321-
322-
vim.keymap.set('n', '<CR>', on_secondary_enter_press, { buffer = M.state.secondary.bufnr })
323-
324-
vim.keymap.set('n', 'q', on_secondary_quit, { buffer = M.state.secondary.bufnr })
328+
vim.iter(confirm_keys):each(function(key)
329+
vim.keymap.set('n', key, on_secondary_enter_press, { buffer = M.state.secondary.bufnr })
330+
end)
331+
vim.iter(quit_keys):each(function(key)
332+
vim.keymap.set('n', key, on_secondary_quit, { buffer = M.state.secondary.bufnr })
333+
end)
325334

326335
return
327336
end

lua/rustaceanvim/config/check.lua

+17
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ function M.validate(cfg)
5555
if not ok then
5656
return false, err
5757
end
58+
local code_actions = tools.code_actions
59+
ok, err = validate('tools.code_actions', {
60+
group_icon = { code_actions.group_icon, 'string' },
61+
ui_select_fallback = { code_actions.ui_select_fallback, 'boolean' },
62+
keys = { code_actions.keys, 'table' },
63+
})
64+
if not ok then
65+
return false, err
66+
end
67+
local keys = code_actions.keys
68+
ok, err = validate('tools.code_actions.keys', {
69+
confirm = { keys.confirm, { 'table', 'string' } },
70+
quit = { keys.quit, { 'table', 'string' } },
71+
})
72+
if not ok then
73+
return false, err
74+
end
5875
local float_win_config = tools.float_win_config
5976
ok, err = validate('tools.float_win_config', {
6077
auto_focus = { float_win_config.auto_focus, 'boolean' },

lua/rustaceanvim/config/init.lua

+12
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
130130
---Whether to fall back to `vim.ui.select` if there are no grouped code actions.
131131
---Default: `false`
132132
---@field ui_select_fallback? boolean
133+
---
134+
---@field keys rustaceanvim.code-action.Keys
135+
136+
---@class rustaceanvim.code-action.Keys
137+
---
138+
---The key or keys with which to confirm a code action
139+
---Default: `"<CR>"`.
140+
---@field confirm? string | string[]
141+
---
142+
---The key or keys with which to close a code action window
143+
---Default: `{ "q", "<Esc>" }`.
144+
---@field quit? string
133145

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

lua/rustaceanvim/config/internal.lua

+8
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ local RustaceanDefaultConfig = {
134134
--- whether to fall back to `vim.ui.select` if there are no grouped code actions
135135
---@type boolean
136136
ui_select_fallback = false,
137+
138+
---@class rustaceanvim.internal.code_action.Keys
139+
keys = {
140+
---@type string | string[]
141+
confirm = { '<CR>' },
142+
---@type string | string[]
143+
quit = { 'q', '<Esc>' },
144+
},
137145
},
138146

139147
--- options same as lsp hover

0 commit comments

Comments
 (0)