Skip to content

Commit

Permalink
feat!(lsp,dap): allow overriding runnable/debuggable executable args (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Jan 25, 2024
1 parent d1e1492 commit d13e9ce
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 53 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## 4.0.0 - 2024-01-25

### BREAKING CHANGES

- To run the previous runnable/debuggable, you would call `:RustLsp runnables last`
or `:RustLsp debuggables last`.
These two functions now take optional arguments that you can pass to the executables.
The new way to run the previous runnable/debuggable is with a bang (`!`).
e.g. `:RustLsp! debuggables`.
In Lua, this is `vim.cmd.RustLsp { 'debuggables', bang = true }`, and the same
for `'runnables'`.

### Added

- LSP: Option to fall back to `vim.ui.select` if there
are no code action groups when running `:RustLsp codeAction`.
- LSP/DAP: Allow overriding executable args with
`:RustLsp runnables args[]` and `:RustLsp debuggables args[]`.

### Fixed

Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,14 @@ vim.keymap.set(
</summary>

```vimscript
:RustLsp debuggables [last?]
:RustLsp[!] debuggables [args[]]
```
```lua
vim.cmd.RustLsp {'debuggables', 'last' --[[ optional ]] }
vim.cmd.RustLsp('debuggables')
-- or, to run the previous debuggable:
vim.cmd.RustLsp { 'debuggables', bang = true }
-- or, to override the executable's args:
vim.cmd.RustLsp {'debuggables', 'arg1', 'arg2' }
```

By default, this plugin will silently attempt to autoload `nvim-dap`
Expand All @@ -191,10 +195,14 @@ vim.keymap.set(
</summary>

```vimscript
:RustLsp runnables [last?]
:RustLsp[!] runnables [args[]]
```
```lua
vim.cmd.RustLsp {'runnables', 'last' --[[ optional ]] }
vim.cmd.RustLsp('runnables')
-- or, to run the previous runnable:
vim.cmd.RustLsp { 'runnables', bang = true }
-- or, to override the executable's args:
vim.cmd.RustLsp {'runnables', 'arg1', 'arg2' }
```

![](https://github.com/mrcjkb/rustaceanvim/assets/12857160/95183192-5669-4a07-804b-83f67831be57)
Expand Down
10 changes: 7 additions & 3 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ Commands:
`:RustAnalyzer stop` - Stop the LSP client.
`:RustAnalyzer restart` - Restart the LSP client.

The `:RustLsp` command is available after the LSP client has initialized.
The `:RustLsp[!]` command is available after the LSP client has initialized.
It accepts the following subcommands:

`runnables [last]?` - Run tests, etc.
`last` means run the last test that was run.
`runnables [args[]]?` - Run tests, executables, etc.
`:RustLsp!` means run the last runnable (ignores any args).
`args[]` allows you to override the executable's arguments.
`debuggables [args[]]?` - Debug tests, executables, etc. (requires |nvim-dap|).
`:RustLsp!` means run the last debuggable (ignores any args).
`args[]` allows you to override the executable's arguments.
`expandMacro` - Expand macros recursively.
`moveItem [up|down]` - Move items up or down.
`hover [action|range]` - Hover actions, or hover over visually selected range.
Expand Down
20 changes: 16 additions & 4 deletions lua/rustaceanvim/commands/debuggables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ local function sanitize_results_for_debugging(result)
end

---@param debuggables RADebuggable[]
local function ui_select_debuggable(debuggables)
---@param executableArgsOverride? string[]
local function ui_select_debuggable(debuggables, executableArgsOverride)
if type(executableArgsOverride) == 'table' and #executableArgsOverride > 0 then
local unique_debuggables = {}
for _, debuggable in pairs(debuggables) do
debuggable.args.executableArgs = executableArgsOverride
unique_debuggables[vim.inspect(debuggable)] = debuggable
end
debuggables = vim.tbl_values(unique_debuggables)
end
local options = get_options(debuggables)
if #options == 0 then
return
Expand Down Expand Up @@ -146,9 +155,12 @@ local function runnables_request(handler)
rl.buf_request(0, 'experimental/runnables', get_params(), handler)
end

--- Sends the request to rust-analyzer to get the debuggables and handles them
function M.debuggables()
runnables_request(mk_handler(ui_select_debuggable))
---Sends the request to rust-analyzer to get the debuggables and handles them
---@param executableArgsOverride? string[]
function M.debuggables(executableArgsOverride)
runnables_request(mk_handler(function(debuggables)
ui_select_debuggable(debuggables, executableArgsOverride)
end))
end

--- Sends the request to rust-analyzer to get the debuggables and adds them to nvim-dap's
Expand Down
37 changes: 17 additions & 20 deletions lua/rustaceanvim/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ local M = {}

local rust_lsp_cmd_name = 'RustLsp'

---@type { string: fun(args: string[]) }
---@type { string: fun(args: string[], opts: vim.api.keyset.user_command) }
local command_tbl = {
codeAction = function(_)
require('rustaceanvim.commands.code_action_group')()
end,
crateGraph = function(args)
require('rustaceanvim.commands.crate_graph')(unpack(args))
end,
debuggables = function(args)
if #args == 0 then
require('rustaceanvim.commands.debuggables').debuggables()
elseif #args == 1 and args[1] == 'last' then
debuggables = function(args, opts)
if opts.bang then
require('rustaceanvim.cached_commands').execute_last_debuggable()
else
vim.notify('debuggables: unexpected arguments: ' .. vim.inspect(args), vim.log.levels.ERROR)
require('rustaceanvim.commands.debuggables').debuggables(args)
end
end,
expandMacro = function(_)
Expand Down Expand Up @@ -53,13 +51,11 @@ local command_tbl = {
vim.notify('hover: unknown subcommand: ' .. subcmd .. " expected 'actions' or 'range'", vim.log.levels.ERROR)
end
end,
runnables = function(args)
if #args == 0 then
require('rustaceanvim.runnables').runnables()
elseif #args == 1 and args[1] == 'last' then
runnables = function(args, opts)
if opts.bang then
require('rustaceanvim.cached_commands').execute_last_runnable()
else
vim.notify('runnables: unexpected arguments: ' .. vim.inspect(args), vim.log.levels.ERROR)
require('rustaceanvim.runnables').runnables(args)
end
end,
joinLines = function(_)
Expand Down Expand Up @@ -134,39 +130,40 @@ local function rust_lsp(opts)
vim.notify(rust_lsp_cmd_name .. ': Unknown subcommand: ' .. cmd, vim.log.levels.ERROR)
return
end
command(args)
command(args, opts)
end

---Create the `:RustLsp` command
function M.create_rust_lsp_command()
vim.api.nvim_create_user_command(rust_lsp_cmd_name, rust_lsp, {
nargs = '+',
range = true,
bang = true,
desc = 'Interacts with the rust-analyzer LSP client',
complete = function(arg_lead, cmdline, _)
local commands = vim.tbl_keys(command_tbl)
local match_start = '^' .. rust_lsp_cmd_name
local match_start = '^' .. rust_lsp_cmd_name .. '[!]*'
local subcmd_match = '%s+%w*$'
-- special case: crateGraph comes with graphviz backend completions
if
cmdline:match(match_start .. ' debuggables' .. subcmd_match)
or cmdline:match(match_start .. ' runnables%s+%w*$')
cmdline:match(match_start .. '%sdebuggables' .. subcmd_match)
or cmdline:match(match_start .. '%srunnables%s+%w*$')
then
return { 'last' }
end
if cmdline:match(match_start .. ' hover' .. subcmd_match) then
if cmdline:match(match_start .. '%shover' .. subcmd_match) then
return { 'actions', 'range' }
end
if cmdline:match(match_start .. ' moveItem' .. subcmd_match) then
if cmdline:match(match_start .. '%smoveItem' .. subcmd_match) then
return { 'up', 'down' }
end
if cmdline:match(match_start .. ' crateGraph' .. subcmd_match) then
if cmdline:match(match_start .. '%scrateGraph' .. subcmd_match) then
return config.tools.crate_graph.enabled_graphviz_backends or {}
end
if cmdline:match(match_start .. ' flyCheck' .. subcmd_match) then
if cmdline:match(match_start .. '%sflyCheck' .. subcmd_match) then
return { 'run', 'clear', 'cancel' }
end
if cmdline:match(match_start .. ' view' .. subcmd_match) then
if cmdline:match(match_start .. '%sview' .. subcmd_match) then
return { 'mir', 'hir' }
end
if cmdline:match(match_start .. '%s+%w*$') then
Expand Down
10 changes: 7 additions & 3 deletions lua/rustaceanvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
--- `:RustAnalyzer stop` - Stop the LSP client.
--- `:RustAnalyzer restart` - Restart the LSP client.
---
---The `:RustLsp` command is available after the LSP client has initialized.
---The `:RustLsp[!]` command is available after the LSP client has initialized.
---It accepts the following subcommands:
---
--- `runnables [last]?` - Run tests, etc.
--- `last` means run the last test that was run.
--- `runnables [args[]]?` - Run tests, executables, etc.
--- `:RustLsp!` means run the last runnable (ignores any args).
--- `args[]` allows you to override the executable's arguments.
--- `debuggables [args[]]?` - Debug tests, executables, etc. (requires |nvim-dap|).
--- `:RustLsp!` means run the last debuggable (ignores any args).
--- `args[]` allows you to override the executable's arguments.
--- `expandMacro` - Expand macros recursively.
--- `moveItem [up|down]` - Move items up or down.
--- `hover [action|range]` - Hover actions, or hover over visually selected range.
Expand Down
50 changes: 32 additions & 18 deletions lua/rustaceanvim/runnables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ end
---@field executableArgs string[]

---@param result RARunnable[]
local function get_options(result)
---@param executableArgsOverride? string[]
local function get_options(result, executableArgsOverride)
local option_strings = {}

for _, runnable in ipairs(result) do
local str = runnable.label
local str = runnable.label .. (executableArgsOverride and ' -- ' .. table.concat(executableArgsOverride, ' ') or '')
table.insert(option_strings, str)
end

Expand Down Expand Up @@ -70,25 +71,38 @@ function M.run_command(choice, runnables)
opts.executor.execute_command(command, args, cwd)
end

---@param result RARunnable[]
local function handler(_, result)
if result == nil then
return
---@param executableArgsOverride? string[]
---@return fun(_, result: RARunnable[])
local function mk_handler(executableArgsOverride)
---@param runnables RARunnable[]
return function(_, runnables)
if runnables == nil then
return
end
if type(executableArgsOverride) == 'table' and #executableArgsOverride > 0 then
local unique_runnables = {}
for _, runnable in pairs(runnables) do
runnable.args.executableArgs = executableArgsOverride
unique_runnables[vim.inspect(runnable)] = runnable
end
runnables = vim.tbl_values(unique_runnables)
end
-- get the choice from the user
local options = get_options(runnables, executableArgsOverride)
vim.ui.select(options, { prompt = 'Runnables', kind = 'rust-tools/runnables' }, function(_, choice)
---@cast choice integer
M.run_command(choice, runnables)

local cached_commands = require('rustaceanvim.cached_commands')
cached_commands.set_last_runnable(choice, runnables)
end)
end
-- get the choice from the user
local options = get_options(result)
vim.ui.select(options, { prompt = 'Runnables', kind = 'rust-tools/runnables' }, function(_, choice)
---@cast choice integer
M.run_command(choice, result)

local cached_commands = require('rustaceanvim.cached_commands')
cached_commands.set_last_runnable(choice, result)
end)
end

-- Sends the request to rust-analyzer to get the runnables and handles them
function M.runnables()
vim.lsp.buf_request(0, 'experimental/runnables', get_params(), handler)
---Sends the request to rust-analyzer to get the runnables and handles them
---@param executableArgsOverride? string[]
function M.runnables(executableArgsOverride)
vim.lsp.buf_request(0, 'experimental/runnables', get_params(), mk_handler(executableArgsOverride))
end

return M

0 comments on commit d13e9ce

Please sign in to comment.