2023-10-20 23:51:26 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
|
|
|
|
2023-11-11 19:07:46 -07:00
|
|
|
local buf_lines = helpers.buf_lines
|
2023-10-20 23:51:26 -07:00
|
|
|
local clear = helpers.clear
|
|
|
|
local eq = helpers.eq
|
|
|
|
local exec_lua = helpers.exec_lua
|
|
|
|
local feed = helpers.feed
|
|
|
|
local matches = helpers.matches
|
|
|
|
local pcall_err = helpers.pcall_err
|
2024-01-12 04:41:09 -07:00
|
|
|
local sleep = vim.uv.sleep
|
2023-10-20 23:51:26 -07:00
|
|
|
|
|
|
|
describe('vim.snippet', function()
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
|
|
|
|
exec_lua([[
|
|
|
|
vim.keymap.set({ 'i', 's' }, '<Tab>', function() vim.snippet.jump(1) end, { buffer = true })
|
|
|
|
vim.keymap.set({ 'i', 's' }, '<S-Tab>', function() vim.snippet.jump(-1) end, { buffer = true })
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
after_each(clear)
|
|
|
|
|
|
|
|
--- @param snippet string[]
|
|
|
|
--- @param expected string[]
|
|
|
|
--- @param settings? string
|
|
|
|
--- @param prefix? string
|
2023-11-11 19:07:46 -07:00
|
|
|
local function test_expand_success(snippet, expected, settings, prefix)
|
2023-10-20 23:51:26 -07:00
|
|
|
if settings then
|
|
|
|
exec_lua(settings)
|
|
|
|
end
|
|
|
|
if prefix then
|
|
|
|
feed('i' .. prefix)
|
|
|
|
end
|
|
|
|
exec_lua('vim.snippet.expand(...)', table.concat(snippet, '\n'))
|
2023-11-11 19:07:46 -07:00
|
|
|
eq(expected, buf_lines(0))
|
2023-10-20 23:51:26 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
--- @param snippet string
|
|
|
|
--- @param err string
|
2023-11-11 19:07:46 -07:00
|
|
|
local function test_expand_fail(snippet, err)
|
2023-10-20 23:51:26 -07:00
|
|
|
matches(err, pcall_err(exec_lua, string.format('vim.snippet.expand("%s")', snippet)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it('adds base indentation to inserted text', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success(
|
2023-10-20 23:51:26 -07:00
|
|
|
{ 'function $1($2)', ' $0', 'end' },
|
|
|
|
{ ' function ()', ' ', ' end' },
|
|
|
|
'',
|
|
|
|
' '
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
2023-10-22 22:38:11 -07:00
|
|
|
it('adds indentation based on the start of snippet lines', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'if $1 then', ' $0', 'end' }, { 'if then', ' ', 'end' })
|
2023-10-22 22:38:11 -07:00
|
|
|
end)
|
|
|
|
|
2023-10-20 23:51:26 -07:00
|
|
|
it('replaces tabs with spaces when expandtab is set', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success(
|
2023-10-20 23:51:26 -07:00
|
|
|
{ 'function $1($2)', '\t$0', 'end' },
|
|
|
|
{ 'function ()', ' ', 'end' },
|
|
|
|
[[
|
|
|
|
vim.o.expandtab = true
|
|
|
|
vim.o.shiftwidth = 2
|
|
|
|
]]
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('respects tabs when expandtab is not set', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success(
|
2023-10-20 23:51:26 -07:00
|
|
|
{ 'function $1($2)', '\t$0', 'end' },
|
|
|
|
{ 'function ()', '\t', 'end' },
|
|
|
|
'vim.o.expandtab = false'
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('inserts known variable value', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ '; print($TM_CURRENT_LINE)' }, { 'foo; print(foo)' }, nil, 'foo')
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('uses default when variable is not set', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'print(${TM_CURRENT_WORD:foo})' }, { 'print(foo)' })
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('replaces unknown variables by placeholders', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'print($UNKNOWN)' }, { 'print(UNKNOWN)' })
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('does not jump outside snippet range', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'function $1($2)', ' $0', 'end' }, { 'function ()', ' ', 'end' })
|
2023-10-20 23:51:26 -07:00
|
|
|
eq(false, exec_lua('return vim.snippet.jumpable(-1)'))
|
|
|
|
feed('<Tab><Tab>i')
|
|
|
|
eq(false, exec_lua('return vim.snippet.jumpable(1)'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('navigates backwards', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'function $1($2) end' }, { 'function () end' })
|
2023-10-20 23:51:26 -07:00
|
|
|
feed('<Tab><S-Tab>foo')
|
2023-11-11 19:07:46 -07:00
|
|
|
eq({ 'function foo() end' }, buf_lines(0))
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('visits all tabstops', function()
|
|
|
|
local function cursor()
|
|
|
|
return exec_lua('return vim.api.nvim_win_get_cursor(0)')
|
|
|
|
end
|
|
|
|
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'function $1($2)', ' $0', 'end' }, { 'function ()', ' ', 'end' })
|
2023-10-20 23:51:26 -07:00
|
|
|
eq({ 1, 9 }, cursor())
|
|
|
|
feed('<Tab>')
|
|
|
|
eq({ 1, 10 }, cursor())
|
|
|
|
feed('<Tab>')
|
|
|
|
eq({ 2, 2 }, cursor())
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('syncs text of tabstops with equal indexes', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'var double = ${1:x} + ${1:x}' }, { 'var double = x + x' })
|
2023-10-20 23:51:26 -07:00
|
|
|
feed('123')
|
2023-11-11 19:07:46 -07:00
|
|
|
eq({ 'var double = 123 + 123' }, buf_lines(0))
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('cancels session with changes outside the snippet', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'print($1)' }, { 'print()' })
|
2023-10-20 23:51:26 -07:00
|
|
|
feed('<Esc>O-- A comment')
|
|
|
|
eq(false, exec_lua('return vim.snippet.active()'))
|
2023-11-11 19:07:46 -07:00
|
|
|
eq({ '-- A comment', 'print()' }, buf_lines(0))
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('handles non-consecutive tabstops', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'class $1($3) {', ' $0', '}' }, { 'class () {', ' ', '}' })
|
2023-10-20 23:51:26 -07:00
|
|
|
feed('Foo') -- First tabstop
|
|
|
|
feed('<Tab><Tab>') -- Jump to $0
|
|
|
|
feed('// Inside') -- Insert text
|
2023-11-11 19:07:46 -07:00
|
|
|
eq({ 'class Foo() {', ' // Inside', '}' }, buf_lines(0))
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('handles multiline placeholders', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success(
|
2023-10-20 23:51:26 -07:00
|
|
|
{ 'public void foo() {', ' ${0:// TODO Auto-generated', ' throw;}', '}' },
|
|
|
|
{ 'public void foo() {', ' // TODO Auto-generated', ' throw;', '}' }
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('inserts placeholder in all tabstops when the first tabstop has the placeholder', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success(
|
2023-10-20 23:51:26 -07:00
|
|
|
{ 'for (${1:int} ${2:x} = ${3:0}; $2 < ${4:N}; $2++) {', ' $0', '}' },
|
|
|
|
{ 'for (int x = 0; x < N; x++) {', ' ', '}' }
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('inserts placeholder in all tabstops when a later tabstop has the placeholder', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success(
|
2023-10-20 23:51:26 -07:00
|
|
|
{ 'for (${1:int} $2 = ${3:0}; ${2:x} < ${4:N}; $2++) {', ' $0', '}' },
|
|
|
|
{ 'for (int x = 0; x < N; x++) {', ' ', '}' }
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('errors with multiple placeholders for the same index', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
test_expand_fail(
|
|
|
|
'class ${1:Foo} { void ${1:foo}() {} }',
|
|
|
|
'multiple placeholders for tabstop $1'
|
|
|
|
)
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('errors with multiple $0 tabstops', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_fail('function $1() { $0 }$0', 'multiple $0 tabstops')
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|
2023-10-22 21:21:02 -07:00
|
|
|
|
|
|
|
it('cancels session when deleting the snippet', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
test_expand_success(
|
|
|
|
{ 'local function $1()', ' $0', 'end' },
|
|
|
|
{ 'local function ()', ' ', 'end' }
|
|
|
|
)
|
2023-10-22 21:21:02 -07:00
|
|
|
feed('<esc>Vjjd')
|
|
|
|
eq(false, exec_lua('return vim.snippet.active()'))
|
|
|
|
end)
|
2023-10-25 22:29:05 -07:00
|
|
|
|
2023-10-30 04:58:28 -07:00
|
|
|
it('cancels session when inserting outside snippet region', function()
|
2023-10-25 22:29:05 -07:00
|
|
|
feed('i<cr>')
|
2024-01-02 18:09:18 -07:00
|
|
|
test_expand_success(
|
|
|
|
{ 'local function $1()', ' $0', 'end' },
|
|
|
|
{ '', 'local function ()', ' ', 'end' }
|
|
|
|
)
|
2023-10-30 04:58:28 -07:00
|
|
|
feed('<esc>O-- A comment')
|
2023-10-25 22:29:05 -07:00
|
|
|
eq(false, exec_lua('return vim.snippet.active()'))
|
|
|
|
end)
|
2023-10-26 22:53:38 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
it('inserts choice', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'console.${1|assert,log,error|}()' }, { 'console.()' })
|
2023-10-26 22:53:38 -07:00
|
|
|
sleep(100)
|
|
|
|
feed('<Down><C-y>')
|
2023-11-11 19:07:46 -07:00
|
|
|
eq({ 'console.log()' }, buf_lines(0))
|
2023-10-26 22:53:38 -07:00
|
|
|
end)
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
it('closes the choice completion menu when jumping', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success({ 'console.${1|assert,log,error|}($2)' }, { 'console.()' })
|
2023-10-26 22:53:38 -07:00
|
|
|
sleep(100)
|
|
|
|
exec_lua('vim.snippet.jump(1)')
|
|
|
|
eq(0, exec_lua('return vim.fn.pumvisible()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('jumps to next tabstop after inserting choice', function()
|
2023-11-11 19:07:46 -07:00
|
|
|
test_expand_success(
|
2023-10-26 22:53:38 -07:00
|
|
|
{ '${1|public,protected,private|} function ${2:name}() {', '\t$0', '}' },
|
|
|
|
{ ' function name() {', '\t', '}' }
|
|
|
|
)
|
|
|
|
sleep(100)
|
|
|
|
feed('<C-y><Tab>')
|
|
|
|
sleep(10)
|
|
|
|
feed('foo')
|
2023-11-11 19:07:46 -07:00
|
|
|
eq({ 'public function foo() {', '\t', '}' }, buf_lines(0))
|
2023-10-26 22:53:38 -07:00
|
|
|
end)
|
2023-10-20 23:51:26 -07:00
|
|
|
end)
|