robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local t_lsp =require ( 'test.functional.plugin.lsp.testutil' ) 5 6local describe , it , before_each , after_each , teardown = 7t . describe , t . it , t . before_each , t . after_each , t . teardown 8local command =n . command 9local eq =t . eq 10local exec_lua =n . exec_lua 11local matches =t . matches 12local pcall_err =t . pcall_err 13local retry =t . retry 14local stop =n . stop 15local NIL =vim . NIL 16local api =n . api 17local skip =t . skip 18local is_os =t . is_os 19 20local clear_notrace =t_lsp . clear_notrace 21local create_server_definition =t_lsp . create_server_definition 22local fake_lsp_logfile =t_lsp . fake_lsp_logfile 23local test_rpc_server =t_lsp . test_rpc_server 24local test_root =vim . uv . cwd () 25 26-- TODO(justinmk): hangs on Windows https://github.com/neovim/neovim/pull/11837 27if skip ( is_os ( 'win' )) then 28return 29end 30 31describe ( 'vim.lsp.buf' , function () 32local function exec_capture ( cmd ) 33return exec_lua ( function ( cmd0 ) 34return vim . api . nvim_exec2 ( cmd0 , { output =true } ). output 35end , cmd ) 36end 37 38before_each ( function () 39clear_notrace () 40command ( 'cd ' .. test_root ) 41end ) 42 43after_each ( function () 44stop () 45exec_lua ( function () 46vim . iter ( vim . lsp . get_clients ( { _uninitialized =true } )): each ( function ( client ) 47client : stop ( true ) 48end ) 49end ) 50api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 51end ) 52 53teardown ( function () 54os . remove ( fake_lsp_logfile ) 55end ) 56 57describe ( 'vim.lsp.buf.outgoing_calls' , function () 58it ( 'does nothing for an empty response' , function () 59local qflist_count =exec_lua ( function () 60require 'vim.lsp.handlers' [ 'callHierarchy/outgoingCalls' ]( nil , nil , {} , nil ) 61return # vim . fn . getqflist () 62end ) 63eq ( 0 , qflist_count ) 64end ) 65 66it ( 'opens the quickfix list with the right caller' , function () 67local qflist =exec_lua ( function () 68local rust_analyzer_response ={ 69{ 70fromRanges ={ 71{ 72[ 'end' ] ={ 73character =7 , 74line =3 , 75} , 76start ={ 77character =4 , 78line =3 , 79} , 80} , 81} , 82to ={ 83detail ='fn foo()' , 84kind =12 , 85name ='foo' , 86range ={ 87[ 'end' ] ={ 88character =11 , 89line =0 , 90} , 91start ={ 92character =0 , 93line =0 , 94} , 95} , 96selectionRange ={ 97[ 'end' ] ={ 98character =6 , 99line =0 , 100} , 101start ={ 102character =3 , 103line =0 , 104} , 105} , 106uri ='file:///src/main.rs' , 107} , 108} , 109} 110local handler =require 'vim.lsp.handlers' [ 'callHierarchy/outgoingCalls' ] 111local bufnr =vim . api . nvim_get_current_buf () 112handler ( nil , rust_analyzer_response , { bufnr =bufnr } ) 113return vim . fn . getqflist () 114end ) 115 116local expected ={ 117{ 118bufnr =1 , 119col =5 , 120end_col =0 , 121lnum =4 , 122end_lnum =0 , 123module ='' , 124nr =0 , 125pattern ='' , 126text ='foo' , 127type ='' , 128valid =1 , 129vcol =0 , 130} , 131} 132 133eq ( expected , qflist ) 134end ) 135end ) 136 137describe ( 'vim.lsp.buf.incoming_calls' , function () 138it ( 'does nothing for an empty response' , function () 139local qflist_count =exec_lua ( function () 140require 'vim.lsp.handlers' [ 'callHierarchy/incomingCalls' ]( nil , nil , {} ) 141return # vim . fn . getqflist () 142end ) 143eq ( 0 , qflist_count ) 144end ) 145 146it ( 'opens the quickfix list with the right callee' , function () 147local qflist =exec_lua ( function () 148local rust_analyzer_response ={ 149{ 150from ={ 151detail ='fn main()' , 152kind =12 , 153name ='main' , 154range ={ 155[ 'end' ] ={ 156character =1 , 157line =4 , 158} , 159start ={ 160character =0 , 161line =2 , 162} , 163} , 164selectionRange ={ 165[ 'end' ] ={ 166character =7 , 167line =2 , 168} , 169start ={ 170character =3 , 171line =2 , 172} , 173} , 174uri ='file:///src/main.rs' , 175} , 176fromRanges ={ 177{ 178[ 'end' ] ={ 179character =7 , 180line =3 , 181} , 182start ={ 183character =4 , 184line =3 , 185} , 186} , 187} , 188} , 189} 190 191local handler =require 'vim.lsp.handlers' [ 'callHierarchy/incomingCalls' ] 192handler ( nil , rust_analyzer_response , {} ) 193return vim . fn . getqflist () 194end ) 195 196local expected ={ 197{ 198bufnr =2 , 199col =5 , 200end_col =0 , 201lnum =4 , 202end_lnum =0 , 203module ='' , 204nr =0 , 205pattern ='' , 206text ='main' , 207type ='' , 208valid =1 , 209vcol =0 , 210} , 211} 212 213eq ( expected , qflist ) 214end ) 215end ) 216 217describe ( 'vim.lsp.buf.typehierarchy subtypes' , function () 218it ( 'does nothing for an empty response' , function () 219local qflist_count =exec_lua ( function () 220require 'vim.lsp.handlers' [ 'typeHierarchy/subtypes' ]( nil , nil , {} ) 221return # vim . fn . getqflist () 222end ) 223eq ( 0 , qflist_count ) 224end ) 225 226it ( 'opens the quickfix list with the right subtypes' , function () 227exec_lua ( create_server_definition ) 228local qflist =exec_lua ( function () 229local clangd_response ={ 230{ 231data ={ 232parents ={ 233{ 234parents ={ 235{ 236parents ={ 237{ 238parents ={} , 239symbolID ='62B3D268A01B9978' , 240} , 241} , 242symbolID ='DC9B0AD433B43BEC' , 243} , 244} , 245symbolID ='06B5F6A19BA9F6A8' , 246} , 247} , 248symbolID ='EDC336589C09ABB2' , 249} , 250kind =5 , 251name ='D2' , 252range ={ 253[ 'end' ] ={ 254character =8 , 255line =3 , 256} , 257start ={ 258character =6 , 259line =3 , 260} , 261} , 262selectionRange ={ 263[ 'end' ] ={ 264character =8 , 265line =3 , 266} , 267start ={ 268character =6 , 269line =3 , 270} , 271} , 272uri ='file:///home/jiangyinzuo/hello.cpp' , 273} , 274{ 275data ={ 276parents ={ 277{ 278parents ={ 279{ 280parents ={ 281{ 282parents ={} , 283symbolID ='62B3D268A01B9978' , 284} , 285} , 286symbolID ='DC9B0AD433B43BEC' , 287} , 288} , 289symbolID ='06B5F6A19BA9F6A8' , 290} , 291} , 292symbolID ='AFFCAED15557EF08' , 293} , 294kind =5 , 295name ='D1' , 296range ={ 297[ 'end' ] ={ 298character =8 , 299line =2 , 300} , 301start ={ 302character =6 , 303line =2 , 304} , 305} , 306selectionRange ={ 307[ 'end' ] ={ 308character =8 , 309line =2 , 310} , 311start ={ 312character =6 , 313line =2 , 314} , 315} , 316uri ='file:///home/jiangyinzuo/hello.cpp' , 317} , 318} 319 320local server =_G . _create_server ( { 321capabilities ={ 322positionEncoding ='utf-8' , 323} , 324} ) 325local client_id =vim . lsp . start ( { name ='dummy' , cmd =server . cmd } ) 326local handler =require 'vim.lsp.handlers' [ 'typeHierarchy/subtypes' ] 327local bufnr =vim . api . nvim_get_current_buf () 328vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , false , { 329'class B : public A{};' , 330'class C : public B{};' , 331'class D1 : public C{};' , 332'class D2 : public C{};' , 333'class E : public D1, D2 {};' , 334} ) 335handler ( nil , clangd_response , { client_id =client_id , bufnr =bufnr } ) 336return vim . fn . getqflist () 337end ) 338 339local expected ={ 340{ 341bufnr =2 , 342col =7 , 343end_col =0 , 344end_lnum =0 , 345lnum =4 , 346module ='' , 347nr =0 , 348pattern ='' , 349text ='D2' , 350type ='' , 351valid =1 , 352vcol =0 , 353} , 354{ 355bufnr =2 , 356col =7 , 357end_col =0 , 358end_lnum =0 , 359lnum =3 , 360module ='' , 361nr =0 , 362pattern ='' , 363text ='D1' , 364type ='' , 365valid =1 , 366vcol =0 , 367} , 368} 369 370eq ( expected , qflist ) 371end ) 372 373it ( 'opens the quickfix list with the right subtypes and details' , function () 374exec_lua ( create_server_definition ) 375local qflist =exec_lua ( function () 376local jdtls_response ={ 377{ 378data ={ element ='=hello-java_ed323c3c/_<{Main.java[Main[A' } , 379detail ='' , 380kind =5 , 381name ='A' , 382range ={ 383[ 'end' ] ={ character =26 , line =3 } , 384start ={ character =1 , line =3 } , 385} , 386selectionRange ={ 387[ 'end' ] ={ character =8 , line =3 } , 388start ={ character =7 , line =3 } , 389} , 390tags ={} , 391uri ='file:///home/jiangyinzuo/hello-java/Main.java' , 392} , 393{ 394data ={ element ='=hello-java_ed323c3c/_<mylist{MyList.java[MyList[Inner' } , 395detail ='mylist' , 396kind =5 , 397name ='MyList$Inner' , 398range ={ 399[ 'end' ] ={ character =37 , line =3 } , 400start ={ character =1 , line =3 } , 401} , 402selectionRange ={ 403[ 'end' ] ={ character =19 , line =3 } , 404start ={ character =14 , line =3 } , 405} , 406tags ={} , 407uri ='file:///home/jiangyinzuo/hello-java/mylist/MyList.java' , 408} , 409} 410 411local server =_G . _create_server ( { 412capabilities ={ 413positionEncoding ='utf-8' , 414} , 415} ) 416local client_id =vim . lsp . start ( { name ='dummy' , cmd =server . cmd } ) 417local handler =require 'vim.lsp.handlers' [ 'typeHierarchy/subtypes' ] 418local bufnr =vim . api . nvim_get_current_buf () 419vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , false , { 420'package mylist;' , 421'' , 422'public class MyList {' , 423' static class Inner extends MyList{}' , 424'~}' , 425} ) 426handler ( nil , jdtls_response , { client_id =client_id , bufnr =bufnr } ) 427return vim . fn . getqflist () 428end ) 429 430local expected ={ 431{ 432bufnr =2 , 433col =2 , 434end_col =0 , 435end_lnum =0 , 436lnum =4 , 437module ='' , 438nr =0 , 439pattern ='' , 440text ='A' , 441type ='' , 442valid =1 , 443vcol =0 , 444} , 445{ 446bufnr =3 , 447col =2 , 448end_col =0 , 449end_lnum =0 , 450lnum =4 , 451module ='' , 452nr =0 , 453pattern ='' , 454text ='MyList$Inner mylist' , 455type ='' , 456valid =1 , 457vcol =0 , 458} , 459} 460eq ( expected , qflist ) 461end ) 462end ) 463 464describe ( 'vim.lsp.buf.typehierarchy supertypes' , function () 465it ( 'does nothing for an empty response' , function () 466local qflist_count =exec_lua ( function () 467require 'vim.lsp.handlers' [ 'typeHierarchy/supertypes' ]( nil , nil , {} ) 468return # vim . fn . getqflist () 469end ) 470eq ( 0 , qflist_count ) 471end ) 472 473it ( 'opens the quickfix list with the right supertypes' , function () 474exec_lua ( create_server_definition ) 475local qflist =exec_lua ( function () 476local clangd_response ={ 477{ 478data ={ 479parents ={ 480{ 481parents ={ 482{ 483parents ={ 484{ 485parents ={} , 486symbolID ='62B3D268A01B9978' , 487} , 488} , 489symbolID ='DC9B0AD433B43BEC' , 490} , 491} , 492symbolID ='06B5F6A19BA9F6A8' , 493} , 494} , 495symbolID ='EDC336589C09ABB2' , 496} , 497kind =5 , 498name ='D2' , 499range ={ 500[ 'end' ] ={ 501character =8 , 502line =3 , 503} , 504start ={ 505character =6 , 506line =3 , 507} , 508} , 509selectionRange ={ 510[ 'end' ] ={ 511character =8 , 512line =3 , 513} , 514start ={ 515character =6 , 516line =3 , 517} , 518} , 519uri ='file:///home/jiangyinzuo/hello.cpp' , 520} , 521{ 522data ={ 523parents ={ 524{ 525parents ={ 526{ 527parents ={ 528{ 529parents ={} , 530symbolID ='62B3D268A01B9978' , 531} , 532} , 533symbolID ='DC9B0AD433B43BEC' , 534} , 535} , 536symbolID ='06B5F6A19BA9F6A8' , 537} , 538} , 539symbolID ='AFFCAED15557EF08' , 540} , 541kind =5 , 542name ='D1' , 543range ={ 544[ 'end' ] ={ 545character =8 , 546line =2 , 547} , 548start ={ 549character =6 , 550line =2 , 551} , 552} , 553selectionRange ={ 554[ 'end' ] ={ 555character =8 , 556line =2 , 557} , 558start ={ 559character =6 , 560line =2 , 561} , 562} , 563uri ='file:///home/jiangyinzuo/hello.cpp' , 564} , 565} 566 567local server =_G . _create_server ( { 568capabilities ={ 569positionEncoding ='utf-8' , 570} , 571} ) 572local client_id =vim . lsp . start ( { name ='dummy' , cmd =server . cmd } ) 573local handler =require 'vim.lsp.handlers' [ 'typeHierarchy/supertypes' ] 574local bufnr =vim . api . nvim_get_current_buf () 575vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , false , { 576'class B : public A{};' , 577'class C : public B{};' , 578'class D1 : public C{};' , 579'class D2 : public C{};' , 580'class E : public D1, D2 {};' , 581} ) 582 583handler ( nil , clangd_response , { client_id =client_id , bufnr =bufnr } ) 584return vim . fn . getqflist () 585end ) 586 587local expected ={ 588{ 589bufnr =2 , 590col =7 , 591end_col =0 , 592end_lnum =0 , 593lnum =4 , 594module ='' , 595nr =0 , 596pattern ='' , 597text ='D2' , 598type ='' , 599valid =1 , 600vcol =0 , 601} , 602{ 603bufnr =2 , 604col =7 , 605end_col =0 , 606end_lnum =0 , 607lnum =3 , 608module ='' , 609nr =0 , 610pattern ='' , 611text ='D1' , 612type ='' , 613valid =1 , 614vcol =0 , 615} , 616} 617 618eq ( expected , qflist ) 619end ) 620 621it ( 'opens the quickfix list with the right supertypes and details' , function () 622exec_lua ( create_server_definition ) 623local qflist =exec_lua ( function () 624local jdtls_response ={ 625{ 626data ={ element ='=hello-java_ed323c3c/_<{Main.java[Main[A' } , 627detail ='' , 628kind =5 , 629name ='A' , 630range ={ 631[ 'end' ] ={ character =26 , line =3 } , 632start ={ character =1 , line =3 } , 633} , 634selectionRange ={ 635[ 'end' ] ={ character =8 , line =3 } , 636start ={ character =7 , line =3 } , 637} , 638tags ={} , 639uri ='file:///home/jiangyinzuo/hello-java/Main.java' , 640} , 641{ 642data ={ element ='=hello-java_ed323c3c/_<mylist{MyList.java[MyList[Inner' } , 643detail ='mylist' , 644kind =5 , 645name ='MyList$Inner' , 646range ={ 647[ 'end' ] ={ character =37 , line =3 } , 648start ={ character =1 , line =3 } , 649} , 650selectionRange ={ 651[ 'end' ] ={ character =19 , line =3 } , 652start ={ character =14 , line =3 } , 653} , 654tags ={} , 655uri ='file:///home/jiangyinzuo/hello-java/mylist/MyList.java' , 656} , 657} 658 659local server =_G . _create_server ( { 660capabilities ={ 661positionEncoding ='utf-8' , 662} , 663} ) 664local client_id =vim . lsp . start ( { name ='dummy' , cmd =server . cmd } ) 665local handler =require 'vim.lsp.handlers' [ 'typeHierarchy/supertypes' ] 666local bufnr =vim . api . nvim_get_current_buf () 667vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , false , { 668'package mylist;' , 669'' , 670'public class MyList {' , 671' static class Inner extends MyList{}' , 672'~}' , 673} ) 674handler ( nil , jdtls_response , { client_id =client_id , bufnr =bufnr } ) 675return vim . fn . getqflist () 676end ) 677 678local expected ={ 679{ 680bufnr =2 , 681col =2 , 682end_col =0 , 683end_lnum =0 , 684lnum =4 , 685module ='' , 686nr =0 , 687pattern ='' , 688text ='A' , 689type ='' , 690valid =1 , 691vcol =0 , 692} , 693{ 694bufnr =3 , 695col =2 , 696end_col =0 , 697end_lnum =0 , 698lnum =4 , 699module ='' , 700nr =0 , 701pattern ='' , 702text ='MyList$Inner mylist' , 703type ='' , 704valid =1 , 705vcol =0 , 706} , 707} 708eq ( expected , qflist ) 709end ) 710end ) 711 712describe ( 'vim.lsp.buf.rename' , function () 713for _ , test in ipairs ( { 714{ 715it ='does not attempt to rename on nil response' , 716name ='prepare_rename_nil' , 717expected_handlers ={ 718{ NIL , {} , { method ='shutdown' , client_id =1 } } , 719{ NIL , {} , { method ='start' , client_id =1 } } , 720} , 721} , 722{ 723it ='handles prepareRename placeholder response' , 724name ='prepare_rename_placeholder' , 725expected_handlers ={ 726{ NIL , {} , { method ='shutdown' , client_id =1 } } , 727{ {} , NIL , { method ='textDocument/rename' , client_id =1 , request_id =3 , bufnr =1 } } , 728{ NIL , {} , { method ='start' , client_id =1 } } , 729} , 730expected_text ='placeholder' , -- see fake lsp response 731} , 732{ 733it ='handles range response' , 734name ='prepare_rename_range' , 735expected_handlers ={ 736{ NIL , {} , { method ='shutdown' , client_id =1 } } , 737{ {} , NIL , { method ='textDocument/rename' , client_id =1 , request_id =3 , bufnr =1 } } , 738{ NIL , {} , { method ='start' , client_id =1 } } , 739} , 740expected_text ='line' , -- see test case and fake lsp response 741} , 742{ 743it ='handles error' , 744name ='prepare_rename_error' , 745expected_handlers ={ 746{ NIL , {} , { method ='shutdown' , client_id =1 } } , 747{ NIL , {} , { method ='start' , client_id =1 } } , 748} , 749} , 750} ) do 751it ( test . it , function () 752local client --- @type vim.lsp.Client 753test_rpc_server { 754test_name =test . name , 755on_init =function ( _client ) 756client =_client 757eq ( true , client . server_capabilities (). renameProvider . prepareProvider ) 758end , 759on_setup =function () 760exec_lua ( function () 761local bufnr =vim . api . nvim_get_current_buf () 762vim . lsp . buf_attach_client ( bufnr , _G . TEST_RPC_CLIENT_ID ) 763vim . lsp . _stubs ={} 764--- @diagnostic disable-next-line:duplicate-set-field 765vim . fn . input =function ( opts , _ ) 766vim . lsp . _stubs . input_prompt =opts . prompt 767vim . lsp . _stubs . input_text =opts . default 768return 'renameto' -- expect this value in fake lsp 769end 770vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , false , { '' , 'this is line two' } ) 771vim . fn . cursor ( 2 , 13 ) -- the space between "line" and "two" 772end ) 773end , 774on_exit =function ( code , signal ) 775eq ( 0 , code , 'exit code' ) 776eq ( 0 , signal , 'exit signal' ) 777end , 778on_handler =function ( err , result , ctx ) 779-- Don't compare & assert params and version, they're not relevant for the testcase 780-- This allows us to be lazy and avoid declaring them 781ctx . params =nil 782ctx . version =nil 783 784eq ( table . remove ( test . expected_handlers ), { err , result , ctx } , 'expected handler' ) 785if ctx . method == 'start' then 786exec_lua ( function () 787vim . lsp . buf . rename () 788end ) 789end 790if ctx . method == 'shutdown' then 791if test . expected_text then 792eq ( 793'New Name: ' , 794exec_lua ( function () 795return vim . lsp . _stubs . input_prompt 796end ) 797) 798eq ( 799test . expected_text , 800exec_lua ( function () 801return vim . lsp . _stubs . input_text 802end ) 803) 804end 805client : stop () 806end 807end , 808} 809end ) 810end 811end ) 812 813describe ( 'vim.lsp.buf.code_action' , function () 814it ( 'calls client side command if available' , function () 815local client --- @type vim.lsp.Client 816local expected_handlers ={ 817{ NIL , {} , { method ='shutdown' , client_id =1 } } , 818{ NIL , {} , { method ='start' , client_id =1 } } , 819} 820test_rpc_server { 821test_name ='code_action_with_resolve' , 822on_init =function ( client_ ) 823client =client_ 824end , 825on_setup =function () end , 826on_exit =function ( code , signal ) 827eq ( 0 , code , 'exit code' ) 828eq ( 0 , signal , 'exit signal' ) 829end , 830on_handler =function ( err , result , ctx ) 831eq ( table . remove ( expected_handlers ), { err , result , ctx } ) 832if ctx . method == 'start' then 833exec_lua ( function () 834vim . lsp . commands [ 'dummy1' ] =function ( _ ) 835vim . lsp . commands [ 'dummy2' ] =function () end 836end 837local bufnr =vim . api . nvim_get_current_buf () 838vim . lsp . buf_attach_client ( bufnr , _G . TEST_RPC_CLIENT_ID ) 839--- @diagnostic disable-next-line:duplicate-set-field 840vim . fn . inputlist =function () 841return 1 842end 843vim . lsp . buf . code_action () 844end ) 845elseif ctx . method == 'shutdown' then 846eq ( 847'function' , 848exec_lua ( function () 849return type ( vim . lsp . commands [ 'dummy2' ]) 850end ) 851) 852client : stop () 853end 854end , 855} 856end ) 857 858it ( 'calls workspace/executeCommand if no client side command' , function () 859local client --- @type vim.lsp.Client 860local expected_handlers ={ 861{ NIL , {} , { method ='shutdown' , client_id =1 } } , 862{ 863NIL , 864{ command ='dummy1' , title ='Command 1' } , 865{ bufnr =1 , method ='workspace/executeCommand' , request_id =3 , client_id =1 } , 866} , 867{ NIL , {} , { method ='start' , client_id =1 } } , 868} 869test_rpc_server ( { 870test_name ='code_action_server_side_command' , 871on_init =function ( client_ ) 872client =client_ 873end , 874on_setup =function () end , 875on_exit =function ( code , signal ) 876eq ( 0 , code , 'exit code' , fake_lsp_logfile ) 877eq ( 0 , signal , 'exit signal' , fake_lsp_logfile ) 878end , 879on_handler =function ( err , result , ctx ) 880ctx . params =nil -- don't compare in assert 881ctx . version =nil 882eq ( table . remove ( expected_handlers ), { err , result , ctx } ) 883if ctx . method == 'start' then 884exec_lua ( function () 885local bufnr =vim . api . nvim_get_current_buf () 886vim . lsp . buf_attach_client ( bufnr , _G . TEST_RPC_CLIENT_ID ) 887vim . fn . inputlist =function () 888return 1 889end 890vim . lsp . buf . code_action () 891end ) 892elseif ctx . method == 'shutdown' then 893client : stop () 894end 895end , 896} ) 897end ) 898 899it ( 'filters and automatically applies action if requested' , function () 900local client --- @type vim.lsp.Client 901local expected_handlers ={ 902{ NIL , {} , { method ='shutdown' , client_id =1 } } , 903{ NIL , {} , { method ='start' , client_id =1 } } , 904} 905test_rpc_server { 906test_name ='code_action_filter' , 907on_init =function ( client_ ) 908client =client_ 909end , 910on_setup =function () end , 911on_exit =function ( code , signal ) 912eq ( 0 , code , 'exit code' ) 913eq ( 0 , signal , 'exit signal' ) 914end , 915on_handler =function ( err , result , ctx ) 916eq ( table . remove ( expected_handlers ), { err , result , ctx } ) 917if ctx . method == 'start' then 918exec_lua ( function () 919vim . lsp . commands [ 'preferred_command' ] =function ( _ ) 920vim . lsp . commands [ 'executed_preferred' ] =function () end 921end 922vim . lsp . commands [ 'type_annotate_command' ] =function ( _ ) 923vim . lsp . commands [ 'executed_type_annotate' ] =function () end 924end 925local bufnr =vim . api . nvim_get_current_buf () 926vim . lsp . buf_attach_client ( bufnr , _G . TEST_RPC_CLIENT_ID ) 927vim . lsp . buf . code_action ( { 928filter =function ( a ) 929return a . isPreferred 930end , 931apply =true , 932} ) 933vim . lsp . buf . code_action ( { 934-- expect to be returned actions 'type-annotate' and 'type-annotate.foo' 935context ={ only ={ 'type-annotate' } } , 936apply =true , 937filter =function ( a ) 938if a . kind == 'type-annotate.foo' then 939vim . lsp . commands [ 'filtered_type_annotate_foo' ] =function () end 940return false 941elseif a . kind == 'type-annotate' then 942return true 943else 944assert ( nil , 'unreachable' ) 945end 946end , 947} ) 948end ) 949elseif ctx . method == 'shutdown' then 950eq ( 951'function' , 952exec_lua ( function () 953return type ( vim . lsp . commands [ 'executed_preferred' ]) 954end ) 955) 956eq ( 957'function' , 958exec_lua ( function () 959return type ( vim . lsp . commands [ 'filtered_type_annotate_foo' ]) 960end ) 961) 962eq ( 963'function' , 964exec_lua ( function () 965return type ( vim . lsp . commands [ 'executed_type_annotate' ]) 966end ) 967) 968client : stop () 969end 970end , 971} 972end ) 973 974it ( 'uses diagnostics at cursor position' , function () 975exec_lua ( create_server_definition ) 976local severity =exec_lua ( function () 977return vim . diagnostic . severity . ERROR 978end ) 979local messages =exec_lua ( function ( severity_ ) 980local server =_G . _create_server ( { 981capabilities ={ 982codeActionProvider =true , 983} , 984handlers ={ 985[ 'textDocument/codeAction' ] =function ( _ , _ , callback ) 986callback ( nil , {} ) 987end , 988} , 989} ) 990 991local bufnr =vim . api . nvim_get_current_buf () 992vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , false , { 'local first, second = 1, 2' } ) 993 994local client_id =assert ( vim . lsp . start ( { 995name ='dummy' , 996cmd =server . cmd , 997} )) 998 999local expected_messages =2 -- initialize, initialized 1000local function wait_for_messages () 1001assert ( 1002vim . wait ( 200 , function () 1003return # server . messages == expected_messages 1004end ), 1005'Timed out waiting for expected number of messages. Current messages seen so far: ' 1006.. vim . inspect ( server . messages ) 1007) 1008end 1009 1010wait_for_messages () 1011 1012vim . api . nvim_win_set_cursor ( 0 , { 1 , 15 } ) 1013 1014local ns =vim . lsp . diagnostic . get_namespace ( client_id ) 1015vim . diagnostic . set ( ns , bufnr , { 1016{ 1017lnum =0 , 1018col =6 , 1019end_lnum =0 , 1020end_col =11 , 1021message ='first' , 1022severity =severity_ , 1023user_data ={ 1024lsp ={ 1025range ={ 1026start ={ line =0 , character =6 } , 1027[ 'end' ] ={ line =0 , character =11 } , 1028} , 1029message ='first' , 1030severity =severity_ , 1031} , 1032} , 1033} , 1034{ 1035lnum =0 , 1036col =13 , 1037end_lnum =0 , 1038end_col =19 , 1039message ='second' , 1040severity =severity_ , 1041user_data ={ 1042lsp ={ 1043range ={ 1044start ={ line =0 , character =13 } , 1045[ 'end' ] ={ line =0 , character =19 } , 1046} , 1047message ='second' , 1048severity =severity_ , 1049} , 1050} , 1051} , 1052} ) 1053 1054vim . lsp . buf . code_action () 1055 1056expected_messages =expected_messages + 1 1057wait_for_messages () 1058 1059vim . lsp . get_client_by_id ( client_id ): stop () 1060 1061return server . messages 1062end , severity ) 1063 1064eq ( 'textDocument/codeAction' , messages [ 3 ]. method ) 1065eq ( { 1066{ 1067range ={ 1068start ={ line =0 , character =13 } , 1069[ 'end' ] ={ line =0 , character =19 } , 1070} , 1071message ='second' , 1072severity =severity , 1073} , 1074} , messages [ 3 ]. params . context . diagnostics ) 1075end ) 1076 1077it ( 'fallback to command execution on resolve error' , function () 1078exec_lua ( create_server_definition ) 1079local result =exec_lua ( function () 1080local server =_G . _create_server ( { 1081capabilities ={ 1082executeCommandProvider ={ 1083commands ={ 'command:1' } , 1084} , 1085codeActionProvider ={ 1086resolveProvider =true , 1087} , 1088} , 1089handlers ={ 1090[ 'textDocument/codeAction' ] =function ( _ , _ , callback ) 1091callback ( nil , { 1092{ 1093title ='Code Action 1' , 1094command ={ 1095title ='Command 1' , 1096command ='command:1' , 1097} , 1098} , 1099} ) 1100end , 1101[ 'codeAction/resolve' ] =function ( _ , _ , callback ) 1102callback ( 1103{ code =vim . lsp . protocol . ErrorCodes . InternalError , message ='resolve failed' } , 1104nil 1105) 1106end , 1107} , 1108} ) 1109 1110local client_id =assert ( vim . lsp . start ( { 1111name ='dummy' , 1112cmd =server . cmd , 1113} )) 1114 1115vim . lsp . buf . code_action ( { apply =true } ) 1116vim . lsp . get_client_by_id ( client_id ): stop () 1117return server . messages 1118end ) 1119eq ( 'codeAction/resolve' , result [ 4 ]. method ) 1120eq ( 'workspace/executeCommand' , result [ 5 ]. method ) 1121eq ( 'command:1' , result [ 5 ]. params . command ) 1122end ) 1123 1124it ( 'resolves command property' , function () 1125exec_lua ( create_server_definition ) 1126local result =exec_lua ( function () 1127local server =_G . _create_server ( { 1128capabilities ={ 1129executeCommandProvider ={ 1130commands ={ 'command:1' } , 1131} , 1132codeActionProvider ={ 1133resolveProvider =true , 1134} , 1135} , 1136handlers ={ 1137[ 'textDocument/codeAction' ] =function ( _ , _ , callback ) 1138callback ( nil , { 1139{ title ='Code Action 1' } , 1140} ) 1141end , 1142[ 'codeAction/resolve' ] =function ( _ , _ , callback ) 1143callback ( nil , { 1144title ='Code Action 1' , 1145command ={ 1146title ='Command 1' , 1147command ='command:1' , 1148} , 1149} ) 1150end , 1151} , 1152} ) 1153 1154local client_id =assert ( vim . lsp . start ( { 1155name ='dummy' , 1156cmd =server . cmd , 1157} )) 1158 1159vim . lsp . buf . code_action ( { apply =true } ) 1160vim . lsp . get_client_by_id ( client_id ): stop () 1161return server . messages 1162end ) 1163eq ( 'codeAction/resolve' , result [ 4 ]. method ) 1164eq ( 'workspace/executeCommand' , result [ 5 ]. method ) 1165eq ( 'command:1' , result [ 5 ]. params . command ) 1166end ) 1167 1168it ( 'supports disabled actions' , function () 1169exec_lua ( create_server_definition ) 1170local result =exec_lua ( function () 1171local server =_G . _create_server ( { 1172capabilities ={ 1173executeCommandProvider ={ 1174commands ={ 'command:1' } , 1175} , 1176codeActionProvider ={ 1177resolveProvider =true , 1178} , 1179} , 1180handlers ={ 1181[ 'textDocument/codeAction' ] =function ( _ , _ , callback ) 1182callback ( nil , { 1183{ 1184title ='Code Action 1' , 1185disabled ={ 1186reason ='This action is disabled' , 1187} , 1188} , 1189} ) 1190end , 1191[ 'codeAction/resolve' ] =function ( _ , _ , callback ) 1192callback ( nil , { 1193title ='Code Action 1' , 1194command ={ 1195title ='Command 1' , 1196command ='command:1' , 1197} , 1198} ) 1199end , 1200} , 1201} ) 1202 1203local client_id =assert ( vim . lsp . start ( { 1204name ='dummy' , 1205cmd =server . cmd , 1206} )) 1207 1208--- @diagnostic disable-next-line:duplicate-set-field 1209vim . notify =function ( message , code ) 1210server . messages [ # server . messages + 1 ] ={ 1211params ={ 1212message =message , 1213code =code , 1214} , 1215} 1216end 1217 1218vim . lsp . buf . code_action ( { apply =true } ) 1219vim . lsp . get_client_by_id ( client_id ): stop () 1220return server . messages 1221end ) 1222eq ( 1223exec_lua ( function () 1224return { message ='This action is disabled' , code =vim . log . levels . ERROR } 1225end ), 1226result [ 4 ]. params 1227) 1228-- No command is resolved/applied after selecting a disabled code action 1229eq ( 'shutdown' , result [ 5 ]. method ) 1230end ) 1231end ) 1232 1233describe ( 'vim.lsp.commands' , function () 1234it ( 'accepts only string keys' , function () 1235matches ( 1236'.*The key for commands in `vim.lsp.commands` must be a string' , 1237pcall_err ( exec_lua , 'vim.lsp.commands[1] = function() end' ) 1238) 1239end ) 1240 1241it ( 'accepts only function values' , function () 1242matches ( 1243'.*Command added to `vim.lsp.commands` must be a function' , 1244pcall_err ( exec_lua , 'vim.lsp.commands.dummy = 10' ) 1245) 1246end ) 1247end ) 1248 1249describe ( 'vim.lsp.buf.format' , function () 1250it ( 'aborts with notify if no client matches filter' , function () 1251local client --- @type vim.lsp.Client 1252test_rpc_server { 1253test_name ='basic_init' , 1254on_init =function ( c ) 1255client =c 1256end , 1257on_handler =function () 1258local notify_msg =exec_lua ( function () 1259local bufnr =vim . api . nvim_get_current_buf () 1260vim . lsp . buf_attach_client ( bufnr , _G . TEST_RPC_CLIENT_ID ) 1261local notify_msg --- @type string? 1262local notify =vim . notify 1263vim . notify =function ( msg , _ ) 1264notify_msg =msg 1265end 1266vim . lsp . buf . format ( { name ='does-not-exist' } ) 1267vim . notify =notify 1268return notify_msg 1269end ) 1270eq ( '[LSP] Format request failed, no matching language servers.' , notify_msg ) 1271client : stop () 1272end , 1273} 1274end ) 1275 1276it ( 'sends textDocument/formatting request to format buffer' , function () 1277local expected_handlers ={ 1278{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1279{ NIL , {} , { method ='start' , client_id =1 } } , 1280} 1281local client --- @type vim.lsp.Client 1282test_rpc_server { 1283test_name ='basic_formatting' , 1284on_init =function ( c ) 1285client =c 1286end , 1287on_handler =function ( _ , _ , ctx ) 1288table . remove ( expected_handlers ) 1289if ctx . method == 'start' then 1290local notify_msg =exec_lua ( function () 1291local bufnr =vim . api . nvim_get_current_buf () 1292vim . lsp . buf_attach_client ( bufnr , _G . TEST_RPC_CLIENT_ID ) 1293local notify_msg --- @type string? 1294local notify =vim . notify 1295vim . notify =function ( msg , _ ) 1296notify_msg =msg 1297end 1298vim . lsp . buf . format ( { bufnr =bufnr } ) 1299vim . notify =notify 1300return notify_msg 1301end ) 1302eq ( nil , notify_msg ) 1303elseif ctx . method == 'shutdown' then 1304client : stop () 1305end 1306end , 1307} 1308end ) 1309 1310it ( 'sends textDocument/rangeFormatting request to format a range' , function () 1311local expected_handlers ={ 1312{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1313{ NIL , {} , { method ='start' , client_id =1 } } , 1314} 1315local client --- @type vim.lsp.Client 1316test_rpc_server { 1317test_name ='range_formatting' , 1318on_init =function ( c ) 1319client =c 1320end , 1321on_handler =function ( _ , _ , ctx ) 1322table . remove ( expected_handlers ) 1323if ctx . method == 'start' then 1324local notify_msg =exec_lua ( function () 1325local bufnr =vim . api . nvim_get_current_buf () 1326vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 'foo' , 'bar' } ) 1327vim . lsp . buf_attach_client ( bufnr , _G . TEST_RPC_CLIENT_ID ) 1328local notify_msg --- @type string? 1329local notify =vim . notify 1330vim . notify =function ( msg , _ ) 1331notify_msg =msg 1332end 1333vim . lsp . buf . format ( { 1334bufnr =bufnr , 1335range ={ 1336start ={ 1 , 1 } , 1337[ 'end' ] ={ 1 , 1 } , 1338} , 1339} ) 1340vim . notify =notify 1341return notify_msg 1342end ) 1343eq ( nil , notify_msg ) 1344elseif ctx . method == 'shutdown' then 1345client : stop () 1346end 1347end , 1348} 1349end ) 1350 1351it ( 'formats range ending with -1 column' , function () 1352exec_lua ( create_server_definition ) 1353local result =exec_lua ( function () 1354local server =_G . _create_server ( { 1355capabilities ={ 1356documentRangeFormattingProvider =true , 1357} , 1358handlers ={ 1359[ 'textDocument/rangeFormatting' ] =function ( _ , params , callback ) 1360callback ( nil , { 1361{ 1362range =params . range , 1363newText ='function fn(abc)\n print("hello")' , 1364} , 1365} ) 1366end , 1367} , 1368} ) 1369local bufnr =vim . api . nvim_get_current_buf () 1370local client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =server . cmd } )) 1371vim . api . nvim_win_set_buf ( 0 , bufnr ) 1372vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 1373'function fn(abc )' , 1374' print("hello" )' , 1375'end' , 1376} ) 1377vim . lsp . buf . format ( { 1378bufnr =bufnr , 1379range ={ 1380start ={ 1 , 0 } , 1381[ 'end' ] ={ 2 , - 1 } , 1382} , 1383} ) 1384vim . lsp . get_client_by_id ( client_id ): stop () 1385return { 1386lines =vim . api . nvim_buf_get_lines ( bufnr , 0 , - 1 , true ), 1387range =server . messages [ 3 ]. params . range , 1388} 1389end ) 1390eq ( { 1391start ={ line =0 , character =0 } , 1392[ 'end' ] ={ line =1 , character =17 } , 1393} , result . range ) 1394eq ( { 'function fn(abc)' , ' print("hello")' , 'end' } , result . lines ) 1395end ) 1396 1397it ( 'sends textDocument/rangesFormatting request to format multiple ranges' , function () 1398local expected_handlers ={ 1399{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1400{ NIL , {} , { method ='start' , client_id =1 } } , 1401} 1402local client --- @type vim.lsp.Client 1403test_rpc_server { 1404test_name ='ranges_formatting' , 1405on_init =function ( c ) 1406client =c 1407end , 1408on_handler =function ( _ , _ , ctx ) 1409table . remove ( expected_handlers ) 1410if ctx . method == 'start' then 1411local notify_msg =exec_lua ( function () 1412local bufnr =vim . api . nvim_get_current_buf () 1413vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 'foo' , 'bar' , 'baz' } ) 1414vim . lsp . buf_attach_client ( bufnr , _G . TEST_RPC_CLIENT_ID ) 1415local notify_msg --- @type string? 1416local notify =vim . notify 1417vim . notify =function ( msg , _ ) 1418notify_msg =msg 1419end 1420vim . lsp . buf . format ( { 1421bufnr =bufnr , 1422range ={ 1423{ 1424start ={ 1 , 1 } , 1425[ 'end' ] ={ 1 , 1 } , 1426} , 1427{ 1428start ={ 2 , 2 } , 1429[ 'end' ] ={ 2 , 2 } , 1430} , 1431} , 1432} ) 1433vim . notify =notify 1434return notify_msg 1435end ) 1436eq ( nil , notify_msg ) 1437elseif ctx . method == 'shutdown' then 1438client : stop () 1439end 1440end , 1441} 1442end ) 1443 1444it ( 'can format async with an optional timeout' , function () 1445exec_lua ( create_server_definition ) 1446exec_lua ( function () 1447_G . edits ={ 1448{ 1449range ={ 1450start ={ line =0 , character =0 } , 1451[ 'end' ] ={ line =0 , character =0 } , 1452} , 1453newText ='formatted' , 1454} , 1455} 1456local opts ={ 1457capabilities ={ documentFormattingProvider =true } , 1458handlers ={ 1459[ 'textDocument/formatting' ] =function ( _ , _ , callback ) 1460table . insert ( _G . callbacks , callback ) 1461if # _G . callbacks == 2 then 1462callback ( nil , _G . edits ) 1463end 1464end , 1465} , 1466} 1467_G . server =_G . _create_server ( opts ) 1468_G . server2 =_G . _create_server ( opts ) 1469vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 1470vim . lsp . start ( { name ='dummy2' , cmd =_G . server2 . cmd } ) 1471end ) 1472 1473for _ , timeout_ms in ipairs ( { false , 20 } ) do 1474eq ( 14751 , 1476exec_lua ( function () 1477_G . callbacks ={} 1478vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { '' } ) 1479vim . lsp . buf . format ( { async =true , timeout_ms =timeout_ms or nil } ) 1480return # _G . callbacks 1481end ) 1482) 1483if not timeout_ms then 1484exec_lua ( '_G.callbacks[1](nil, _G.edits)' ) 1485end 1486local expected ={ timeout_ms and 'formatted' or 'formattedformatted' } 1487retry ( nil , 1000 , function () 1488eq ( expected , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1489end ) 1490if timeout_ms then 1491eq ( 1492{ method ='$/cancelRequest' , params ={ id =4 } } , 1493exec_lua ( 'return _G.server.messages[5] or _G.server2.messages[5]' ) 1494) 1495exec_lua ( '_G.callbacks[1](nil, _G.edits)' ) 1496eq ( expected , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1497end 1498eq ( 2 , exec_lua ( 'return #_G.callbacks' )) 1499end 1500end ) 1501 1502it ( 'format formats range in visual mode' , function () 1503exec_lua ( create_server_definition ) 1504local result =exec_lua ( function () 1505local server =_G . _create_server ( { 1506capabilities ={ 1507documentFormattingProvider =true , 1508documentRangeFormattingProvider =true , 1509} , 1510} ) 1511local bufnr =vim . api . nvim_get_current_buf () 1512local client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =server . cmd } )) 1513vim . api . nvim_win_set_buf ( 0 , bufnr ) 1514vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 'foo' , 'bar' } ) 1515vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1516vim . cmd . normal ( 'v' ) 1517vim . api . nvim_win_set_cursor ( 0 , { 2 , 3 } ) 1518vim . lsp . buf . format ( { bufnr =bufnr , false } ) 1519vim . lsp . get_client_by_id ( client_id ): stop () 1520return server . messages 1521end ) 1522eq ( 'textDocument/rangeFormatting' , result [ 3 ]. method ) 1523local expected_range ={ 1524start ={ line =0 , character =0 } , 1525[ 'end' ] ={ line =2 , character =0 } , 1526} 1527eq ( expected_range , result [ 3 ]. params . range ) 1528end ) 1529 1530it ( 'format formats range in visual line mode' , function () 1531exec_lua ( create_server_definition ) 1532local result =exec_lua ( function () 1533local server =_G . _create_server ( { 1534capabilities ={ 1535documentFormattingProvider =true , 1536documentRangeFormattingProvider =true , 1537} , 1538} ) 1539local bufnr =vim . api . nvim_get_current_buf () 1540local client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =server . cmd } )) 1541vim . api . nvim_win_set_buf ( 0 , bufnr ) 1542vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 'foo' , 'bar baz' } ) 1543vim . api . nvim_win_set_cursor ( 0 , { 1 , 2 } ) 1544vim . cmd . normal ( 'V' ) 1545vim . api . nvim_win_set_cursor ( 0 , { 2 , 1 } ) 1546vim . lsp . buf . format ( { bufnr =bufnr , false } ) 1547 1548-- Format again with visual lines going from bottom to top 1549-- Must result in same formatting 1550vim . cmd . normal ( '<ESC>' ) 1551vim . api . nvim_win_set_cursor ( 0 , { 2 , 1 } ) 1552vim . cmd . normal ( 'V' ) 1553vim . api . nvim_win_set_cursor ( 0 , { 1 , 2 } ) 1554vim . lsp . buf . format ( { bufnr =bufnr , false } ) 1555 1556vim . lsp . get_client_by_id ( client_id ): stop () 1557return server . messages 1558end ) 1559local expected_methods ={ 1560'initialize' , 1561'initialized' , 1562'textDocument/rangeFormatting' , 1563'$/cancelRequest' , 1564'textDocument/rangeFormatting' , 1565'$/cancelRequest' , 1566'shutdown' , 1567'exit' , 1568} 1569eq ( 1570expected_methods , 1571vim . tbl_map ( function ( x ) 1572return x . method 1573end , result ) 1574) 1575-- uses first column of start line and last column of end line 1576local expected_range ={ 1577start ={ line =0 , character =0 } , 1578[ 'end' ] ={ line =1 , character =7 } , 1579} 1580eq ( expected_range , result [ 3 ]. params . range ) 1581eq ( expected_range , result [ 5 ]. params . range ) 1582end ) 1583 1584it ( 'aborts with notify if no clients support requested method' , function () 1585exec_lua ( create_server_definition ) 1586exec_lua ( function () 1587vim . notify =function ( msg , _ ) 1588_G . notify_msg =msg 1589end 1590end ) 1591local fail_msg ='[LSP] Format request failed, no matching language servers.' 1592--- @param name string 1593--- @param formatting boolean 1594--- @param range_formatting boolean 1595local function check_notify ( name , formatting , range_formatting ) 1596local timeout_msg ='[LSP][' .. name .. '] timeout' 1597exec_lua ( function () 1598local server =_G . _create_server ( { 1599capabilities ={ 1600documentFormattingProvider =formatting , 1601documentRangeFormattingProvider =range_formatting , 1602} , 1603} ) 1604vim . lsp . start ( { name =name , cmd =server . cmd } ) 1605_G . notify_msg =nil 1606vim . lsp . buf . format ( { name =name , timeout_ms =1 } ) 1607end ) 1608eq ( 1609formatting and timeout_msg or fail_msg , 1610exec_lua ( function () 1611return _G . notify_msg 1612end ) 1613) 1614exec_lua ( function () 1615_G . notify_msg =nil 1616vim . lsp . buf . format ( { 1617name =name , 1618timeout_ms =1 , 1619range ={ 1620start ={ 1 , 0 } , 1621[ 'end' ] ={ 16221 , 16230 , 1624} , 1625} , 1626} ) 1627end ) 1628eq ( 1629range_formatting and timeout_msg or fail_msg , 1630exec_lua ( function () 1631return _G . notify_msg 1632end ) 1633) 1634end 1635check_notify ( 'none' , false , false ) 1636check_notify ( 'formatting' , true , false ) 1637check_notify ( 'rangeFormatting' , false , true ) 1638check_notify ( 'both' , true , true ) 1639end ) 1640end ) 1641 1642describe ( 'vim.lsp.buf.definition' , function () 1643it ( 'jumps to single location and can reuse win' , function () 1644exec_lua ( create_server_definition ) 1645local result =exec_lua ( function () 1646local bufnr =vim . api . nvim_get_current_buf () 1647local server =_G . _create_server ( { 1648capabilities ={ 1649definitionProvider =true , 1650} , 1651handlers ={ 1652[ 'textDocument/definition' ] =function ( _ , _ , callback ) 1653local location ={ 1654range ={ 1655start ={ line =0 , character =0 } , 1656[ 'end' ] ={ line =0 , character =0 } , 1657} , 1658uri =vim . uri_from_bufnr ( bufnr ), 1659} 1660callback ( nil , location ) 1661end , 1662} , 1663} ) 1664local win =vim . api . nvim_get_current_win () 1665vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 'local x = 10' , '' , 'print(x)' } ) 1666vim . api . nvim_win_set_cursor ( win , { 3 , 6 } ) 1667local client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =server . cmd } )) 1668vim . lsp . buf . definition () 1669return { 1670win =win , 1671bufnr =bufnr , 1672client_id =client_id , 1673cursor =vim . api . nvim_win_get_cursor ( win ), 1674messages =server . messages , 1675tagstack =vim . fn . gettagstack ( win ), 1676} 1677end ) 1678eq ( 'textDocument/definition' , result . messages [ 3 ]. method ) 1679eq ( { 1 , 0 } , result . cursor ) 1680eq ( 1 , # result . tagstack . items ) 1681eq ( 'x' , result . tagstack . items [ 1 ]. tagname ) 1682eq ( 3 , result . tagstack . items [ 1 ]. from [ 2 ]) 1683eq ( 7 , result . tagstack . items [ 1 ]. from [ 3 ]) 1684 1685local result_bufnr =api . nvim_get_current_buf () 1686n . feed ( ':tabe<CR>' ) 1687api . nvim_win_set_buf ( 0 , result_bufnr ) 1688local displayed_result_win =api . nvim_get_current_win () 1689n . feed ( ':vnew<CR>' ) 1690api . nvim_win_set_buf ( 0 , result . bufnr ) 1691api . nvim_win_set_cursor ( 0 , { 3 , 6 } ) 1692n . feed ( ':set switchbuf=usetab<CR>' ) 1693n . feed ( ':=vim.lsp.buf.definition()<CR>' ) 1694eq ( displayed_result_win , api . nvim_get_current_win ()) 1695exec_lua ( function () 1696vim . lsp . get_client_by_id ( result . client_id ): stop () 1697end ) 1698end ) 1699it ( 'merges results from multiple servers' , function () 1700exec_lua ( create_server_definition ) 1701local result =exec_lua ( function () 1702local bufnr =vim . api . nvim_get_current_buf () 1703local function serveropts ( character ) 1704return { 1705capabilities ={ 1706definitionProvider =true , 1707} , 1708handlers ={ 1709[ 'textDocument/definition' ] =function ( _ , _ , callback ) 1710local location ={ 1711range ={ 1712start ={ line =0 , character =character } , 1713[ 'end' ] ={ line =0 , character =character } , 1714} , 1715uri =vim . uri_from_bufnr ( bufnr ), 1716} 1717callback ( nil , location ) 1718end , 1719} , 1720} 1721end 1722local server1 =_G . _create_server ( serveropts ( 0 )) 1723local server2 =_G . _create_server ( serveropts ( 7 )) 1724local win =vim . api . nvim_get_current_win () 1725vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , true , { 'local x = 10' , '' , 'print(x)' } ) 1726vim . api . nvim_win_set_cursor ( win , { 3 , 6 } ) 1727local client_id1 =assert ( vim . lsp . start ( { name ='dummy1' , cmd =server1 . cmd } )) 1728local client_id2 =assert ( vim . lsp . start ( { name ='dummy2' , cmd =server2 . cmd } )) 1729local response 1730vim . lsp . buf . definition ( { 1731on_list =function ( r ) 1732response =r 1733end , 1734} ) 1735vim . lsp . get_client_by_id ( client_id1 ): stop () 1736vim . lsp . get_client_by_id ( client_id2 ): stop () 1737return response 1738end ) 1739eq ( 2 , # result . items ) 1740end ) 1741end ) 1742 1743describe ( 'vim.lsp.buf.workspace_diagnostics()' , function () 1744local fake_uri ='file:///fake/uri' 1745 1746--- @param kind lsp.DocumentDiagnosticReportKind 1747--- @param msg string 1748--- @param pos integer 1749--- @return lsp.WorkspaceDocumentDiagnosticReport 1750local function make_report ( kind , msg , pos ) 1751return { 1752kind =kind , 1753uri =fake_uri , 1754items ={ 1755{ 1756range ={ 1757start ={ line =pos , character =pos } , 1758[ 'end' ] ={ line =pos , character =pos } , 1759} , 1760message =msg , 1761severity =1 , 1762} , 1763} , 1764} 1765end 1766 1767--- @param items lsp.WorkspaceDocumentDiagnosticReport[] 1768--- @return integer 1769local function setup_server ( items ) 1770exec_lua ( create_server_definition ) 1771return exec_lua ( function () 1772_G . server =_G . _create_server ( { 1773capabilities ={ 1774diagnosticProvider ={ 1775documentSelector =vim . NIL , 1776interFileDependencies =false , 1777workspaceDiagnostics =true , 1778} , 1779} , 1780handlers ={ 1781[ 'workspace/diagnostic' ] =function ( _ , _ , callback ) 1782callback ( nil , { items =items } ) 1783end , 1784} , 1785} ) 1786local client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } )) 1787vim . lsp . buf . workspace_diagnostics () 1788return client_id 1789end , { items } ) 1790end 1791 1792it ( 'updates diagnostics obtained with vim.diagnostic.get()' , function () 1793setup_server ( { make_report ( 'full' , 'Error here' , 1 ) } ) 1794 1795retry ( nil , nil , function () 1796eq ( 17971 , 1798exec_lua ( function () 1799return # vim . diagnostic . get () 1800end ) 1801) 1802end ) 1803 1804eq ( 1805'Error here' , 1806exec_lua ( function () 1807return vim . diagnostic . get ()[ 1 ]. message 1808end ) 1809) 1810end ) 1811 1812it ( 'ignores unchanged diagnostic reports' , function () 1813setup_server ( { make_report ( 'unchanged' , '' , 1 ) } ) 1814 1815eq ( 18160 , 1817exec_lua ( function () 1818-- Wait for diagnostics to be processed. 1819vim . uv . sleep ( 50 ) 1820 1821return # vim . diagnostic . get () 1822end ) 1823) 1824end ) 1825 1826it ( 'favors document diagnostics over workspace diagnostics' , function () 1827local client_id =setup_server ( { make_report ( 'full' , 'Workspace error' , 1 ) } ) 1828local diagnostic_bufnr =exec_lua ( function () 1829return vim . uri_to_bufnr ( fake_uri ) 1830end ) 1831 1832exec_lua ( function () 1833vim . lsp . diagnostic . on_diagnostic ( nil , { 1834kind ='full' , 1835items ={ 1836{ 1837range ={ 1838start ={ line =2 , character =2 } , 1839[ 'end' ] ={ line =2 , character =2 } , 1840} , 1841message ='Document error' , 1842severity =1 , 1843} , 1844} , 1845} , { 1846method ='textDocument/diagnostic' , 1847params ={ 1848textDocument ={ uri =fake_uri } , 1849} , 1850client_id =client_id , 1851bufnr =diagnostic_bufnr , 1852} ) 1853end ) 1854 1855eq ( 18561 , 1857exec_lua ( function () 1858return # vim . diagnostic . get ( diagnostic_bufnr ) 1859end ) 1860) 1861 1862eq ( 1863'Document error' , 1864exec_lua ( function () 1865return vim . diagnostic . get ( vim . uri_to_bufnr ( fake_uri ))[ 1 ]. message 1866end ) 1867) 1868end ) 1869end ) 1870 1871describe ( 'vim.lsp.buf.hover()' , function () 1872it ( 'handles empty contents' , function () 1873exec_lua ( create_server_definition ) 1874exec_lua ( function () 1875local server =_G . _create_server ( { 1876capabilities ={ 1877hoverProvider =true , 1878} , 1879handlers ={ 1880[ 'textDocument/hover' ] =function ( _ , _ , callback ) 1881local res ={ 1882contents ={ 1883kind ='markdown' , 1884value ='' , 1885} , 1886} 1887callback ( nil , res ) 1888end , 1889} , 1890} ) 1891vim . lsp . start ( { name ='dummy' , cmd =server . cmd } ) 1892end ) 1893 1894eq ( 'Empty hover response' , exec_capture ( 'lua vim.lsp.buf.hover()' )) 1895end ) 1896 1897it ( 'treats markedstring array as not empty' , function () 1898exec_lua ( create_server_definition ) 1899exec_lua ( function () 1900local server =_G . _create_server ( { 1901capabilities ={ 1902hoverProvider =true , 1903} , 1904handlers ={ 1905[ 'textDocument/hover' ] =function ( _ , _ , callback ) 1906local res ={ 1907contents ={ 1908{ 1909language ='java' , 1910value ='Example' , 1911} , 1912'doc comment' , 1913} , 1914} 1915callback ( nil , res ) 1916end , 1917} , 1918} ) 1919vim . lsp . start ( { name ='dummy' , cmd =server . cmd } ) 1920end ) 1921 1922eq ( '' , exec_capture ( 'lua vim.lsp.buf.hover()' )) 1923end ) 1924end ) 1925end )