Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run and debug commands to run targets at current position #341

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- DAP/LSP: `RustLsp [run|debug]` commands for running/debugging targets
at the current cursor position.
- LSP: Join multiple visually selected lines with `:RustLsp joinLines`.

### Fixed
Expand Down
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,17 @@ vim.keymap.set(
<summary>
<b>Debugging</b>
</summary>

- `debuggables` opens a prompt to select from available targets.
- `debug` searches for a target at the current cursor position.


```vimscript
```vim
:RustLsp[!] debuggables {args[]}?
:RustLsp[!] debug {args[]}?
```
```lua
vim.cmd.RustLsp('debug')
vim.cmd.RustLsp('debuggables')
-- or, to run the previous debuggable:
vim.cmd.RustLsp { 'debuggables', bang = true }
Expand Down Expand Up @@ -210,11 +216,16 @@ vim.keymap.set(
<summary>
<b>Runnables</b>
</summary>

- `runnables` opens a prompt to select from available targets.
- `run` searches for a target at the current cursor position.

```vimscript
```vim
:RustLsp[!] runnables {args[]}?
:RustLsp[!] run {args[]}?
```
```lua
vim.cmd.RustLsp('run')
vim.cmd.RustLsp('runnables')
-- or, to run the previous runnable:
vim.cmd.RustLsp { 'runnables', bang = true }
Expand All @@ -241,7 +252,7 @@ vim.keymap.set(
This is also possible in Neovim 0.9, but tests won't be run in the background,
and will block the UI.

```vimscript
```vim
:RustLsp[!] testables {args[]}?
```
```lua
Expand Down Expand Up @@ -300,7 +311,7 @@ vim.keymap.set(
<b>Expand macros recursively</b>
</summary>

```vimscript
```vim
:RustLsp expandMacro
```
```lua
Expand All @@ -314,7 +325,7 @@ vim.keymap.set(
<b>Rebuild proc macros</b>
</summary>

```vimscript
```vim
:RustLsp rebuildProcMacros
```
```lua
Expand All @@ -328,7 +339,7 @@ vim.keymap.set(
<b>Move item up/down</b>
</summary>

```vimscript
```vim
:RustLsp moveItem {up|down}
```
```lua
Expand All @@ -346,7 +357,7 @@ vim.keymap.set(
which is not supported by Neovim's built-in `vim.lsp.buf.codeAction`.
This plugin provides a command with a UI that does:

```vimscript
```vim
:RustLsp codeAction
```
```lua
Expand All @@ -371,7 +382,7 @@ vim.keymap.set(
Alternatively, you can set `auto_focus` to `true` in your config and you will
automatically enter the hover actions window.

```vimscript
```vim
:RustLsp hover actions
```
```lua
Expand All @@ -390,7 +401,7 @@ vim.keymap.set(
<b>Hover range</b>
</summary>

```vimscript
```vim
:RustLsp hover range
```
```lua
Expand All @@ -406,7 +417,7 @@ vim.keymap.set(
Display a hover window with explanations from the [rust error codes index](https://doc.rust-lang.org/error_codes/error-index.html)
over error diagnostics (if they have an error code).

```vimscript
```vim
:RustLsp explainError
```
```lua
Expand All @@ -428,7 +439,7 @@ vim.keymap.set(
as it consolidates the important bits (sometimes across files)
together.

```vimscript
```vim
:RustLsp renderDiagnostic
```
```lua
Expand All @@ -444,7 +455,7 @@ vim.keymap.set(
<b>Open Cargo.toml</b>
</summary>

```vimscript
```vim
:RustLsp openCargo
```
```lua
Expand All @@ -459,7 +470,7 @@ vim.keymap.set(

Open docs.rs documentation for the symbol under the cursor.

```vimscript
```vim
:RustLsp openDocs
```
```lua
Expand All @@ -472,7 +483,7 @@ vim.keymap.set(
<b>Parent Module</b>
</summary>

```vimscript
```vim
:RustLsp parentModule
```
```lua
Expand All @@ -487,7 +498,7 @@ vim.keymap.set(

rust-analyzer supports filtering workspace symbol searches.

```vimscript
```vim
:RustLsp[!] workspaceSymbol {onlyTypes?|allSymbols?} {query?}
```
```lua
Expand Down Expand Up @@ -516,7 +527,7 @@ by setting the rust-analyzer
Join selected lines into one, smartly fixing up whitespace, trailing commas, and braces.
Works with individual lines in normal mode and multiple lines in visual mode.

```vimscript
```vim
:RustLsp joinLines
```
```lua
Expand All @@ -532,7 +543,7 @@ by setting the rust-analyzer
<b>Structural search replace</b>
</summary>

```vimscript
```vim
:RustLsp ssr {query}
```
```lua
Expand All @@ -548,7 +559,7 @@ by setting the rust-analyzer
<b>View crate graph</b>
</summary>

```vimscript
```vim
:RustLsp crateGraph {backend {output}}
```
```lua
Expand All @@ -566,7 +577,7 @@ by setting the rust-analyzer
<b>View syntax tree</b>
</summary>

```vimscript
```vim
:RustLsp syntaxTree
```
```lua
Expand All @@ -589,7 +600,7 @@ by setting the rust-analyzer
Useful in large projects where running `cargo check` on each save
can be costly.

```vimscript
```vim
:RustLsp flyCheck {run?|clear?|cancel?}
```
```lua
Expand All @@ -615,7 +626,7 @@ by setting the rust-analyzer
of the function containing the cursor.
Useful for debugging or when working on rust-analyzer itself.

```vimscript
```vim
:RustLsp view {hir|mir}
```
```lua
Expand All @@ -636,7 +647,7 @@ by setting the rust-analyzer
NOTE: This currently requires a tree-sitter parser for Rust,
and a nightly compiler toolchain.

```vimscript
```vim
:Rustc unpretty {hir|mir|...}
```
```lua
Expand Down
2 changes: 2 additions & 0 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ It accepts the following subcommands:
'runnables {args[]}?' - Run tests, executables, etc.
':RustLsp!' means run the last runnable (ignores any args).
`args[]` allows you to override the executable's arguments.
'run {args[]}?' - Like 'runnables', but runs the target at the current cursor position.
'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.
'debug {args[]}?' - Like 'debuggables', but debugs the target at the current cursor position.
'testables {args[]}?' - Run tests
':RustLsp!' means run the last testable (ignores any args).
`args[]` allows you to override the executable's arguments.
Expand Down
56 changes: 40 additions & 16 deletions lua/rustaceanvim/commands/debuggables.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

local compat = require('rustaceanvim.compat')
local ra_runnables = require('rustaceanvim.runnables')

---@return { textDocument: lsp_text_document, position: nil }
local function get_params()
Expand Down Expand Up @@ -78,17 +79,23 @@ local function sanitize_results_for_debugging(result)
return ret
end

local function dap_run(args)
local rt_dap = require('rustaceanvim.dap')
local ok, dap = pcall(require, 'dap')
if ok then
rt_dap.start(args, true, dap.run)
local cached_commands = require('rustaceanvim.cached_commands')
cached_commands.set_last_debuggable(args)
else
vim.notify('nvim-dap is required for debugging', vim.log.levels.ERROR)
return
end
end

---@param debuggables RARunnable[]
---@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
debuggables = ra_runnables.apply_exec_args_override(executableArgsOverride, debuggables)
local options = get_options(debuggables)
if #options == 0 then
return
Expand All @@ -97,15 +104,8 @@ local function ui_select_debuggable(debuggables, executableArgsOverride)
if choice == nil then
return
end

local args = debuggables[choice].args
local rt_dap = require('rustaceanvim.dap')
local ok, dap = pcall(require, 'dap')
if ok then
rt_dap.start(args, true, dap.run)
end
local cached_commands = require('rustaceanvim.cached_commands')
cached_commands.set_last_debuggable(args)
dap_run(args)
end)
end

Expand All @@ -129,6 +129,22 @@ local function add_debuggables_to_nvim_dap(debuggables)
end
end

---@param debuggables RARunnable[]
---@param executableArgsOverride? string[]
local function debug_at_cursor_position(debuggables, executableArgsOverride)
if debuggables == nil then
return
end
debuggables = ra_runnables.apply_exec_args_override(executableArgsOverride, debuggables)
local choice = ra_runnables.get_runnable_at_cursor_position(debuggables)
if not choice then
vim.notify('No debuggable targets found for the current position.', vim.log.levels.ERROR)
return
end
local args = debuggables[choice].args
dap_run(args)
end

---@param callback fun(result:RARunnable[])
local function mk_handler(callback)
return function(_, result, _, _)
Expand Down Expand Up @@ -156,6 +172,14 @@ function M.debuggables(executableArgsOverride)
end))
end

---Runs the debuggable under the cursor, if present
---@param executableArgsOverride? string[]
function M.debug(executableArgsOverride)
runnables_request(mk_handler(function(debuggables)
debug_at_cursor_position(debuggables, executableArgsOverride)
end))
end

--- Sends the request to rust-analyzer to get the debuggables and adds them to nvim-dap's
--- configurations
function M.add_dap_debuggables()
Expand Down
23 changes: 23 additions & 0 deletions lua/rustaceanvim/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ local rustlsp_command_tbl = {
end,
bang = true,
},
debug = {
---@param args string[]
---@param opts vim.api.keyset.user_command
impl = function(args, opts)
if opts.bang then
require('rustaceanvim.cached_commands').execute_last_debuggable(args)
else
require('rustaceanvim.commands.debuggables').debug(args)
end
end,
bang = true,
},
expandMacro = {
impl = function(_)
require('rustaceanvim.commands.expand_macro')()
Expand Down Expand Up @@ -97,6 +109,17 @@ local rustlsp_command_tbl = {
end,
bang = true,
},
run = {
---@param opts vim.api.keyset.user_command
impl = function(args, opts)
if opts.bang then
require('rustaceanvim.cached_commands').execute_last_runnable(args)
else
require('rustaceanvim.runnables').run(args)
end
end,
bang = true,
},
testables = {
---@param opts vim.api.keyset.user_command
impl = function(args, opts)
Expand Down
2 changes: 2 additions & 0 deletions lua/rustaceanvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
--- 'runnables {args[]}?' - Run tests, executables, etc.
--- ':RustLsp!' means run the last runnable (ignores any args).
--- `args[]` allows you to override the executable's arguments.
--- 'run {args[]}?' - Like 'runnables', but runs the target at the current cursor position.
--- '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.
--- 'debug {args[]}?' - Like 'debuggables', but debugs the target at the current cursor position.
--- 'testables {args[]}?' - Run tests
--- ':RustLsp!' means run the last testable (ignores any args).
--- `args[]` allows you to override the executable's arguments.
Expand Down
Loading
Loading