dotfiles/dot_config/nvim/lua/lang/rust.lua
2026-02-06 09:15:12 +01:00

102 lines
3.2 KiB
Lua

-- English comments only.
-- =========================
-- Formatting (Conform)
-- =========================
local ok_conform, conform = pcall(require, "conform")
if ok_conform then
conform.setup({
formatters_by_ft = {
rust = { "rustfmt" },
},
format_on_save = function(_)
return { timeout_ms = 2000, lsp_fallback = true }
end,
})
end
vim.keymap.set("n", "<leader>rf", function()
if ok_conform then
conform.format({ timeout_ms = 2000, lsp_fallback = true })
else
vim.lsp.buf.format({ async = true })
end
end, { desc = "Rust format" })
-- =========================
-- Cargo -> Quickfix
-- =========================
local function cargo_to_quickfix(cmd)
local output = vim.fn.systemlist(cmd)
vim.fn.setqflist({}, " ", { title = cmd, lines = output })
vim.cmd("copen")
end
vim.keymap.set("n", "<leader>rr", function() cargo_to_quickfix("cargo run") end, { desc = "Cargo run (quickfix)" })
vim.keymap.set("n", "<leader>rt", function() cargo_to_quickfix("cargo test") end, { desc = "Cargo test (quickfix)" })
vim.keymap.set("n", "<leader>rc", function() cargo_to_quickfix("cargo check") end, { desc = "Cargo check (quickfix)" })
vim.keymap.set("n", "<leader>rl", function() cargo_to_quickfix("cargo clippy") end, { desc = "Cargo clippy (quickfix)" })
vim.keymap.set("n", "<leader>rb", function() cargo_to_quickfix("cargo build") end, { desc = "Cargo build (quickfix)" })
-- =========================
-- DAP (Rust adapter + configurations)
-- =========================
local ok_dap, dap = pcall(require, "dap")
if not ok_dap then
return
end
local function rust_default_binary()
-- Try to read package name from Cargo.toml (common single-crate case).
local cargo_toml = vim.fn.getcwd() .. "/Cargo.toml"
if vim.fn.filereadable(cargo_toml) == 1 then
for _, line in ipairs(vim.fn.readfile(cargo_toml)) do
local name = line:match('^name%s*=%s*"(.-)"')
if name then
return vim.fn.getcwd() .. "/target/debug/" .. name
end
end
end
return vim.fn.getcwd() .. "/target/debug/"
end
-- codelldb adapter (installed via Mason)
local mason_path = vim.fn.stdpath("data") .. "/mason"
local codelldb_path = mason_path .. "/packages/codelldb/extension/adapter/codelldb"
dap.adapters.codelldb = {
type = "server",
port = "${port}",
executable = {
command = codelldb_path,
args = { "--port", "${port}" },
},
}
dap.configurations.rust = {
{
name = "Debug (default binary)",
type = "codelldb",
request = "launch",
program = function()
local default_path = rust_default_binary()
return vim.fn.input("Path to executable: ", default_path, "file")
end,
cwd = "${workspaceFolder}",
stopOnEntry = false,
},
{
name = "Debug tests (cargo test)",
type = "codelldb",
request = "launch",
-- This runs the test binary; you'll pick it once, then you can use <leader>dl (run last).
program = function()
-- Tip: run `cargo test --no-run` once, then pick the produced test binary.
local default_path = vim.fn.getcwd() .. "/target/debug/deps/"
return vim.fn.input("Path to test executable: ", default_path, "file")
end,
cwd = "${workspaceFolder}",
stopOnEntry = false,
},
}