Skip to content

Commit d1e1492

Browse files
authored
feat(lsp): option to fall back to vim.ui.select for code action (#185)
1 parent 87fc16d commit d1e1492

File tree

6 files changed

+64
-1
lines changed

6 files changed

+64
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- LSP: Option to fall back to `vim.ui.select` if there
14+
are no code action groups when running `:RustLsp codeAction`.
15+
1116
### Fixed
1217

1318
- LSP: Focus lost when secondary float opens on `:RustLsp codeAction` [[#169](https://github.com/mrcjkb/rustaceanvim/issues/169)].

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,30 @@ vim.keymap.set(
245245
```
246246
</details>
247247

248+
<details>
249+
<summary>
250+
<b>Grouped code actions</b>
251+
</summary>
252+
253+
Sometimes, rust-analyzer groups code actions by category,
254+
which is not supported by Neovim's built-in `vim.lsp.buf.codeAction`.
255+
This plugin provides a command with a UI that does:
256+
257+
```vimscript
258+
:RustLsp codeAction
259+
```
260+
```lua
261+
vim.cmd.RustLsp('codeAction')
262+
```
263+
264+
If you set the option `vim.g.rustaceanvim.tools.code_actions.ui_select_fallback`
265+
to `true` (defaults to `false`), it will fall back to `vim.ui.select`
266+
if there are no grouped code actions.
267+
268+
![](https://github.com/mrcjkb/rustaceanvim/assets/12857160/866d3cb1-8e56-4380-8c03-812386441f47)
269+
270+
</details>
271+
248272
<details>
249273
<summary>
250274
<b>Hover Actions</b>

doc/rustaceanvim.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ RustaceanToolsOpts *RustaceanToolsOpts*
109109
{on_initialized?} (fun(health:RustAnalyzerInitializedStatus)) Function that is invoked when the LSP server has finished initializing
110110
{reload_workspace_from_cargo_toml?} (boolean) Automatically call `RustReloadWorkspace` when writing to a Cargo.toml file
111111
{hover_actions?} (RustaceanHoverActionsOpts) Options for hover actions
112+
{code_actions?} (RustaceanCodeActionOpts) Options for code actions
112113
{float_win_config?} (table) Options applied to floating windows. See |api-win_config|.
113114
{create_graph?} (RustaceanCrateGraphConfig) Options for showing the crate graph based on graphviz and the dot
114115
{open_url?} (fun(url:string):nil) If set, overrides how to open URLs
@@ -120,6 +121,12 @@ RustaceanHoverActionsOpts *RustaceanHoverActionsOpts*
120121
{replace_builtin_hover?} (boolean) Whether to replace Neovim's built-in `vim.lsp.buf.hover` with hover actions. Default: `true`
121122

122123

124+
RustaceanCodeActionOpts *RustaceanCodeActionOpts*
125+
126+
Fields: ~
127+
{ui_select_fallback?} (boolean) Whether to fall back to `vim.ui.select` if there are no grouped code actions. Default: `false`
128+
129+
123130
lsp_server_health_status *lsp_server_health_status*
124131

125132
Type: ~

lua/rustaceanvim/commands/code_action_group.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local ui = require('rustaceanvim.ui')
2+
local config = require('rustaceanvim.config.internal')
23
local M = {}
34

45
---@class RACodeAction
@@ -147,6 +148,22 @@ local function on_code_action_results(results, ctx)
147148
table.insert(M.state.actions.ungrouped, value)
148149
end
149150
end
151+
152+
if #M.state.actions.grouped == 0 and config.tools.code_actions.ui_select_fallback then
153+
---@param item action_tuple
154+
local function format_item(item)
155+
local title = item[2].title:gsub('\r\n', '\\r\\n')
156+
return title:gsub('\n', '\\n')
157+
end
158+
local select_opts = {
159+
prompt = 'Code actions:',
160+
kind = 'codeaction',
161+
format_item = format_item,
162+
}
163+
vim.ui.select(M.state.actions.ungrouped, select_opts, M.on_user_choice)
164+
return
165+
end
166+
150167
M.state.primary.bufnr = vim.api.nvim_create_buf(false, true)
151168
M.state.primary.winnr = vim.api.nvim_open_win(M.state.primary.bufnr, true, {
152169
relative = 'cursor',

lua/rustaceanvim/config/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
5959
---@field on_initialized? fun(health:RustAnalyzerInitializedStatus) Function that is invoked when the LSP server has finished initializing
6060
---@field reload_workspace_from_cargo_toml? boolean Automatically call `RustReloadWorkspace` when writing to a Cargo.toml file
6161
---@field hover_actions? RustaceanHoverActionsOpts Options for hover actions
62+
---@field code_actions? RustaceanCodeActionOpts Options for code actions
6263
---@field float_win_config? table Options applied to floating windows. See |api-win_config|.
6364
---@field create_graph? RustaceanCrateGraphConfig Options for showing the crate graph based on graphviz and the dot
6465
---@field open_url? fun(url:string):nil If set, overrides how to open URLs
6566

6667
---@class RustaceanHoverActionsOpts
6768
---@field replace_builtin_hover? boolean Whether to replace Neovim's built-in `vim.lsp.buf.hover` with hover actions. Default: `true`
6869

70+
---@class RustaceanCodeActionOpts
71+
---@field ui_select_fallback? boolean Whether to fall back to `vim.ui.select` if there are no grouped code actions. Default: `false`
72+
6973
---@alias lsp_server_health_status 'ok' | 'warning' | 'error'
7074

7175
---@class RustAnalyzerInitializedStatus

lua/rustaceanvim/config/internal.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ local RustaceanDefaultConfig = {
5353
hover_actions = {
5454

5555
--- whether to replace Neovim's built-in `vim.lsp.buf.hover`.
56-
--- default: true
5756
---@type boolean
5857
replace_builtin_hover = true,
5958
},
6059

60+
code_actions = {
61+
--- whether to fall back to `vim.ui.select` if there are no grouped code actions
62+
---@type boolean
63+
ui_select_fallback = false,
64+
},
65+
6166
--- options same as lsp hover
6267
---@see vim.lsp.util.open_floating_preview
6368
---@type table Options applied to floating windows.
@@ -331,6 +336,7 @@ local RustaceanDefaultConfig = {
331336
return dap_config
332337
end,
333338
},
339+
-- debug info
334340
was_g_rustaceanvim_sourced = vim.g.rustaceanvim ~= nil,
335341
}
336342
local rustaceanvim = vim.g.rustaceanvim or {}

0 commit comments

Comments
 (0)