robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local t_lsp =require ( 'test.functional.plugin.lsp.testutil' ) 4local Screen =require ( 'test.functional.ui.screen' ) 5 6local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 7local dedent =t . dedent 8local eq =t . eq 9 10local api =n . api 11local exec_lua =n . exec_lua 12local insert =n . insert 13local feed =n . feed 14 15local clear_notrace =t_lsp . clear_notrace 16local create_server_definition =t_lsp . create_server_definition 17 18describe ( 'vim.lsp.inline_completion' , function () 19local text =dedent ( [[ 20function fibonacci() 21]] ) 22 23local grid_without_candidates =dedent ( [[ 24function fibonacci() | 25^ | 26{1:~ }|*11 27| 28]] ) 29 30local grid_with_candidates =dedent ( [[ 31function fibonacci({1:n) {} | 32{1: if (n <= 0) return 0;} | 33{1: if (n === 1) return 1;} | 34| 35{1: let a = 0, b = 1, c;} | 36{1: for (let i = 2; i <= n; i++) {} | 37{1: c = a + b;} | 38{1: a = b;} | 39{1: b = c;} | 40{1: }} | 41{1: return b;} | 42{1:}} | 43^ | 44{3:-- INSERT --} | 45]] ) 46 47local grid_applied_candidates =dedent ( [[ 48function fibonacci(n) { | 49if (n <= 0) return 0; | 50if (n === 1) return 1; | 51| 52let a = 0, b = 1, c; | 53for (let i = 2; i <= n; i++) { | 54c = a + b; | 55a = b; | 56b = c; | 57} | 58return b; | 59^} | 60|*2 61]] ) 62 63--- @type test.functional.ui.screen 64local screen 65 66--- @type integer 67local client_id 68 69before_each ( function () 70clear_notrace () 71exec_lua ( create_server_definition ) 72 73screen =Screen . new () 74screen : set_default_attr_ids ( { 75[ 1 ] ={ bold =true , foreground =Screen . colors . Blue1 } , 76[ 2 ] ={ bold =true , foreground =Screen . colors . SeaGreen4 } , 77[ 3 ] ={ bold =true } , 78} ) 79 80client_id =exec_lua ( function () 81_G . server =_G . _create_server ( { 82capabilities ={ 83inlineCompletionProvider =true , 84} , 85handlers ={ 86[ 'textDocument/inlineCompletion' ] =function ( _ , _ , callback ) 87if _G . items then 88callback ( nil , { items =_G . items } ) 89return 90end 91 92if _G . empty then 93callback ( nil , { 94items ={ 95{ 96insertText ='foobar' , 97range ={ 98start ={ 99line =0 , 100character =19 , 101} , 102[ 'end' ] ={ 103line =0 , 104character =19 , 105} , 106} , 107} , 108} , 109} ) 110return 111end 112 113callback ( nil , { 114items ={ 115{ 116command ={ 117command ='dummy' , 118title ='Completion Accepted' , 119} , 120insertText ='function fibonacci(n) {\n if (n <= 0) return 0;\n if (n === 1) return 1;\n\n let a = 0, b = 1, c;\n for (let i = 2; i <= n; i++) {\n c = a + b;\n a = b;\n b = c;\n }\n return b;\n}' , 121range ={ 122[ 'end' ] ={ 123character =20 , 124line =0 , 125} , 126start ={ 127character =0 , 128line =0 , 129} , 130} , 131} , 132{ 133command ={ 134command ='dummy' , 135title ='Completion Accepted' , 136} , 137insertText ='function fibonacci(n) {\n if (n <= 0) return 0;\n if (n === 1) return 1;\n\n let a = 0, b = 1, c;\n for (let i = 2; i <= n; i++) {\n c = a + b;\n a = b;\n b = c;\n }\n return c;\n}' , 138range ={ 139[ 'end' ] ={ 140character =20 , 141line =0 , 142} , 143start ={ 144character =0 , 145line =0 , 146} , 147} , 148} , 149{ 150command ={ 151command ='dummy' , 152title ='Completion Accepted' , 153} , 154insertText ='function fibonacci(n) {\n if (n < 0) {\n throw new Error("Input must be a non-negative integer.");\n }\n if (n === 0) return 0;\n if (n === 1) return 1;\n\n let a = 0, b = 1, c;\n for (let i = 2; i <= n; i++) {\n c = a + b;\n a = b;\n b = c;\n }\n return b;\n}' , 155range ={ 156[ 'end' ] ={ 157character =20 , 158line =0 , 159} , 160start ={ 161character =0 , 162line =0 , 163} , 164} , 165} , 166} , 167} ) 168end , 169} , 170} ) 171 172return vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 173end ) 174 175exec_lua ( function () 176local client =assert ( vim . lsp . get_client_by_id ( client_id )) 177_G . called =false 178client . commands . dummy =function () 179_G . called =true 180end 181end ) 182 183insert ( text ) 184feed ( '$' ) 185exec_lua ( function () 186vim . lsp . inline_completion . enable () 187end ) 188end ) 189 190after_each ( function () 191api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 192end ) 193 194describe ( 'enable()' , function () 195it ( 'requests or abort when entered/left insert mode' , function () 196screen : expect ( { grid =grid_without_candidates } ) 197feed ( 'i' ) 198screen : expect ( { grid =grid_with_candidates } ) 199feed ( '<Esc>' ) 200screen : expect ( { grid =grid_without_candidates } ) 201end ) 202 203it ( 'no request when leaving insert mode immediately after typing' , function () 204screen : expect ( { grid =grid_without_candidates } ) 205feed ( 'ifoobar<Esc>' ) 206screen : expect ( [[ 207function fibonacci() | 208fooba^r | 209{1:~ }|*11 210| 211]] ) 212screen : expect_unchanged ( false , 500 ) 213end ) 214end ) 215 216describe ( 'get()' , function () 217it ( 'applies the current candidate' , function () 218feed ( 'i' ) 219screen : expect ( { grid =grid_with_candidates } ) 220exec_lua ( function () 221vim . lsp . inline_completion . get () 222end ) 223n . poke_eventloop () 224feed ( '<Esc>' ) 225screen : expect ( { grid =grid_applied_candidates } ) 226end ) 227 228it ( 'correctly displays with absent/empty range' , function () 229exec_lua ( function () 230_G . empty =true 231end ) 232feed ( 'I' ) 233screen : expect ( [[ 234function fibonacci({1:foobar}) | 235^ | 236{1:~ }|*11 237{3:-- INSERT --} | 238]] ) 239end ) 240 241it ( 'accepts on_accept callback' , function () 242feed ( 'i' ) 243screen : expect ( { grid =grid_with_candidates } ) 244local result =exec_lua ( function () 245---@type vim.lsp.inline_completion.Item 246local result 247vim . lsp . inline_completion . get ( { 248on_accept =function ( item ) 249result =item 250end , 251} ) 252vim . wait ( 1000 , function () 253return result ~= nil 254end ) -- Wait for async callback. 255return result 256end ) 257feed ( '<Esc>' ) 258screen : expect ( { grid =grid_without_candidates } ) 259eq ( { 260_index =1 , 261client_id =1 , 262command ={ 263command ='dummy' , 264title ='Completion Accepted' , 265} , 266insert_text =dedent ( [[ 267function fibonacci(n) { 268if (n <= 0) return 0; 269if (n === 1) return 1; 270 271let a = 0, b = 1, c; 272for (let i = 2; i <= n; i++) { 273c = a + b; 274a = b; 275b = c; 276} 277return b; 278}]] ), 279range ={ 2800 , 2810 , 2820 , 28320 , 2841 , 285} , 286} , result ) 287end ) 288 289it ( 'accepts an item after the line shrank past its range' , function () 290feed ( 'i' ) 291screen : expect ( { grid =grid_with_candidates } ) 292exec_lua ( function () 293-- Shrink the line the item's range covers, as backspacing would. 294vim . api . nvim_buf_set_text ( 0 , 0 , 18 , 0 , 20 , {} ) 295vim . lsp . inline_completion . get () 296end ) 297n . poke_eventloop () 298feed ( '<Esc>' ) 299screen : expect ( { grid =grid_applied_candidates } ) 300end ) 301 302it ( 'does not leave behind text typed since the request' , function () 303exec_lua ( function () 304_G . items ={ 305{ 306insertText ='foobar' , 307range ={ 308start ={ line =1 , character =0 } , 309[ 'end' ] ={ line =1 , character =1 } , 310} , 311} , 312} 313end ) 314feed ( 'ifo' ) 315screen : expect ( [[ 316function fibonacci() | 317fo{1:^obar} | 318{1:~ }|*11 319{3:-- INSERT --} | 320]] ) 321exec_lua ( function () 322vim . lsp . inline_completion . get () 323end ) 324n . poke_eventloop () 325feed ( '<Esc>' ) 326screen : expect ( [[ 327function fibonacci() | 328fooba^r | 329{1:~ }|*11 330| 331]] ) 332end ) 333end ) 334 335describe ( 'select()' , function () 336it ( 'selects the next candidate' , function () 337feed ( 'i' ) 338screen : expect ( { grid =grid_with_candidates } ) 339 340exec_lua ( function () 341vim . lsp . inline_completion . select () 342end ) 343 344screen : expect ( [[ 345function fibonacci({1:n) {} | 346{1: if (n <= 0) return 0;} | 347{1: if (n === 1) return 1;} | 348| 349{1: let a = 0, b = 1, c;} | 350{1: for (let i = 2; i <= n; i++) {} | 351{1: c = a + b;} | 352{1: a = b;} | 353{1: b = c;} | 354{1: }} | 355{1: return c;} | 356{1:}}{2: (2/3)} | 357^ | 358{3:-- INSERT --} | 359]] ) 360exec_lua ( function () 361vim . lsp . inline_completion . get () 362end ) 363n . poke_eventloop () 364feed ( '<Esc>' ) 365screen : expect ( [[ 366function fibonacci(n) { | 367if (n <= 0) return 0; | 368if (n === 1) return 1; | 369| 370let a = 0, b = 1, c; | 371for (let i = 2; i <= n; i++) { | 372c = a + b; | 373a = b; | 374b = c; | 375} | 376return c; | 377^} | 378|*2 379]] ) 380end ) 381end ) 382end )