summaryrefslogtreecommitdiffstats
path: root/modules/plugins/completion/nvim-cmp/nvim-cmp.nix
blob: a16da8d33c13c262c5f3c110f3980c5a44df81fb (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{
  lib,
  config,
  ...
}: let
  inherit (lib.options) mkEnableOption mkOption literalMD;
  inherit (lib.types) str attrsOf nullOr either listOf;
  inherit (lib.generators) mkLuaInline;
  inherit (lib.nvim.types) mkPluginSetupOption luaInline mergelessListOf pluginType;
  inherit (lib.nvim.lua) toLuaObject;
  inherit (config.vim.lib) mkMappingOption;
  inherit (builtins) isString;

  cfg = config.vim.autocomplete.nvim-cmp;
in {
  options.vim.autocomplete.nvim-cmp = {
    enable = mkEnableOption "nvim-cmp";
    setupOpts = mkPluginSetupOption "the autocomplete plugin" {
      completion.completeopt = mkOption {
        type = str;
        default = "menu,menuone,noinsert";
        description = ''
          A comma-separated list of options for completion.

          See `:help completeopt` for the complete list.
        '';
      };

      sorting.comparators = mkOption {
        type = mergelessListOf (either str luaInline);
        default = [
          (mkLuaInline "deprio(kinds.Text)")
          (mkLuaInline "deprio(kinds.Snippet)")
          "offset"
          "exact"
          "score"
          "kind"
          "length"
          "sort_text"
        ];
        description = ''
          The comparator functions used for sorting completions.

          You can either pass a valid inline lua function
          (see `:help cmp-config.sorting.comparators`),
          or a string, in which case the builtin comparator with that name will
          be used.

          A `deprio` function and a `kinds`
          (`require("cmp.types").lsp.CompletionItemKind`) variable is provided
          above `setupOpts`. By passing a type to the function, the returned
          function will be a comparator that always ranks the specified kind the
          lowest.
        '';
        apply = map (
          c:
            if isString c
            then mkLuaInline ("cmp.config.compare." + c)
            else c
        );
      };
    };

    mappings = {
      complete = mkMappingOption "Complete [nvim-cmp]" "<C-Space>";
      confirm = mkMappingOption "Confirm [nvim-cmp]" "<CR>";
      next = mkMappingOption "Next item [nvim-cmp]" "<Tab>";
      previous = mkMappingOption "Previous item [nvim-cmp]" "<S-Tab>";
      close = mkMappingOption "Close [nvim-cmp]" "<C-e>";
      scrollDocsUp = mkMappingOption "Scroll docs up [nvim-cmp]" "<C-d>";
      scrollDocsDown = mkMappingOption "Scroll docs down [nvim-cmp]" "<C-f>";
    };

    format = mkOption {
      type = nullOr luaInline;
      default = mkLuaInline ''
        function(entry, vim_item)
          vim_item.menu = (${toLuaObject cfg.sources})[entry.source.name]
          return vim_item
        end
      '';
      defaultText = literalMD ''
        ```lua
        function(entry, vim_item)
          vim_item.menu = (''${toLuaObject config.vim.autocomplete.nvim-cmp.sources})[entry.source.name]
          return vim_item
        end
        ```
      '';
      description = ''
        The function used to customize the completion menu entries. This is
        outside of `setupOpts` to allow for an easier integration with
        lspkind.nvim.

        See `:help cmp-config.formatting.format`.
      '';
    };

    sources = mkOption {
      type = attrsOf (nullOr str);
      default = {
        nvim-cmp = null;
        buffer = "[Buffer]";
        path = "[Path]";
      };
      example = {
        nvim-cmp = null;
        buffer = "[Buffer]";
      };
      description = "The list of sources used by nvim-cmp";
    };

    sourcePlugins = mkOption {
      type = listOf pluginType;
      default = [];
      description = "List of source plugins used by nvim-cmp.";
    };
  };
}