From 5af402af988bc5541b697601fc898b9604081667 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Mon, 23 Dec 2024 20:27:03 +0100 Subject: [PATCH] blink: add blink.compat support --- .../completion/blink-cmp/blink-cmp.nix | 18 ++++++- .../plugins/completion/blink-cmp/config.nix | 47 +++++++++++++++---- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/modules/plugins/completion/blink-cmp/blink-cmp.nix b/modules/plugins/completion/blink-cmp/blink-cmp.nix index a55dd43d..7ca36f57 100644 --- a/modules/plugins/completion/blink-cmp/blink-cmp.nix +++ b/modules/plugins/completion/blink-cmp/blink-cmp.nix @@ -1,6 +1,6 @@ {lib, ...}: let inherit (lib.options) mkEnableOption mkOption literalMD; - inherit (lib.types) listOf str either attrsOf submodule enum; + inherit (lib.types) listOf str either attrsOf submodule enum anything; inherit (lib.nvim.types) mkPluginSetupOption luaInline; inherit (lib.nvim.binds) mkMappingOption; @@ -14,6 +14,16 @@ }; }; }; + + providerType = submodule { + freeformType = anything; + options = { + module = mkOption { + type = str; + description = "module of the provider"; + }; + }; + }; in { options.vim.autocomplete.blink-cmp = { enable = mkEnableOption "blink.cmp"; @@ -24,6 +34,12 @@ in { description = "Default list of sources to enable for completion."; default = ["lsp" "path" "snippets" "buffer"]; }; + + providers = mkOption { + type = attrsOf providerType; + description = "Providers"; + default = {}; + }; }; keymap = mkOption { diff --git a/modules/plugins/completion/blink-cmp/config.nix b/modules/plugins/completion/blink-cmp/config.nix index 50fe4563..295fbde2 100644 --- a/modules/plugins/completion/blink-cmp/config.nix +++ b/modules/plugins/completion/blink-cmp/config.nix @@ -3,26 +3,57 @@ config, ... }: let - inherit (lib.modules) mkIf; + inherit (lib.modules) mkIf mkDefault; + inherit (lib.strings) optionalString; inherit (lib.generators) mkLuaInline; + inherit (lib.nvim.lua) toLuaObject; + inherit (builtins) concatStringsSep typeOf tryEval attrNames mapAttrs; cfg = config.vim.autocomplete.blink-cmp; + autocompleteCfg = config.vim.autocomplete; inherit (cfg) mappings; + + getPluginName = plugin: + if typeOf plugin == "string" + then plugin + else if (plugin ? pname && (tryEval plugin.pname).success) + then plugin.pname + else plugin.name; in { vim = mkIf cfg.enable { - lazy.plugins.blink-cmp = { - package = "blink-cmp"; - setupModule = "blink.cmp"; - inherit (cfg) setupOpts; - # TODO: lazy disabled until lspconfig is lazy loaded - # - # event = ["InsertEnter" "CmdlineEnter"]; + startPlugins = ["blink-compat"]; + lazy.plugins = { + blink-cmp = { + package = "blink-cmp"; + setupModule = "blink.cmp"; + inherit (cfg) setupOpts; + + # TODO: lazy disabled until lspconfig is lazy loaded + # + # event = ["InsertEnter" "CmdlineEnter"]; + + after = '' + ${optionalString config.vim.lazy.enable + (concatStringsSep "\n" (map + (package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})") + autocompleteCfg.sourcePlugins))} + ''; + }; }; autocomplete = { enableSharedCmpSources = true; blink-cmp.setupOpts = { + sources = { + default = mkDefault (attrNames autocompleteCfg.nvim-cmp.sources); + providers = + mapAttrs (name: _: { + inherit name; + module = "blink.compat.source"; + }) + autocompleteCfg.nvim-cmp.sources; + }; snippets = mkIf config.vim.snippets.luasnip.enable { expand = mkLuaInline '' function(snippet)