From e527e400d33d07de8ba6935b7bfbf1febef09ef4 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Wed, 25 Dec 2024 17:28:29 +0200 Subject: [PATCH] fix(snippets): fix parsing of `${1:{$2\}}` like cases --- lua/mini/snippets.lua | 6 ++++-- tests/test_snippets.lua | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lua/mini/snippets.lua b/lua/mini/snippets.lua index 7abc4677..b37d3e7c 100644 --- a/lua/mini/snippets.lua +++ b/lua/mini/snippets.lua @@ -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) @@ -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) diff --git a/tests/test_snippets.lua b/tests/test_snippets.lua index e32b8845..b561b6e5 100644 --- a/tests/test_snippets.lua +++ b/tests/test_snippets.lua @@ -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' } } } } }) @@ -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 "?"')