summaryrefslogtreecommitdiffstats
path: root/modules/wrapper/lazy/lazy.nix
blob: 6061c9b1397c5cbe2f56aa1c125012c418b33001 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
{lib, ...}: let
  inherit (lib.options) mkOption mkEnableOption;
  inherit (lib.types) enum listOf submodule nullOr str bool int attrsOf anything either oneOf lines;
  inherit (lib.nvim.types) pluginType luaInline;
  inherit (lib.nvim.config) mkBool;

  lznKeysSpec = submodule {
    options = {
      key = mkOption {
        type = nullOr str;
        description = "Key to bind to. If key is null this entry is ignored.";
      };

      action = mkOption {
        type = nullOr str;
        default = null;
        description = "Action to trigger.";
      };

      lua = mkBool false ''
        If true, `action` is considered to be lua code. Thus, it will not be
        wrapped in `""`.
      '';

      desc = mkOption {
        type = nullOr str;
        default = null;
        description = "Description of the key map";
      };

      ft = mkOption {
        type = nullOr (listOf str);
        default = null;
        description = "TBD";
      };

      mode = mkOption {
        type = either str (listOf str);
        example = ["n" "x" "o"];
        description = "Modes to bind in";
      };

      silent = mkBool true "Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
      nowait = mkBool false "Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
      script = mkBool false "Equivalent to adding <script> to a map.";
      expr = mkBool false "Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
      unique = mkBool false "Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
      noremap = mkBool true "Whether to use the 'noremap' variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default.";
    };
  };

  lznEvent = submodule {
    options = {
      event = mkOption {
        type = nullOr (either str (listOf str));
        example = "BufEnter";
        description = "Exact event name";
      };
      pattern = mkOption {
        type = nullOr (either str (listOf str));
        example = "BufEnter *.lua";
        description = "Event pattern";
      };
    };
  };

  lznPluginType = submodule {
    options = {
      package = mkOption {
        type = nullOr pluginType;
        description = ''
          Plugin package.

          If `null`, a custom load function must be provided
        '';
      };

      beforeSetup = mkOption {
        type = nullOr lines;
        default = null;
        description = ''
          Lua code to run after the plugin is loaded, but before the setup
          function is called.
        '';
      };

      setupModule = mkOption {
        type = nullOr str;
        default = null;
        description = "Lua module to run setup function on.";
      };

      setupOpts = mkOption {
        type = either (attrsOf anything) luaInline;
        default = {};
        description = ''
          Options to pass to the setup function.

          Accepts either an attribute set or a raw Lua expression via
          `lib.mkLuaInline`. When set to a `luaInline` value, the expression
          is passed verbatim as the argument to `setup()`.
        '';
      };

      setupCond = mkOption {
        type = nullOr lines;
        default = null;
        description = ''
          A Lua expression used as the condition for calling setup. When set,
          the setup call is wrapped as `if (condition) then ... end`, allowing
          runtime-conditional plugin initialization.

          For example, `"not vim.g.vscode"` will only call setup when not running
          inside VSCode with vscode-neovim.
        '';
      };

      # lz.n options
      enabled = mkOption {
        type = nullOr (either bool luaInline);
        default = null;
        description = ''
          When `false`, or if the Lua function returns false, this plugin will
          not be included in the spec.
        '';
      };

      beforeAll = mkOption {
        type = nullOr lines;
        default = null;
        description = ''
          Lua code to run before any plugins are loaded. This will be wrapped
          in a function.
        '';
      };

      before = mkOption {
        type = nullOr lines;
        default = null;
        description = ''
          Lua code to run before plugin is loaded. This will be wrapped in a
          function.
        '';
      };

      after = mkOption {
        type = nullOr lines;
        default = null;
        description = ''
          Lua code to run after plugin is loaded. This will be wrapped in a function.

          If {option}`vim.lazy.plugins._name_.setupModule` is provided, the setup will be ran before `after`.
        '';
      };

      event = mkOption {
        type = nullOr (oneOf [str lznEvent (listOf (either str lznEvent))]);
        default = null;
        description = "Lazy-load on event";
      };

      cmd = mkOption {
        type = nullOr (either str (listOf str));
        default = null;
        description = "Lazy-load on command";
      };

      ft = mkOption {
        type = nullOr (either str (listOf str));
        default = null;
        description = "Lazy-load on filetype";
      };

      keys = mkOption {
        type = nullOr (oneOf [str (listOf lznKeysSpec) (listOf str)]);
        default = null;
        example = ''
          keys = [
            {
              mode = "n";
              key = "<leader>s";
              action = ":DapStepOver<cr>";
              desc = "DAP Step Over";
            }
            {
              mode = ["n", "x"];
              key = "<leader>dc";
              action = "function() require('dap').continue() end";
              lua = true;
              desc = "DAP Continue";
            }
          ]
        '';
        description = "Lazy-load on key mapping";
      };

      colorscheme = mkOption {
        type = nullOr (either str (listOf str));
        default = null;
        description = "Lazy-load on colorscheme.";
      };

      lazy = mkOption {
        type = nullOr bool;
        default = null;
        description = ''
          Force enable/disable lazy-loading. `null` means only lazy-load if
          a valid lazy-load condition is set e.g. `cmd`, `ft`, `keys` etc.
        '';
      };

      priority = mkOption {
        type = nullOr int;
        default = null;
        description = "Only useful for stat plugins (not lazy-loaded) to force loading certain plugins first.";
      };

      load = mkOption {
        type = nullOr lines;
        default = null;
        description = ''
          Lua code to override the `vim.g.lz_n.load()` function for a single plugin.

          This will be wrapped in a `function(name) ... end`.
        '';
      };
    };
  };
in {
  options.vim.lazy = {
    enable = mkEnableOption "plugin lazy-loading via lz.n and lzn-auto-require" // {default = true;};
    loader = mkOption {
      type = enum ["lz.n"];
      default = "lz.n";
      description = "Lazy loader to use";
    };

    plugins = mkOption {
      type = attrsOf lznPluginType;
      default = {};
      example = ''
        {
          toggleterm-nvim = {
            package = "toggleterm-nvim";
            setupModule = "toggleterm";
            setupOpts = cfg.setupOpts;

            after = "require('toggleterm').do_something()";
            cmd = ["ToggleTerm"];
          };

          $${pkgs.vimPlugins.vim-bbye.pname} = {
            package = pkgs.vimPlugins.vim-bbye;
            cmd = ["Bdelete" "Bwipeout"];
          };
        }
      '';
      description = ''
        Plugins to lazy load.

        The attribute key is used as the plugin name: for the default `vim.g.lz_n.load`
        function this should be either the `package.pname` or `package.name`.
      '';
    };

    enableLznAutoRequire = mkOption {
      type = bool;
      default = true;
      description = ''
        Enable lzn-auto-require. Since builtin plugins rely on this, only turn
        off for debugging.
      '';
    };

    builtLazyConfig = mkOption {
      internal = true;
      type = lines;
      description = ''
        The built config for lz.n, or if `vim.lazy.enable` is false, the
        individual plugin configs.
      '';
    };
  };
}