neovim-flake/modules/plugins/languages/rust.nix

198 lines
6 KiB
Nix
Raw Normal View History

{
config,
pkgs,
lib,
...
}: let
2024-06-24 18:53:53 +02:00
inherit (builtins) attrNames;
2024-03-24 01:14:39 +01:00
inherit (lib.modules) mkIf mkMerge;
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.strings) optionalString;
inherit (lib.trivial) boolToString;
inherit (lib.lists) isList optionals;
2024-06-24 18:53:53 +02:00
inherit (lib.types) bool package str listOf either enum;
2024-03-24 01:14:39 +01:00
inherit (lib.nvim.types) mkGrammarOption;
inherit (lib.nvim.lua) expToLua;
inherit (lib.nvim.dag) entryAnywhere;
cfg = config.vim.languages.rust;
2024-06-24 18:53:53 +02:00
defaultFormat = "rustfmt";
formats = {
rustfmt = {
package = pkgs.rustfmt;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.rustfmt.with({
command = "${cfg.format.package}/bin/rustfmt",
})
)
'';
};
};
in {
options.vim.languages.rust = {
enable = mkEnableOption "Rust language support";
treesitter = {
enable = mkEnableOption "Rust treesitter" // {default = config.vim.languages.enableTreesitter;};
2024-03-24 01:14:39 +01:00
package = mkGrammarOption pkgs "rust";
};
crates = {
enable = mkEnableOption "crates-nvim, tools for managing dependencies";
codeActions = mkOption {
description = "Enable code actions through null-ls";
2024-03-24 01:14:39 +01:00
type = bool;
default = true;
};
};
lsp = {
enable = mkEnableOption "Rust LSP support (rust-analyzer with extra tools)" // {default = config.vim.languages.enableLSP;};
package = mkOption {
description = "rust-analyzer package, or the command to run as a list of strings";
example = ''[lib.getExe pkgs.jdt-language-server "-data" "~/.cache/jdtls/workspace"]'';
2024-03-24 01:14:39 +01:00
type = either package (listOf str);
default = pkgs.rust-analyzer;
};
opts = mkOption {
description = "Options to pass to rust analyzer";
2024-03-24 01:14:39 +01:00
type = str;
default = "";
};
};
2023-05-22 01:55:26 +02:00
2024-06-24 18:53:53 +02:00
format = {
enable = mkEnableOption "Rust formatting" // {default = config.vim.languages.enableFormat;};
type = mkOption {
description = "Rust formatter to use";
type = enum (attrNames formats);
default = defaultFormat;
};
package = mkOption {
description = "Rust formatter package";
type = package;
default = formats.${cfg.format.type}.package;
};
};
2023-05-22 01:55:26 +02:00
dap = {
enable = mkOption {
description = "Rust Debug Adapter support";
2024-03-24 01:14:39 +01:00
type = bool;
2023-05-22 01:55:26 +02:00
default = config.vim.languages.enableDAP;
};
2023-05-22 01:55:26 +02:00
package = mkOption {
description = "lldb pacakge";
2024-03-24 01:14:39 +01:00
type = package;
2023-05-22 01:55:26 +02:00
default = pkgs.lldb;
};
};
};
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.crates.enable {
vim = {
startPlugins = ["crates-nvim"];
lsp.null-ls.enable = mkIf cfg.crates.codeActions true;
autocomplete.sources = {"crates" = "[Crates]";};
pluginRC.rust-crates = entryAnywhere ''
require('crates').setup {
null_ls = {
enabled = ${boolToString cfg.crates.codeActions},
name = "crates.nvim",
}
}
'';
};
})
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package];
})
2024-06-24 18:53:53 +02:00
(mkIf cfg.format.enable {
vim.lsp.null-ls.enable = true;
vim.lsp.null-ls.sources.rust-format = formats.${cfg.format.type}.nullConfig;
})
(mkIf (cfg.lsp.enable || cfg.dap.enable) {
vim = {
startPlugins = ["rust-tools"] ++ optionals cfg.dap.enable [cfg.dap.package];
2023-05-22 01:55:26 +02:00
lsp.lspconfig = {
enable = true;
sources.rust-lsp = ''
local rt = require('rust-tools')
rust_on_attach = function(client, bufnr)
default_on_attach(client, bufnr)
local opts = { noremap=true, silent=true, buffer = bufnr }
vim.keymap.set("n", "<leader>ris", rt.inlay_hints.set, opts)
vim.keymap.set("n", "<leader>riu", rt.inlay_hints.unset, opts)
vim.keymap.set("n", "<leader>rr", rt.runnables.runnables, opts)
vim.keymap.set("n", "<leader>rp", rt.parent_module.parent_module, opts)
vim.keymap.set("n", "<leader>rm", rt.expand_macro.expand_macro, opts)
vim.keymap.set("n", "<leader>rc", rt.open_cargo_toml.open_cargo_toml, opts)
vim.keymap.set("n", "<leader>rg", function() rt.crate_graph.view_crate_graph("x11", nil) end, opts)
${optionalString cfg.dap.enable ''
vim.keymap.set("n", "<leader>rd", ":RustDebuggables<cr>", opts)
vim.keymap.set(
"n", "${config.vim.debugger.nvim-dap.mappings.continue}",
function()
local dap = require("dap")
if dap.status() == "" then
vim.cmd "RustDebuggables"
else
dap.continue()
end
end,
opts
)
''}
end
local rustopts = {
tools = {
autoSetHints = true,
hover_with_actions = false,
inlay_hints = {
only_current_line = false,
}
},
server = {
capabilities = capabilities,
on_attach = rust_on_attach,
cmd = ${
if isList cfg.lsp.package
2024-03-24 01:14:39 +01:00
then expToLua cfg.lsp.package
else ''{"${cfg.lsp.package}/bin/rust-analyzer"}''
2023-05-22 01:55:26 +02:00
},
settings = {
${cfg.lsp.opts}
}
},
${optionalString cfg.dap.enable ''
dap = {
adapter = {
type = "executable",
command = "${cfg.dap.package}/bin/lldb-vscode",
name = "rt_lldb",
},
},
''}
}
rt.setup(rustopts)
'';
};
};
})
]);
}