cmp: lazy load

this moves cmp itself to lazy.plugins but other plugins that call cmp
are not yet lazy so cmp is technically not yet lazy
This commit is contained in:
Ching Pei Yang 2024-10-21 18:25:42 +02:00
parent b636b22046
commit 46a1f0427f
No known key found for this signature in database
GPG key ID: 062FBBCE1D0C5DD9

View file

@ -6,7 +6,6 @@
inherit (lib.modules) mkIf; inherit (lib.modules) mkIf;
inherit (lib.strings) optionalString; inherit (lib.strings) optionalString;
inherit (lib.generators) mkLuaInline; inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.dag) entryAfter;
inherit (lib.nvim.lua) toLuaObject; inherit (lib.nvim.lua) toLuaObject;
inherit (builtins) attrNames; inherit (builtins) attrNames;
@ -16,78 +15,92 @@
in { in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
vim = { vim = {
startPlugins = [ lazy.plugins = {
"nvim-cmp" # cmp sources are loaded via lzn-auto-require as long as it is defined
"cmp-buffer" # in cmp sources
"cmp-path" cmp-buffer = {
]; package = "cmp-buffer";
lazy = true;
};
cmp-path = {
package = "cmp-path";
lazy = true;
};
nvim-cmp = {
package = "nvim-cmp";
after = ''
${optionalString luasnipEnable "local luasnip = require('luasnip')"}
local cmp = require("cmp")
cmp.setup(${toLuaObject cfg.setupOpts})
'';
autocomplete.nvim-cmp.sources = { event = ["InsertEnter" "CmdlineEnter"];
nvim-cmp = null; lazy = true;
buffer = "[Buffer]"; };
path = "[Path]";
}; };
autocomplete.nvim-cmp.setupOpts = { autocomplete.nvim-cmp = {
sources = map (s: {name = s;}) (attrNames cfg.sources); sources = {
nvim-cmp = null;
# TODO: try to get nvim-cmp to follow global border style buffer = "[Buffer]";
window = mkIf config.vim.ui.borders.enable { path = "[Path]";
completion = mkLuaInline "cmp.config.window.bordered()";
documentation = mkLuaInline "cmp.config.window.bordered()";
}; };
formatting.format = cfg.format; setupOpts = {
}; sources = map (s: {name = s;}) (attrNames cfg.sources);
pluginRC.nvim-cmp = mkIf cfg.enable (entryAfter ["autopairs" "luasnip"] '' # TODO: try to get nvim-cmp to follow global border style
${optionalString luasnipEnable "local luasnip = require('luasnip')"} window = mkIf config.vim.ui.borders.enable {
local cmp = require("cmp") completion = mkLuaInline "cmp.config.window.bordered()";
cmp.setup(${toLuaObject cfg.setupOpts}) documentation = mkLuaInline "cmp.config.window.bordered()";
''); };
# `cmp` and `luasnip` are defined above, in the `nvim-cmp` section formatting.format = cfg.format;
autocomplete.nvim-cmp.setupOpts.mapping = { };
${mappings.complete} = mkLuaInline "cmp.mapping.complete()";
${mappings.close} = mkLuaInline "cmp.mapping.abort()";
${mappings.scrollDocsUp} = mkLuaInline "cmp.mapping.scroll_docs(-4)";
${mappings.scrollDocsDown} = mkLuaInline "cmp.mapping.scroll_docs(4)";
${mappings.confirm} = mkLuaInline "cmp.mapping.confirm({ select = true })";
${mappings.next} = mkLuaInline '' # `cmp` and `luasnip` are defined above, in the `nvim-cmp` section
cmp.mapping(function(fallback) setupOpts.mapping = {
local has_words_before = function() ${mappings.complete} = mkLuaInline "cmp.mapping.complete()";
local line, col = unpack(vim.api.nvim_win_get_cursor(0)) ${mappings.close} = mkLuaInline "cmp.mapping.abort()";
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil ${mappings.scrollDocsUp} = mkLuaInline "cmp.mapping.scroll_docs(-4)";
end ${mappings.scrollDocsDown} = mkLuaInline "cmp.mapping.scroll_docs(4)";
${mappings.confirm} = mkLuaInline "cmp.mapping.confirm({ select = true })";
if cmp.visible() then ${mappings.next} = mkLuaInline ''
cmp.select_next_item() cmp.mapping(function(fallback)
${optionalString luasnipEnable '' local has_words_before = function()
elseif luasnip.locally_jumpable(1) then local line, col = unpack(vim.api.nvim_win_get_cursor(0))
luasnip.jump(1) return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
''} end
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end)
'';
${mappings.previous} = mkLuaInline '' if cmp.visible() then
cmp.mapping(function(fallback) cmp.select_next_item()
if cmp.visible() then ${optionalString luasnipEnable ''
cmp.select_prev_item() elseif luasnip.locally_jumpable(1) then
${optionalString luasnipEnable '' luasnip.jump(1)
elseif luasnip.locally_jumpable(-1) then ''}
luasnip.jump(-1) elseif has_words_before() then
''} cmp.complete()
else else
fallback() fallback()
end end
end) end)
''; '';
${mappings.previous} = mkLuaInline ''
cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
${optionalString luasnipEnable ''
elseif luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
''}
else
fallback()
end
end)
'';
};
}; };
}; };
}; };