robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
13.2 KiB443 linesraw
1---@diagnostic disable: no-unknown
2
3local t = require('test.testutil')
4local n = require('test.functional.testnvim')()
5
6local describe, it, before_each = t.describe, t.it, t.before_each
7local buf_lines = n.buf_lines
8local clear = n.clear
9local eq = t.eq
10local exec_lua = n.exec_lua
11local feed = n.feed
12local api = n.api
13local fn = n.fn
14local matches = t.matches
15local pcall_err = t.pcall_err
16local poke_eventloop = n.poke_eventloop
17local retry = t.retry
18
19describe('vim.snippet', function()
20  before_each(function()
21    clear()
22    exec_lua(function()
23      local function set_snippet_jump(direction, key)
24        vim.keymap.set({ 'i', 's' }, key, function()
25          if vim.snippet.active({ direction = direction }) then
26            return string.format('<Cmd>lua vim.snippet.jump(%d)<CR>', direction)
27          else
28            return key
29          end
30        end, { silent = true, expr = true })
31      end
32
33      set_snippet_jump(1, '<Tab>')
34      set_snippet_jump(-1, '<S-Tab>')
35    end)
36  end)
37
38  --- @param snippet string[]
39  --- @param expected string[]
40  --- @param settings? string
41  --- @param prefix? string
42  local function test_expand_success(snippet, expected, settings, prefix)
43    if settings then
44      exec_lua(settings)
45    end
46    if prefix then
47      feed('i' .. prefix)
48    end
49    exec_lua('vim.snippet.expand(...)', table.concat(snippet, '\n'))
50    eq(expected, buf_lines(0))
51  end
52
53  local function wait_for_pum()
54    retry(nil, nil, function()
55      eq(1, fn.pumvisible())
56    end)
57  end
58
59  --- @param snippet string
60  --- @param err string
61  local function test_expand_fail(snippet, err)
62    matches(err, pcall_err(exec_lua, string.format('vim.snippet.expand("%s")', snippet)))
63  end
64
65  it('adds base indentation to inserted text', function()
66    test_expand_success(
67      { 'function $1($2)', '  $0', 'end' },
68      { '  function ()', '    ', '  end' },
69      '',
70      '  '
71    )
72  end)
73
74  it('adds indentation based on the start of snippet lines', function()
75    local curbuf = api.nvim_get_current_buf()
76
77    test_expand_success({ 'if $1 then', '  $0', 'end' }, { 'if  then', '  ', 'end' })
78
79    -- Regression test: #29658
80    api.nvim_buf_set_lines(curbuf, 0, -1, false, {})
81    test_expand_success({ '${1:foo^bar}\n' }, { 'foo^bar', '' })
82
83    -- Regression test: #30950
84    api.nvim_buf_set_lines(curbuf, 0, -1, false, {})
85    test_expand_success({ 'a^ b$1', 'b$2', 'd' }, { 'a^ b', 'b', 'd' })
86  end)
87
88  it('replaces tabs with spaces when expandtab is set', function()
89    test_expand_success(
90      { 'function $1($2)', '\t$0', 'end' },
91      { 'function ()', '  ', 'end' },
92      [[
93      vim.o.expandtab = true
94      vim.o.shiftwidth = 2
95      ]]
96    )
97  end)
98
99  it('respects tabs when expandtab is not set', function()
100    test_expand_success(
101      { 'function $1($2)', '\t$0', 'end' },
102      { 'function ()', '\t', 'end' },
103      'vim.o.expandtab = false'
104    )
105  end)
106
107  it('inserts known variable value', function()
108    test_expand_success({ '; print($TM_CURRENT_LINE)' }, { 'foo; print(foo)' }, nil, 'foo')
109  end)
110
111  it('uses default when variable is not set', function()
112    test_expand_success({ 'print(${TM_CURRENT_WORD:foo})' }, { 'print(foo)' })
113  end)
114
115  it('replaces unknown variables by placeholders', function()
116    test_expand_success({ 'print($UNKNOWN)' }, { 'print(UNKNOWN)' })
117  end)
118
119  it('highlights active tabstop with SnippetTabstopActive', function()
120    local function get_extmark_details(col, end_col)
121      return api.nvim_buf_get_extmarks(0, -1, { 0, col }, { 0, end_col }, { details = true })[1][4]
122    end
123
124    test_expand_success({ 'local ${1:name} = ${2:value}' }, { 'local name = value' })
125    eq('SnippetTabstopActive', get_extmark_details(6, 10).hl_group)
126    eq('SnippetTabstop', get_extmark_details(13, 18).hl_group)
127    feed('<Tab>')
128    poke_eventloop()
129    eq('SnippetTabstop', get_extmark_details(6, 10).hl_group)
130    eq('SnippetTabstopActive', get_extmark_details(13, 18).hl_group)
131  end)
132
133  it('does not jump outside snippet range', function()
134    test_expand_success({ 'function $1($2)', '  $0', 'end' }, { 'function ()', '  ', 'end' })
135    eq(false, exec_lua('return vim.snippet.active({ direction = -1 })'))
136    feed('<Tab><Tab>i')
137    eq(false, exec_lua('return vim.snippet.active( { direction = 1 })'))
138  end)
139
140  it('navigates backwards', function()
141    test_expand_success({ 'function $1($2) end' }, { 'function () end' })
142    feed('<Tab><S-Tab>foo')
143    eq({ 'function foo() end' }, buf_lines(0))
144  end)
145
146  it('visits all tabstops', function()
147    local function cursor()
148      return exec_lua('return vim.api.nvim_win_get_cursor(0)')
149    end
150
151    test_expand_success({ 'function $1($2)', '  $0', 'end' }, { 'function ()', '  ', 'end' })
152    eq({ 1, 9 }, cursor())
153    feed('<Tab>')
154    eq({ 1, 10 }, cursor())
155    feed('<Tab>')
156    eq({ 2, 2 }, cursor())
157  end)
158
159  it('syncs text of tabstops with equal indexes', function()
160    test_expand_success({ 'var double = ${1:x} + ${1:x}' }, { 'var double = x + x' })
161    feed('123')
162    eq({ 'var double = 123 + 123' }, buf_lines(0))
163  end)
164
165  it('cancels session with changes outside the snippet', function()
166    test_expand_success({ 'print($1)' }, { 'print()' })
167    feed('<Esc>O-- A comment')
168    eq(false, exec_lua('return vim.snippet.active()'))
169    eq({ '-- A comment', 'print()' }, buf_lines(0))
170  end)
171
172  it('handles non-consecutive tabstops', function()
173    test_expand_success({ 'class $1($3) {', '  $0', '}' }, { 'class () {', '  ', '}' })
174    feed('Foo') -- First tabstop
175    feed('<Tab><Tab>') -- Jump to $0
176    feed('// Inside') -- Insert text
177    eq({ 'class Foo() {', '  // Inside', '}' }, buf_lines(0))
178  end)
179
180  it('handles directly adjacent tabstops (ascending order)', function()
181    test_expand_success({ '${1:one}${2:-two}${3:-three}' }, { 'one-two-three' })
182    feed('1')
183    feed('<Tab>')
184    poke_eventloop()
185    feed('2')
186    feed('<Tab>')
187    poke_eventloop()
188    feed('3')
189    feed('<Tab>')
190    poke_eventloop()
191    eq({ '123' }, buf_lines(0))
192  end)
193
194  it('handles directly adjacent tabstops (descending order)', function()
195    test_expand_success({ '${3:three}${2:-two}${1:-one}' }, { 'three-two-one' })
196    feed('1')
197    feed('<Tab>')
198    poke_eventloop()
199    feed('2')
200    feed('<Tab>')
201    poke_eventloop()
202    feed('3')
203    feed('<Tab>')
204    poke_eventloop()
205    eq({ '321' }, buf_lines(0))
206  end)
207
208  it('handles directly adjacent tabstops (mixed order)', function()
209    test_expand_success({ '${3:three}${1:-one}${2:-two}' }, { 'three-one-two' })
210    feed('1')
211    feed('<Tab>')
212    poke_eventloop()
213    feed('2')
214    feed('<Tab>')
215    poke_eventloop()
216    feed('3')
217    feed('<Tab>')
218    poke_eventloop()
219    eq({ '312' }, buf_lines(0))
220  end)
221
222  it('handles multiline placeholders', function()
223    test_expand_success(
224      { 'public void foo() {', '  ${0:// TODO Auto-generated', '  throw;}', '}' },
225      { 'public void foo() {', '  // TODO Auto-generated', '  throw;', '}' }
226    )
227  end)
228
229  it('inserts placeholder in all tabstops when the first tabstop has the placeholder', function()
230    test_expand_success(
231      { 'for (${1:int} ${2:x} = ${3:0}; $2 < ${4:N}; $2++) {', '  $0', '}' },
232      { 'for (int x = 0; x < N; x++) {', '  ', '}' }
233    )
234  end)
235
236  it('inserts placeholder in all tabstops when a later tabstop has the placeholder', function()
237    test_expand_success(
238      { 'for (${1:int} $2 = ${3:0}; ${2:x} < ${4:N}; $2++) {', '  $0', '}' },
239      { 'for (int x = 0; x < N; x++) {', '  ', '}' }
240    )
241  end)
242
243  it('errors with multiple placeholders for the same index', function()
244    test_expand_fail(
245      'class ${1:Foo} { void ${1:foo}() {} }',
246      'multiple placeholders for tabstop $1'
247    )
248  end)
249
250  it('errors with multiple $0 tabstops', function()
251    test_expand_fail('function $1() { $0 }$0', 'multiple $0 tabstops')
252  end)
253
254  it('cancels session when deleting the snippet', function()
255    test_expand_success(
256      { 'local function $1()', '  $0', 'end' },
257      { 'local function ()', '  ', 'end' }
258    )
259    feed('<esc>Vjjd')
260    eq(false, exec_lua('return vim.snippet.active()'))
261  end)
262
263  it('cancels session when inserting outside snippet region', function()
264    feed('i<cr>')
265    test_expand_success(
266      { 'local function $1()', '  $0', 'end' },
267      { '', 'local function ()', '  ', 'end' }
268    )
269    feed('<esc>O-- A comment')
270    eq(false, exec_lua('return vim.snippet.active()'))
271  end)
272
273  it('cancels session on <Esc> from tabstop #39220', function()
274    local ns = api.nvim_create_namespace('nvim.snippet')
275    test_expand_success({ 'local ${1:name} = ${2:value}' }, { 'local name = value' })
276    eq('s', fn.mode())
277    eq(true, exec_lua('return vim.snippet.active()'))
278    t.neq(0, #api.nvim_buf_get_extmarks(0, ns, 0, -1, {}))
279    feed('<Esc>')
280    poke_eventloop()
281    eq(false, exec_lua('return vim.snippet.active()'))
282    eq(0, #api.nvim_buf_get_extmarks(0, ns, 0, -1, {}))
283
284    feed('dal')
285    test_expand_success({ 'local $1 = ${2:value}' }, { 'local  = value' })
286    eq('i', fn.mode())
287    eq(true, exec_lua('return vim.snippet.active()'))
288    t.neq(0, #api.nvim_buf_get_extmarks(0, ns, 0, -1, {}))
289    feed('<Esc>')
290    poke_eventloop()
291    eq(false, exec_lua('return vim.snippet.active()'))
292    eq(0, #api.nvim_buf_get_extmarks(0, ns, 0, -1, {}))
293  end)
294
295  it('stop session when jumping to $0', function()
296    test_expand_success({ 'local ${1:name} = ${2:value}$0' }, { 'local name = value' })
297    -- Jump to $2
298    feed('<Tab>')
299    poke_eventloop()
300    -- Jump to $0 (stop snippet)
301    feed('<Tab>')
302    poke_eventloop()
303    -- Insert literal \t
304    feed('<Tab>')
305    poke_eventloop()
306    eq({ 'local name = value\t' }, buf_lines(0))
307  end)
308
309  it('inserts choice', function()
310    test_expand_success({ 'console.${1|assert,log,error|}()' }, { 'console.()' })
311    wait_for_pum()
312    feed('<Down><C-y>')
313    eq({ 'console.log()' }, buf_lines(0))
314  end)
315
316  it('closes the choice completion menu when jumping', function()
317    test_expand_success({ 'console.${1|assert,log,error|}($2)' }, { 'console.()' })
318    wait_for_pum()
319    exec_lua('vim.snippet.jump(1)')
320    eq(0, fn.pumvisible())
321  end)
322
323  it('jumps to next tabstop after inserting choice', function()
324    test_expand_success(
325      { '${1|public,protected,private|} function ${2:name}() {', '\t$0', '}' },
326      { ' function name() {', '\t', '}' }
327    )
328    wait_for_pum()
329    feed('<C-y><Tab>')
330    poke_eventloop()
331    feed('foo')
332    eq({ 'public function foo() {', '\t', '}' }, buf_lines(0))
333  end)
334
335  it('does not change the chosen text when jumping back to a choice tabstop', function()
336    test_expand_success(
337      { '${1|public,protected,private|} function ${2:name}() {', '\t$0', '}' },
338      { ' function name() {', '\t', '}' }
339    )
340    wait_for_pum()
341    feed('<C-n><C-y><Tab>')
342    poke_eventloop()
343    feed('<S-Tab>')
344    poke_eventloop()
345    wait_for_pum()
346    feed('<Tab>')
347    poke_eventloop()
348    feed('foo')
349    eq({ 'protected function foo() {', '\t', '}' }, buf_lines(0))
350  end)
351
352  it('jumps through adjacent tabstops', function()
353    test_expand_success(
354      { 'for i=1,${1:to}${2:,step} do\n\t$3\nend' },
355      { 'for i=1,to,step do', '\t', 'end' }
356    )
357    feed('10')
358    feed('<Tab>')
359    poke_eventloop()
360    feed(',2')
361    -- Make sure changes on previous tabstops does not change following ones
362    feed('<S-Tab>')
363    poke_eventloop()
364    feed('20')
365    eq({ 'for i=1,20,2 do', '\t', 'end' }, buf_lines(0))
366  end)
367
368  it('updates snippet state when built-in completion menu is visible', function()
369    test_expand_success({ '$1 = function($2)\nend' }, { ' = function()', 'end' })
370    -- Show the completion menu.
371    feed('<C-n>')
372    -- Make sure no item is selected.
373    feed('<C-p>')
374    -- Jump forward (the 2nd tabstop).
375    exec_lua('vim.snippet.jump(1)')
376    feed('foo')
377    eq({ ' = function(foo)', 'end' }, buf_lines(0))
378  end)
379
380  it('correctly indents with newlines', function()
381    local curbuf = api.nvim_get_current_buf()
382    test_expand_success(
383      { 'function($2)\n\t$3\nend' },
384      { 'function()', '  ', 'end' },
385      [[
386      vim.opt.sw = 2
387      vim.opt.expandtab = true
388    ]]
389    )
390    api.nvim_buf_set_lines(curbuf, 0, -1, false, {})
391    test_expand_success(
392      { 'function($2)\n$3\nend' },
393      { 'function()', '', 'end' },
394      [[
395      vim.opt.sw = 2
396      vim.opt.expandtab = true
397    ]]
398    )
399    api.nvim_buf_set_lines(curbuf, 0, -1, false, {})
400    test_expand_success(
401      { 'func main() {\n\t$1\n}' },
402      { 'func main() {', '\t', '}' },
403      [[
404      vim.opt.sw = 4
405      vim.opt.ts = 4
406      vim.opt.expandtab = false
407    ]]
408    )
409    api.nvim_buf_set_lines(curbuf, 0, -1, false, {})
410    test_expand_success(
411      { '${1:name} :: ${2}\n${1:name} ${3}= ${0:undefined}' },
412      {
413        'name :: ',
414        'name = undefined',
415      },
416      [[
417      vim.opt.sw = 4
418      vim.opt.ts = 4
419      vim.opt.expandtab = false
420    ]]
421    )
422  end)
423
424  it('correct visual selection with multi-byte text', function()
425    test_expand_success({ 'function(${1:var})' }, { '口口function(var)' }, nil, '口口')
426    feed('foo')
427    eq({ '口口function(foo)' }, buf_lines(0))
428  end)
429
430  it('supports multiple snippet sessions', function()
431    test_expand_success({ 'func $1($3) {', '  $2', '}' }, { 'func () {', '  ', '}' })
432
433    feed('foo<Tab>')
434
435    test_expand_success({ 'var x = $1 + $2' }, { 'func foo() {', '  var x =  + ', '}' })
436
437    feed('a<Tab>b')
438
439    feed('<Tab><Tab>a, b')
440
441    eq({ 'func foo(a, b) {', '  var x = a + b', '}' }, buf_lines(0))
442  end)
443end)