summaryrefslogtreecommitdiffstats
path: root/modules/plugins/lsp/presets/csharp_ls.nix
blob: fcd21aa8bc3cc1665c91866596a3eef83f67f8dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
  config,
  lib,
  pkgs,
  ...
}: let
  inherit (lib.modules) mkIf;
  inherit (lib.nvim.types) mkLspPresetEnableOption;
  inherit (lib.meta) getExe;
  inherit (lib.generators) mkLuaInline;

  cfg = config.vim.lsp.presets.csharp_ls;
in {
  # HACK: this server should be named `csharp-ls`, but the extension `csharpls-extended-lsp-nvim` only works if it is named `csharp_ls`
  options.vim.lsp.presets.csharp_ls = {
    enable = mkLspPresetEnableOption {
      option = "csharp_ls";
      display = "C#";
    };
  };

  config = mkIf cfg.enable {
    vim.lsp.servers.csharp_ls = {
      cmd = mkLuaInline ''
        function(dispatchers, config)
           return vim.lsp.rpc.start({ '${getExe pkgs.csharp-ls}', '--features', 'razor-support' , '--features', 'metadata-uris'}, dispatchers, {
            -- csharp-ls attempt to locate sln, slnx or csproj files from cwd, so set cwd to root directory.
            -- If cmd_cwd is provided, use it instead.
            cwd = config.cmd_cwd or config.root_dir,
            env = config.cmd_env,
            detached = config.detached,
          })
        end
      '';
      root_dir = mkLuaInline ''
        function(bufnr, on_dir)
           local fname = vim.api.nvim_buf_get_name(bufnr)
           on_dir(util.root_pattern '*.sln'(fname) or util.root_pattern '*.slnx'(fname) or util.root_pattern '*.csproj'(fname))
         end
      '';
      init_options = {
        AutomaticWorkspaceInit = true;
      };
      get_language_id = mkLuaInline ''
        function(_, ft)
            if ft == 'cs' then
              return 'csharp'
            end
            return ft
          end
      '';
    };
  };
}