robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local t_lsp =require ( 'test.functional.plugin.lsp.testutil' ) 5 6local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 7local eq =t . eq 8local pcall_err =t . pcall_err 9local dedent =t . dedent 10local exec_lua =n . exec_lua 11local insert =n . insert 12local feed =n . feed 13local api =n . api 14 15local clear_notrace =t_lsp . clear_notrace 16local create_server_definition =t_lsp . create_server_definition 17 18describe ( 'vim.lsp.inlay_hint' , function () 19local text =dedent ( [[ 20auto add(int a, int b) { return a + b; } 21 22int main() { 23int x = 1; 24int y = 2; 25return add(x,y); 26} 27}]] ) 28 29---@type lsp.InlayHint[] 30local response ={ 31{ 32kind =1 , 33paddingLeft =false , 34paddingRight =false , 35label ='-> int' , 36position ={ character =22 , line =0 } , 37} , 38{ 39kind =2 , 40paddingLeft =false , 41paddingRight =true , 42label ='a:' , 43position ={ character =15 , line =5 } , 44} , 45{ 46kind =2 , 47paddingLeft =false , 48paddingRight =true , 49label ='b:' , 50position ={ character =17 , line =5 } , 51} , 52} 53 54local grid_without_inlay_hints =[[ 55auto add(int a, int b) { return a + b; } | 56| 57int main() { | 58int x = 1; | 59int y = 2; | 60return add(x,y); | 61} | 62^} | 63| 64]] 65 66local grid_with_inlay_hints =[[ 67auto add(int a, int b){1:-> int} { return a + b; } | 68| 69int main() { | 70int x = 1; | 71int y = 2; | 72return add({1:a:} x,{1:b:} y); | 73} | 74^} | 75| 76]] 77 78--- @type test.functional.ui.screen 79local screen 80 81--- @type integer 82local client_id 83 84--- @type integer 85local bufnr 86 87before_each ( function () 88clear_notrace () 89screen =Screen . new ( 50 , 9 ) 90 91bufnr =n . api . nvim_get_current_buf () 92exec_lua ( create_server_definition ) 93client_id =exec_lua ( function () 94_G . server =_G . _create_server ( { 95capabilities ={ 96textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 97inlayHintProvider =true , 98} , 99handlers ={ 100[ 'textDocument/inlayHint' ] =function ( _ , _ , callback ) 101callback ( nil , response ) 102end , 103} , 104} ) 105 106return vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 107end ) 108 109insert ( text ) 110exec_lua ( function () 111vim . lsp . inlay_hint . enable ( true , { bufnr =bufnr } ) 112end ) 113screen : expect ( { grid =grid_with_inlay_hints } ) 114end ) 115 116after_each ( function () 117api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 118end ) 119 120it ( 'clears inlay hints when sole client detaches' , function () 121exec_lua ( function () 122vim . lsp . get_client_by_id ( client_id ): stop () 123end ) 124screen : expect ( { grid =grid_without_inlay_hints , unchanged =true } ) 125end ) 126 127it ( 'does not clear inlay hints when one of several clients detaches' , function () 128local client_id2 =exec_lua ( function () 129_G . server2 =_G . _create_server ( { 130capabilities ={ 131textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 132inlayHintProvider =true , 133} , 134handlers ={ 135[ 'textDocument/inlayHint' ] =function ( _ , _ , callback ) 136callback ( nil , {} ) 137end , 138} , 139} ) 140return vim . lsp . start ( { name ='dummy2' , cmd =_G . server2 . cmd } ) 141end ) 142 143exec_lua ( function () 144vim . lsp . get_client_by_id ( client_id2 ): stop () 145end ) 146screen : expect ( { grid =grid_with_inlay_hints , unchanged =true } ) 147end ) 148 149describe ( 'enable()' , function () 150it ( 'validation' , function () 151t . matches ( 152'enable: expected boolean, got table' , 153t . pcall_err ( exec_lua , function () 154--- @diagnostic disable-next-line:param-type-mismatch 155vim . lsp . inlay_hint . enable ( {} , { bufnr =bufnr } ) 156end ) 157) 158t . matches ( 159'enable: expected boolean, got number' , 160t . pcall_err ( exec_lua , function () 161--- @diagnostic disable-next-line:param-type-mismatch 162vim . lsp . inlay_hint . enable ( 42 ) 163end ) 164) 165t . matches ( 166'filter: expected table, got number' , 167t . pcall_err ( exec_lua , function () 168--- @diagnostic disable-next-line:param-type-mismatch 169vim . lsp . inlay_hint . enable ( true , 42 ) 170end ) 171) 172end ) 173end ) 174 175describe ( 'clears/applies inlay hints when passed false/true/nil' , function () 176local bufnr2 --- @type integer 177before_each ( function () 178bufnr2 =exec_lua ( function () 179local bufnr2_0 =vim . api . nvim_create_buf ( true , false ) 180vim . lsp . buf_attach_client ( bufnr2_0 , client_id ) 181vim . api . nvim_win_set_buf ( 0 , bufnr2_0 ) 182return bufnr2_0 183end ) 184insert ( text ) 185screen : expect ( { grid =grid_without_inlay_hints } ) 186exec_lua ( function () 187vim . lsp . inlay_hint . enable ( true , { bufnr =bufnr2 } ) 188end ) 189screen : expect ( { grid =grid_with_inlay_hints } ) 190end ) 191 192it ( 'for one single buffer' , function () 193exec_lua ( function () 194vim . lsp . inlay_hint . enable ( false , { bufnr =bufnr } ) 195vim . api . nvim_win_set_buf ( 0 , bufnr2 ) 196end ) 197screen : expect ( { grid =grid_with_inlay_hints , unchanged =true } ) 198n . api . nvim_win_set_buf ( 0 , bufnr ) 199screen : expect ( { grid =grid_without_inlay_hints , unchanged =true } ) 200 201exec_lua ( function () 202vim . lsp . inlay_hint . enable ( true , { bufnr =bufnr } ) 203end ) 204screen : expect ( { grid =grid_with_inlay_hints , unchanged =true } ) 205 206exec_lua ( function () 207vim . lsp . inlay_hint . enable ( 208not vim . lsp . inlay_hint . is_enabled ( { bufnr =bufnr } ), 209{ bufnr =bufnr } 210) 211end ) 212screen : expect ( { grid =grid_without_inlay_hints , unchanged =true } ) 213 214exec_lua ( function () 215vim . lsp . inlay_hint . enable ( true , { bufnr =bufnr } ) 216end ) 217screen : expect ( { grid =grid_with_inlay_hints , unchanged =true } ) 218end ) 219 220it ( 'for all buffers' , function () 221exec_lua ( function () 222vim . lsp . inlay_hint . enable ( false ) 223end ) 224screen : expect ( { grid =grid_without_inlay_hints , unchanged =true } ) 225n . api . nvim_win_set_buf ( 0 , bufnr2 ) 226screen : expect ( { grid =grid_without_inlay_hints , unchanged =true } ) 227 228exec_lua ( function () 229vim . lsp . inlay_hint . enable ( true ) 230end ) 231screen : expect ( { grid =grid_with_inlay_hints , unchanged =true } ) 232n . api . nvim_win_set_buf ( 0 , bufnr ) 233screen : expect ( { grid =grid_with_inlay_hints , unchanged =true } ) 234end ) 235end ) 236 237describe ( 'get()' , function () 238it ( 'returns filtered inlay hints' , function () 239local expected2 ={ 240kind =1 , 241paddingLeft =false , 242label =': int' , 243position ={ 244character =10 , 245line =2 , 246} , 247paddingRight =false , 248} 249 250exec_lua ( function () 251_G . server2 =_G . _create_server ( { 252capabilities ={ 253textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 254inlayHintProvider =true , 255} , 256handlers ={ 257[ 'textDocument/inlayHint' ] =function ( _ , _ , callback ) 258callback ( nil , { expected2 } ) 259end , 260} , 261} ) 262_G . client2 =vim . lsp . start ( { name ='dummy2' , cmd =_G . server2 . cmd } ) 263end ) 264 265--- @type vim.lsp.inlay_hint.get.ret 266eq ( 267{ 268{ bufnr =1 , client_id =1 , inlay_hint =response [ 1 ] } , 269{ bufnr =1 , client_id =1 , inlay_hint =response [ 2 ] } , 270{ bufnr =1 , client_id =1 , inlay_hint =response [ 3 ] } , 271{ bufnr =1 , client_id =2 , inlay_hint =expected2 } , 272} , 273exec_lua ( function () 274return vim . lsp . inlay_hint . get () 275end ) 276) 277 278eq ( 279{ 280{ bufnr =1 , client_id =2 , inlay_hint =expected2 } , 281} , 282exec_lua ( function () 283return vim . lsp . inlay_hint . get ( { 284range ={ 285start ={ line =2 , character =10 } , 286[ 'end' ] ={ line =2 , character =10 } , 287} , 288} ) 289end ) 290) 291 292eq ( 293{ 294{ bufnr =1 , client_id =1 , inlay_hint =response [ 2 ] } , 295{ bufnr =1 , client_id =1 , inlay_hint =response [ 3 ] } , 296} , 297exec_lua ( function () 298return vim . lsp . inlay_hint . get ( { 299bufnr =vim . api . nvim_get_current_buf (), 300range ={ 301start ={ line =4 , character =18 } , 302[ 'end' ] ={ line =5 , character =17 } , 303} , 304} ) 305end ) 306) 307 308eq ( 309{} , 310exec_lua ( function () 311return vim . lsp . inlay_hint . get ( { 312bufnr =vim . api . nvim_get_current_buf () + 1 , 313} ) 314end ) 315) 316end ) 317end ) 318 319it ( 'does not request hints from lsp when disabled' , function () 320local client_id2 =exec_lua ( function () 321_G . server2 =_G . _create_server ( { 322capabilities ={ 323textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 324inlayHintProvider =true , 325} , 326handlers ={ 327[ 'textDocument/inlayHint' ] =function ( _ , _ , callback ) 328_G . got_inlay_hint_request =true 329callback ( nil , {} ) 330end , 331} , 332} ) 333return vim . lsp . start ( { 334name ='dummy2' , 335cmd =_G . server2 . cmd , 336on_attach =function ( client , _ ) 337vim . lsp . inlay_hint . enable ( false , { client_id =client . id } ) 338end , 339} ) 340end ) 341 342local function was_request_sent () 343return exec_lua ( function () 344return _G . got_inlay_hint_request or false 345end ) 346end 347 348eq ( false , was_request_sent ()) 349 350exec_lua ( function () 351vim . lsp . inlay_hint . get () 352end ) 353 354eq ( false , was_request_sent ()) 355 356exec_lua ( function () 357vim . lsp . inlay_hint . enable ( true , { client_id =client_id2 } ) 358end ) 359 360eq ( true , was_request_sent ()) 361end ) 362end ) 363 364describe ( 'Inlay hints handler' , function () 365local text =dedent ( [[ 366test text 367]] ) 368 369local response ={ 370{ position ={ line =0 , character =0 } , label ='0' } , 371{ position ={ line =0 , character =0 } , label ='1' } , 372{ position ={ line =0 , character =0 } , label ='2' } , 373{ position ={ line =0 , character =0 } , label ='3' } , 374{ position ={ line =0 , character =0 } , label ='4' } , 375} 376 377local grid_without_inlay_hints =[[ 378test text | 379^ | 380| 381]] 382 383local grid_with_inlay_hints =[[ 384{1:01234}test text | 385^ | 386| 387]] 388 389--- @type test.functional.ui.screen 390local screen 391 392--- @type integer 393local client_id 394 395--- @type integer 396local bufnr 397 398before_each ( function () 399clear_notrace () 400screen =Screen . new ( 50 , 3 ) 401 402exec_lua ( create_server_definition ) 403bufnr =n . api . nvim_get_current_buf () 404client_id =exec_lua ( function () 405_G . server =_G . _create_server ( { 406capabilities ={ 407textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 408inlayHintProvider =true , 409} , 410handlers ={ 411[ 'textDocument/inlayHint' ] =function ( _ , _ , callback ) 412callback ( nil , response ) 413end , 414} , 415} ) 416 417vim . api . nvim_win_set_buf ( 0 , bufnr ) 418 419return vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 420end ) 421insert ( text ) 422end ) 423 424it ( 'renders hints with same position in received order' , function () 425exec_lua ( [[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]] ) 426screen : expect ( { grid =grid_with_inlay_hints } ) 427exec_lua ( function () 428vim . lsp . get_client_by_id ( client_id ): stop () 429end ) 430screen : expect ( { grid =grid_without_inlay_hints , unchanged =true } ) 431end ) 432 433it ( 'refreshes hints on request' , function () 434exec_lua ( [[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]] ) 435screen : expect ( { grid =grid_with_inlay_hints } ) 436feed ( 'kibefore <Esc>' ) 437screen : expect ( [[ 438before^ {1:01234}test text | 439|*2 440]] ) 441exec_lua ( function () 442vim . lsp . inlay_hint . on_refresh ( 443nil , 444nil , 445{ method ='workspace/inlayHint/refresh' , client_id =client_id } 446) 447end ) 448screen : expect ( [[ 449{1:01234}before^ test text | 450|*2 451]] ) 452end ) 453 454after_each ( function () 455api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 456end ) 457end )