completion/blink-cmp: blink sources options

Use a submodule to allow arbitrary source additions.
Automatically load sources that are enabled.
Automatically add enabled sources via blink-cmp setupOpts.
Prepopulate with a few basic sources, disabled by default.
This commit is contained in:
alfarel 2025-02-25 22:33:11 -05:00 committed by raf
commit 9e35fd8d02
4 changed files with 136 additions and 18 deletions

View file

@ -2,7 +2,7 @@
inherit (lib.options) mkEnableOption mkOption literalMD;
inherit (lib.types) listOf str either attrsOf submodule enum anything int nullOr;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.types) mkPluginSetupOption luaInline;
inherit (lib.nvim.types) mkPluginSetupOption luaInline pluginType;
inherit (lib.nvim.binds) mkMappingOption;
inherit (lib.nvim.config) mkBool;
@ -118,5 +118,64 @@ in {
scrollDocsUp = mkMappingOption "Scroll docs up [blink.cmp]" "<C-d>";
scrollDocsDown = mkMappingOption "Scroll docs down [blink.cmp]" "<C-f>";
};
sourcePlugins = let
sourcePluginType = submodule {
options = {
package = mkOption {
type = pluginType;
description = ''
`blink-cmp` source plugin package.
'';
};
module = mkOption {
type = str;
description = ''
Value of {option}`vim.autocomplete.blink-cmp.setupOpts.sources.providers.<name>.module`.
Should be present in the source's documentation.
'';
};
enable = mkEnableOption "this source";
};
};
in
mkOption {
type = submodule {
freeformType = attrsOf sourcePluginType;
options = let
defaultSourcePluginOption = name: package: module: {
package = mkOption {
type = pluginType;
default = package;
description = ''
`blink-cmp` ${name} source plugin package.
'';
};
module = mkOption {
type = str;
default = module;
description = ''
Value of {option}`vim.autocomplete.blink-cmp.setupOpts.sources.providers.${name}.module`.
'';
};
enable = mkEnableOption "${name} source";
};
in {
# emoji completion after :
emoji = defaultSourcePluginOption "emoji" "blink-emoji-nvim" "blink-emoji";
# spelling suggestions as completions
spell = defaultSourcePluginOption "spell" "blink-cmp-spell" "blink-cmp-spell";
# words from nearby files
ripgrep = defaultSourcePluginOption "ripgrep" "blink-ripgrep-nvim" "blink-ripgrep";
};
};
default = {};
description = ''
`blink.cmp` sources.
Attribute names must be source names used in {option}`vim.autocomplete.blink-cmp.setupOpts.sources.default`.
'';
};
};
}

View file

@ -6,6 +6,8 @@
inherit (lib.modules) mkIf;
inherit (lib.strings) optionalString;
inherit (lib.generators) mkLuaInline;
inherit (lib.attrsets) attrValues filterAttrs;
inherit (lib.lists) map;
inherit (lib.nvim.lua) toLuaObject;
inherit (builtins) concatStringsSep typeOf tryEval attrNames mapAttrs;
@ -19,9 +21,12 @@
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);
in {
vim = mkIf cfg.enable {
startPlugins = ["blink-compat"];
startPlugins = ["blink-compat"] ++ blinkSourcePlugins;
lazy.plugins = {
blink-cmp = {
package = "blink-cmp";
@ -32,12 +37,14 @@ in {
#
# event = ["InsertEnter" "CmdlineEnter"];
after = ''
${optionalString config.vim.lazy.enable
(concatStringsSep "\n" (map
(package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})")
cmpCfg.sourcePlugins))}
'';
after =
# lua
''
${optionalString config.vim.lazy.enable
(concatStringsSep "\n" (map
(package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})")
cmpCfg.sourcePlugins))}
'';
};
};
@ -45,13 +52,26 @@ in {
enableSharedCmpSources = true;
blink-cmp.setupOpts = {
sources = {
default = ["lsp" "path" "snippets" "buffer"] ++ (attrNames cmpCfg.sources);
default =
[
"lsp"
"path"
"snippets"
"buffer"
]
++ (attrNames cmpCfg.sources)
++ (attrNames enabledBlinkSources);
providers =
mapAttrs (name: _: {
inherit name;
module = "blink.compat.source";
})
cmpCfg.sources;
cmpCfg.sources
// (mapAttrs (name: definition: {
inherit name;
inherit (definition) module;
})
enabledBlinkSources);
};
snippets = mkIf config.vim.snippets.luasnip.enable {
preset = "luasnip";
@ -67,16 +87,18 @@ in {
${mappings.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
(mkLuaInline
# lua
''
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()
if has_words_before then
return cmp.show()
end
end
end
'')
'')
"fallback"
];
${mappings.previous} = [