From 9e35fd8d02c745291c0a0504baae53bcad5c8637 Mon Sep 17 00:00:00 2001 From: alfarel Date: Tue, 25 Feb 2025 22:33:11 -0500 Subject: [PATCH] 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. --- docs/release-notes/rl-0.8.md | 1 + .../completion/blink-cmp/blink-cmp.nix | 61 ++++++++++++++++++- .../plugins/completion/blink-cmp/config.nix | 56 +++++++++++------ npins/sources.json | 36 +++++++++++ 4 files changed, 136 insertions(+), 18 deletions(-) diff --git a/docs/release-notes/rl-0.8.md b/docs/release-notes/rl-0.8.md index 5cf939be..a28ffd67 100644 --- a/docs/release-notes/rl-0.8.md +++ b/docs/release-notes/rl-0.8.md @@ -206,6 +206,7 @@ for in-neovim `nix develop`, `nix shell` and more. - Add [direnv.vim](https://github.com/direnv/direnv.vim) plugin for automatic syncing of nvim shell environment with direnv's. +- Add [blink.cmp] source options and some default-disabled sources. [TheColorman](https://github.com/TheColorman) diff --git a/modules/plugins/completion/blink-cmp/blink-cmp.nix b/modules/plugins/completion/blink-cmp/blink-cmp.nix index 13cdb9f4..9bb76b71 100644 --- a/modules/plugins/completion/blink-cmp/blink-cmp.nix +++ b/modules/plugins/completion/blink-cmp/blink-cmp.nix @@ -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]" ""; scrollDocsDown = mkMappingOption "Scroll docs down [blink.cmp]" ""; }; + + 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..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`. + ''; + }; }; } diff --git a/modules/plugins/completion/blink-cmp/config.nix b/modules/plugins/completion/blink-cmp/config.nix index 914821f9..96ced502 100644 --- a/modules/plugins/completion/blink-cmp/config.nix +++ b/modules/plugins/completion/blink-cmp/config.nix @@ -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} = [ diff --git a/npins/sources.json b/npins/sources.json index b69bf509..a68a1ea4 100644 --- a/npins/sources.json +++ b/npins/sources.json @@ -51,6 +51,18 @@ "url": "https://api.github.com/repos/saghen/blink.cmp/tarball/v0.12.4", "hash": "0jdifjifxjqa8r80wlqgkn5rm48wziap92340xz228nrgd0c9g69" }, + "blink-cmp-spell": { + "type": "Git", + "repository": { + "type": "GitHub", + "owner": "ribru17", + "repo": "blink-cmp-spell" + }, + "branch": "master", + "revision": "38d6797dea6f72baa6e8b3bfca6da96d8fcac64d", + "url": "https://github.com/ribru17/blink-cmp-spell/archive/38d6797dea6f72baa6e8b3bfca6da96d8fcac64d.tar.gz", + "hash": "19pnasa446iiapgsr3z2fpk0nnrzh8g5wrzrq8n0y4q0z6spc9f6" + }, "blink-compat": { "type": "Git", "repository": { @@ -63,6 +75,30 @@ "url": "https://github.com/saghen/blink.compat/archive/4104671562c663d059d91a99da3780bead5bc467.tar.gz", "hash": "0bsf8kg5s3m1xk9d4n0yl0h5xyk484hip3z8va547f6ibim9ccv4" }, + "blink-emoji-nvim": { + "type": "Git", + "repository": { + "type": "GitHub", + "owner": "moyiz", + "repo": "blink-emoji.nvim" + }, + "branch": "master", + "revision": "a77aebc092ebece1eed108f301452ae774d6b67a", + "url": "https://github.com/moyiz/blink-emoji.nvim/archive/a77aebc092ebece1eed108f301452ae774d6b67a.tar.gz", + "hash": "0n4qv2mk7zx910gnwf9ri2w5qxwx8szx99nqqzik4yyvl4axm41d" + }, + "blink-ripgrep-nvim": { + "type": "Git", + "repository": { + "type": "GitHub", + "owner": "mikavilpas", + "repo": "blink-ripgrep.nvim" + }, + "branch": "main", + "revision": "305e1ae5363f527abdfd71915a3fe1f42af52824", + "url": "https://github.com/mikavilpas/blink-ripgrep.nvim/archive/305e1ae5363f527abdfd71915a3fe1f42af52824.tar.gz", + "hash": "1hcfyicgf33dlr2hhgnhhzdcxxqw1v8v1yjfbnwvlcsgw0rhjl8w" + }, "bufdelete-nvim": { "type": "Git", "repository": {