robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
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.codelens' , function () 19local text =dedent ( [[ 20struct S { 21a: i32, 22b: String, 23} 24 25impl S { 26fn new(a: i32, b: String) -> Self { 27S { a, b } 28} 29} 30 31fn main() { 32let s = S::new(42, String::from("Hello, world!")); 33println!("S.a: {}, S.b: {}", s.a, s.b); 34} 35]] ) 36 37local grid_with_lenses =dedent ( [[ 38{1: 1 implementation} | 39struct S { | 40a: i32, | 41b: String, | 42} | 43| 44impl S { | 45fn new(a: i32, b: String) -> Self { | 46S { a, b } | 47} | 48} | 49| 50{1: ▶︎ Run } | 51fn main() { | 52let s = S::new(42, String::from("Hello, world!"))| 53; | 54println!("S.a: {}, S.b: {}", s.a, s.b); | 55} | 56^ | 57| 58]] ) 59 60local grid_without_lenses =dedent ( [[ 61struct S { | 62a: i32, | 63b: String, | 64} | 65| 66impl S { | 67fn new(a: i32, b: String) -> Self { | 68S { a, b } | 69} | 70} | 71| 72fn main() { | 73let s = S::new(42, String::from("Hello, world!"))| 74; | 75println!("S.a: {}, S.b: {}", s.a, s.b); | 76} | 77^ | 78{1:~ }|*2 79| 80]] ) 81 82--- @type test.functional.ui.screen 83local screen 84 85--- @type integer 86local client_id 87 88before_each ( function () 89clear_notrace () 90exec_lua ( create_server_definition ) 91exec_lua ( function () 92vim . lsp . config ( '*' , { 93flags ={ debounce_text_changes =0 } , 94} ) 95end ) 96 97screen =Screen . new ( nil , 20 ) 98 99client_id =exec_lua ( function () 100_G . server =_G . _create_server ( { 101capabilities ={ 102textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 103codeLensProvider ={ 104resolveProvider =true , 105} , 106} , 107handlers ={ 108[ 'textDocument/codeLens' ] =function ( _ , _ , callback ) 109callback ( nil , { 110{ 111data ={ 112kind ={ 113impls ={ 114position ={ 115character =7 , 116line =0 , 117} , 118} , 119} , 120version =0 , 121} , 122range ={ 123[ 'end' ] ={ 124character =8 , 125line =0 , 126} , 127start ={ 128character =7 , 129line =0 , 130} , 131} , 132} , 133{ 134command ={ 135arguments ={} , 136command ='rust-analyzer.runSingle' , 137title ='▶︎ Run ' , 138} , 139range ={ 140[ 'end' ] ={ 141character =7 , 142line =11 , 143} , 144start ={ 145character =3 , 146line =11 , 147} , 148} , 149} , 150} ) 151end , 152[ 'codeLens/resolve' ] =function ( _ , _ , callback ) 153vim . schedule ( function () 154callback ( nil , { 155command ={ 156arguments ={} , 157command ='rust-analyzer.showReferences' , 158title ='1 implementation' , 159} , 160range ={ 161[ 'end' ] ={ 162character =8 , 163line =0 , 164} , 165start ={ 166character =7 , 167line =0 , 168} , 169} , 170} ) 171end ) 172end , 173} , 174} ) 175 176return vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 177end ) 178 179insert ( text ) 180 181exec_lua ( function () 182vim . lsp . codelens . enable () 183end ) 184 185screen : expect ( { grid =grid_with_lenses } ) 186end ) 187 188it ( 'clears/shows code lenses when disabled/enabled' , function () 189exec_lua ( function () 190vim . lsp . codelens . enable ( false ) 191end ) 192 193screen : expect ( { grid =grid_without_lenses } ) 194 195exec_lua ( function () 196vim . lsp . codelens . enable ( true ) 197end ) 198 199screen : expect ( { grid =grid_with_lenses } ) 200end ) 201 202it ( 'clears code lenses when sole client detaches' , function () 203exec_lua ( function () 204vim . lsp . get_client_by_id ( client_id ): stop () 205end ) 206 207screen : expect ( { grid =grid_without_lenses } ) 208end ) 209 210it ( 'get code lenses in the current buffer' , function () 211local result =exec_lua ( function () 212vim . api . nvim_win_set_cursor ( 0 , { 12 , 3 } ) 213return vim . lsp . codelens . get () 214end ) 215 216eq ( { 217{ 218client_id =1 , 219lens ={ 220command ={ 221arguments ={} , 222command ='rust-analyzer.showReferences' , 223title ='1 implementation' , 224} , 225range ={ 226[ 'end' ] ={ 227character =8 , 228line =0 , 229} , 230start ={ 231character =7 , 232line =0 , 233} , 234} , 235} , 236} , 237{ 238client_id =1 , 239lens ={ 240command ={ 241arguments ={} , 242command ='rust-analyzer.runSingle' , 243title ='▶︎ Run ' , 244} , 245range ={ 246[ 'end' ] ={ 247character =7 , 248line =11 , 249} , 250start ={ 251character =3 , 252line =11 , 253} , 254} , 255} , 256} , 257} , result ) 258end ) 259 260it ( 'refreshes code lenses on request' , function () 261local get_message_count =function () 262local messages =exec_lua ( 'return _G.server.messages' ) 263local count =0 264for _ , m in ipairs ( messages ) do 265if m . method == 'textDocument/codeLens' then 266count =count + 1 267end 268end 269return count 270end 271 272eq ( 2 , get_message_count ()) 273 274feed ( 'ggdd' ) 275 276-- Second codelens (▶︎ Run) is not resolved immediately, so its previous extmark (same row) isn't 277-- cleared and it stays anchored to where it was. The new one (which would effectively look like 278-- it moved a row down) needs to be resolved first, which goes through another screen update. 279-- This is a quirk of the test since it returns the exact same result no matter what the text in 280-- the buffer actually is 281 282screen : expect ( [[ 283{1: 1 implementation} | 284^a: i32, | 285b: String, | 286} | 287| 288impl S { | 289fn new(a: i32, b: String) -> Self { | 290S { a, b } | 291} | 292} | 293| 294{1: ▶︎ Run } | 295fn main() { | 296let s = S::new(42, String::from("Hello, world!"))| 297; | 298println!("S.a: {}, S.b: {}", s.a, s.b); | 299} | 300| 301{1:~ }| 302| 303]] ) 304 305eq ( 2 , get_message_count ()) 306 307screen : expect ( [[ 308{1: 1 implementation} | 309^a: i32, | 310b: String, | 311} | 312| 313impl S { | 314fn new(a: i32, b: String) -> Self { | 315S { a, b } | 316} | 317} | 318| 319fn main() { | 320{1: ▶︎ Run } | 321let s = S::new(42, String::from("Hello, world!"))| 322; | 323println!("S.a: {}, S.b: {}", s.a, s.b); | 324} | 325| 326{1:~ }| 327| 328]] ) 329 330eq ( 3 , get_message_count ()) 331 332exec_lua ( function () 333vim . lsp . codelens . on_refresh ( 334nil , 335nil , 336{ method ='workspace/codeLens/refresh' , client_id =client_id } 337) 338end ) 339 340eq ( 4 , get_message_count ()) 341end ) 342 343it ( 'ignores stale codeLens/resolve responses' , function () 344clear_notrace () 345exec_lua ( create_server_definition ) 346 347insert ( 'line1\nline2\n' ) 348 349exec_lua ( function () 350local codelens_request_count =0 351_G . stale_resolve_sent =false 352_G . server =_G . _create_server ( { 353capabilities ={ 354textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 355codeLensProvider ={ 356resolveProvider =true , 357} , 358} , 359handlers ={ 360[ 'textDocument/codeLens' ] =function ( _ , _ , callback ) 361codelens_request_count =codelens_request_count + 1 362if codelens_request_count == 1 then 363callback ( nil , { 364{ 365range ={ 366[ 'end' ] ={ 367character =1 , 368line =0 , 369} , 370start ={ 371character =0 , 372line =0 , 373} , 374} , 375} , 376} ) 377else 378callback ( nil , {} ) 379end 380end , 381[ 'codeLens/resolve' ] =function ( _ , lens , callback ) 382vim . defer_fn ( function () 383_G . stale_resolve_sent =true 384callback ( nil , { 385command ={ 386arguments ={} , 387command ='dummy.command' , 388title ='resolved' , 389} , 390range =lens . range , 391} ) 392end , 100 ) 393end , 394} , 395} ) 396 397local stale_client_id =vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 398vim . lsp . codelens . enable () 399vim . wait ( 1000 , function () 400return # vim . lsp . codelens . get () > 0 401end ) 402 403vim . api . nvim__redraw ( { flush =true } ) 404 405vim . lsp . codelens . on_refresh ( nil , nil , { 406method ='workspace/codeLens/refresh' , 407client_id =stale_client_id , 408} ) 409 410assert ( 411vim . wait ( 1000 , function () 412return _G . stale_resolve_sent 413end ), 414'timed out waiting for stale resolve response' 415) 416end ) 417 418eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 419end ) 420 421it ( 'ignores refresh responses for deleted buffer' , function () 422clear_notrace () 423exec_lua ( create_server_definition ) 424 425insert ( 'line1\n' ) 426 427exec_lua ( function () 428local codelens_request_count =0 429_G . refresh_response_sent =false 430_G . server =_G . _create_server ( { 431capabilities ={ 432textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 433codeLensProvider ={ 434resolveProvider =true , 435} , 436} , 437handlers ={ 438[ 'textDocument/codeLens' ] =function ( _ , _ , callback ) 439codelens_request_count =codelens_request_count + 1 440 441local lenses ={ 442{ 443command ={ 444arguments ={} , 445command ='dummy.command' , 446title ='Lens' , 447} , 448range ={ 449[ 'end' ] ={ 450character =1 , 451line =0 , 452} , 453start ={ 454character =0 , 455line =0 , 456} , 457} , 458} , 459} 460 461if codelens_request_count == 1 then 462callback ( nil , lenses ) 463else 464-- Delay the refresh response so the buffer is wiped before it arrives. 465vim . schedule ( function () 466_G . refresh_response_sent =true 467callback ( nil , lenses ) 468end ) 469end 470end , 471} , 472} ) 473 474local client_id =vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 475vim . lsp . codelens . enable () 476 477assert ( 478vim . wait ( 1000 , function () 479return # vim . lsp . codelens . get () > 0 480end ), 481'timed out waiting for initial codelens response' 482) 483 484vim . lsp . codelens . on_refresh ( nil , nil , { 485method ='workspace/codeLens/refresh' , 486client_id =client_id , 487} ) 488vim . cmd . bwipeout ( { bang =true } ) 489 490assert ( 491vim . wait ( 1000 , function () 492return _G . refresh_response_sent 493end ), 494'timed out waiting for refresh response' 495) 496end ) 497 498eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 499end ) 500 501it ( 'clears extmarks beyond the bottom of the buffer' , function () 502feed ( '12G4dd' ) 503screen : expect ( [[ 504{1: 1 implementation} | 505struct S { | 506a: i32, | 507b: String, | 508} | 509| 510impl S { | 511fn new(a: i32, b: String) -> Self { | 512S { a, b } | 513} | 514} | 515| 516{1:▶︎ Run } | 517^ | 518{1:~ }|*5 5194 fewer lines | 520]] ) 521 522feed ( 'dd' ) 523screen : expect ( [[ 524{1: 1 implementation} | 525struct S { | 526a: i32, | 527b: String, | 528} | 529| 530impl S { | 531fn new(a: i32, b: String) -> Self { | 532S { a, b } | 533} | 534} | 535^ | 536{1:~ }|*7 5374 fewer lines | 538]] ) 539end ) 540 541after_each ( function () 542api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 543end ) 544end )