diff --git a/lua/lazyvim/config/keymaps.lua b/lua/lazyvim/config/keymaps.lua index e12ff3f7..6f9968d7 100644 --- a/lua/lazyvim/config/keymaps.lua +++ b/lua/lazyvim/config/keymaps.lua @@ -180,3 +180,13 @@ map("n", "", "tabnew", { desc = "New Tab" }) map("n", "]", "tabnext", { desc = "Next Tab" }) map("n", "d", "tabclose", { desc = "Close Tab" }) map("n", "[", "tabprevious", { desc = "Previous Tab" }) + +-- native snippets +if vim.fn.has("nvim-0.11") == 0 then + map("s", "", function() + return vim.snippet.active({ direction = 1 }) and "lua vim.snippet.jump(1)" or "" + end, { expr = true, desc = "Jump Next" }) + map({ "i", "s" }, "", function() + return vim.snippet.active({ direction = -1 }) and "lua vim.snippet.jump(-1)" or "" + end, { expr = true, desc = "Jump Previous" }) +end diff --git a/lua/lazyvim/plugins/coding.lua b/lua/lazyvim/plugins/coding.lua index 249b7baf..5b2901e3 100644 --- a/lua/lazyvim/plugins/coding.lua +++ b/lua/lazyvim/plugins/coding.lua @@ -43,6 +43,9 @@ return { cmp.abort() fallback() end, + [""] = function(fallback) + return LazyVim.cmp.map({ "snippet_forward", "ai_accept" }, fallback)() + end, }), sources = cmp.config.sources({ { name = "nvim_lsp" }, @@ -106,29 +109,6 @@ return { table.insert(opts.sources, { name = "snippets" }) end end, - -- stylua: ignore - keys = { - { - "", - function() - return vim.snippet.active({ direction = 1 }) and "lua vim.snippet.jump(1)" - or LazyVim.cmp.ai_accept() - or "" - end, - expr = true, silent = true, mode = "i", - }, - }, - init = function() - -- Neovim enabled snippet navigation mappings by default in v0.11 - if vim.fn.has("nvim-0.11") == 0 then - vim.keymap.set({ "s" }, "", function() - return vim.snippet.active({ direction = 1 }) and "lua vim.snippet.jump(1)" or "" - end, { expr = true, silent = true }) - vim.keymap.set({ "i", "s" }, "", function() - return vim.snippet.active({ direction = -1 }) and "lua vim.snippet.jump(-1)" or "" - end, { expr = true, silent = true }) - end - end, }, -- auto pairs diff --git a/lua/lazyvim/plugins/extras/ai/codeium.lua b/lua/lazyvim/plugins/extras/ai/codeium.lua index 9fdec062..6d2646a1 100644 --- a/lua/lazyvim/plugins/extras/ai/codeium.lua +++ b/lua/lazyvim/plugins/extras/ai/codeium.lua @@ -9,26 +9,26 @@ return { enable_cmp_source = vim.g.ai_cmp, virtual_text = { enabled = not vim.g.ai_cmp, - accept_fallback = "", key_bindings = { - accept = "", + accept = false, -- handled by nvim-cmp / blink.cmp next = "", prev = "", }, }, }, - config = function(_, opts) - LazyVim.cmp.ai_accept = function() + }, + + -- add ai_accept action + { + "Exafunction/codeium.nvim", + opts = function() + LazyVim.cmp.actions.ai_accept = function() if require("codeium.virtual_text").get_current_completion_item() then LazyVim.create_undo() vim.api.nvim_input(require("codeium.virtual_text").accept()) return true end end - if opts.virtual_text.key_bindings.accept == "" then - opts.virtual_text.key_bindings.accept = false - end - require("codeium").setup(opts) end, }, @@ -65,7 +65,7 @@ return { }, dependencies = { "codeium.nvim", - vim.g.ai_cmp and { "saghen/blink.compat" } or {}, + vim.g.ai_cmp and "saghen/blink.compat" or nil, }, }, } diff --git a/lua/lazyvim/plugins/extras/ai/copilot.lua b/lua/lazyvim/plugins/extras/ai/copilot.lua index 53d38cf0..69429e33 100644 --- a/lua/lazyvim/plugins/extras/ai/copilot.lua +++ b/lua/lazyvim/plugins/extras/ai/copilot.lua @@ -11,7 +11,7 @@ return { enabled = not vim.g.ai_cmp, auto_trigger = true, keymap = { - accept = "", + accept = false, -- handled by nvim-cmp / blink.cmp next = "", prev = "", }, @@ -22,20 +22,19 @@ return { help = true, }, }, - config = function(_, opts) - LazyVim.cmp.ai_accept = function() + }, + + -- add ai_accept action + { + "zbirenbaum/copilot.lua", + opts = function() + LazyVim.cmp.actions.ai_accept = function() if require("copilot.suggestion").is_visible() then LazyVim.create_undo() require("copilot.suggestion").accept() - return "" + return true end end - -- tab is handled by nvim-cmp / blink.cmp - local key = opts.suggestion.keymap.accept - if key == "" then - opts.suggestion.keymap.accept = false - end - require("copilot").setup(opts) end, }, diff --git a/lua/lazyvim/plugins/extras/coding/blink.lua b/lua/lazyvim/plugins/extras/coding/blink.lua index 3b054c27..040897a3 100644 --- a/lua/lazyvim/plugins/extras/coding/blink.lua +++ b/lua/lazyvim/plugins/extras/coding/blink.lua @@ -1,3 +1,10 @@ +if lazyvim_docs then + -- set to `true` to follow the main branch + -- you need to have a working rust toolchain to build the plugin + -- in this case. + vim.g.lazyvim_blink_main = false +end + return { { "hrsh7th/nvim-cmp", @@ -5,7 +12,8 @@ return { }, { "saghen/blink.cmp", - version = "*", + version = not vim.g.lazyvim_blink_main and "*", + build = vim.g.lazyvim_blink_main and "cargo build --release", opts_extend = { "sources.completion.enabled_providers", "sources.compat", @@ -59,11 +67,8 @@ return { keymap = { preset = "enter", - [""] = { - "snippet_forward", - function() - return LazyVim.cmp.ai_accept() - end, + [""] = { + LazyVim.cmp.map({ "snippet_forward", "ai_accept" }), "fallback", }, }, diff --git a/lua/lazyvim/plugins/extras/coding/luasnip.lua b/lua/lazyvim/plugins/extras/coding/luasnip.lua index 8f2aa70f..178cdd0a 100644 --- a/lua/lazyvim/plugins/extras/coding/luasnip.lua +++ b/lua/lazyvim/plugins/extras/coding/luasnip.lua @@ -1,4 +1,8 @@ return { + -- disable builtin snippet support + { "garymjr/nvim-snippets", enabled = false }, + + -- add luasnip { "L3MON4D3/LuaSnip", lazy = true, @@ -19,6 +23,19 @@ return { }, }, + -- add snippet_forward action + { + "L3MON4D3/LuaSnip", + opts = function() + LazyVim.cmp.actions.snippet_forward = function() + if require("luasnip").jumpable(1) then + require("luasnip").jump(1) + return true + end + end + end, + }, + -- nvim-cmp integration { "nvim-cmp", @@ -34,23 +51,21 @@ return { end, -- stylua: ignore keys = { - { - "", - function() - return require("luasnip").jumpable(1) and "luasnip-jump-next" - or LazyVim.cmp.ai_accept() - or "" - end, - expr = true, silent = true, mode = "i", - }, { "", function() require("luasnip").jump(1) end, mode = "s" }, { "", function() require("luasnip").jump(-1) end, mode = { "i", "s" } }, }, }, - { - "garymjr/nvim-snippets", - enabled = false, - }, - -- TODO: blink.cmp integration + -- blink.cmp integration + { + "saghen/blink.cmp", + optional = true, + opts = { + accept = { + expand_snippet = function(...) + return require("luasnip").lsp_expand(...) + end, + }, + }, + }, } diff --git a/lua/lazyvim/util/cmp.lua b/lua/lazyvim/util/cmp.lua index 1c2ebc4c..ebe0209b 100644 --- a/lua/lazyvim/util/cmp.lua +++ b/lua/lazyvim/util/cmp.lua @@ -1,8 +1,35 @@ ---@class lazyvim.util.cmp local M = {} ----@return string? -function M.ai_accept() end +---@alias lazyvim.util.cmp.Action fun():boolean? +---@type table +M.actions = { + -- Native Snippets + snippet_forward = function() + if vim.snippet.active({ direction = 1 }) then + vim.schedule(function() + vim.snippet.jump(1) + end) + return true + end + end, +} + +---@param actions string[] +---@param fallback? string|fun() +function M.map(actions, fallback) + return function() + for _, name in ipairs(actions) do + if M.actions[name] then + local ret = M.actions[name]() + if ret then + return true + end + end + end + return type(fallback) == "function" and fallback() or fallback + end +end ---@alias Placeholder {n:number, text:string}