|
| 1 | +{ |
| 2 | + lib, |
| 3 | + pkgs, |
| 4 | + config, |
| 5 | + options, |
| 6 | + ... |
| 7 | +}: let |
| 8 | + inherit (builtins) attrNames; |
| 9 | + inherit (lib.options) mkEnableOption mkOption; |
| 10 | + inherit (lib.types) either listOf package str enum; |
| 11 | + inherit (lib.modules) mkIf mkMerge; |
| 12 | + inherit (lib.lists) isList; |
| 13 | + inherit (lib.strings) optionalString; |
| 14 | + inherit (lib.nvim.types) mkGrammarOption; |
| 15 | + inherit (lib.nvim.lua) expToLua; |
| 16 | + |
| 17 | + lspKeyConfig = config.vim.lsp.mappings; |
| 18 | + lspKeyOptions = options.vim.lsp.mappings; |
| 19 | + mkLspBinding = optionName: action: let |
| 20 | + key = lspKeyConfig.${optionName}; |
| 21 | + desc = lspKeyOptions.${optionName}.description; |
| 22 | + in |
| 23 | + optionalString (key != null) "vim.keymap.set('n', '${key}', ${action}, {buffer=bufnr, noremap=true, silent=true, desc='${desc}'})"; |
| 24 | + |
| 25 | + # Omnisharp doesn't have colors in popup docs for some reason, and I've also |
| 26 | + # seen mentions of it being way slower, so someone finds missing |
| 27 | + # functionality, this will be the default. |
| 28 | + defaultServer = "csharp_ls"; |
| 29 | + servers = { |
| 30 | + omnisharp = { |
| 31 | + package = pkgs.omnisharp-roslyn; |
| 32 | + internalFormatter = true; |
| 33 | + lspConfig = '' |
| 34 | + lspconfig.omnisharp.setup { |
| 35 | + capabilities = capabilities, |
| 36 | + on_attach = function(client, bufnr) |
| 37 | + default_on_attach(client, bufnr) |
| 38 | +
|
| 39 | + local oe = require("omnisharp_extended") |
| 40 | + ${mkLspBinding "goToDefinition" "oe.lsp_definition"} |
| 41 | + ${mkLspBinding "goToType" "oe.lsp_type_definition"} |
| 42 | + ${mkLspBinding "listReferences" "oe.lsp_references"} |
| 43 | + ${mkLspBinding "listImplementations" "oe.lsp_implementation"} |
| 44 | + end, |
| 45 | + cmd = ${ |
| 46 | + if isList cfg.lsp.package |
| 47 | + then expToLua cfg.lsp.package |
| 48 | + else "{'${cfg.lsp.package}/bin/OmniSharp'}" |
| 49 | + } |
| 50 | + } |
| 51 | + ''; |
| 52 | + }; |
| 53 | + |
| 54 | + csharp_ls = { |
| 55 | + package = pkgs.csharp-ls; |
| 56 | + internalFormatter = true; |
| 57 | + lspConfig = '' |
| 58 | + local extended_handler = require("csharpls_extended").handler |
| 59 | +
|
| 60 | + lspconfig.csharp_ls.setup { |
| 61 | + capabilities = capabilities, |
| 62 | + on_attach = default_on_attach, |
| 63 | + handlers = { |
| 64 | + ["textDocument/definition"] = extended_handler, |
| 65 | + ["textDocument/typeDefinition"] = extended_handler |
| 66 | + }, |
| 67 | + cmd = ${ |
| 68 | + if isList cfg.lsp.package |
| 69 | + then expToLua cfg.lsp.package |
| 70 | + else "{'${cfg.lsp.package}/bin/csharp-ls'}" |
| 71 | + } |
| 72 | + } |
| 73 | + ''; |
| 74 | + }; |
| 75 | + }; |
| 76 | + |
| 77 | + extraServerPlugins = { |
| 78 | + omnisharp = ["omnisharp-extended"]; |
| 79 | + csharp_ls = ["csharpls-extended"]; |
| 80 | + }; |
| 81 | + |
| 82 | + cfg = config.vim.languages.csharp; |
| 83 | +in { |
| 84 | + options = { |
| 85 | + vim.languages.csharp = { |
| 86 | + enable = mkEnableOption "C# language support"; |
| 87 | + |
| 88 | + treesitter = { |
| 89 | + enable = mkEnableOption "C# treesitter" // {default = config.vim.languages.enableTreesitter;}; |
| 90 | + package = mkGrammarOption pkgs "c-sharp"; |
| 91 | + }; |
| 92 | + |
| 93 | + lsp = { |
| 94 | + enable = mkEnableOption "C# LSP support" // {default = config.vim.languages.enableLSP;}; |
| 95 | + server = mkOption { |
| 96 | + description = "C# LSP server to use"; |
| 97 | + type = enum (attrNames servers); |
| 98 | + default = defaultServer; |
| 99 | + }; |
| 100 | + |
| 101 | + package = mkOption { |
| 102 | + description = "C# LSP server package, or the command to run as a list of strings"; |
| 103 | + type = either package (listOf str); |
| 104 | + default = servers.${cfg.lsp.server}.package; |
| 105 | + }; |
| 106 | + }; |
| 107 | + }; |
| 108 | + }; |
| 109 | + |
| 110 | + config = mkIf cfg.enable (mkMerge [ |
| 111 | + (mkIf cfg.treesitter.enable { |
| 112 | + vim.treesitter.enable = true; |
| 113 | + vim.treesitter.grammars = [cfg.treesitter.package]; |
| 114 | + }) |
| 115 | + |
| 116 | + (mkIf cfg.lsp.enable { |
| 117 | + vim.startPlugins = extraServerPlugins.${cfg.lsp.server} or []; |
| 118 | + vim.lsp.lspconfig.enable = true; |
| 119 | + vim.lsp.lspconfig.sources.csharp-lsp = servers.${cfg.lsp.server}.lspConfig; |
| 120 | + }) |
| 121 | + ]); |
| 122 | +} |
0 commit comments