summaryrefslogtreecommitdiffstats
path: root/modules/plugins/lsp/presets/ccls.nix
blob: 5ff6e78db1edf33c79665af825d86c2317cfe844 (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
55
56
57
{
  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.ccls;
in {
  options.vim.lsp.presets.ccls = {
    enable = mkLspPresetEnableOption {
      option = "ccls";
      display = "CC";
    };
  };

  config = mkIf cfg.enable {
    vim.lsp.servers.ccls = {
      enable = true;
      cmd = [(getExe pkgs.ccls)];
      offset_encoding = "utf-32";
      root_markers = [".git" ".ccls" "compile_commands.json"];
      workspace_required = true;
      on_attach = mkLuaInline ''
        function(client, bufnr)
          local function switch_source_header(bufnr)
            local method_name = "textDocument/switchSourceHeader"
            local params = vim.lsp.util.make_text_document_params(bufnr)
            client:request(method_name, params, function(err, result)
              if err then
                error(tostring(err))
              end
              if not result then
                vim.notify('corresponding file cannot be determined')
                return
              end
              vim.cmd.edit(vim.uri_to_fname(result))
            end, bufnr)
          end

          vim.api.nvim_buf_create_user_command(
            bufnr,
            "LspCclsSwitchSourceHeader",
            function(arg)
              switch_source_header(client, 0)
            end,
            {desc = "Switch between source/header"}
          )
        end
      '';
    };
  };
}