summaryrefslogtreecommitdiffstats
path: root/modules/plugins/completion/blink-cmp/config.nix
blob: a17a0ec864f020191f7e0f494a5120774b0d6828 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{
  lib,
  config,
  ...
}: let
  inherit (lib.modules) mkIf;
  inherit (lib.strings) optionalString;
  inherit (lib.attrsets) mapAttrs' optionalAttrs;
  inherit (lib.generators) mkLuaInline;
  inherit (lib.attrsets) attrValues filterAttrs mapAttrsToList;
  inherit (lib.lists) map optional optionals elem;
  inherit (lib.nvim.lua) toLuaObject;
  inherit (builtins) concatStringsSep typeOf tryEval attrNames mapAttrs removeAttrs;

  cfg = config.vim.autocomplete.blink-cmp;
  cmpCfg = config.vim.autocomplete.nvim-cmp;
  inherit (cfg) mappings;

  getPluginName = plugin:
    if typeOf plugin == "string"
    then plugin
    else if (plugin ? pname && (tryEval plugin.pname).success)
    then plugin.pname
    else plugin.name;

  enabledBlinkSources = filterAttrs (_source: definition: definition.enable) cfg.sourcePlugins;
  blinkSourcePlugins = map (definition: definition.package) (attrValues enabledBlinkSources);

  blinkBuiltins = [
    "path"
    "lsp"
    "snippets"
    "buffer"
    "omni"
  ];
in {
  assertions =
    mapAttrsToList (provider: definition: {
      assertion = elem provider blinkBuiltins || definition.module != null;
      message = "`config.vim.autocomplete.blink-cmp.setupOpts.sources.providers.${provider}.module` is `null`: non-builtin providers must set `module`.";
    })
    cfg.setupOpts.sources.providers;

  vim = mkIf cfg.enable {
    startPlugins = ["blink-compat"] ++ blinkSourcePlugins ++ (optional cfg.friendly-snippets.enable "friendly-snippets");
    lazy.plugins = {
      blink-cmp = {
        package = "blink-cmp";
        setupModule = "blink.cmp";
        inherit (cfg) setupOpts;
        event = ["InsertEnter" "CmdlineEnter"];
        after =
          optionalString (config.vim.lazy.enable && cmpCfg.enable)
          (concatStringsSep "\n" (map
            (package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})")
            cmpCfg.sourcePlugins));
      };
    };

    autocomplete = {
      enableSharedCmpSources = true;
      blink-cmp.setupOpts = {
        sources = let
          # We do not want nvim-cmp compat sources overriding built-in blink sources
          filteredCmpSources = removeAttrs cmpCfg.sources blinkBuiltins;
        in {
          default =
            [
              "lsp"
              "path"
              "snippets"
              "buffer"
            ]
            ++ optionals cmpCfg.enable (attrNames filteredCmpSources)
            ++ (attrNames enabledBlinkSources);
          providers =
            optionalAttrs cmpCfg.enable (
              mapAttrs (name: _: {
                inherit name;
                module = "blink.compat.source";
              })
              filteredCmpSources
            )
            // (mapAttrs (name: definition: {
                inherit name;
                inherit (definition) module;
              })
              enabledBlinkSources);
        };
        snippets = mkIf config.vim.snippets.luasnip.enable {
          preset = "luasnip";
        };

        keymap =
          mapAttrs' (mapping-name: action: {
            name = mappings.${mapping-name};
            value = action;
          }) (filterAttrs (mapping-name: _action: mappings.${mapping-name} != null) {
            complete = ["show" "fallback"];
            close = ["hide" "fallback"];
            scrollDocsUp = ["scroll_documentation_up" "fallback"];
            scrollDocsDown = ["scroll_documentation_down" "fallback"];
            confirm = ["accept" "fallback"];
            next = [
              "select_next"
              "snippet_forward"
              (mkLuaInline ''
                function(cmp)
                  local line, col = unpack(vim.api.nvim_win_get_cursor(0))
                  has_words_before = col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil

                  if has_words_before then
                    return cmp.show()
                  end
                end
              '')
              "fallback"
            ];
            previous = [
              "select_prev"
              "snippet_backward"
              "fallback"
            ];
          });

        # cmdline is not enabled by default, we're just providing keymaps in
        # case the user enables them
        cmdline.keymap =
          mapAttrs' (mapping-name: action: {
            name = mappings.${mapping-name};
            value = action;
          }) (filterAttrs (mapping-name: _action: mappings.${mapping-name} != null) {
            complete = ["show" "fallback"];
            close = ["hide" "fallback"];
            scrollDocsUp = ["scroll_documentation_up" "fallback"];
            scrollDocsDown = ["scroll_documentation_down" "fallback"];
            # NOTE: mappings.confirm is skipped because our default, <CR> would
            # lead to accidental triggers of blink.accept instead of executing
            # the cmd
            next = ["select_next" "show" "fallback"];
            previous = ["select_prev" "fallback"];
          });
      };
    };
  };
}