robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
4.2 KiB174 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local snippet = require('vim.lsp._snippet_grammar')
5local describe, it, before_each = t.describe, t.it, t.before_each
6local type = snippet.NodeType
7
8local eq = t.eq
9local exec_lua = n.exec_lua
10
11describe('vim.lsp._snippet_grammar', function()
12  before_each(n.clear)
13
14  local parse = function(...)
15    local results = exec_lua('return { require("vim.lsp._snippet_grammar").parse(...) }', ...)
16    eq(1, #results)
17    return results[1].data.children
18  end
19
20  it('parses only text', function()
21    eq({
22      { type = type.Text, data = { text = 'TE$}XT' } },
23    }, parse('TE\\$\\}XT'))
24  end)
25
26  it('parses tabstops', function()
27    eq({
28      { type = type.Tabstop, data = { tabstop = 1 } },
29      { type = type.Tabstop, data = { tabstop = 2 } },
30    }, parse('$1${2}'))
31  end)
32
33  it('parses nested placeholders', function()
34    eq({
35      {
36        type = type.Placeholder,
37        data = {
38          tabstop = 1,
39          value = {
40            type = type.Placeholder,
41            data = {
42              tabstop = 2,
43              value = { type = type.Tabstop, data = { tabstop = 3 } },
44            },
45          },
46        },
47      },
48    }, parse('${1:${2:${3}}}'))
49  end)
50
51  it('parses variables', function()
52    eq({
53      { type = type.Variable, data = { name = 'VAR' } },
54      { type = type.Variable, data = { name = 'VAR' } },
55      {
56        type = type.Variable,
57        data = {
58          name = 'VAR',
59          default = { type = type.Tabstop, data = { tabstop = 1 } },
60        },
61      },
62      {
63        type = type.Variable,
64        data = {
65          name = 'VAR',
66          regex = 'regex',
67          options = '',
68          format = {
69            {
70              type = type.Format,
71              data = { capture = 1, modifier = 'upcase' },
72            },
73          },
74        },
75      },
76    }, parse('$VAR${VAR}${VAR:$1}${VAR/regex/${1:/upcase}/}'))
77  end)
78
79  it('parses choice', function()
80    eq({
81      {
82        type = type.Choice,
83        data = { tabstop = 1, values = { ',', '|' } },
84      },
85    }, parse('${1|\\,,\\||}'))
86  end)
87
88  it('parses format', function()
89    eq(
90      {
91        {
92          type = type.Variable,
93          data = {
94            name = 'VAR',
95            regex = 'regex',
96            options = '',
97            format = {
98              {
99                type = type.Format,
100                data = { capture = 1, modifier = 'upcase' },
101              },
102              {
103                type = type.Format,
104                data = { capture = 1, if_text = 'if_text' },
105              },
106              {
107                type = type.Format,
108                data = { capture = 1, else_text = 'else_text' },
109              },
110              {
111                type = type.Format,
112                data = { capture = 1, if_text = 'if_text', else_text = 'else_text' },
113              },
114              {
115                type = type.Format,
116                data = { capture = 1, else_text = 'else_text' },
117              },
118            },
119          },
120        },
121      },
122      parse(
123        '${VAR/regex/${1:/upcase}${1:+if_text}${1:-else_text}${1:?if_text:else_text}${1:else_text}/}'
124      )
125    )
126  end)
127
128  it('parses empty strings', function()
129    eq({
130      {
131        type = type.Placeholder,
132        data = {
133          tabstop = 1,
134          value = { type = type.Text, data = { text = '' } },
135        },
136      },
137      {
138        type = type.Text,
139        data = { text = ' ' },
140      },
141      {
142        type = type.Variable,
143        data = {
144          name = 'VAR',
145          regex = 'erg',
146          format = {
147            {
148              type = type.Format,
149              data = { capture = 1, if_text = '' },
150            },
151          },
152          options = 'g',
153        },
154      },
155    }, parse('${1:} ${VAR/erg/${1:+}/g}'))
156  end)
157
158  it('parses closing curly brace as text', function()
159    eq(
160      {
161        { type = type.Text, data = { text = 'function ' } },
162        { type = type.Tabstop, data = { tabstop = 1 } },
163        { type = type.Text, data = { text = '() {\n  ' } },
164        { type = type.Tabstop, data = { tabstop = 0 } },
165        { type = type.Text, data = { text = '\n}' } },
166      },
167      parse(table.concat({
168        'function $1() {',
169        '  $0',
170        '}',
171      }, '\n'))
172    )
173  end)
174end)