Skip to content

Commit

Permalink
feat(lsp): option to fall back to vim.ui.select for code action (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Jan 25, 2024
1 parent 87fc16d commit d1e1492
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- LSP: Option to fall back to `vim.ui.select` if there
are no code action groups when running `:RustLsp codeAction`.

### Fixed

- LSP: Focus lost when secondary float opens on `:RustLsp codeAction` [[#169](https://github.com/mrcjkb/rustaceanvim/issues/169)].
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,30 @@ vim.keymap.set(
```
</details>

<details>
<summary>
<b>Grouped code actions</b>
</summary>

Sometimes, rust-analyzer groups code actions by category,
which is not supported by Neovim's built-in `vim.lsp.buf.codeAction`.
This plugin provides a command with a UI that does:

```vimscript
:RustLsp codeAction
```
```lua
vim.cmd.RustLsp('codeAction')
```

If you set the option `vim.g.rustaceanvim.tools.code_actions.ui_select_fallback`
to `true` (defaults to `false`), it will fall back to `vim.ui.select`
if there are no grouped code actions.

![](https://github.com/mrcjkb/rustaceanvim/assets/12857160/866d3cb1-8e56-4380-8c03-812386441f47)

</details>

<details>
<summary>
<b>Hover Actions</b>
Expand Down
7 changes: 7 additions & 0 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ RustaceanToolsOpts *RustaceanToolsOpts*
{on_initialized?} (fun(health:RustAnalyzerInitializedStatus)) Function that is invoked when the LSP server has finished initializing
{reload_workspace_from_cargo_toml?} (boolean) Automatically call `RustReloadWorkspace` when writing to a Cargo.toml file
{hover_actions?} (RustaceanHoverActionsOpts) Options for hover actions
{code_actions?} (RustaceanCodeActionOpts) Options for code actions
{float_win_config?} (table) Options applied to floating windows. See |api-win_config|.
{create_graph?} (RustaceanCrateGraphConfig) Options for showing the crate graph based on graphviz and the dot
{open_url?} (fun(url:string):nil) If set, overrides how to open URLs
Expand All @@ -120,6 +121,12 @@ RustaceanHoverActionsOpts *RustaceanHoverActionsOpts*
{replace_builtin_hover?} (boolean) Whether to replace Neovim's built-in `vim.lsp.buf.hover` with hover actions. Default: `true`


RustaceanCodeActionOpts *RustaceanCodeActionOpts*

Fields: ~
{ui_select_fallback?} (boolean) Whether to fall back to `vim.ui.select` if there are no grouped code actions. Default: `false`


lsp_server_health_status *lsp_server_health_status*

Type: ~
Expand Down
17 changes: 17 additions & 0 deletions lua/rustaceanvim/commands/code_action_group.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local ui = require('rustaceanvim.ui')
local config = require('rustaceanvim.config.internal')
local M = {}

---@class RACodeAction
Expand Down Expand Up @@ -147,6 +148,22 @@ local function on_code_action_results(results, ctx)
table.insert(M.state.actions.ungrouped, value)
end
end

if #M.state.actions.grouped == 0 and config.tools.code_actions.ui_select_fallback then
---@param item action_tuple
local function format_item(item)
local title = item[2].title:gsub('\r\n', '\\r\\n')
return title:gsub('\n', '\\n')
end
local select_opts = {
prompt = 'Code actions:',
kind = 'codeaction',
format_item = format_item,
}
vim.ui.select(M.state.actions.ungrouped, select_opts, M.on_user_choice)
return
end

M.state.primary.bufnr = vim.api.nvim_create_buf(false, true)
M.state.primary.winnr = vim.api.nvim_open_win(M.state.primary.bufnr, true, {
relative = 'cursor',
Expand Down
4 changes: 4 additions & 0 deletions lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
---@field on_initialized? fun(health:RustAnalyzerInitializedStatus) Function that is invoked when the LSP server has finished initializing
---@field reload_workspace_from_cargo_toml? boolean Automatically call `RustReloadWorkspace` when writing to a Cargo.toml file
---@field hover_actions? RustaceanHoverActionsOpts Options for hover actions
---@field code_actions? RustaceanCodeActionOpts Options for code actions
---@field float_win_config? table Options applied to floating windows. See |api-win_config|.
---@field create_graph? RustaceanCrateGraphConfig Options for showing the crate graph based on graphviz and the dot
---@field open_url? fun(url:string):nil If set, overrides how to open URLs

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

---@class RustaceanCodeActionOpts
---@field ui_select_fallback? boolean Whether to fall back to `vim.ui.select` if there are no grouped code actions. Default: `false`

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

---@class RustAnalyzerInitializedStatus
Expand Down
8 changes: 7 additions & 1 deletion lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ local RustaceanDefaultConfig = {
hover_actions = {

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

code_actions = {
--- whether to fall back to `vim.ui.select` if there are no grouped code actions
---@type boolean
ui_select_fallback = false,
},

--- options same as lsp hover
---@see vim.lsp.util.open_floating_preview
---@type table Options applied to floating windows.
Expand Down Expand Up @@ -331,6 +336,7 @@ local RustaceanDefaultConfig = {
return dap_config
end,
},
-- debug info
was_g_rustaceanvim_sourced = vim.g.rustaceanvim ~= nil,
}
local rustaceanvim = vim.g.rustaceanvim or {}
Expand Down

0 comments on commit d1e1492

Please sign in to comment.