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