robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Insert-mode tests. 2 3local t =require ( 'test.testutil' ) 4local n =require ( 'test.functional.testnvim' )() 5local Screen =require ( 'test.functional.ui.screen' ) 6 7local describe , it , before_each =t . describe , t . it , t . before_each 8local clear , feed , insert =n . clear , n . feed , n . insert 9local expect =n . expect 10local command =n . command 11local eq =t . eq 12local eval =n . eval 13local curbuf_contents =n . curbuf_contents 14local api =n . api 15 16describe ( 'insert-mode' , function () 17before_each ( function () 18clear () 19end ) 20 21it ( 'indents only once after "!" keys #12894' , function () 22command ( 'let counter = []' ) 23command ( 'set indentexpr=len(add(counter,0))' ) 24feed ( 'i<C-F>x' ) 25eq ( ' x' , curbuf_contents ()) 26end ) 27 28it ( 'indent works properly with autocompletion enabled #35381' , function () 29command ( 'set autoindent cindent autocomplete' ) 30feed ( 'ivoid func(void) {<CR>' ) 31expect ( 'void func(void) {\n\t' ) 32feed ( '}' ) 33expect ( 'void func(void) {\n}' ) 34end ) 35 36it ( 'CTRL-@' , function () 37-- Inserts last-inserted text, leaves insert-mode. 38insert ( 'hello' ) 39feed ( 'i<C-@>x' ) 40expect ( 'hellhello' ) 41 42-- C-Space is the same as C-@. 43-- CTRL-SPC inserts last-inserted text, leaves insert-mode. 44feed ( 'i<C-Space>x' ) 45expect ( 'hellhellhello' ) 46 47-- CTRL-A inserts last inserted text 48feed ( 'i<C-A>x' ) 49expect ( 'hellhellhellhelloxo' ) 50end ) 51 52it ( 'InsertCharPre is not triggered for stuffed text (redo/dot-repeat) #25296' , function () 53n . exec_lua ( [[ 54_G.n = 0 55vim.api.nvim_create_autocmd('InsertCharPre', { 56callback = function() 57_G.n = _G.n + 1 58if vim.v.char == '(' then 59vim.v.char = '()' 60end 61end, 62}) 63]] ) 64local function calls () 65return n . exec_lua ( 'return _G.n' ) 66end 67api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'a' , 'b' , 'c' , 'd' } ) 68feed ( 'gg0i(<Esc>' ) 69eq ( 1 , calls ()) 70 71-- Dot-repeat ("redo") inserts literally (no InsertCharPre). 72feed ( 'j.' ) 73expect ( [[ 74()a 75()b 76c 77d]] ) 78eq ( 1 , calls ()) 79 80-- i_CTRL-R, i_CTRL-A inserts as stuffed text (no InsertCharPre). 81n . fn . setreg ( 'a' , '(' ) 82feed ( 'ji<C-R>a<Esc>' ) 83feed ( 'ji<C-A><Esc>' ) 84expect ( [[ 85()a 86()b 87(c 88(d]] ) 89eq ( 1 , calls ()) 90 91-- Macro keys are typeahead, not stuffed, thus trigger InsertCharPre. 92n . fn . setreg ( 'q' , 'A(\27' ) 93feed ( 'gg@q' ) 94expect ( [[ 95()a() 96()b 97(c 98(d]] ) 99eq ( 2 , calls ()) 100end ) 101 102describe ( 'Ctrl-R' , function () 103it ( 'works' , function () 104command ( "let @@ = 'test'" ) 105feed ( 'i<C-r>"' ) 106expect ( 'test' ) 107end ) 108 109it ( 'works with multi-byte text' , function () 110command ( "let @@ = 'påskägg'" ) 111feed ( 'i<C-r>"' ) 112expect ( 'påskägg' ) 113end ) 114 115it ( 'double quote is removed after hit-enter prompt #22609' , function () 116local screen =Screen . new ( 50 , 6 ) 117feed ( 'i<C-R>' ) 118screen : expect ( [[ 119{18:^"} | 120{1:~ }|*4 121{5:-- INSERT --} | 122]] ) 123feed ( "=function('add')" ) 124screen : expect ( [[ 125{18:"} | 126{1:~ }|*4 127={25:function}{16:(}{26:'add'}{16:)}^ | 128]] ) 129feed ( '<CR>' ) 130screen : expect ( [[ 131{18:"} | 132{1:~ }| 133{3: }| 134={25:function}{16:(}{26:'add'}{16:)} | 135{9:E729: Using a Funcref as a String} | 136{6:Press ENTER or type command to continue}^ | 137]] ) 138feed ( '<CR>' ) 139screen : expect ( [[ 140^ | 141{1:~ }|*4 142{5:-- INSERT --} | 143]] ) 144end ) 145 146it ( 'inserts named/clipboard registers literally' , function () 147local screen =Screen . new ( 50 , 6 ) 148-- regular text without special character command 149command ( 'let @a = "test"' ) 150feed ( 'i<C-R>a<ESC>' ) 151screen : expect ( [[ 152tes^t | 153{1:~ }|*4 154| 155]] ) 156 157-- text with backspace character gets written literally by default 158command ( 'let @a = "test\\<C-H>"' ) 159feed ( 'cc<C-R>a<ESC>' ) 160screen : expect ( [[ 161test{18:^^H} | 162{1:~ }|*4 163| 164]] ) 165 166-- =@reg<CR> can be used to get effect of keypress 167command ( 'let @a = "test\\<C-H>"' ) 168feed ( 'cc<C-R>=@a<CR><ESC>' ) 169screen : expect ( [[ 170te^s | 171{1:~ }|*4 172| 173]] ) 174end ) 175end ) 176 177describe ( 'Ctrl-O' , function () 178it ( 'enters command mode for one command' , function () 179feed ( 'ihello world<C-o>' ) 180feed ( ':let ctrlo = "test"<CR>' ) 181feed ( 'iii' ) 182expect ( 'hello worldiii' ) 183eq ( 1 , eval ( 'ctrlo ==# "test"' )) 184end ) 185 186it ( 're-enters insert mode at the end of the line when running startinsert' , function () 187-- #6962 188feed ( 'ihello world<C-o>' ) 189feed ( ':startinsert<CR>' ) 190feed ( 'iii' ) 191expect ( 'hello worldiii' ) 192end ) 193 194it ( 're-enters insert mode at the beginning of the line when running startinsert' , function () 195insert ( 'hello world' ) 196feed ( '0<C-o>' ) 197feed ( ':startinsert<CR>' ) 198feed ( 'aaa' ) 199expect ( 'aaahello world' ) 200end ) 201 202it ( 're-enters insert mode in the middle of the line when running startinsert' , function () 203insert ( 'hello world' ) 204feed ( 'bi<C-o>' ) 205feed ( ':startinsert<CR>' ) 206feed ( 'ooo' ) 207expect ( 'hello oooworld' ) 208end ) 209end ) 210 211describe ( 'Ctrl-V' , function () 212it ( 'supports entering the decimal value of a character' , function () 213feed ( 'i<C-V>076<C-V>167' ) 214expect ( 'L§' ) 215end ) 216 217it ( 'supports entering the octal value of a character with "o"' , function () 218feed ( 'i<C-V>o114<C-V>o247<Esc>' ) 219expect ( 'L§' ) 220end ) 221 222it ( 'supports entering the octal value of a character with "O"' , function () 223feed ( 'i<C-V>O114<C-V>O247<Esc>' ) 224expect ( 'L§' ) 225end ) 226 227it ( 'supports entering the hexadecimal value of a character with "x"' , function () 228feed ( 'i<C-V>x4c<C-V>xA7<Esc>' ) 229expect ( 'L§' ) 230end ) 231 232it ( 'supports entering the hexadecimal value of a character with "X"' , function () 233feed ( 'i<C-V>X4c<C-V>XA7<Esc>' ) 234expect ( 'L§' ) 235end ) 236 237it ( 'supports entering the hexadecimal value of a character with "u"' , function () 238feed ( 'i<C-V>u25ba<C-V>u25C7<Esc>' ) 239expect ( '►◇' ) 240end ) 241 242it ( 'supports entering the hexadecimal value of a character with "U"' , function () 243feed ( 'i<C-V>U0001f600<C-V>U0001F601<Esc>' ) 244expect ( '😀😁' ) 245end ) 246 247it ( 'entering character by value is interrupted by invalid character' , function () 248feed ( 'i<C-V>76c<C-V>76<C-F2><C-V>u3c0j<C-V>u3c0<M-F3><C-V>U1f600j<C-V>U1f600<D-F4><Esc>' ) 249expect ( 'LcL<C-F2>πjπ<M-F3>😀j😀<D-F4>' ) 250end ) 251 252it ( 'shows o, O, u, U, x, X, and digits with modifiers' , function () 253feed ( 'i<C-V><M-o><C-V><D-o><C-V><M-O><C-V><D-O><Esc>' ) 254expect ( '<M-o><D-o><M-O><D-O>' ) 255feed ( 'cc<C-V><M-u><C-V><D-u><C-V><M-U><C-V><D-U><Esc>' ) 256expect ( '<M-u><D-u><M-U><D-U>' ) 257feed ( 'cc<C-V><M-x><C-V><D-x><C-V><M-X><C-V><D-X><Esc>' ) 258expect ( '<M-x><D-x><M-X><D-X>' ) 259feed ( 'cc<C-V><M-1><C-V><D-2><C-V><M-7><C-V><D-8><Esc>' ) 260expect ( '<M-1><D-2><M-7><D-8>' ) 261end ) 262end ) 263 264it ( 'Ctrl-Shift-V supports entering unsimplified key notations' , function () 265feed ( 'i<C-S-V><C-J><C-S-V><C-@><C-S-V><C-[><C-S-V><C-S-M><C-S-V><M-C-I><C-S-V><C-D-J><Esc>' ) 266expect ( '<C-J><C-@><C-[><C-S-M><M-C-I><C-D-J>' ) 267end ) 268 269it ( 'multi-char mapping updates screen properly #25626' , function () 270local screen =Screen . new ( 60 , 6 ) 271command ( 'vnew' ) 272insert ( 'foo\nfoo\nfoo' ) 273command ( 'wincmd w' ) 274command ( 'set timeoutlen=10000' ) 275command ( 'inoremap jk <Esc>' ) 276feed ( 'i<CR>βββ<Left><Left>j' ) 277screen : expect { 278grid =[[ 279foo │ | 280foo │β^jβ | 281foo │{1:~ }| 282{1:~ }│{1:~ }| 283{2:[No Name] [+] }{3:[No Name] [+] }| 284{5:-- INSERT --} | 285]] , 286} 287feed ( 'k' ) 288screen : expect { 289grid =[[ 290foo │ | 291foo │^βββ | 292foo │{1:~ }| 293{1:~ }│{1:~ }| 294{2:[No Name] [+] }{3:[No Name] [+] }| 295| 296]] , 297} 298end ) 299 300describe ( 'backspace' , function () 301local function set_lines ( line_b , line_e , ... ) 302api . nvim_buf_set_lines ( 0 , line_b , line_e , true , { ... } ) 303end 304local function s ( count ) 305return ( ' ' ): rep ( count ) 306end 307 308local function test_cols ( expected_cols ) 309local cols ={ { n . fn . col ( '.' ), n . fn . virtcol ( '.' ) } } 310for _ =2 , # expected_cols do 311feed ( '<BS>' ) 312table . insert ( cols , { n . fn . col ( '.' ), n . fn . virtcol ( '.' ) } ) 313end 314eq ( expected_cols , cols ) 315end 316 317it ( 'works with tabs and spaces' , function () 318local _ =Screen . new ( 30 , 2 ) 319command ( 'setl ts=4 sw=4' ) 320set_lines ( 0 , 1 , '\t' .. s ( 4 ) .. '\t' .. s ( 9 ) .. '\t a' ) 321feed ( '$i' ) 322test_cols ( { 323{ 18 , 26 } , 324{ 17 , 25 } , 325{ 15 , 21 } , 326{ 11 , 17 } , 327{ 7 , 13 } , 328{ 6 , 9 } , 329{ 2 , 5 } , 330{ 1 , 1 } , 331} ) 332end ) 333 334it ( 'works with varsofttabstop' , function () 335local _ =Screen . new ( 30 , 2 ) 336command ( 'setl vsts=6,2,5,3' ) 337set_lines ( 0 , 1 , 'a\t' .. s ( 4 ) .. '\t a' ) 338feed ( '$i' ) 339test_cols ( { 340{ 9 , 18 } , 341{ 8 , 17 } , 342{ 8 , 14 } , 343{ 3 , 9 } , 344{ 7 , 7 } , 345{ 2 , 2 } , 346{ 1 , 1 } , 347} ) 348end ) 349 350it ( 'works with tab as ^I' , function () 351local _ =Screen . new ( 30 , 2 ) 352command ( 'set list listchars=space:.' ) 353command ( 'setl ts=4 sw=4' ) 354set_lines ( 0 , 1 , '\t' .. s ( 4 ) .. '\t' .. s ( 9 ) .. '\t a' ) 355feed ( '$i' ) 356test_cols ( { 357{ 18 , 21 } , 358{ 15 , 17 } , 359{ 11 , 13 } , 360{ 7 , 9 } , 361{ 4 , 5 } , 362{ 1 , 1 } , 363} ) 364end ) 365 366it ( 'works in replace mode' , function () 367local _ =Screen . new ( 50 , 2 ) 368command ( 'setl ts=8 sw=8 sts=8' ) 369set_lines ( 0 , 1 , '\t' .. s ( 4 ) .. '\t' .. s ( 9 ) .. '\t a' ) 370feed ( '$R' ) 371test_cols ( { 372{ 18 , 34 } , 373{ 17 , 33 } , 374{ 15 , 25 } , 375{ 7 , 17 } , 376{ 2 , 9 } , 377{ 1 , 8 } , -- last screen cell of first tab is at vcol 8 378} ) 379end ) 380 381it ( 'works with breakindent' , function () 382local _ =Screen . new ( 17 , 4 ) 383command ( 'setl ts=4 sw=4 bri briopt=min:5' ) 384set_lines ( 0 , 1 , '\t' .. s ( 4 ) .. '\t' .. s ( 9 ) .. '\t a' ) 385feed ( '$i' ) 386test_cols ( { 387{ 18 , 50 } , 388{ 17 , 49 } , 389{ 15 , 33 } , 390{ 11 , 17 } , 391{ 7 , 13 } , 392{ 6 , 9 } , 393{ 2 , 5 } , 394{ 1 , 1 } , 395} ) 396end ) 397 398it ( 'works with inline virtual text' , function () 399local _ =Screen . new ( 50 , 2 ) 400command ( 'setl ts=4 sw=4' ) 401set_lines ( 0 , 1 , '\t' .. s ( 4 ) .. '\t' .. s ( 9 ) .. '\t a' ) 402local ns =api . nvim_create_namespace ( '' ) 403local vt_opts ={ virt_text ={ { 'text' } } , virt_text_pos ='inline' } 404api . nvim_buf_set_extmark ( 0 , ns , 0 , 2 , vt_opts ) 405feed ( '$i' ) 406test_cols ( { 407{ 18 , 30 } , 408{ 17 , 29 } , 409{ 15 , 25 } , 410{ 11 , 21 } , 411{ 7 , 17 } , 412{ 6 , 13 } , 413{ 2 , 9 } , 414{ 1 , 5 } , 415} ) 416end ) 417 418it ( "works with 'revins'" , function () 419local _ =Screen . new ( 30 , 3 ) 420command ( 'setl ts=4 sw=4 revins' ) 421set_lines ( 0 , 1 , ( 'a' ): rep ( 16 ), s ( 3 ) .. '\t' .. s ( 4 ) .. '\t a' ) 422feed ( 'j$i' ) 423test_cols ( { 424{ 11 , 14 } , 425{ 10 , 13 } , 426{ 9 , 9 } , 427{ 5 , 5 } , 428{ 1 , 1 } , 429{ 1 , 1 } , -- backspace on empty line does nothing 430} ) 431eq ( 2 , api . nvim_win_get_cursor ( 0 )[ 1 ]) 432end ) 433end ) 434 435it ( 'backspace after replacing multibyte chars' , function () 436local screen =Screen . new ( 30 , 3 ) 437api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'test ȧ̟̜̝̅̚m̆̉̐̐̇̈ å' } ) 438feed ( '^Rabcdefghi' ) 439screen : expect ( [[ 440abcdefghi^ | 441{1:~ }| 442{5:-- REPLACE --} | 443]] ) 444 445feed ( '<bs>' ) 446screen : expect ( [[ 447abcdefgh^å | 448{1:~ }| 449{5:-- REPLACE --} | 450]] ) 451 452feed ( '<bs>' ) 453screen : expect ( [[ 454abcdefg^ å | 455{1:~ }| 456{5:-- REPLACE --} | 457]] ) 458 459feed ( '<bs>' ) 460screen : expect ( [[ 461abcdef^m̆̉̐̐̇̈ å | 462{1:~ }| 463{5:-- REPLACE --} | 464]] ) 465 466feed ( '<bs>' ) 467screen : expect ( [[ 468abcde^ȧ̟̜̝̅̚m̆̉̐̐̇̈ å | 469{1:~ }| 470{5:-- REPLACE --} | 471]] ) 472 473feed ( '<bs>' ) 474screen : expect ( [[ 475abcd^ ȧ̟̜̝̅̚m̆̉̐̐̇̈ å | 476{1:~ }| 477{5:-- REPLACE --} | 478]] ) 479 480feed ( '<esc>' ) 481 482api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'wow 🧑🌾🏳️⚧️x' } ) 483feed ( '^Rabcd' ) 484 485screen : expect ( [[ 486abcd^🧑🌾🏳️⚧️x | 487{1:~ }| 488{5:-- REPLACE --} | 489]] ) 490 491feed ( 'e' ) 492screen : expect ( [[ 493abcde^🏳️⚧️x | 494{1:~ }| 495{5:-- REPLACE --} | 496]] ) 497 498feed ( 'f' ) 499screen : expect ( [[ 500abcdef^x | 501{1:~ }| 502{5:-- REPLACE --} | 503]] ) 504 505feed ( '<bs>' ) 506screen : expect ( [[ 507abcde^🏳️⚧️x | 508{1:~ }| 509{5:-- REPLACE --} | 510]] ) 511 512feed ( '<bs>' ) 513screen : expect ( [[ 514abcd^🧑🌾🏳️⚧️x | 515{1:~ }| 516{5:-- REPLACE --} | 517]] ) 518 519feed ( '<bs>' ) 520screen : expect ( [[ 521abc^ 🧑🌾🏳️⚧️x | 522{1:~ }| 523{5:-- REPLACE --} | 524]] ) 525end ) 526end )