feat: an extra for mini.snippets. Load the plugin on insertenter

This commit is contained in:
abeldekat 2025-01-03 21:02:55 +01:00
parent 650ce200f1
commit cc07da5c10

View file

@ -43,6 +43,11 @@ return {
} }
--]] --]]
-- Start with a default snippet_select that also works when the user disables a competion engine
local snippet_select = function(snippets, insert)
MiniSnippets.default_select(snippets, insert)
end
local snippet_select_for_cmp = function(snippets, insert) local snippet_select_for_cmp = function(snippets, insert)
local cmp = require("cmp") local cmp = require("cmp")
if cmp.visible() then if cmp.visible() then
@ -50,6 +55,7 @@ local snippet_select_for_cmp = function(snippets, insert)
end end
MiniSnippets.default_select(snippets, insert) MiniSnippets.default_select(snippets, insert)
end end
local snippet_select_for_blink = function(snippets, insert) local snippet_select_for_blink = function(snippets, insert)
-- Blink's cancel uses vim.schedule! -- Blink's cancel uses vim.schedule!
require("blink.cmp").cancel() require("blink.cmp").cancel()
@ -58,9 +64,6 @@ local snippet_select_for_blink = function(snippets, insert)
MiniSnippets.default_select(snippets, insert) MiniSnippets.default_select(snippets, insert)
end) end)
end end
local snippet_select = function(snippets, insert)
MiniSnippets.default_select(snippets, insert)
end
local function expand(args) local function expand(args)
---@diagnostic disable-next-line: undefined-global ---@diagnostic disable-next-line: undefined-global
@ -86,8 +89,7 @@ return {
desc = "mini.snippets(beta), a plugin to manage and expand snippets (alternative for luasnip)", desc = "mini.snippets(beta), a plugin to manage and expand snippets (alternative for luasnip)",
{ {
"echasnovski/mini.snippets", "echasnovski/mini.snippets",
lazy = true, event = "InsertEnter", -- don't depend on other plugins to load...
event = not snippets_in_cmp and { "InsertEnter" } or nil,
dependencies = "rafamadriz/friendly-snippets", dependencies = "rafamadriz/friendly-snippets",
opts = function() opts = function()
local snippets = require("mini.snippets") local snippets = require("mini.snippets")
@ -97,14 +99,14 @@ return {
local ret = { snippets = { snippets.gen_loader.from_lang() } } local ret = { snippets = { snippets.gen_loader.from_lang() } }
LazyVim.cmp.actions.snippet_stop = function() end -- by design, <esc> should not stop the session! LazyVim.cmp.actions.snippet_stop = function() end -- by design, <esc> should not stop the session!
if not snippets_in_cmp then if snippets_in_cmp then
-- stylua: ignore
LazyVim.cmp.actions.snippet_forward = function() return jump("next") end
else
LazyVim.cmp.actions.snippet_forward = nil LazyVim.cmp.actions.snippet_forward = nil
-- stylua: ignore -- stylua: ignore
-- Close completion windows on snippet select to handle virtual text: -- Close completion windows on snippet select to handle virtual text:
ret.expand = { select = function(...) snippet_select(...) end, } ret.expand = { select = function(...) snippet_select(...) end, }
else
-- stylua: ignore
LazyVim.cmp.actions.snippet_forward = function() return jump("next") end
end end
return ret return ret
end, end,
@ -149,14 +151,14 @@ return {
table.insert(opts.sources.default, "mini_snippets") table.insert(opts.sources.default, "mini_snippets")
end end
-- Blink defines the <s-tab> key. -- Note: blink defines the <s-tab> key...
opts.snippets = { opts.snippets = {
-- Use mini.snippets to expand snippets from lsp: -- Use mini.snippets to expand snippets from lsp:
expand = function(snippet) expand = function(snippet)
expand({ body = snippet }) expand({ body = snippet })
end, end,
active = function() active = function()
return require("mini.snippets").session.get(false) ~= nil -- lazy loading... return MiniSnippets.session.get(false) ~= nil
end, end,
jump = function(direction) jump = function(direction)
jump(direction < 0 and "prev" or "next") jump(direction < 0 and "prev" or "next")