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 ---@private
snippet.to_snippet = function(template, marks, pos) snippet.to_snippet = function(template, marks, pos)
local offset, ph = {}, {} 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 for i, m in ipairs(marks) do
local r, col = m.row - pos[1] + 1, m.col 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 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 -- Convert the snippet to string
local _snip = table.concat(snip, "\n") local _snip = table.concat(snip, "\n")
vim.print(snip)
ls.snip_expand( ls.snip_expand(
ls.s("", ls.parser.parse_snippet(nil, _snip, { trim_empty = false, dedent = false }), { ls.s("", ls.parser.parse_snippet(nil, _snip, { trim_empty = false, dedent = false }), {
@@ -111,7 +120,7 @@ snippet.engines.snippy = function(snip, pos)
end end
local row, _ = unpack(pos) local row, _ = unpack(pos)
vim.api.nvim_buf_set_lines(0, row, row, true, { "" }) -- snippy will change `row` vim.api.nvim_buf_set_lines(0, row, row, true, { "" }) -- snippy will change `row`
vim.api.nvim_win_set_cursor(0, { row + 1, 0 }) -- `snip` already has indent so we should ignore `col` vim.api.nvim_win_set_cursor(0, { row + 1, 0 }) -- `snip` already has indent so we should ignore `col`
snippy.expand_snippet({ body = snip }) snippy.expand_snippet({ body = snip })
end end
@@ -127,8 +136,8 @@ snippet.engines.vsnip = function(snip, pos)
end end
local row, _ = unpack(pos) local row, _ = unpack(pos)
vim.api.nvim_buf_set_lines(0, row, row, true, { "" }) -- vsnip will change `row` vim.api.nvim_buf_set_lines(0, row, row, true, { "" }) -- vsnip will change `row`
vim.api.nvim_win_set_cursor(0, { row + 1, 0 }) -- `snip` already has indent so we should ignore `col` vim.api.nvim_win_set_cursor(0, { row + 1, 0 }) -- `snip` already has indent so we should ignore `col`
snip = table.concat(snip, "\n") -- vsnip expects on string instead of a list/table of lines snip = table.concat(snip, "\n") -- vsnip expects on string instead of a list/table of lines
vim.fn["vsnip#anonymous"](snip) vim.fn["vsnip#anonymous"](snip)
end end