From 0d0c885d7051f0e9ca4538dcf3153440a745c597 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Mon, 23 Dec 2024 02:48:03 +0100 Subject: [PATCH] completion: migrate to shared cmp source options --- modules/plugins/assistant/copilot/config.nix | 4 +- .../plugins/completion/nvim-cmp/config.nix | 114 +++++++++--------- modules/plugins/completion/options.nix | 2 +- modules/plugins/lsp/config.nix | 9 +- modules/plugins/lsp/lspkind/config.nix | 3 +- modules/plugins/notes/obsidian/obsidian.nix | 2 +- modules/plugins/snippets/luasnip/config.nix | 4 +- modules/plugins/treesitter/config.nix | 4 +- 8 files changed, 75 insertions(+), 67 deletions(-) diff --git a/modules/plugins/assistant/copilot/config.nix b/modules/plugins/assistant/copilot/config.nix index 37da046f..1a070fe3 100644 --- a/modules/plugins/assistant/copilot/config.nix +++ b/modules/plugins/assistant/copilot/config.nix @@ -58,8 +58,8 @@ in { }; }; - autocomplete.nvim-cmp = { - sources = {copilot = "[Copilot]";}; + autocomplete = { + nvim-cmp.sources = {copilot = "[Copilot]";}; sourcePlugins = ["copilot-cmp"]; }; diff --git a/modules/plugins/completion/nvim-cmp/config.nix b/modules/plugins/completion/nvim-cmp/config.nix index 62f6c7d4..7745cb21 100644 --- a/modules/plugins/completion/nvim-cmp/config.nix +++ b/modules/plugins/completion/nvim-cmp/config.nix @@ -12,6 +12,7 @@ borders = config.vim.ui.borders.plugins.nvim-cmp; cfg = config.vim.autocomplete.nvim-cmp; + autocompleteCfg = config.vim.autocomplete; luasnipEnable = config.vim.snippets.luasnip.enable; getPluginName = plugin: if typeOf plugin == "string" @@ -23,8 +24,6 @@ in { config = mkIf cfg.enable { vim = { - autocomplete.enableSharedCmpSources = true; - lazy.plugins = { nvim-cmp = { package = "nvim-cmp"; @@ -50,74 +49,77 @@ in { ${optionalString config.vim.lazy.enable (concatStringsSep "\n" (map (package: "require('lz.n').trigger_load(${toLuaObject (getPluginName package)})") - cfg.sourcePlugins))} + autocompleteCfg.sourcePlugins))} ''; event = ["InsertEnter" "CmdlineEnter"]; }; }; - autocomplete.nvim-cmp = { - sources = { - nvim-cmp = null; - buffer = "[Buffer]"; - path = "[Path]"; - }; - + autocomplete = { + enableSharedCmpSources = true; sourcePlugins = ["cmp-buffer" "cmp-path"]; - setupOpts = { - sources = map (s: {name = s;}) (attrNames cfg.sources); - - window = mkIf borders.enable { - completion.border = borders.style; - documentation.border = borders.style; + nvim-cmp = { + sources = { + nvim-cmp = null; + buffer = "[Buffer]"; + path = "[Path]"; }; - formatting.format = cfg.format; + setupOpts = { + sources = map (s: {name = s;}) (attrNames cfg.sources); - # `cmp` and `luasnip` are defined above, in the `nvim-cmp` section - 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 })"; + window = mkIf borders.enable { + completion.border = borders.style; + documentation.border = borders.style; + }; - ${mappings.next} = mkLuaInline '' - cmp.mapping(function(fallback) - local has_words_before = function() - local line, col = unpack(vim.api.nvim_win_get_cursor(0)) - return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil - end + formatting.format = cfg.format; - if cmp.visible() then - cmp.select_next_item() - ${optionalString luasnipEnable '' - elseif luasnip.locally_jumpable(1) then - luasnip.jump(1) - ''} - elseif has_words_before() then - cmp.complete() - else - fallback() - end - end) - ''; + # `cmp` and `luasnip` are defined above, in the `nvim-cmp` section + 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.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) - ''; + ${mappings.next} = mkLuaInline '' + cmp.mapping(function(fallback) + local has_words_before = function() + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil + end + + if cmp.visible() then + cmp.select_next_item() + ${optionalString luasnipEnable '' + elseif luasnip.locally_jumpable(1) then + luasnip.jump(1) + ''} + elseif has_words_before() then + cmp.complete() + else + fallback() + 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) + ''; + }; }; }; }; diff --git a/modules/plugins/completion/options.nix b/modules/plugins/completion/options.nix index 4066ddf9..330fbaf4 100644 --- a/modules/plugins/completion/options.nix +++ b/modules/plugins/completion/options.nix @@ -6,7 +6,7 @@ in { options.vim.autocomplete = { enableSharedCmpSources = mkEnableOption "cmp sources shared by nvim-cmp and blink.cmp"; - cmpSourcePlugins = mkOption { + sourcePlugins = mkOption { type = listOf pluginType; default = []; description = "List of cmp source plugins."; diff --git a/modules/plugins/lsp/config.nix b/modules/plugins/lsp/config.nix index e73a5387..e0a6660f 100644 --- a/modules/plugins/lsp/config.nix +++ b/modules/plugins/lsp/config.nix @@ -11,6 +11,7 @@ cfg = config.vim.lsp; usingNvimCmp = config.vim.autocomplete.nvim-cmp.enable; + usingBlinkCmp = config.vim.autocomplete.blink-cmp.enable; self = import ./module.nix {inherit config lib pkgs;}; mappingDefinitions = self.options.vim.lsp.mappings; @@ -22,8 +23,8 @@ in { config = mkIf cfg.enable { vim = { - autocomplete.nvim-cmp = { - sources = {nvim_lsp = "[LSP]";}; + autocomplete = mkIf usingNvimCmp { + nvim-cmp.sources = {nvim_lsp = "[LSP]";}; sourcePlugins = ["cmp-nvim-lsp"]; }; @@ -170,6 +171,10 @@ in { }, } ''} + + ${optionalString usingBlinkCmp '' + -- TODO + ''} ''; }; }; diff --git a/modules/plugins/lsp/lspkind/config.nix b/modules/plugins/lsp/lspkind/config.nix index 2ef20ff8..bad67125 100644 --- a/modules/plugins/lsp/lspkind/config.nix +++ b/modules/plugins/lsp/lspkind/config.nix @@ -8,11 +8,12 @@ inherit (lib.nvim.lua) toLuaObject; cfg = config.vim.lsp.lspkind; + inherit (config.vim) autocomplete; in { config = mkIf cfg.enable { assertions = [ { - assertion = config.vim.autocomplete.nvim-cmp.enable; + assertion = autocomplete.nvim-cmp.enable || autocomplete.blink-cmp.enable; message = '' While lspkind supports Neovim's native lsp upstream, using that over nvim-cmp isn't recommended, nor supported by nvf. diff --git a/modules/plugins/notes/obsidian/obsidian.nix b/modules/plugins/notes/obsidian/obsidian.nix index 2dae5a9c..ca61583f 100644 --- a/modules/plugins/notes/obsidian/obsidian.nix +++ b/modules/plugins/notes/obsidian/obsidian.nix @@ -48,7 +48,7 @@ in { # If using nvim-cmp, otherwise set to false type = bool; description = "If using nvim-cmp, otherwise set to false"; - default = config.vim.autocomplete.nvim-cmp.enable; + default = config.vim.autocomplete.enableSharedCmpSources; }; }; }; diff --git a/modules/plugins/snippets/luasnip/config.nix b/modules/plugins/snippets/luasnip/config.nix index 927b21fd..2e5df6f5 100644 --- a/modules/plugins/snippets/luasnip/config.nix +++ b/modules/plugins/snippets/luasnip/config.nix @@ -17,8 +17,8 @@ in { }; }; startPlugins = cfg.providers; - autocomplete.nvim-cmp = { - sources = {luasnip = "[LuaSnip]";}; + autocomplete = { + nvim-cmp.sources = {luasnip = "[LuaSnip]";}; sourcePlugins = ["cmp-luasnip"]; }; }; diff --git a/modules/plugins/treesitter/config.nix b/modules/plugins/treesitter/config.nix index 566a716e..b7d8c177 100644 --- a/modules/plugins/treesitter/config.nix +++ b/modules/plugins/treesitter/config.nix @@ -20,8 +20,8 @@ in { vim = { startPlugins = ["nvim-treesitter"]; - autocomplete.nvim-cmp = { - sources = {treesitter = "[Treesitter]";}; + autocomplete = { + nvim-cmp.sources = {treesitter = "[Treesitter]";}; sourcePlugins = ["cmp-treesitter"]; };