mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-26 10:48:45 +02:00
feat(cmp): better error handling and automatic fixing of snippets that fail to parse with native Neovim snippets
This commit is contained in:
parent
7d72436193
commit
97862f4259
2 changed files with 31 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue