Skip to content

Commit

Permalink
fix(snippets): fix parsing of ${1:{$2\}} like cases
Browse files Browse the repository at this point in the history
  • Loading branch information
echasnovski committed Dec 25, 2024
1 parent 2011aff commit e527e40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lua/mini/snippets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1796,8 +1796,9 @@ H.parse_processors.dollar_tabstop = function(c, s, n)
local new_node = { text = {} }
s:add_node(new_node)
if c == '$' then return s:set_name('dollar') end -- Case of `$1$2` and `$1$a`
table.insert(new_node.text, c) -- Case of `$1a`
s:set_name('text')
if c == '\\' then return s:set_in(new_node, 'after_slash', true) end -- Case of `${1:{$2\}}`
table.insert(new_node.text, c) -- Case of `$1a`
end

H.parse_processors.dollar_var = function(c, s, n)
Expand All @@ -1806,8 +1807,9 @@ H.parse_processors.dollar_var = function(c, s, n)
local new_node = { text = {} }
s:add_node(new_node)
if c == '$' then return s:set_name('dollar') end -- Case of `$a$b` and `$a$1`
table.insert(new_node.text, c) -- Case of `$a-`
s:set_name('text')
if c == '\\' then return s:set_in(new_node, 'after_slash', true) end -- Case of `${AAA:{$1\}}`
table.insert(new_node.text, c) -- Case of `$a-`
end

H.parse_processors.dollar_lbrace = function(c, s, n)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_snippets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,11 @@ T['parse()']['placeholder'] = function()

eq(parse([[${1:${2:aa\$bb\}cc\\dd}}]]), { { tabstop = '1', placeholder = { { tabstop = '2', placeholder = { { text = [[aa$bb}cc\dd]] } } } } } })

eq(parse([[${1:{$2\}}]]), { { tabstop = '1', placeholder = { { text = '{' }, { tabstop = '2' }, { text = '}' } } } })
eq(parse([[${aa:{$1\}}]]), { { var = 'aa', placeholder = { { text = '{' }, { tabstop = '1' }, { text = '}' } } } })
eq(parse([[${1:{$aa\}}]]), { { tabstop = '1', placeholder = { { text = '{' }, { var = 'aa' }, { text = '}' } } } })
eq(parse([[${aa:{$bb\}}]]), { { var = 'aa', placeholder = { { text = '{' }, { var = 'bb' }, { text = '}' } } } })

-- - Choice
eq(parse('${1:${2|aa|}}'), { { tabstop = '1', placeholder = { { tabstop = '2', choices = { 'aa' } } } } })
eq(parse('${1:${3|aa|}}'), { { tabstop = '1', placeholder = { { tabstop = '3', choices = { 'aa' } } } } })
Expand Down Expand Up @@ -3085,6 +3090,9 @@ T['parse()']['throws informative errors'] = function()
validate('${1:a bb', 'Placeholder should be closed with "}"')
validate('${1:${2:a', 'Placeholder should be closed with "}"')

validate([[${1:{$2\}]], 'Placeholder should be closed with "}"')
validate([[${1:{$aa\}]], 'Placeholder should be closed with "}"')

-- - Nested nodes should error according to their rules
validate('${1:${2?}}', 'Tabstop id should be followed by "}", ":", "|", or "/" not "?"')
validate('${1:${2?', 'Tabstop id should be followed by "}", ":", "|", or "/" not "?"')
Expand Down

0 comments on commit e527e40

Please sign in to comment.