From 1d90aaec555f65fda72925f0f80c7db1a62d78e8 Mon Sep 17 00:00:00 2001 From: Soliprem Date: Mon, 4 Nov 2024 08:50:56 +0100 Subject: [PATCH 1/4] 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; From 152acfce5ade0edb8fea3c8d6a6c20b7eb42b9ba Mon Sep 17 00:00:00 2001 From: Soliprem Date: Mon, 4 Nov 2024 08:52:26 +0100 Subject: [PATCH 2/4] changelog: added entry for r formatter --- docs/release-notes/rl-0.7.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/release-notes/rl-0.7.md b/docs/release-notes/rl-0.7.md index 87df91e..273555e 100644 --- a/docs/release-notes/rl-0.7.md +++ b/docs/release-notes/rl-0.7.md @@ -285,6 +285,7 @@ To migrate to `nixfmt`, simply change `vim.languages.nix.format.type` to [Soliprem](https://github.com/Soliprem): - Add LSP and Treesitter support for R under `vim.languages.R`. + - Add formatter suppoort for R - Add Otter support under `vim.lsp.otter` and an assert to prevent conflict with ccc - Fixed typo in Otter's setupOpts From fbd15f5f3a4e70d96fe8b891e460bdec5f212a7b Mon Sep 17 00:00:00 2001 From: Soliprem Date: Tue, 5 Nov 2024 17:20:31 +0100 Subject: [PATCH 3/4] languages/r: attempting to create a toggle for wrappers --- modules/plugins/languages/r.nix | 53 ++++++++++++++++----------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/modules/plugins/languages/r.nix b/modules/plugins/languages/r.nix index 752ccdd..9c32376 100644 --- a/modules/plugins/languages/r.nix +++ b/modules/plugins/languages/r.nix @@ -20,24 +20,14 @@ 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]; - }; + package = + if cfg.useWrappedPackages + then + (pkgs.rWrapper.override { + packages = with pkgs.rPackages; [styler]; + }) + else pkgs.rPackages.styler; nullConfig = '' table.insert( ls_sources, @@ -49,9 +39,13 @@ }; format_r = { - package = pkgs.rWrapper.override { - packages = with pkgs.rPackages; [formatR]; - }; + package = + if cfg.useWrappedPackages + then + (pkgs.rWrapper.override { + packages = with pkgs.rPackages; [formatR]; + }) + else pkgs.rPackages.formatR; nullConfig = '' table.insert( ls_sources, @@ -66,9 +60,16 @@ defaultServer = "r_language_server"; servers = { r_language_server = { - package = pkgs.writeShellScriptBin "r_lsp" '' - ${r-with-languageserver}/bin/R --slave -e "languageserver::run()" - ''; + package = + if cfg.useWrappedPackages + then + (pkgs.writeShellScriptBin "r_lsp" '' + ${r-with-languageserver}/bin/R --slave -e "languageserver::run()" + '') + else + pkgs.writeShellScriptBin "r_lsp" '' + R --slave -e "languageserver::run()" + ''; lspConfig = '' lspconfig.r_language_server.setup{ capabilities = capabilities; @@ -85,6 +86,7 @@ in { options.vim.languages.r = { enable = mkEnableOption "R language support"; + useWrappedPackages = mkEnableOption "use rWrapper for packages to work without R in user's PATH" // {default = true;}; treesitter = { enable = mkEnableOption "R treesitter" // {default = config.vim.languages.enableTreesitter;}; @@ -93,16 +95,13 @@ in { lsp = { enable = mkEnableOption "R LSP support" // {default = config.vim.languages.enableLSP;}; - server = mkOption { description = "R LSP server to use"; type = enum (attrNames servers); default = defaultServer; }; - package = mkOption { description = "R LSP server package, or the command to run as a list of strings"; - example = literalExpression "[ (lib.getExe pkgs.jdt-language-server) \"-data\" \"~/.cache/jdtls/workspace\" ]"; type = either package (listOf str); default = servers.${cfg.lsp.server}.package; }; @@ -110,13 +109,11 @@ in { 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; From b321c010477adc2c37bbd37a2c24b188418f3841 Mon Sep 17 00:00:00 2001 From: Soliprem Date: Tue, 5 Nov 2024 17:21:37 +0100 Subject: [PATCH 4/4] languages/r: removing toggle for wrappers --- modules/plugins/languages/r.nix | 39 ++++++++++++--------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/modules/plugins/languages/r.nix b/modules/plugins/languages/r.nix index 9c32376..b336e91 100644 --- a/modules/plugins/languages/r.nix +++ b/modules/plugins/languages/r.nix @@ -21,13 +21,9 @@ defaultFormat = "format_r"; formats = { styler = { - package = - if cfg.useWrappedPackages - then - (pkgs.rWrapper.override { - packages = with pkgs.rPackages; [styler]; - }) - else pkgs.rPackages.styler; + package = pkgs.rWrapper.override { + packages = with pkgs.rPackages; [styler]; + }; nullConfig = '' table.insert( ls_sources, @@ -39,13 +35,9 @@ }; format_r = { - package = - if cfg.useWrappedPackages - then - (pkgs.rWrapper.override { - packages = with pkgs.rPackages; [formatR]; - }) - else pkgs.rPackages.formatR; + package = pkgs.rWrapper.override { + packages = with pkgs.rPackages; [formatR]; + }; nullConfig = '' table.insert( ls_sources, @@ -60,16 +52,9 @@ defaultServer = "r_language_server"; servers = { r_language_server = { - package = - if cfg.useWrappedPackages - then - (pkgs.writeShellScriptBin "r_lsp" '' - ${r-with-languageserver}/bin/R --slave -e "languageserver::run()" - '') - else - pkgs.writeShellScriptBin "r_lsp" '' - R --slave -e "languageserver::run()" - ''; + package = pkgs.writeShellScriptBin "r_lsp" '' + ${r-with-languageserver}/bin/R --slave -e "languageserver::run()" + ''; lspConfig = '' lspconfig.r_language_server.setup{ capabilities = capabilities; @@ -86,7 +71,6 @@ in { options.vim.languages.r = { enable = mkEnableOption "R language support"; - useWrappedPackages = mkEnableOption "use rWrapper for packages to work without R in user's PATH" // {default = true;}; treesitter = { enable = mkEnableOption "R treesitter" // {default = config.vim.languages.enableTreesitter;}; @@ -95,13 +79,16 @@ in { lsp = { enable = mkEnableOption "R LSP support" // {default = config.vim.languages.enableLSP;}; + server = mkOption { description = "R LSP server to use"; type = enum (attrNames servers); default = defaultServer; }; + package = mkOption { description = "R LSP server package, or the command to run as a list of strings"; + example = literalExpression "[ (lib.getExe pkgs.jdt-language-server) \"-data\" \"~/.cache/jdtls/workspace\" ]"; type = either package (listOf str); default = servers.${cfg.lsp.server}.package; }; @@ -109,11 +96,13 @@ in { 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;