From 8a0cddd9bbc010925fc147e16b0b20a10fe8f764 Mon Sep 17 00:00:00 2001 From: diniamo Date: Mon, 24 Jun 2024 18:53:53 +0200 Subject: [PATCH] languages/rust: add format --- docs/release-notes/rl-0.7.md | 2 ++ modules/plugins/languages/rust.nix | 39 +++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/release-notes/rl-0.7.md b/docs/release-notes/rl-0.7.md index 8aab123..a0f6fca 100644 --- a/docs/release-notes/rl-0.7.md +++ b/docs/release-notes/rl-0.7.md @@ -53,3 +53,5 @@ Release notes for release 0.7 [diniamo](https://github.com/diniamo): - Move the `theme` dag entry to before `luaScript`. + +- Add rustfmt as the default formatter for Rust diff --git a/modules/plugins/languages/rust.nix b/modules/plugins/languages/rust.nix index 8b4f1b4..0753ab4 100644 --- a/modules/plugins/languages/rust.nix +++ b/modules/plugins/languages/rust.nix @@ -4,17 +4,33 @@ lib, ... }: let + inherit (builtins) attrNames; inherit (lib.modules) mkIf mkMerge; inherit (lib.options) mkOption mkEnableOption; inherit (lib.strings) optionalString; inherit (lib.trivial) boolToString; 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.lua) expToLua; inherit (lib.nvim.dag) entryAnywhere; 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 { options.vim.languages.rust = { 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 = { enable = mkOption { description = "Rust Debug Adapter support"; @@ -86,6 +118,11 @@ in { 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) { vim = { startPlugins = ["rust-tools"] ++ optionals cfg.dap.enable [cfg.dap.package];