Skip to content

Commit

Permalink
feat: tools.cargo_override option (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Apr 1, 2024
1 parent e74e0e5 commit 0db93dc
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ 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).

## [4.20.0] - 2024-04-01

### Added

- DAP/LSP: `tools.cargo_override` option to
override the `cargo` command for runnables/debuggables/testables.

## [4.19.0] - 2024-04-01

### Added
Expand Down
3 changes: 2 additions & 1 deletion doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ RustaceanToolsOpts *RustaceanToolsOpts*
{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
{crate_test_executor?} (RustaceanExecutor|test_executor_alias) The executor to use for runnables that are crate test suites (--all-targets)
{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.
{cargo_override?} (string) Set this to override the 'cargo' command for runnables, debuggables (etc., e.g. to 'cross'). If set, this takes precedence over 'enable_nextest'.
{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. Ignored if `cargo_override` is set.
{enable_clippy?} (boolean) Whether to enable clippy checks on save if a clippy installation is detected. Default: `true`
{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
Expand Down
4 changes: 4 additions & 0 deletions lua/rustaceanvim/commands/debuggables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local M = {}

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

---@return { textDocument: lsp_text_document, position: nil }
local function get_params()
Expand Down Expand Up @@ -44,6 +45,9 @@ local function get_options(result)
for _, debuggable in ipairs(result) do
local label = build_label(debuggable.args)
local str = label
if config.tools.cargo_override then
str = str:gsub('^cargo', config.tools.cargo_override)
end
table.insert(option_strings, str)
end

Expand Down
1 change: 1 addition & 0 deletions lua/rustaceanvim/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function M.validate(cfg)
executor = { tools.executor, { 'table', 'string' } },
test_executor = { tools.test_executor, { 'table', 'string' } },
crate_test_executor = { tools.crate_test_executor, { 'table', 'string' } },
cargo_override = { tools.cargo_override, 'string', true },
enable_nextest = { tools.enable_nextest, 'boolean' },
enable_clippy = { tools.enable_clippy, 'boolean' },
on_initialized = { tools.on_initialized, 'function', true },
Expand Down
3 changes: 2 additions & 1 deletion lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
---@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 crate_test_executor? RustaceanExecutor | test_executor_alias The executor to use for runnables that are crate test suites (--all-targets)
---@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 cargo_override? string Set this to override the 'cargo' command for runnables, debuggables (etc., e.g. to 'cross'). If set, this takes precedence over 'enable_nextest'.
---@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. Ignored if `cargo_override` is set.
---@field enable_clippy? boolean Whether to enable clippy checks on save if a clippy installation is detected. Default: `true`
---@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
Expand Down
3 changes: 3 additions & 0 deletions lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ local RustaceanDefaultConfig = {
---@type RustaceanExecutor
crate_test_executor = get_crate_test_executor(),

---@type string | nil
cargo_override = nil,

---@type boolean
enable_nextest = true,

Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function M.start(args, verbose, callback, on_error)
handle_configured_options(adapter, args, verbose)

local cargo_args = get_cargo_args_from_runnables_args(args)
local cmd = vim.list_extend({ 'cargo' }, cargo_args)
local cmd = vim.list_extend({ config.tools.cargo_override or 'cargo' }, cargo_args)
if verbose then
vim.notify('Compiling a debug build for debugging. This might take some time...')
end
Expand Down
16 changes: 16 additions & 0 deletions lua/rustaceanvim/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,22 @@ function health.check()
},
}

if config.tools.cargo_override then
table.insert(external_dependencies, {
name = 'Cargo override: ' .. config.tools.cargo_override,
get_binaries = function()
return { config.tools.cargo_override }
end,
optional = function()
return true
end,
url = '',
info = [[
Set in the config to override the 'cargo' command for debugging and testing.
]],
})
end

if adapter ~= false then
table.insert(external_dependencies, {
name = adapter.name or 'debug adapter',
Expand Down
11 changes: 9 additions & 2 deletions lua/rustaceanvim/runnables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ local function get_options(result, executableArgsOverride, opts)
if opts.tests_only then
str = prettify_test_option(str)
end
if config.tools.cargo_override then
str = str:gsub('^cargo', config.tools.cargo_override)
end
table.insert(option_strings, str)
end

Expand All @@ -74,11 +77,15 @@ function M.get_command(runnable)
ret = vim.list_extend(ret, args.cargoExtraArgs or {})
table.insert(ret, '--')
ret = vim.list_extend(ret, args.executableArgs or {})
if config.tools.enable_nextest and not vim.startswith(runnable.label, 'doctest') then
if
config.tools.enable_nextest
and not config.tools.cargo_override
and not vim.startswith(runnable.label, 'doctest')
then
ret = overrides.try_nextest_transform(ret)
end

return 'cargo', ret, dir
return config.tools.cargo_override or 'cargo', ret, dir
end

---@param choice integer
Expand Down

0 comments on commit 0db93dc

Please sign in to comment.