languages/rust: add format

This commit is contained in:
diniamo 2024-06-24 18:53:53 +02:00
parent 923481640e
commit 8a0cddd9bb
2 changed files with 40 additions and 1 deletions

View File

@ -53,3 +53,5 @@ Release notes for release 0.7
[diniamo](https://github.com/diniamo): [diniamo](https://github.com/diniamo):
- Move the `theme` dag entry to before `luaScript`. - Move the `theme` dag entry to before `luaScript`.
- Add rustfmt as the default formatter for Rust

View File

@ -4,17 +4,33 @@
lib, lib,
... ...
}: let }: let
inherit (builtins) attrNames;
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf mkMerge;
inherit (lib.options) mkOption mkEnableOption; inherit (lib.options) mkOption mkEnableOption;
inherit (lib.strings) optionalString; inherit (lib.strings) optionalString;
inherit (lib.trivial) boolToString; inherit (lib.trivial) boolToString;
inherit (lib.lists) isList optionals; inherit (lib.lists) isList optionals;
inherit (lib.types) bool package str listOf either; inherit (lib.types) bool package str listOf either enum;
inherit (lib.nvim.types) mkGrammarOption; inherit (lib.nvim.types) mkGrammarOption;
inherit (lib.nvim.lua) expToLua; inherit (lib.nvim.lua) expToLua;
inherit (lib.nvim.dag) entryAnywhere; inherit (lib.nvim.dag) entryAnywhere;
cfg = config.vim.languages.rust; cfg = config.vim.languages.rust;
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 { in {
options.vim.languages.rust = { options.vim.languages.rust = {
enable = mkEnableOption "Rust language support"; enable = mkEnableOption "Rust language support";
@ -49,6 +65,22 @@ in {
}; };
}; };
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;
};
};
dap = { dap = {
enable = mkOption { enable = mkOption {
description = "Rust Debug Adapter support"; description = "Rust Debug Adapter support";
@ -86,6 +118,11 @@ in {
vim.treesitter.grammars = [cfg.treesitter.package]; vim.treesitter.grammars = [cfg.treesitter.package];
}) })
(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) { (mkIf (cfg.lsp.enable || cfg.dap.enable) {
vim = { vim = {
startPlugins = ["rust-tools"] ++ optionals cfg.dap.enable [cfg.dap.package]; startPlugins = ["rust-tools"] ++ optionals cfg.dap.enable [cfg.dap.package];