Skip to content

Commit 75c0bd1

Browse files
committed
lib/plugins/mkVimPlugin: Support lazyloading
Signed-off-by: saygo-png <[email protected]>
1 parent faf1fb4 commit 75c0bd1

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

lib/plugins/mk-vim-plugin.nix

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,86 @@ let
5959
options = lib.setAttrByPath loc (
6060
{
6161
enable = lib.mkEnableOption name;
62+
lazyLoad = lib.nixvim.mkLazyLoadOption name;
6263
autoLoad = lib.nixvim.mkAutoLoadOption cfg name;
6364
}
6465
// settingsOption
6566
// extraOptions
6667
);
6768

6869
config = lib.mkIf cfg.enable (
70+
let
71+
globalsConfig = lib.nixvim.applyPrefixToAttrs globalPrefix (cfg.settings or { });
72+
in
6973
lib.mkMerge [
70-
{
71-
inherit extraPackages extraPlugins;
72-
globals = lib.nixvim.applyPrefixToAttrs globalPrefix (cfg.settings or { });
73-
}
74+
{ inherit extraPackages extraPlugins; }
75+
76+
(lib.mkIf cfg.lazyLoad.enable {
77+
assertions = [
78+
{
79+
assertion = (isColorscheme && colorscheme != null) || cfg.lazyLoad.settings != { };
80+
message = "You have enabled lazy loading for ${name} but have not provided any configuration.";
81+
}
82+
];
83+
84+
plugins.lz-n = {
85+
plugins = [
86+
(
87+
{
88+
# The packpath name is always the derivation name
89+
__unkeyed-1 = lib.getName cfg.package;
90+
# Use provided before, otherwise fallback to normal function wrapped globals config
91+
before =
92+
let
93+
before = cfg.lazyLoad.settings.before or null;
94+
default =
95+
lib.mkIf
96+
(
97+
(lib.filterAttrs (
98+
_: v:
99+
!builtins.elem v [
100+
[ ]
101+
null
102+
{ }
103+
]
104+
) globalsConfig) != { }
105+
)
106+
''
107+
function()
108+
local globals = ${lib.nixvim.toLuaObject globalsConfig}
109+
110+
for k,v in pairs(globals) do
111+
vim.g[k] = v
112+
end
113+
end
114+
'';
115+
in
116+
if (lib.isString before || lib.types.rawLua.check before) then before else default;
117+
colorscheme = lib.mkIf isColorscheme (cfg.lazyLoad.settings.colorscheme or colorscheme);
118+
}
119+
// lib.removeAttrs cfg.lazyLoad.settings [
120+
"before"
121+
"colorscheme"
122+
]
123+
)
124+
];
125+
};
126+
})
127+
128+
(lib.mkIf (!cfg.lazyLoad.enable) {
129+
globals = globalsConfig;
130+
})
131+
74132
(lib.optionalAttrs (isColorscheme && colorscheme != null) {
75133
colorscheme = lib.mkDefault colorscheme;
76134
})
135+
77136
(lib.optionalAttrs (args ? extraConfig) (
78137
lib.nixvim.plugins.utils.applyExtraConfig {
79138
inherit extraConfig cfg opts;
80139
}
81140
))
141+
82142
(lib.nixvim.plugins.utils.enableDependencies dependencies)
83143
]
84144
);

0 commit comments

Comments
 (0)