From cb136a9485ac90f93ffbc84869f2be17f049f399 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Mon, 23 Dec 2024 02:00:47 +0100 Subject: [PATCH] cmp: move source plugin into parent option --- modules/plugins/completion/config.nix | 34 +++++++++ modules/plugins/completion/default.nix | 3 + .../plugins/completion/nvim-cmp/config.nix | 70 ++++++++----------- .../plugins/completion/nvim-cmp/nvim-cmp.nix | 10 +-- modules/plugins/completion/options.nix | 12 +++- 5 files changed, 77 insertions(+), 52 deletions(-) create mode 100644 modules/plugins/completion/config.nix diff --git a/modules/plugins/completion/config.nix b/modules/plugins/completion/config.nix new file mode 100644 index 00000000..809dc3af --- /dev/null +++ b/modules/plugins/completion/config.nix @@ -0,0 +1,34 @@ +{ + lib, + config, + ... +}: let + inherit (builtins) typeOf tryEval; + inherit (lib.modules) mkIf; + inherit (lib.nvim.attrsets) mapListToAttrs; + cfg = config.vim.autocomplete; + + 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.enableSharedCmpSources { + startPlugins = ["rtp-nvim"]; + lazy.plugins = + mapListToAttrs (package: { + name = getPluginName package; + value = { + inherit package; + lazy = true; + after = '' + local path = vim.fn.globpath(vim.o.packpath, 'pack/*/opt/${getPluginName package}') + require("rtp_nvim").source_after_plugin_dir(path) + ''; + }; + }) + cfg.sourcePlugins; + }; +} diff --git a/modules/plugins/completion/default.nix b/modules/plugins/completion/default.nix index 0c0a61a0..2695606c 100644 --- a/modules/plugins/completion/default.nix +++ b/modules/plugins/completion/default.nix @@ -1,5 +1,8 @@ { imports = [ + ./config.nix + ./options.nix + ./nvim-cmp ./blink-cmp ]; diff --git a/modules/plugins/completion/nvim-cmp/config.nix b/modules/plugins/completion/nvim-cmp/config.nix index cfd3e53a..62f6c7d4 100644 --- a/modules/plugins/completion/nvim-cmp/config.nix +++ b/modules/plugins/completion/nvim-cmp/config.nix @@ -3,11 +3,10 @@ config, ... }: let - inherit (lib.modules) mkIf mkMerge; + inherit (lib.modules) mkIf; inherit (lib.strings) optionalString; inherit (lib.generators) mkLuaInline; inherit (lib.nvim.lua) toLuaObject; - inherit (lib.nvim.attrsets) mapListToAttrs; inherit (builtins) attrNames typeOf tryEval concatStringsSep; borders = config.vim.ui.borders.plugins.nvim-cmp; @@ -24,52 +23,39 @@ in { config = mkIf cfg.enable { vim = { - startPlugins = ["rtp-nvim"]; - lazy.plugins = mkMerge [ - (mapListToAttrs (package: { - name = getPluginName package; - value = { - inherit package; - lazy = true; - after = '' - local path = vim.fn.globpath(vim.o.packpath, 'pack/*/opt/${getPluginName package}') - require("rtp_nvim").source_after_plugin_dir(path) - ''; - }; - }) - cfg.sourcePlugins) - { - nvim-cmp = { - package = "nvim-cmp"; - after = '' - ${optionalString luasnipEnable "local luasnip = require('luasnip')"} - local cmp = require("cmp") + autocomplete.enableSharedCmpSources = true; - local kinds = require("cmp.types").lsp.CompletionItemKind - local deprio = function(kind) - return function(e1, e2) - if e1:get_kind() == kind then - return false - end - if e2:get_kind() == kind then - return true - end - return nil + lazy.plugins = { + nvim-cmp = { + package = "nvim-cmp"; + after = '' + ${optionalString luasnipEnable "local luasnip = require('luasnip')"} + local cmp = require("cmp") + + local kinds = require("cmp.types").lsp.CompletionItemKind + local deprio = function(kind) + return function(e1, e2) + if e1:get_kind() == kind then + return false end + if e2:get_kind() == kind then + return true + end + return nil end + end - cmp.setup(${toLuaObject cfg.setupOpts}) + cmp.setup(${toLuaObject cfg.setupOpts}) - ${optionalString config.vim.lazy.enable - (concatStringsSep "\n" (map - (package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})") - cfg.sourcePlugins))} - ''; + ${optionalString config.vim.lazy.enable + (concatStringsSep "\n" (map + (package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})") + cfg.sourcePlugins))} + ''; - event = ["InsertEnter" "CmdlineEnter"]; - }; - } - ]; + event = ["InsertEnter" "CmdlineEnter"]; + }; + }; autocomplete.nvim-cmp = { sources = { diff --git a/modules/plugins/completion/nvim-cmp/nvim-cmp.nix b/modules/plugins/completion/nvim-cmp/nvim-cmp.nix index 0c790455..00e8c056 100644 --- a/modules/plugins/completion/nvim-cmp/nvim-cmp.nix +++ b/modules/plugins/completion/nvim-cmp/nvim-cmp.nix @@ -4,10 +4,10 @@ ... }: let inherit (lib.options) mkEnableOption mkOption literalExpression literalMD; - inherit (lib.types) str attrsOf nullOr either listOf; + inherit (lib.types) str attrsOf nullOr either; inherit (lib.generators) mkLuaInline; inherit (lib.nvim.binds) mkMappingOption; - inherit (lib.nvim.types) mkPluginSetupOption luaInline mergelessListOf pluginType; + inherit (lib.nvim.types) mkPluginSetupOption luaInline mergelessListOf; inherit (lib.nvim.lua) toLuaObject; inherit (builtins) isString; @@ -107,11 +107,5 @@ in { } ''; }; - - sourcePlugins = mkOption { - type = listOf pluginType; - default = []; - description = "List of source plugins used by nvim-cmp."; - }; }; } diff --git a/modules/plugins/completion/options.nix b/modules/plugins/completion/options.nix index b919b8ae..4066ddf9 100644 --- a/modules/plugins/completion/options.nix +++ b/modules/plugins/completion/options.nix @@ -1,7 +1,15 @@ {lib, ...}: let - inherit (lib.options) mkEnableOption; + inherit (lib.options) mkEnableOption mkOption; + inherit (lib.types) listOf; + inherit (lib.nvim.types) pluginType; in { options.vim.autocomplete = { - enableSharedCmpSources = mkEnableOption "cmp sources that can work in nvim-cmp and blink.cmp"; + enableSharedCmpSources = mkEnableOption "cmp sources shared by nvim-cmp and blink.cmp"; + + cmpSourcePlugins = mkOption { + type = listOf pluginType; + default = []; + description = "List of cmp source plugins."; + }; }; }