fix(snippet): Double $ in snippet template

Closes #207

Due to $ being automatically interpreted as snippet variables, we need
to double the $ in the template to let it pass when generating the
snippet. Solves issues with php parameters that have their $ deleted
when using snippets.
This commit is contained in:
danymat
2025-02-08 13:48:31 +01:00
parent dd0301bfba
commit aeea21f7c3

View File

@@ -48,6 +48,13 @@ snippet.engines = {}
---@private
snippet.to_snippet = function(template, marks, pos)
local offset, ph = {}, {}
-- Add double $ for template before creating the snippet so that text with $ (such as parameters in php)
-- are not interpreted as snippet
for i, str in ipairs(template) do
template[i] = str:gsub("%$", "$$") -- Escape $
end
for i, m in ipairs(marks) do
local r, col = m.row - pos[1] + 1, m.col
ph[i] = (m.text and conf.enable_placeholders) and string.format("${%d:%s}", i, m.text) or "$" .. i
@@ -81,6 +88,8 @@ snippet.engines.luasnip = function(snip, pos)
-- Convert the snippet to string
local _snip = table.concat(snip, "\n")
vim.print(snip)
ls.snip_expand(
ls.s("", ls.parser.parse_snippet(nil, _snip, { trim_empty = false, dedent = false }), {