diff --git a/lua/lazyvim/config/options.lua b/lua/lazyvim/config/options.lua index bc504675..47ced41a 100644 --- a/lua/lazyvim/config/options.lua +++ b/lua/lazyvim/config/options.lua @@ -11,6 +11,12 @@ vim.g.autoformat = true -- enabled with `:LazyExtras` vim.g.lazyvim_picker = "auto" +-- LazyVim completion engine to use. +-- Can be one of: nvim-cmp, blink.cmp +-- Leave it to "auto" to automatically use the completion engine +-- enabled with `:LazyExtras` +vim.g.lazyvim_cmp = "auto" + -- if the completion engine supports the AI source, -- use that instead of inline suggestions vim.g.ai_cmp = true diff --git a/lua/lazyvim/plugins/coding.lua b/lua/lazyvim/plugins/coding.lua index dc470791..adc76177 100644 --- a/lua/lazyvim/plugins/coding.lua +++ b/lua/lazyvim/plugins/coding.lua @@ -1,113 +1,14 @@ return { - - -- auto completion { - "hrsh7th/nvim-cmp", - version = false, -- last release is way too old - event = "InsertEnter", - dependencies = { - "hrsh7th/cmp-nvim-lsp", - "hrsh7th/cmp-buffer", - "hrsh7th/cmp-path", - }, - -- Not all LSP servers add brackets when completing a function. - -- To better deal with this, LazyVim adds a custom option to cmp, - -- that you can configure. For example: - -- - -- ```lua - -- opts = { - -- auto_brackets = { "python" } - -- } - -- ``` - opts = function() - vim.api.nvim_set_hl(0, "CmpGhostText", { link = "Comment", default = true }) - local cmp = require("cmp") - local defaults = require("cmp.config.default")() - local auto_select = true - return { - auto_brackets = {}, -- configure any filetype to auto add brackets - completion = { - completeopt = "menu,menuone,noinsert" .. (auto_select and "" or ",noselect"), - }, - preselect = auto_select and cmp.PreselectMode.Item or cmp.PreselectMode.None, - mapping = cmp.mapping.preset.insert({ - [""] = cmp.mapping.scroll_docs(-4), - [""] = cmp.mapping.scroll_docs(4), - [""] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }), - [""] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }), - [""] = cmp.mapping.complete(), - [""] = LazyVim.cmp.confirm({ select = auto_select }), - [""] = LazyVim.cmp.confirm({ select = true }), - [""] = LazyVim.cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. - [""] = function(fallback) - cmp.abort() - fallback() - end, - [""] = function(fallback) - return LazyVim.cmp.map({ "snippet_forward", "ai_accept" }, fallback)() - end, - }), - sources = cmp.config.sources({ - { name = "nvim_lsp" }, - { name = "path" }, - }, { - { name = "buffer" }, - }), - formatting = { - format = function(entry, item) - local icons = LazyVim.config.icons.kinds - if icons[item.kind] then - item.kind = icons[item.kind] .. item.kind - end - - local widths = { - abbr = vim.g.cmp_widths and vim.g.cmp_widths.abbr or 40, - menu = vim.g.cmp_widths and vim.g.cmp_widths.menu or 30, - } - - for key, width in pairs(widths) do - if item[key] and vim.fn.strdisplaywidth(item[key]) > width then - item[key] = vim.fn.strcharpart(item[key], 0, width - 1) .. "…" - end - end - - return item - end, - }, - experimental = { - -- only show ghost text when we show ai completions - ghost_text = vim.g.ai_cmp and { - hl_group = "CmpGhostText", - } or false, - }, - sorting = defaults.sorting, - } + import = "lazyvim.plugins.extras.coding.nvim-cmp", + enabled = function() + return LazyVim.cmp_engine() == "nvim-cmp" end, - main = "lazyvim.util.cmp", }, - - -- snippets { - "nvim-cmp", - optional = true, - dependencies = { - { - "garymjr/nvim-snippets", - opts = { - friendly_snippets = true, - }, - dependencies = { "rafamadriz/friendly-snippets" }, - }, - }, - opts = function(_, opts) - opts.snippet = { - expand = function(item) - return LazyVim.cmp.expand(item.body) - end, - } - if LazyVim.has("nvim-snippets") then - table.insert(opts.sources, { name = "snippets" }) - end + import = "lazyvim.plugins.extras.coding.blink", + enabled = function() + return LazyVim.cmp_engine() == "blink.cmp" end, }, @@ -189,12 +90,4 @@ return { }, }, }, - -- Add lazydev source to cmp - { - "hrsh7th/nvim-cmp", - optional = true, - opts = function(_, opts) - table.insert(opts.sources, { name = "lazydev", group_index = 0 }) - end, - }, } diff --git a/lua/lazyvim/plugins/compat/nvim-0_9.lua b/lua/lazyvim/plugins/compat/nvim-0_9.lua index 4f971b6f..81514ef8 100644 --- a/lua/lazyvim/plugins/compat/nvim-0_9.lua +++ b/lua/lazyvim/plugins/compat/nvim-0_9.lua @@ -4,6 +4,9 @@ return { { "garymjr/nvim-snippets", enabled = false }, { import = "lazyvim.plugins.extras.coding.luasnip" }, + -- Use nvim-cmp instead of blink.cmp + { import = "lazyvim.plugins.extras.coding.nvim-cmp" }, + -- Use mini.comment instead of ts-comments { "folke/ts-comments.nvim", enabled = false }, { import = "lazyvim.plugins.extras.coding.mini-comment" }, diff --git a/lua/lazyvim/plugins/extras/coding/nvim-cmp.lua b/lua/lazyvim/plugins/extras/coding/nvim-cmp.lua new file mode 100644 index 00000000..0cf36647 --- /dev/null +++ b/lua/lazyvim/plugins/extras/coding/nvim-cmp.lua @@ -0,0 +1,122 @@ +return { + + -- Setup nvim-cmp + { + "hrsh7th/nvim-cmp", + version = false, -- last release is way too old + event = "InsertEnter", + dependencies = { + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-path", + }, + -- Not all LSP servers add brackets when completing a function. + -- To better deal with this, LazyVim adds a custom option to cmp, + -- that you can configure. For example: + -- + -- ```lua + -- opts = { + -- auto_brackets = { "python" } + -- } + -- ``` + opts = function() + vim.api.nvim_set_hl(0, "CmpGhostText", { link = "Comment", default = true }) + local cmp = require("cmp") + local defaults = require("cmp.config.default")() + local auto_select = true + return { + auto_brackets = {}, -- configure any filetype to auto add brackets + completion = { + completeopt = "menu,menuone,noinsert" .. (auto_select and "" or ",noselect"), + }, + preselect = auto_select and cmp.PreselectMode.Item or cmp.PreselectMode.None, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }), + [""] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }), + [""] = cmp.mapping.complete(), + [""] = LazyVim.cmp.confirm({ select = auto_select }), + [""] = LazyVim.cmp.confirm({ select = true }), + [""] = LazyVim.cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + [""] = function(fallback) + cmp.abort() + fallback() + end, + [""] = function(fallback) + return LazyVim.cmp.map({ "snippet_forward", "ai_accept" }, fallback)() + end, + }), + sources = cmp.config.sources({ + { name = "nvim_lsp" }, + { name = "path" }, + }, { + { name = "buffer" }, + }), + formatting = { + format = function(entry, item) + local icons = LazyVim.config.icons.kinds + if icons[item.kind] then + item.kind = icons[item.kind] .. item.kind + end + + local widths = { + abbr = vim.g.cmp_widths and vim.g.cmp_widths.abbr or 40, + menu = vim.g.cmp_widths and vim.g.cmp_widths.menu or 30, + } + + for key, width in pairs(widths) do + if item[key] and vim.fn.strdisplaywidth(item[key]) > width then + item[key] = vim.fn.strcharpart(item[key], 0, width - 1) .. "…" + end + end + + return item + end, + }, + experimental = { + -- only show ghost text when we show ai completions + ghost_text = vim.g.ai_cmp and { + hl_group = "CmpGhostText", + } or false, + }, + sorting = defaults.sorting, + } + end, + main = "lazyvim.util.cmp", + }, + + -- Add lazydev source to cmp + { + "hrsh7th/nvim-cmp", + optional = true, + opts = function(_, opts) + table.insert(opts.sources, { name = "lazydev", group_index = 0 }) + end, + }, + + -- snippets + { + "nvim-cmp", + optional = true, + dependencies = { + { + "garymjr/nvim-snippets", + opts = { + friendly_snippets = true, + }, + dependencies = { "rafamadriz/friendly-snippets" }, + }, + }, + opts = function(_, opts) + opts.snippet = { + expand = function(item) + return LazyVim.cmp.expand(item.body) + end, + } + if LazyVim.has("nvim-snippets") then + table.insert(opts.sources, { name = "snippets" }) + end + end, + }, +} diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 8d94f582..c831606a 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -265,4 +265,13 @@ function M.memoize(fn) end end +---@return "nvim-cmp" | "blink.cmp" +function M.cmp_engine() + vim.g.lazyvim_cmp = vim.g.lazyvim_cmp or "auto" + if vim.g.lazyvim_cmp == "auto" then + return LazyVim.has_extra("nvim-cmp") and "nvim-cmp" or "blink.cmp" + end + return vim.g.lazyvim_cmp +end + return M diff --git a/lua/lazyvim/util/pick.lua b/lua/lazyvim/util/pick.lua index fa9ff821..526025a3 100644 --- a/lua/lazyvim/util/pick.lua +++ b/lua/lazyvim/util/pick.lua @@ -42,6 +42,7 @@ function M.register(picker) return true end +---@return "telescope" | "fzf" function M.want() vim.g.lazyvim_picker = vim.g.lazyvim_picker or "auto" if vim.g.lazyvim_picker == "auto" then