From 1d90aaec555f65fda72925f0f80c7db1a62d78e8 Mon Sep 17 00:00:00 2001 From: Soliprem Date: Mon, 4 Nov 2024 08:50:56 +0100 Subject: [PATCH] r: added formatters --- modules/plugins/languages/r.nix | 66 +++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/modules/plugins/languages/r.nix b/modules/plugins/languages/r.nix index 27d7c99..752ccdd 100644 --- a/modules/plugins/languages/r.nix +++ b/modules/plugins/languages/r.nix @@ -18,6 +18,51 @@ packages = with pkgs.rPackages; [languageserver]; }; + defaultFormat = "format_r"; + formats = { + prettier = { + package = pkgs.nodePackages.prettier; + nullConfig = '' + local prettier = null_ls.builtins.formatting.prettier.with({ + filetypes = { "r" }, + extra_args = { + "--plugin", "prettier-plugin-r", + "--parser", "r" + } + }) + table.insert(ls_sources, prettier) + ''; + }; + + styler = { + package = pkgs.rWrapper.override { + packages = with pkgs.rPackages; [styler]; + }; + nullConfig = '' + table.insert( + ls_sources, + null_ls.builtins.formatting.styler.with({ + command = "${cfg.format.package}/bin/R", + }) + ) + ''; + }; + + format_r = { + package = pkgs.rWrapper.override { + packages = with pkgs.rPackages; [formatR]; + }; + nullConfig = '' + table.insert( + ls_sources, + null_ls.builtins.formatting.format_r.with({ + command = "${cfg.format.package}/bin/R", + }) + ) + ''; + }; + }; + defaultServer = "r_language_server"; servers = { r_language_server = { @@ -62,6 +107,22 @@ in { default = servers.${cfg.lsp.server}.package; }; }; + + format = { + enable = mkEnableOption "R formatting" // {default = config.vim.languages.enableFormat;}; + + type = mkOption { + description = "R formatter to use"; + type = enum (attrNames formats); + default = defaultFormat; + }; + + package = mkOption { + description = "R formatter package"; + type = package; + default = formats.${cfg.format.type}.package; + }; + }; }; config = mkIf cfg.enable (mkMerge [ @@ -70,6 +131,11 @@ in { vim.treesitter.grammars = [cfg.treesitter.package]; }) + (mkIf cfg.format.enable { + vim.lsp.null-ls.enable = true; + vim.lsp.null-ls.sources.r-format = formats.${cfg.format.type}.nullConfig; + }) + (mkIf cfg.lsp.enable { vim.lsp.lspconfig.enable = true; vim.lsp.lspconfig.sources.r-lsp = servers.${cfg.lsp.server}.lspConfig;