summaryrefslogtreecommitdiffstats
path: root/modules/wrapper/rc/config.nix
blob: de14c08420e35f2f95d395d145c5bca57dd860b3 (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
{
  config,
  lib,
  ...
}: let
  inherit (builtins) map mapAttrs filter;
  inherit (lib.lists) partition;
  inherit (lib.attrsets) mapAttrsToList;
  inherit (lib.strings) concatLines concatMapStringsSep optionalString;
  inherit (lib.trivial) showWarnings;
  inherit (lib.generators) mkLuaInline;
  inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere;
  inherit (lib.nvim.lua) toLuaObject;

  cfg = config.vim;
in {
  config = let
    globalsScript =
      mapAttrsToList (name: value: "vim.g.${name} = ${toLuaObject value}") cfg.globals;

    optionsScript =
      mapAttrsToList (name: value: "vim.opt.${name} = ${toLuaObject value}") cfg.options;

    extraPluginConfigs = resolveDag {
      name = "extra plugin configs";
      dag = mapAttrs (_: value: entryAfter value.after value.setup) cfg.extraPlugins;
      mapResult = result: concatLines (map mkLuarcSection result);
    };

    pluginConfigs = resolveDag {
      name = "plugin configs";
      dag = cfg.pluginRC;
      mapResult = result: concatLines (map mkLuarcSection result);
    };

    getAction = keymap:
      if keymap.lua
      then mkLuaInline keymap.action
      else keymap.action;

    getOpts = keymap: {
      inherit (keymap) desc silent nowait script expr unique noremap;
      remap = !keymap.noremap;
    };

    toLuaKeymap = bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject bind.key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})";

    keymaps = concatLines (map toLuaKeymap (filter (x: x.key != null) cfg.keymaps));
  in {
    vim = {
      luaConfigRC = {
        # `vim.g` and `vim.o`
        globalsScript = entryAnywhere (concatLines globalsScript);
        optionsScript = entryAfter ["basic"] (concatLines optionsScript);

        # Basic
        lazyConfigs = entryAfter ["optionsScript"] cfg.lazy.builtLazyConfig;
        pluginConfigs = entryAfter ["lazyConfigs"] pluginConfigs;
        extraPluginConfigs = entryAfter ["pluginConfigs"] extraPluginConfigs;
        mappings = entryAfter ["extraPluginConfigs"] keymaps;
      };

      builtLuaConfigRC = let
        # Catch assertions and warnings
        # and throw for each failed assertion. If no assertions are found, show warnings.
        failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions);
        baseSystemAssertWarn =
          if failedAssertions != []
          then throw "\nFailed assertions:\n${concatMapStringsSep "\n" (x: "- ${x}") failedAssertions}"
          else showWarnings config.warnings;

        luaConfig = resolveDag {
          name = "lua config script";
          dag = cfg.luaConfigRC;
          mapResult = result:
            concatLines [
              (optionalString (cfg.additionalRuntimePaths != []) (let
                grouped = partition (p: p.position == "prepend") cfg.additionalRuntimePaths;
                prependPaths = map (p: p.path) grouped.right;
                appendPaths = map (p: p.path) grouped.wrong;
              in
                concatLines [
                  (optionalString (prependPaths != []) ''
                    vim.opt.runtimepath:prepend(${toLuaObject prependPaths})
                  '')
                  (optionalString (appendPaths != []) ''
                    vim.opt.runtimepath:append(${toLuaObject appendPaths})
                  '')
                ]))
              (optionalString cfg.enableLuaLoader ''
                if vim.loader then
                  vim.loader.enable()
                end
              '')
              cfg.luaConfigPre
              (concatMapStringsSep "\n" mkLuarcSection result)
              cfg.luaConfigPost
            ];
        };
      in
        baseSystemAssertWarn luaConfig;
    };
  };
}