diff --git a/lua/lazyvim/plugins/coding.lua b/lua/lazyvim/plugins/coding.lua index b754eaf6..589e4b1c 100644 --- a/lua/lazyvim/plugins/coding.lua +++ b/lua/lazyvim/plugins/coding.lua @@ -93,6 +93,11 @@ return { { "garymjr/nvim-snippets", opts = { friendly_snippets = true } }, }, opts = function(_, opts) + opts.snippet = { + expand = function(item) + return LazyVim.cmp.expand(item.body) + end, + } table.insert(opts.sources, { name = "snippets" }) end, keys = { diff --git a/lua/lazyvim/util/cmp.lua b/lua/lazyvim/util/cmp.lua index 64c43f0a..d0e5b48f 100644 --- a/lua/lazyvim/util/cmp.lua +++ b/lua/lazyvim/util/cmp.lua @@ -18,6 +18,13 @@ function M.snippet_resolve(snippet) end):gsub("%$0", "") end +-- This function replaces nested placeholders in a snippet with LSP placeholders. +function M.snippet_fix(snippet) + return M.snippet_replace(snippet, function(placeholder) + return "${" .. placeholder.n .. ":" .. M.snippet_resolve(placeholder.text) .. "}" + end) +end + ---@param entry cmp.Entry function M.auto_brackets(entry) local cmp = require("cmp") @@ -74,4 +81,23 @@ function M.confirm(opts) return fallback() end end + +function M.expand(snippet) + local ok = pcall(vim.snippet.expand, snippet) + if not ok then + local fixed = M.snippet_fix(snippet) + ok = pcall(vim.snippet.expand, fixed) + + local msg = ok and "Failed to parse snippet,\nbut was able to fix it automatically." or "Failed to parse snippet." + + LazyVim[ok and "warn" or "error"]( + ([[%s +```%s +%s +```]]):format(msg, vim.bo.filetype, snippet), + { title = "vim.snippet" } + ) + end +end + return M