Skip to content

Commit

Permalink
feat(testables,neotest): cargo-nextest support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jan 31, 2024
1 parent 38558fa commit 144c40d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ RustaceanToolsOpts *RustaceanToolsOpts*
Fields: ~
{executor?} (RustaceanExecutor|executor_alias) The executor to use for runnables/debuggables
{test_executor?} (RustaceanExecutor|test_executor_alias) The executor to use for runnables that are tests / testables
{enable_nextest?} (boolean) Whether to enable nextest. If enabled, `cargo test` commands will be transformed to `cargo nextest run` commands. Defaults to `true` if cargo-nextest is detected.
{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
Expand All @@ -148,7 +149,7 @@ RustaceanExecutorOpts *RustaceanExecutorOpts*
executor_alias *executor_alias*

Type: ~
"termopen"|"quickfix"|"toggleterm"|"vimux"
"termopen"|"quickfix"|"toggleterm"|"vimux"|"neotest"


test_executor_alias *test_executor_alias*
Expand Down
2 changes: 2 additions & 0 deletions lua/rustaceanvim/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ function M.validate(cfg)
end
ok, err = validate('tools', {
executor = { tools.executor, { 'table', 'string' } },
test_executor = { tools.test_executor, { 'table', 'string' } },
enable_nextest = { tools.enable_nextest, 'boolean' },
on_initialized = { tools.on_initialized, 'function', true },
reload_workspace_from_cargo_toml = { tools.reload_workspace_from_cargo_toml, 'boolean' },
open_url = { tools.open_url, 'function' },
Expand Down
1 change: 1 addition & 0 deletions lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
---@class RustaceanToolsOpts
---@field executor? RustaceanExecutor | executor_alias The executor to use for runnables/debuggables
---@field test_executor? RustaceanExecutor | test_executor_alias The executor to use for runnables that are tests / testables
---@field enable_nextest? boolean Whether to enable nextest. If enabled, `cargo test` commands will be transformed to `cargo nextest run` commands. Defaults to `true` if cargo-nextest is detected.
---@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
Expand Down
5 changes: 4 additions & 1 deletion lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ local RustaceanDefaultConfig = {
---@type RustaceanExecutor
test_executor = get_test_executor(),

---@type boolean
enable_nextest = vim.fn.executable('cargo-nextest') == 1,

--- callback to execute once rust-analyzer is done initializing the workspace
--- The callback receives one parameter indicating the `health` of the server: "ok" | "warning" | "error"
---@type fun(health:RustAnalyzerInitializedStatus) | nil
Expand Down Expand Up @@ -201,7 +204,7 @@ local RustaceanDefaultConfig = {
require('rustaceanvim.os').open_url(url)
end,
---settings for rustc
---@type table
---@class RustaceanRustcConfig
rustc = {
---@type string
edition = '2021',
Expand Down
21 changes: 21 additions & 0 deletions lua/rustaceanvim/overrides.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ function M.snippet_text_edits_to_text_edits(spe)
end
end

---Transforms the args to cargo-nextest args if it is detected.
---Mutates command!
---@param args string[]
function M.try_nextest_transform(args)
if vim.fn.executable('cargo-nextest') ~= 1 then
return args
end
if args[1] == 'test' then
args[1] = 'run'
table.insert(args, 1, 'nextest')
end
if args[#args] == '--nocapture' then
table.insert(args, 3, '--nocapture')
table.remove(args, #args)
end
if args[#args] == '--exact' then
table.remove(args, #args)
end
return args
end

-- sanitize_command_for_debugging substitutes the command arguments so it can be used to run a
-- debugger.
--
Expand Down
2 changes: 2 additions & 0 deletions lua/rustaceanvim/runnables.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require('rustaceanvim.config.internal')
local overrides = require('rustaceanvim.overrides')

local M = {}

Expand Down Expand Up @@ -73,6 +74,7 @@ function M.get_command(runnable)
ret = vim.list_extend(ret, args.cargoExtraArgs or {})
table.insert(ret, '--')
ret = vim.list_extend(ret, args.executableArgs or {})
ret = overrides.try_nextest_transform(ret)

return 'cargo', ret, dir
end
Expand Down

0 comments on commit 144c40d

Please sign in to comment.