From 8e96f0aaa325b992d17a3e7c9613344680375791 Mon Sep 17 00:00:00 2001 From: Soliprem <73885403+Soliprem@users.noreply.github.com> Date: Tue, 24 Sep 2024 14:00:31 +0200 Subject: [PATCH] R: adding LSP (#378) * r: implementing lsp * r: version bump to context fixes treesitter bug * r: changing treesitter package definition to mkGrammarOption * added changelog entry * docs: wording --------- Co-authored-by: raf --- configuration.nix | 1 + docs/release-notes/rl-0.7.md | 5 ++ flake.lock | 6 +-- modules/plugins/languages/default.nix | 1 + modules/plugins/languages/r.nix | 78 +++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 modules/plugins/languages/r.nix diff --git a/configuration.nix b/configuration.nix index 7479012..08b0f9b 100644 --- a/configuration.nix +++ b/configuration.nix @@ -59,6 +59,7 @@ isMaximal: { python.enable = isMaximal; dart.enable = isMaximal; bash.enable = isMaximal; + r.enable = isMaximal; tailwind.enable = isMaximal; typst.enable = isMaximal; clang = { diff --git a/docs/release-notes/rl-0.7.md b/docs/release-notes/rl-0.7.md index cf250e0..7fd4876 100644 --- a/docs/release-notes/rl-0.7.md +++ b/docs/release-notes/rl-0.7.md @@ -108,6 +108,7 @@ everyone. plugin's options can now be found under `indentBlankline.setupOpts`, the previous iteration of the module also included out of place/broken options, which have been removed for the time being. These are: + - `listChar` - this was already unused - `fillChar` - this had nothing to do with the plugin, please configure it yourself by adding `vim.opt.listchars:append({ space = '' })` to your @@ -191,3 +192,7 @@ everyone. - Telescope: - Fixed `project-nvim` command and keybinding - Added default ikeybind/command for `Telescope resume` (`fr`) + +[Soliprem](https://github.com/Soliprem) + +- Add LSP and Treesitter support for R under `vim.languages.R`. diff --git a/flake.lock b/flake.lock index 377c084..9e11e8c 100644 --- a/flake.lock +++ b/flake.lock @@ -1280,11 +1280,11 @@ "plugin-nvim-treesitter-context": { "flake": false, "locked": { - "lastModified": 1716388265, - "narHash": "sha256-EY5Si6t7LXcxOP3ubGAAMd3lgbeaCOCIybSKi1Ucx98=", + "lastModified": 1726947805, + "narHash": "sha256-5oN/vyhSqDqjLEzECj01A7A+Yq7U1H1HXLbzkC1Ljqw=", "owner": "nvim-treesitter", "repo": "nvim-treesitter-context", - "rev": "f62bfe19e0fbc13ae95649dfb3cf22f4ff85b683", + "rev": "3d5390c49e3f8fe457b376df2a49aa39d75b7911", "type": "github" }, "original": { diff --git a/modules/plugins/languages/default.nix b/modules/plugins/languages/default.nix index e86a521..28c1fd8 100644 --- a/modules/plugins/languages/default.nix +++ b/modules/plugins/languages/default.nix @@ -17,6 +17,7 @@ in { ./ocaml.nix ./php.nix ./python.nix + ./r.nix ./rust.nix ./sql.nix ./svelte.nix diff --git a/modules/plugins/languages/r.nix b/modules/plugins/languages/r.nix new file mode 100644 index 0000000..27d7c99 --- /dev/null +++ b/modules/plugins/languages/r.nix @@ -0,0 +1,78 @@ +{ + config, + pkgs, + lib, + ... +}: let + inherit (builtins) attrNames; + inherit (lib.options) mkEnableOption mkOption literalExpression; + inherit (lib.modules) mkIf mkMerge; + inherit (lib.lists) isList; + inherit (lib.types) enum either listOf package str; + inherit (lib.nvim.lua) expToLua; + inherit (lib.nvim.types) mkGrammarOption; + + cfg = config.vim.languages.r; + + r-with-languageserver = pkgs.rWrapper.override { + packages = with pkgs.rPackages; [languageserver]; + }; + + defaultServer = "r_language_server"; + servers = { + r_language_server = { + package = pkgs.writeShellScriptBin "r_lsp" '' + ${r-with-languageserver}/bin/R --slave -e "languageserver::run()" + ''; + lspConfig = '' + lspconfig.r_language_server.setup{ + capabilities = capabilities; + on_attach = default_on_attach; + cmd = ${ + if isList cfg.lsp.package + then expToLua cfg.lsp.package + else ''{"${lib.getExe cfg.lsp.package}"}'' + } + } + ''; + }; + }; +in { + options.vim.languages.r = { + enable = mkEnableOption "R language support"; + + treesitter = { + enable = mkEnableOption "R treesitter" // {default = config.vim.languages.enableTreesitter;}; + package = mkGrammarOption pkgs "r"; + }; + + 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; + }; + }; + }; + + config = mkIf cfg.enable (mkMerge [ + (mkIf cfg.treesitter.enable { + vim.treesitter.enable = true; + vim.treesitter.grammars = [cfg.treesitter.package]; + }) + + (mkIf cfg.lsp.enable { + vim.lsp.lspconfig.enable = true; + vim.lsp.lspconfig.sources.r-lsp = servers.${cfg.lsp.server}.lspConfig; + }) + ]); +}