2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
2023-10-01 09:54:04 -07:00
|
|
|
local snippet = require('vim.lsp._snippet_grammar')
|
2023-10-14 00:06:40 -07:00
|
|
|
local type = snippet.NodeType
|
2021-09-14 04:31:41 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
2024-04-20 08:44:13 -07:00
|
|
|
local exec_lua = n.exec_lua
|
2021-09-14 04:31:41 -07:00
|
|
|
|
2023-10-01 09:54:04 -07:00
|
|
|
describe('vim.lsp._snippet_grammar', function()
|
2024-04-20 08:44:13 -07:00
|
|
|
before_each(n.clear)
|
|
|
|
after_each(n.clear)
|
2021-09-14 04:31:41 -07:00
|
|
|
|
|
|
|
local parse = function(...)
|
2023-10-01 09:54:04 -07:00
|
|
|
local res = exec_lua('return require("vim.lsp._snippet_grammar").parse(...)', ...)
|
|
|
|
return res.data.children
|
2021-09-14 04:31:41 -07:00
|
|
|
end
|
|
|
|
|
2023-10-01 09:54:04 -07:00
|
|
|
it('parses only text', function()
|
2021-09-14 04:31:41 -07:00
|
|
|
eq({
|
2023-10-14 00:06:40 -07:00
|
|
|
{ type = type.Text, data = { text = 'TE$}XT' } },
|
2021-09-14 04:31:41 -07:00
|
|
|
}, parse('TE\\$\\}XT'))
|
|
|
|
end)
|
|
|
|
|
2023-10-01 09:54:04 -07:00
|
|
|
it('parses tabstops', function()
|
2021-09-14 04:31:41 -07:00
|
|
|
eq({
|
2023-10-14 00:06:40 -07:00
|
|
|
{ type = type.Tabstop, data = { tabstop = 1 } },
|
|
|
|
{ type = type.Tabstop, data = { tabstop = 2 } },
|
2021-09-14 04:31:41 -07:00
|
|
|
}, parse('$1${2}'))
|
|
|
|
end)
|
|
|
|
|
2023-10-01 09:54:04 -07:00
|
|
|
it('parses nested placeholders', function()
|
2021-09-14 04:31:41 -07:00
|
|
|
eq({
|
2023-10-01 09:54:04 -07:00
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Placeholder,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = {
|
2021-09-14 04:31:41 -07:00
|
|
|
tabstop = 1,
|
2023-10-01 09:54:04 -07:00
|
|
|
value = {
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Placeholder,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = {
|
2021-09-14 04:31:41 -07:00
|
|
|
tabstop = 2,
|
2023-10-14 00:06:40 -07:00
|
|
|
value = { type = type.Tabstop, data = { tabstop = 3 } },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
|
|
|
},
|
2021-09-14 04:31:41 -07:00
|
|
|
},
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
2023-10-01 09:54:04 -07:00
|
|
|
}, parse('${1:${2:${3}}}'))
|
2021-09-14 04:31:41 -07:00
|
|
|
end)
|
|
|
|
|
2023-10-01 09:54:04 -07:00
|
|
|
it('parses variables', function()
|
2021-09-14 04:31:41 -07:00
|
|
|
eq({
|
2023-10-14 00:06:40 -07:00
|
|
|
{ type = type.Variable, data = { name = 'VAR' } },
|
|
|
|
{ type = type.Variable, data = { name = 'VAR' } },
|
2023-10-01 09:54:04 -07:00
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Variable,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = {
|
2021-09-14 04:31:41 -07:00
|
|
|
name = 'VAR',
|
2023-10-14 00:06:40 -07:00
|
|
|
default = { type = type.Tabstop, data = { tabstop = 1 } },
|
2021-09-14 04:31:41 -07:00
|
|
|
},
|
2023-10-01 09:54:04 -07:00
|
|
|
},
|
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Variable,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = {
|
2021-09-14 04:31:41 -07:00
|
|
|
name = 'VAR',
|
2023-10-01 09:54:04 -07:00
|
|
|
regex = 'regex',
|
|
|
|
options = '',
|
|
|
|
format = {
|
2021-09-14 04:31:41 -07:00
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Format,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = { capture = 1, modifier = 'upcase' },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
|
|
|
},
|
2021-09-14 04:31:41 -07:00
|
|
|
},
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
2021-09-14 04:31:41 -07:00
|
|
|
}, parse('$VAR${VAR}${VAR:$1}${VAR/regex/${1:/upcase}/}'))
|
|
|
|
end)
|
|
|
|
|
2023-10-01 09:54:04 -07:00
|
|
|
it('parses choice', function()
|
2021-09-14 04:31:41 -07:00
|
|
|
eq({
|
2023-10-01 09:54:04 -07:00
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Choice,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = { tabstop = 1, values = { ',', '|' } },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
2021-09-14 04:31:41 -07:00
|
|
|
}, parse('${1|\\,,\\||}'))
|
|
|
|
end)
|
|
|
|
|
2023-10-01 09:54:04 -07:00
|
|
|
it('parses format', function()
|
|
|
|
eq(
|
|
|
|
{
|
2022-06-29 09:53:49 -07:00
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Variable,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = {
|
|
|
|
name = 'VAR',
|
|
|
|
regex = 'regex',
|
|
|
|
options = '',
|
2022-06-29 09:53:49 -07:00
|
|
|
format = {
|
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Format,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = { capture = 1, modifier = 'upcase' },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Format,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = { capture = 1, if_text = 'if_text' },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Format,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = { capture = 1, else_text = 'else_text' },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Format,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = { capture = 1, if_text = 'if_text', else_text = 'else_text' },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Format,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = { capture = 1, else_text = 'else_text' },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-10-01 09:54:04 -07:00
|
|
|
parse(
|
|
|
|
'${VAR/regex/${1:/upcase}${1:+if_text}${1:-else_text}${1:?if_text:else_text}${1:else_text}/}'
|
|
|
|
)
|
|
|
|
)
|
2022-06-29 09:53:49 -07:00
|
|
|
end)
|
2021-09-14 04:31:41 -07:00
|
|
|
|
2023-10-01 09:54:04 -07:00
|
|
|
it('parses empty strings', function()
|
2022-06-29 09:53:49 -07:00
|
|
|
eq({
|
2023-10-01 09:54:04 -07:00
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Placeholder,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = {
|
2022-06-29 09:53:49 -07:00
|
|
|
tabstop = 1,
|
2023-10-14 00:06:40 -07:00
|
|
|
value = { type = type.Text, data = { text = '' } },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
2023-10-01 09:54:04 -07:00
|
|
|
},
|
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Text,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = { text = ' ' },
|
|
|
|
},
|
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Variable,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = {
|
2022-06-29 09:53:49 -07:00
|
|
|
name = 'VAR',
|
2023-10-01 09:54:04 -07:00
|
|
|
regex = 'erg',
|
|
|
|
format = {
|
|
|
|
{
|
2023-10-14 00:06:40 -07:00
|
|
|
type = type.Format,
|
2023-10-01 09:54:04 -07:00
|
|
|
data = { capture = 1, if_text = '' },
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
|
|
|
},
|
2023-10-01 09:54:04 -07:00
|
|
|
options = 'g',
|
2022-06-29 09:53:49 -07:00
|
|
|
},
|
|
|
|
},
|
2023-10-01 09:54:04 -07:00
|
|
|
}, parse('${1:} ${VAR/erg/${1:+}/g}'))
|
2022-06-29 09:53:49 -07:00
|
|
|
end)
|
2023-10-14 00:06:40 -07:00
|
|
|
|
|
|
|
it('parses closing curly brace as text', function()
|
|
|
|
eq(
|
|
|
|
{
|
|
|
|
{ type = type.Text, data = { text = 'function ' } },
|
|
|
|
{ type = type.Tabstop, data = { tabstop = 1 } },
|
|
|
|
{ type = type.Text, data = { text = '() {\n ' } },
|
|
|
|
{ type = type.Tabstop, data = { tabstop = 0 } },
|
|
|
|
{ type = type.Text, data = { text = '\n}' } },
|
|
|
|
},
|
|
|
|
parse(table.concat({
|
|
|
|
'function $1() {',
|
|
|
|
' $0',
|
|
|
|
'}',
|
|
|
|
}, '\n'))
|
|
|
|
)
|
|
|
|
end)
|
2022-06-29 09:53:49 -07:00
|
|
|
end)
|