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 , setup , teardown , pending = 7t . describe , t . it , t . before_each , t . after_each , t . setup , t . teardown , t . pending 8local buf_lines =n . buf_lines 9local command =n . command 10local dedent =t . dedent 11local exec_lua =n . exec_lua 12local feed =n . feed 13local eq =t . eq 14local eval =n . eval 15local matches =t . matches 16local pcall_err =t . pcall_err 17local pesc =vim . pesc 18local insert =n . insert 19local fn =n . fn 20local retry =t . retry 21local stop =n . stop 22local NIL =vim . NIL 23local read_file =t . read_file 24local write_file =t . write_file 25local is_ci =t . is_ci 26local api =n . api 27local is_os =t . is_os 28local skip =t . skip 29local mkdir =t . mkdir 30local tmpname =t . tmpname 31 32local clear_notrace =t_lsp . clear_notrace 33local create_server_definition =t_lsp . create_server_definition 34local fake_lsp_code =t_lsp . fake_lsp_code 35local fake_lsp_logfile =t_lsp . fake_lsp_logfile 36local test_rpc_server =t_lsp . test_rpc_server 37local create_tcp_echo_server =t_lsp . create_tcp_echo_server 38 39local function get_buf_option ( name , bufnr ) 40return exec_lua ( function () 41bufnr =bufnr or _G . BUFFER 42return vim . api . nvim_get_option_value ( name , { buf =bufnr } ) 43end ) 44end 45 46--- True if buf-local option (func option like 'tagfunc') holds `vim.lsp[name]`. 47local function buf_option_is_lsp ( name , bufnr ) 48return exec_lua ( function () 49bufnr =bufnr or _G . BUFFER 50return vim . api . nvim_get_option_value ( name , { buf =bufnr } ) == vim . lsp [ name ] 51end ) 52end 53 54local function make_edit ( y_0 , x_0 , y_1 , x_1 , text ) 55return { 56range ={ 57start ={ line =y_0 , character =x_0 } , 58[ 'end' ] ={ line =y_1 , character =x_1 } , 59} , 60newText =type ( text ) == 'table' and table . concat ( text , '\n' ) or ( text or '' ), 61} 62end 63 64--- @param edits [integer, integer, integer, integer, string|string[]][] 65--- @param encoding? string 66local function apply_text_edits ( edits , encoding ) 67local edits1 =vim . tbl_map ( 68--- @param edit [integer, integer, integer, integer, string|string[]] 69function ( edit ) 70return make_edit ( unpack ( edit )) 71end , 72edits 73) 74exec_lua ( function () 75vim . lsp . util . apply_text_edits ( edits1 , 1 , encoding or 'utf-16' ) 76end ) 77end 78 79--- @param notification_cb fun(method: 'body' | 'error', args: any) 80local function verify_single_notification ( notification_cb ) 81local called =false 82n . run ( nil , function ( method , args ) 83notification_cb ( method , args ) 84stop () 85called =true 86end , nil , 1000 ) 87eq ( true , called ) 88end 89 90-- TODO(justinmk): hangs on Windows https://github.com/neovim/neovim/pull/11837 91if skip ( is_os ( 'win' )) then 92return 93end 94 95describe ( 'LSP' , function () 96before_each ( function () 97clear_notrace () 98end ) 99 100after_each ( function () 101stop () 102exec_lua ( function () 103vim . iter ( vim . lsp . get_clients ( { _uninitialized =true } )): each ( function ( client ) 104client : stop ( true ) 105end ) 106end ) 107api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 108end ) 109 110teardown ( function () 111os . remove ( fake_lsp_logfile ) 112end ) 113 114describe ( 'server_name specified' , function () 115before_each ( function () 116-- Run an instance of nvim on the file which contains our "scripts". 117-- Pass TEST_NAME to pick the script. 118local test_name ='basic_init' 119exec_lua ( function () 120_G . lsp =require ( 'vim.lsp' ) 121function _G . test__start_client () 122return vim . lsp . start ( { 123cmd_env ={ 124NVIM_LOG_FILE =fake_lsp_logfile , 125NVIM_APPNAME ='nvim_lsp_test' , 126} , 127cmd ={ 128vim . v . progpath , 129'-l' , 130fake_lsp_code , 131test_name , 132} , 133workspace_folders ={ 134{ 135uri ='file://' .. vim . uv . cwd (), 136name ='test_folder' , 137} , 138} , 139} , { attach =false } ) 140end 141_G . TEST_CLIENT1 =_G . test__start_client () 142end ) 143end ) 144 145it ( 'start_client(), Client:stop()' , function () 146retry ( nil , 4000 , function () 147eq ( 1481 , 149exec_lua ( function () 150return # vim . lsp . get_clients () 151end ) 152) 153end ) 154eq ( 1552 , 156exec_lua ( function () 157_G . TEST_CLIENT2 =_G . test__start_client () 158return _G . TEST_CLIENT2 159end ) 160) 161eq ( 1623 , 163exec_lua ( function () 164_G . TEST_CLIENT3 =_G . test__start_client () 165return _G . TEST_CLIENT3 166end ) 167) 168retry ( nil , 4000 , function () 169eq ( 1703 , 171exec_lua ( function () 172return # vim . lsp . get_clients () 173end ) 174) 175end ) 176 177eq ( 178false , 179exec_lua ( function () 180return vim . lsp . get_client_by_id ( _G . TEST_CLIENT1 ) == nil 181end ) 182) 183eq ( 184false , 185exec_lua ( function () 186return vim . lsp . get_client_by_id ( _G . TEST_CLIENT1 ). is_stopped () 187end ) 188) 189exec_lua ( function () 190return vim . lsp . get_client_by_id ( _G . TEST_CLIENT1 ). stop () 191end ) 192retry ( nil , 4000 , function () 193eq ( 1942 , 195exec_lua ( function () 196return # vim . lsp . get_clients () 197end ) 198) 199end ) 200eq ( 201true , 202exec_lua ( function () 203return vim . lsp . get_client_by_id ( _G . TEST_CLIENT1 ) == nil 204end ) 205) 206 207exec_lua ( function () 208vim . lsp . get_client_by_id ( _G . TEST_CLIENT2 ): stop () 209vim . lsp . get_client_by_id ( _G . TEST_CLIENT3 ): stop () 210end ) 211retry ( nil , 4000 , function () 212eq ( 2130 , 214exec_lua ( function () 215return # vim . lsp . get_clients () 216end ) 217) 218end ) 219end ) 220 221it ( 'does not reuse an already-stopping client #33616' , function () 222-- we immediately try to start a second client with the same name/root 223-- before the first one has finished shutting down; we must get a new id. 224local clients =exec_lua ( function () 225local client1 =assert ( vim . lsp . start ( { 226name ='dup-test' , 227cmd ={ vim . v . progpath , '-l' , fake_lsp_code , 'basic_init' } , 228} , { attach =false } )) 229vim . lsp . get_client_by_id ( client1 ): stop () 230local client2 =assert ( vim . lsp . start ( { 231name ='dup-test' , 232cmd ={ vim . v . progpath , '-l' , fake_lsp_code , 'basic_init' } , 233} , { attach =false } )) 234return { client1 , client2 } 235end ) 236local c1 , c2 =clients [ 1 ], clients [ 2 ] 237eq ( false , c1 == c2 , 'Expected a fresh client while the old one is stopping' ) 238end ) 239end ) 240 241describe ( 'basic_init test' , function () 242it ( 'should run correctly' , function () 243local expected_handlers ={ 244{ NIL , {} , { method ='test' , client_id =1 } } , 245} 246test_rpc_server { 247test_name ='basic_init' , 248on_init =function ( client , _ ) 249-- client is a dummy object which will queue up commands to be run 250-- once the server initializes. It can't accept lua callbacks or 251-- other types that may be unserializable for now. 252client : stop () 253end , 254-- If the program timed out, then code will be nil. 255on_exit =function ( code , signal ) 256eq ( 0 , code , 'exit code' ) 257eq ( 0 , signal , 'exit signal' ) 258end , 259-- Note that NIL must be used here. 260-- on_handler(err, method, result, client_id) 261on_handler =function ( ... ) 262eq ( table . remove ( expected_handlers ), { ... } ) 263end , 264} 265end ) 266 267it ( 'should fail' , function () 268local expected_handlers ={ 269{ NIL , {} , { method ='test' , client_id =1 } } , 270} 271test_rpc_server { 272test_name ='basic_init' , 273on_init =function ( client ) 274client : notify ( 'test' ) 275client : stop () 276end , 277on_exit =function ( code , signal ) 278eq ( 101 , code , 'exit code' ) -- See fake-lsp-server.lua 279eq ( 0 , signal , 'exit signal' ) 280t . assert_log ( 281pesc ( [[assert_eq failed: left == "\"shutdown\"", right == "\"test\""]] ), 282fake_lsp_logfile 283) 284end , 285on_handler =function ( ... ) 286eq ( table . remove ( expected_handlers ), { ... } , 'expected handler' ) 287end , 288} 289end ) 290 291it ( 'should send didChangeConfiguration after initialize if there are settings' , function () 292test_rpc_server ( { 293test_name ='basic_init_did_change_configuration' , 294on_init =function ( client , _ ) 295client : stop () 296end , 297on_exit =function ( code , signal ) 298eq ( 0 , code , 'exit code' ) 299eq ( 0 , signal , 'exit signal' ) 300end , 301settings ={ 302dummy =1 , 303} , 304} ) 305end ) 306 307it ( 308"should set the client's offset_encoding when positionEncoding capability is supported" , 309function () 310exec_lua ( create_server_definition ) 311local result =exec_lua ( function () 312local server =_G . _create_server ( { 313capabilities ={ 314positionEncoding ='utf-8' , 315} , 316} ) 317 318local client_id =vim . lsp . start ( { 319name ='dummy' , 320cmd =server . cmd , 321} ) 322 323if not client_id then 324return 'vim.lsp.start did not return client_id' 325end 326 327local client =vim . lsp . get_client_by_id ( client_id ) 328if not client then 329return 'No client found with id ' .. client_id 330end 331return client . offset_encoding 332end ) 333eq ( 'utf-8' , result ) 334end 335) 336 337it ( 'should succeed with manual shutdown' , function () 338local expected_handlers ={ 339{ NIL , {} , { method ='shutdown' , bufnr =1 , client_id =1 , request_id =2 , version =0 } } , 340{ NIL , {} , { method ='test' , client_id =1 } } , 341} 342test_rpc_server { 343test_name ='basic_init' , 344on_init =function ( client ) 345eq ( 0 , client . server_capabilities (). textDocumentSync . change ) 346client : request ( 'shutdown' ) 347client : notify ( 'exit' ) 348client : stop () 349end , 350on_exit =function ( code , signal ) 351eq ( 0 , code , 'exit code' ) 352eq ( 0 , signal , 'exit signal' ) 353end , 354on_handler =function ( ... ) 355eq ( table . remove ( expected_handlers ), { ... } , 'expected handler' ) 356end , 357} 358end ) 359 360it ( 'should detach buffer in response to nvim_buf_detach' , function () 361local expected_handlers ={ 362{ NIL , {} , { method ='shutdown' , client_id =1 } } , 363{ NIL , {} , { method ='finish' , client_id =1 } } , 364} 365local client --- @type vim.lsp.Client 366test_rpc_server { 367test_name ='basic_finish' , 368on_setup =function () 369exec_lua ( function () 370_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 371end ) 372eq ( 373true , 374exec_lua ( function () 375return vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 376end ) 377) 378eq ( 379true , 380exec_lua ( function () 381return vim . lsp . buf_is_attached ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 382end ) 383) 384exec_lua ( function () 385vim . cmd ( _G . BUFFER .. 'bwipeout' ) 386end ) 387end , 388on_init =function ( _client ) 389client =_client 390client : notify ( 'finish' ) 391end , 392on_exit =function ( code , signal ) 393eq ( 0 , code , 'exit code' ) 394eq ( 0 , signal , 'exit signal' ) 395end , 396on_handler =function ( err , result , ctx ) 397eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 398if ctx . method == 'finish' then 399exec_lua ( function () 400return vim . lsp . buf_detach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 401end ) 402eq ( 403false , 404exec_lua ( function () 405return vim . lsp . buf_is_attached ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 406end ) 407) 408client : stop () 409end 410end , 411} 412end ) 413 414it ( 'should fire autocommands on attach and detach' , function () 415local client --- @type vim.lsp.Client 416test_rpc_server { 417test_name ='basic_init' , 418on_setup =function () 419exec_lua ( function () 420_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 421vim . api . nvim_create_autocmd ( 'LspAttach' , { 422callback =function ( ev ) 423local client0 =assert ( vim . lsp . get_client_by_id ( ev . data . client_id )) 424vim . g . lsp_attached =client0 . name 425end , 426} ) 427vim . api . nvim_create_autocmd ( 'LspDetach' , { 428callback =function ( ev ) 429local client0 =assert ( vim . lsp . get_client_by_id ( ev . data . client_id )) 430vim . g . lsp_detached =client0 . name 431end , 432} ) 433end ) 434end , 435on_init =function ( _client ) 436client =_client 437eq ( 438true , 439exec_lua ( function () 440return vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 441end ) 442) 443client : notify ( 'finish' ) 444end , 445on_handler =function ( _ , _ , ctx ) 446if ctx . method == 'finish' then 447eq ( 'basic_init' , api . nvim_get_var ( 'lsp_attached' )) 448exec_lua ( function () 449return vim . lsp . buf_detach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 450end ) 451eq ( 'basic_init' , api . nvim_get_var ( 'lsp_detached' )) 452client : stop () 453end 454end , 455} 456end ) 457 458it ( 'should set default options on attach' , function () 459local client --- @type vim.lsp.Client 460test_rpc_server { 461test_name ='set_defaults_all_capabilities' , 462on_init =function ( _client ) 463client =_client 464exec_lua ( function () 465_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 466vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 467end ) 468end , 469on_handler =function ( _ , _ , ctx ) 470if ctx . method == 'test' then 471eq ( true , buf_option_is_lsp ( 'tagfunc' )) 472eq ( true , buf_option_is_lsp ( 'omnifunc' )) 473eq ( 'v:lua.vim.lsp.formatexpr()' , get_buf_option ( 'formatexpr' )) 474eq ( '' , get_buf_option ( 'keywordprg' )) 475eq ( 476true , 477exec_lua ( function () 478local keymap --- @type table<string,any> 479local called =false 480local origin =vim . lsp . buf . hover 481vim . lsp . buf . hover =function () 482called =true 483end 484vim . _with ( { buf =_G . BUFFER } , function () 485keymap =vim . fn . maparg ( 'K' , 'n' , false , true ) 486end ) 487keymap . callback () 488vim . lsp . buf . hover =origin 489return called 490end ) 491) 492client : stop () 493end 494end , 495on_exit =function ( _ , _ ) 496eq ( '' , get_buf_option ( 'tagfunc' )) 497eq ( '' , get_buf_option ( 'omnifunc' )) 498eq ( '' , get_buf_option ( 'formatexpr' )) 499eq ( 500true , 501exec_lua ( function () 502local keymap --- @type string 503vim . _with ( { buf =_G . BUFFER } , function () 504keymap =vim . fn . maparg ( 'K' , 'n' , false , false ) 505end ) 506return keymap : match ( '<Lua %d+: .*runtime/lua/vim/lsp%.lua:%d+>' ) ~= nil 507end ) 508) 509end , 510} 511end ) 512 513it ( 'should overwrite options set by ftplugins' , function () 514local client --- @type vim.lsp.Client 515local BUFFER_1 --- @type integer 516local BUFFER_2 --- @type integer 517test_rpc_server { 518test_name ='set_defaults_all_capabilities' , 519on_init =function ( _client ) 520client =_client 521exec_lua ( function () 522vim . api . nvim_command ( 'filetype plugin on' ) 523BUFFER_1 =vim . api . nvim_create_buf ( false , true ) 524BUFFER_2 =vim . api . nvim_create_buf ( false , true ) 525vim . api . nvim_set_option_value ( 'filetype' , 'man' , { buf =BUFFER_1 } ) 526vim . api . nvim_set_option_value ( 'filetype' , 'xml' , { buf =BUFFER_2 } ) 527end ) 528 529-- Sanity check to ensure that some values are set after setting filetype. 530eq ( "v:lua.require'man'.goto_tag" , get_buf_option ( 'tagfunc' , BUFFER_1 )) 531eq ( 'xmlcomplete#CompleteTags' , get_buf_option ( 'omnifunc' , BUFFER_2 )) 532eq ( 'xmlformat#Format()' , get_buf_option ( 'formatexpr' , BUFFER_2 )) 533 534exec_lua ( function () 535vim . lsp . buf_attach_client ( BUFFER_1 , _G . TEST_RPC_CLIENT_ID ) 536vim . lsp . buf_attach_client ( BUFFER_2 , _G . TEST_RPC_CLIENT_ID ) 537end ) 538end , 539on_handler =function ( _ , _ , ctx ) 540if ctx . method == 'test' then 541eq ( true , buf_option_is_lsp ( 'tagfunc' , BUFFER_1 )) 542eq ( true , buf_option_is_lsp ( 'omnifunc' , BUFFER_2 )) 543eq ( 'v:lua.vim.lsp.formatexpr()' , get_buf_option ( 'formatexpr' , BUFFER_2 )) 544client : stop () 545end 546end , 547on_exit =function ( _ , _ ) 548eq ( '' , get_buf_option ( 'tagfunc' , BUFFER_1 )) 549eq ( '' , get_buf_option ( 'omnifunc' , BUFFER_2 )) 550eq ( '' , get_buf_option ( 'formatexpr' , BUFFER_2 )) 551end , 552} 553end ) 554 555it ( 'should not overwrite user-defined options' , function () 556local client --- @type vim.lsp.Client 557test_rpc_server { 558test_name ='set_defaults_all_capabilities' , 559on_init =function ( _client ) 560client =_client 561exec_lua ( function () 562_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 563vim . api . nvim_set_option_value ( 'tagfunc' , 'tfu' , { buf =_G . BUFFER } ) 564vim . api . nvim_set_option_value ( 'omnifunc' , 'ofu' , { buf =_G . BUFFER } ) 565vim . api . nvim_set_option_value ( 'formatexpr' , 'fex' , { buf =_G . BUFFER } ) 566vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 567end ) 568end , 569on_handler =function ( _ , _ , ctx ) 570if ctx . method == 'test' then 571eq ( 'tfu' , get_buf_option ( 'tagfunc' )) 572eq ( 'ofu' , get_buf_option ( 'omnifunc' )) 573eq ( 'fex' , get_buf_option ( 'formatexpr' )) 574client : stop () 575end 576end , 577on_exit =function ( _ , _ ) 578eq ( 'tfu' , get_buf_option ( 'tagfunc' )) 579eq ( 'ofu' , get_buf_option ( 'omnifunc' )) 580eq ( 'fex' , get_buf_option ( 'formatexpr' )) 581end , 582} 583end ) 584 585it ( 'should detach buffer on bufwipe' , function () 586exec_lua ( create_server_definition ) 587local result =exec_lua ( function () 588local server =_G . _create_server () 589local bufnr1 =vim . api . nvim_create_buf ( false , true ) 590local bufnr2 =vim . api . nvim_create_buf ( false , true ) 591local detach_called1 =false 592local detach_called2 =false 593vim . api . nvim_create_autocmd ( 'LspDetach' , { 594buf =bufnr1 , 595callback =function () 596detach_called1 =true 597end , 598} ) 599vim . api . nvim_create_autocmd ( 'LspDetach' , { 600buf =bufnr2 , 601callback =function () 602detach_called2 =true 603end , 604} ) 605vim . api . nvim_set_current_buf ( bufnr1 ) 606local client_id =assert ( vim . lsp . start ( { name ='detach-dummy' , cmd =server . cmd } )) 607local client =assert ( vim . lsp . get_client_by_id ( client_id )) 608vim . api . nvim_set_current_buf ( bufnr2 ) 609vim . lsp . start ( { name ='detach-dummy' , cmd =server . cmd } ) 610assert ( vim . tbl_count ( client . attached_buffers ) == 2 ) 611vim . api . nvim_buf_delete ( bufnr1 , { force =true } ) 612assert ( vim . tbl_count ( client . attached_buffers ) == 1 ) 613vim . api . nvim_buf_delete ( bufnr2 , { force =true } ) 614assert ( vim . tbl_count ( client . attached_buffers ) == 0 ) 615return detach_called1 and detach_called2 616end ) 617eq ( true , result ) 618end ) 619 620it ( 'should not re-attach buffer if it was deleted in on_init #28575' , function () 621exec_lua ( create_server_definition ) 622exec_lua ( function () 623local server =_G . _create_server ( { 624handlers ={ 625initialize =function ( _ , _ , callback ) 626vim . schedule ( function () 627callback ( nil , { capabilities ={} } ) 628end ) 629end , 630} , 631} ) 632local bufnr =vim . api . nvim_create_buf ( false , true ) 633local on_init_called =false 634local client_id =assert ( vim . lsp . start ( { 635name ='detach-dummy' , 636cmd =server . cmd , 637on_init =function () 638vim . api . nvim_buf_delete ( bufnr , {} ) 639on_init_called =true 640end , 641} )) 642vim . lsp . buf_attach_client ( bufnr , client_id ) 643local ok =vim . wait ( 1000 , function () 644return on_init_called 645end ) 646assert ( ok , 'on_init was not called' ) 647end ) 648end ) 649 650it ( 'should allow on_lines + nvim_buf_delete during LSP initialization #28575' , function () 651exec_lua ( create_server_definition ) 652exec_lua ( function () 653local initialized =false 654local server =_G . _create_server ( { 655handlers ={ 656initialize =function ( _ , _ , callback ) 657vim . schedule ( function () 658callback ( nil , { capabilities ={} } ) 659initialized =true 660end ) 661end , 662} , 663} ) 664local bufnr =vim . api . nvim_create_buf ( false , true ) 665vim . api . nvim_set_current_buf ( bufnr ) 666vim . lsp . start ( { 667name ='detach-dummy' , 668cmd =server . cmd , 669} ) 670vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , false , { 'hello' } ) 671vim . api . nvim_buf_delete ( bufnr , {} ) 672local ok =vim . wait ( 1000 , function () 673return initialized 674end ) 675assert ( ok , 'lsp did not initialize' ) 676end ) 677end ) 678 679it ( 'client should return settings via workspace/configuration handler' , function () 680local expected_handlers ={ 681{ NIL , {} , { method ='shutdown' , client_id =1 } } , 682{ 683NIL , 684{ 685items ={ 686{ section ='testSetting1' } , 687{ section ='testSetting2' } , 688{ section ='test.Setting3' } , 689{ section ='test.Setting4' } , 690{} , 691{ section ='' } , 692} , 693} , 694{ method ='workspace/configuration' , client_id =1 } , 695} , 696{ NIL , {} , { method ='start' , client_id =1 } } , 697} 698local client --- @type vim.lsp.Client 699test_rpc_server { 700test_name ='check_workspace_configuration' , 701on_init =function ( _client ) 702client =_client 703end , 704on_exit =function ( code , signal ) 705eq ( 0 , code , 'exit code' ) 706eq ( 0 , signal , 'exit signal' ) 707end , 708on_handler =function ( err , result , ctx ) 709eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 710if ctx . method == 'start' then 711exec_lua ( function () 712local client0 =vim . lsp . get_client_by_id ( _G . TEST_RPC_CLIENT_ID ) 713client0 . settings ={ 714testSetting1 =true , 715testSetting2 =false , 716test ={ Setting3 ='nested' } , 717} 718end ) 719end 720if ctx . method == 'workspace/configuration' then 721local server_result =exec_lua ( 722[[ 723local method, params = ... 724return require 'vim.lsp.handlers'['workspace/configuration']( 725err, 726params, 727{ method = method, client_id = _G.TEST_RPC_CLIENT_ID } 728) 729]] , 730ctx . method , 731result 732) 733client : notify ( 'workspace/configuration' , server_result ) 734end 735if ctx . method == 'shutdown' then 736client : stop () 737end 738end , 739} 740end ) 741 742it ( 743'workspace/configuration returns NIL per section if client was started without config.settings' , 744function () 745local result =nil 746test_rpc_server { 747test_name ='basic_init' , 748on_init =function ( c ) 749c . stop () 750end , 751on_setup =function () 752result =exec_lua ( function () 753local result0 ={ 754items ={ 755{ section ='foo' } , 756{ section ='bar' } , 757} , 758} 759return vim . lsp . handlers [ 'workspace/configuration' ]( 760nil , 761result0 , 762{ client_id =_G . TEST_RPC_CLIENT_ID } 763) 764end ) 765end , 766} 767eq ( { NIL , NIL } , result ) 768end 769) 770 771it ( 'should verify capabilities sent' , function () 772local expected_handlers ={ 773{ NIL , {} , { method ='shutdown' , client_id =1 } } , 774} 775test_rpc_server { 776test_name ='basic_check_capabilities' , 777on_init =function ( client ) 778client : stop () 779local full_kind =exec_lua ( function () 780return require 'vim.lsp.protocol' . TextDocumentSyncKind . Full 781end ) 782eq ( full_kind , client . server_capabilities (). textDocumentSync . change ) 783eq ( { includeText =false } , client . server_capabilities (). textDocumentSync . save ) 784eq ( false , client . server_capabilities (). codeLensProvider ) 785end , 786on_exit =function ( code , signal ) 787eq ( 0 , code , 'exit code' ) 788eq ( 0 , signal , 'exit signal' ) 789end , 790on_handler =function ( ... ) 791eq ( table . remove ( expected_handlers ), { ... } , 'expected handler' ) 792end , 793} 794end ) 795 796it ( 'BufWritePost sends didSave with bool textDocumentSync.save' , function () 797local expected_handlers ={ 798{ NIL , {} , { method ='shutdown' , client_id =1 } } , 799{ NIL , {} , { method ='start' , client_id =1 } } , 800} 801local client --- @type vim.lsp.Client 802test_rpc_server { 803test_name ='text_document_sync_save_bool' , 804on_init =function ( c ) 805client =c 806end , 807on_exit =function ( code , signal ) 808eq ( 0 , code , 'exit code' ) 809eq ( 0 , signal , 'exit signal' ) 810end , 811on_handler =function ( err , result , ctx ) 812eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 813if ctx . method == 'start' then 814exec_lua ( function () 815_G . BUFFER =vim . api . nvim_get_current_buf () 816vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 817vim . api . nvim_exec_autocmds ( 'BufWritePost' , { buf =_G . BUFFER , modeline =false } ) 818end ) 819else 820client : stop () 821end 822end , 823} 824end ) 825 826it ( 'saveas sends didOpen to all attached servers only, if filename changed' , function () 827local tmpfile_new =tmpname ( false ) 828exec_lua ( create_server_definition ) 829local messages =exec_lua ( function () 830local server1 =_G . _create_server () 831local server2 =_G . _create_server () 832local server3 =_G . _create_server () 833local client1_id =assert ( vim . lsp . start ( { name ='dummy1' , cmd =server1 . cmd } )) 834local client2_id =assert ( vim . lsp . start ( { name ='dummy2' , cmd =server2 . cmd } )) 835-- Attached to another buffer, so it must not be notified about this one. 836local other_buf =vim . api . nvim_create_buf ( true , false ) 837local client3_id = 838assert ( vim . lsp . start ( { name ='dummy3' , cmd =server3 . cmd } , { bufnr =other_buf } )) 839 840vim . cmd ( 'saveas ' .. tmpfile_new ) 841 842vim . lsp . get_client_by_id ( client1_id ): stop () 843vim . lsp . get_client_by_id ( client2_id ): stop () 844vim . lsp . get_client_by_id ( client3_id ): stop () 845 846return { 847server1 =server1 . messages , 848server2 =server2 . messages , 849server3 =vim . tbl_map ( function ( m ) 850return m . method 851end , server3 . messages ), 852} 853end ) 854eq ( 'textDocument/didClose' , messages . server1 [ 3 ]. method ) 855eq ( 'textDocument/didOpen' , messages . server1 [ 4 ]. method ) 856eq ( 'textDocument/didSave' , messages . server1 [ 5 ]. method ) 857 858eq ( 'textDocument/didClose' , messages . server2 [ 3 ]. method ) 859eq ( 'textDocument/didOpen' , messages . server2 [ 4 ]. method ) 860eq ( 'textDocument/didSave' , messages . server2 [ 5 ]. method ) 861 862eq ( { 'initialize' , 'initialized' , 'shutdown' , 'exit' } , messages . server3 ) 863end ) 864 865it ( 'BufWritePre does not send notifications if server lacks willSave capabilities' , function () 866exec_lua ( create_server_definition ) 867local messages =exec_lua ( function () 868local server =_G . _create_server ( { 869capabilities ={ 870textDocumentSync ={ 871willSave =false , 872willSaveWaitUntil =false , 873} , 874} , 875} ) 876local client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =server . cmd } )) 877local buf =vim . api . nvim_get_current_buf () 878vim . api . nvim_exec_autocmds ( 'BufWritePre' , { buf =buf , modeline =false } ) 879vim . lsp . get_client_by_id ( client_id ): stop () 880return server . messages 881end ) 882eq ( 4 , # messages ) 883eq ( 'initialize' , messages [ 1 ]. method ) 884eq ( 'initialized' , messages [ 2 ]. method ) 885eq ( 'shutdown' , messages [ 3 ]. method ) 886eq ( 'exit' , messages [ 4 ]. method ) 887end ) 888 889it ( 'BufWritePre sends willSave / willSaveWaitUntil, applies textEdits' , function () 890exec_lua ( create_server_definition ) 891local result =exec_lua ( function () 892local server =_G . _create_server ( { 893capabilities ={ 894textDocumentSync ={ 895willSave =true , 896willSaveWaitUntil =true , 897} , 898} , 899handlers ={ 900[ 'textDocument/willSaveWaitUntil' ] =function ( _ , _ , callback ) 901local text_edit ={ 902range ={ 903start ={ line =0 , character =0 } , 904[ 'end' ] ={ line =0 , character =0 } , 905} , 906newText ='Hello' , 907} 908callback ( nil , { text_edit } ) 909end , 910} , 911} ) 912local buf =vim . api . nvim_get_current_buf () 913local client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =server . cmd } )) 914vim . api . nvim_exec_autocmds ( 'BufWritePre' , { buf =buf , modeline =false } ) 915vim . lsp . get_client_by_id ( client_id ): stop () 916return { 917messages =server . messages , 918lines =vim . api . nvim_buf_get_lines ( buf , 0 , - 1 , true ), 919} 920end ) 921local messages =result . messages 922eq ( 'textDocument/willSave' , messages [ 3 ]. method ) 923eq ( 'textDocument/willSaveWaitUntil' , messages [ 4 ]. method ) 924eq ( { 'Hello' } , result . lines ) 925end ) 926 927it ( 'saveas sends didOpen if filename changed' , function () 928local expected_handlers ={ 929{ NIL , {} , { method ='shutdown' , client_id =1 } } , 930{ NIL , {} , { method ='start' , client_id =1 } } , 931} 932local client --- @type vim.lsp.Client 933test_rpc_server ( { 934test_name ='text_document_save_did_open' , 935on_init =function ( c ) 936client =c 937end , 938on_exit =function ( code , signal ) 939eq ( 0 , code , 'exit code' ) 940eq ( 0 , signal , 'exit signal' ) 941end , 942on_handler =function ( err , result , ctx ) 943eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 944if ctx . method == 'start' then 945local tmpfile_old =tmpname () 946local tmpfile_new =tmpname ( false ) 947exec_lua ( function () 948_G . BUFFER =vim . api . nvim_get_current_buf () 949vim . api . nvim_buf_set_name ( _G . BUFFER , tmpfile_old ) 950vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , true , { 'help me' } ) 951vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 952vim . _with ( { buf =_G . BUFFER } , function () 953vim . cmd ( 'saveas ' .. tmpfile_new ) 954end ) 955end ) 956else 957client : stop () 958end 959end , 960} ) 961end ) 962 963it ( 'BufWritePost sends didSave including text if server capability is set' , function () 964local expected_handlers ={ 965{ NIL , {} , { method ='shutdown' , client_id =1 } } , 966{ NIL , {} , { method ='start' , client_id =1 } } , 967} 968local client --- @type vim.lsp.Client 969test_rpc_server { 970test_name ='text_document_sync_save_includeText' , 971on_init =function ( c ) 972client =c 973end , 974on_exit =function ( code , signal ) 975eq ( 0 , code , 'exit code' ) 976eq ( 0 , signal , 'exit signal' ) 977end , 978on_handler =function ( err , result , ctx ) 979eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 980if ctx . method == 'start' then 981exec_lua ( function () 982_G . BUFFER =vim . api . nvim_get_current_buf () 983vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , true , { 'help me' } ) 984vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 985vim . api . nvim_exec_autocmds ( 'BufWritePost' , { buf =_G . BUFFER , modeline =false } ) 986end ) 987else 988client : stop () 989end 990end , 991} 992end ) 993 994it ( 'client:supports_methods() should validate capabilities' , function () 995local expected_handlers ={ 996{ NIL , {} , { method ='shutdown' , client_id =1 } } , 997} 998test_rpc_server { 999test_name ='capabilities_for_client_supports_method' , 1000on_init =function ( client ) 1001client : stop () 1002local expected_sync_capabilities ={ 1003change =1 , 1004openClose =true , 1005save ={ includeText =false } , 1006willSave =false , 1007willSaveWaitUntil =false , 1008} 1009eq ( expected_sync_capabilities , client . server_capabilities (). textDocumentSync ) 1010eq ( true , client . server_capabilities (). completionProvider ) 1011eq ( true , client . server_capabilities (). hoverProvider ) 1012eq ( false , client . server_capabilities (). definitionProvider ) 1013eq ( false , client . server_capabilities (). renameProvider ) 1014eq ( true , client . server_capabilities (). codeLensProvider . resolveProvider ) 1015 1016-- known methods for resolved capabilities 1017eq ( true , client : supports_method ( 'textDocument/hover' )) 1018eq ( false , client : supports_method ( 'textDocument/definition' )) 1019 1020eq ( false , client : supports_method ( 'workspace/didCreateFiles' )) 1021 1022-- Self-mapped methods do not have a related server capability and should be assumed 1023-- to be supported. 1024eq ( true , client : supports_method ( 'shutdown' )) 1025 1026-- unknown methods are assumed to be supported. 1027eq ( true , client : supports_method ( 'unknown-method' )) 1028end , 1029on_exit =function ( code , signal ) 1030eq ( 0 , code , 'exit code' ) 1031eq ( 0 , signal , 'exit signal' ) 1032end , 1033on_handler =function ( ... ) 1034eq ( table . remove ( expected_handlers ), { ... } , 'expected handler' ) 1035end , 1036} 1037end ) 1038 1039it ( 'should not call unsupported_method when trying to call an unsupported method' , function () 1040local expected_handlers ={ 1041{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1042} 1043test_rpc_server { 1044test_name ='capabilities_for_client_supports_method' , 1045on_setup =function () 1046exec_lua ( function () 1047_G . BUFFER =vim . api . nvim_get_current_buf () 1048vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ) 1049vim . lsp . handlers [ 'textDocument/typeDefinition' ] =function () end 1050vim . cmd ( _G . BUFFER .. 'bwipeout' ) 1051end ) 1052end , 1053on_init =function ( client ) 1054client : stop () 1055exec_lua ( function () 1056vim . lsp . buf . type_definition () 1057end ) 1058end , 1059on_exit =function ( code , signal ) 1060eq ( 0 , code , 'exit code' ) 1061eq ( 0 , signal , 'exit signal' ) 1062end , 1063on_handler =function ( ... ) 1064eq ( table . remove ( expected_handlers ), { ... } , 'expected handler' ) 1065end , 1066} 1067end ) 1068 1069it ( 1070'should not call unsupported_method when no client and trying to call an unsupported method' , 1071function () 1072local expected_handlers ={ 1073{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1074} 1075test_rpc_server { 1076test_name ='capabilities_for_client_supports_method' , 1077on_setup =function () 1078exec_lua ( function () 1079vim . lsp . handlers [ 'textDocument/typeDefinition' ] =function () end 1080end ) 1081end , 1082on_init =function ( client ) 1083client : stop () 1084exec_lua ( function () 1085vim . lsp . buf . type_definition () 1086end ) 1087end , 1088on_exit =function ( code , signal ) 1089eq ( 0 , code , 'exit code' ) 1090eq ( 0 , signal , 'exit signal' ) 1091end , 1092on_handler =function ( ... ) 1093eq ( table . remove ( expected_handlers ), { ... } , 'expected handler' ) 1094end , 1095} 1096end 1097) 1098 1099it ( 'should not forward RequestCancelled to callback' , function () 1100local expected_handlers ={ 1101{ NIL , {} , { method ='finish' , client_id =1 } } , 1102} 1103local client --- @type vim.lsp.Client 1104test_rpc_server { 1105test_name ='check_forward_request_cancelled' , 1106on_init =function ( _client ) 1107_client : request ( 'error_code_test' ) 1108client =_client 1109end , 1110on_exit =function ( code , signal ) 1111eq ( 0 , code , 'exit code' ) 1112eq ( 0 , signal , 'exit signal' ) 1113eq ( 0 , # expected_handlers , 'did not call expected handler' ) 1114end , 1115on_handler =function ( err , _ , ctx ) 1116eq ( table . remove ( expected_handlers ), { err , {} , ctx } , 'expected handler' ) 1117if ctx . method == 'finish' then 1118client : stop () 1119end 1120end , 1121} 1122end ) 1123 1124it ( 'should forward ServerCancelled to callback' , function () 1125local expected_handlers ={ 1126{ NIL , {} , { method ='finish' , client_id =1 } } , 1127{ 1128{ code =- 32802 } , 1129NIL , 1130{ method ='error_code_test' , bufnr =1 , client_id =1 , request_id =2 , version =0 } , 1131} , 1132} 1133local client --- @type vim.lsp.Client 1134test_rpc_server { 1135test_name ='check_forward_server_cancelled' , 1136on_init =function ( _client ) 1137_client : request ( 'error_code_test' ) 1138client =_client 1139end , 1140on_exit =function ( code , signal ) 1141eq ( 0 , code , 'exit code' ) 1142eq ( 0 , signal , 'exit signal' ) 1143eq ( 0 , # expected_handlers , 'did not call expected handler' ) 1144end , 1145on_handler =function ( err , _ , ctx ) 1146eq ( table . remove ( expected_handlers ), { err , _ , ctx } , 'expected handler' ) 1147if ctx . method ~= 'finish' then 1148client : notify ( 'finish' ) 1149end 1150if ctx . method == 'finish' then 1151client : stop () 1152end 1153end , 1154} 1155end ) 1156 1157it ( 'should forward ContentModified to callback' , function () 1158local expected_handlers ={ 1159{ NIL , {} , { method ='finish' , client_id =1 } } , 1160{ 1161{ code =- 32801 } , 1162NIL , 1163{ method ='error_code_test' , bufnr =1 , client_id =1 , request_id =2 , version =0 } , 1164} , 1165} 1166local client --- @type vim.lsp.Client 1167test_rpc_server { 1168test_name ='check_forward_content_modified' , 1169on_init =function ( _client ) 1170_client : request ( 'error_code_test' ) 1171client =_client 1172end , 1173on_exit =function ( code , signal ) 1174eq ( 0 , code , 'exit code' ) 1175eq ( 0 , signal , 'exit signal' ) 1176eq ( 0 , # expected_handlers , 'did not call expected handler' ) 1177end , 1178on_handler =function ( err , _ , ctx ) 1179eq ( table . remove ( expected_handlers ), { err , _ , ctx } , 'expected handler' ) 1180if ctx . method ~= 'finish' then 1181client : notify ( 'finish' ) 1182end 1183if ctx . method == 'finish' then 1184client : stop () 1185end 1186end , 1187} 1188end ) 1189 1190it ( 'should track pending requests to the language server' , function () 1191local expected_handlers ={ 1192{ NIL , {} , { method ='finish' , client_id =1 } } , 1193{ 1194NIL , 1195{} , 1196{ method ='slow_request' , bufnr =1 , client_id =1 , request_id =2 , version =0 } , 1197} , 1198} 1199local client --- @type vim.lsp.Client 1200test_rpc_server { 1201test_name ='check_pending_request_tracked' , 1202on_init =function ( _client ) 1203client =_client 1204client : request ( 'slow_request' ) 1205local request =exec_lua ( function () 1206return _G . TEST_RPC_CLIENT . requests [ 2 ] 1207end ) 1208eq ( 'slow_request' , request . method ) 1209eq ( 'pending' , request . type ) 1210client : notify ( 'release' ) 1211end , 1212on_exit =function ( code , signal ) 1213eq ( 0 , code , 'exit code' ) 1214eq ( 0 , signal , 'exit signal' ) 1215eq ( 0 , # expected_handlers , 'did not call expected handler' ) 1216end , 1217on_handler =function ( err , _ , ctx ) 1218eq ( table . remove ( expected_handlers ), { err , {} , ctx } , 'expected handler' ) 1219if ctx . method == 'slow_request' then 1220local request =exec_lua ( function () 1221return _G . TEST_RPC_CLIENT . requests [ 2 ] 1222end ) 1223eq ( nil , request ) 1224client : notify ( 'finish' ) 1225end 1226if ctx . method == 'finish' then 1227client : stop () 1228end 1229end , 1230} 1231end ) 1232 1233it ( 'should track cancel requests to the language server' , function () 1234local expected_handlers ={ 1235{ NIL , {} , { method ='finish' , client_id =1 } } , 1236} 1237local client --- @type vim.lsp.Client 1238test_rpc_server { 1239test_name ='check_cancel_request_tracked' , 1240on_init =function ( _client ) 1241client =_client 1242client : request ( 'slow_request' ) 1243client : cancel_request ( 2 ) 1244local request =exec_lua ( function () 1245return _G . TEST_RPC_CLIENT . requests [ 2 ] 1246end ) 1247eq ( 'slow_request' , request . method ) 1248eq ( 'cancel' , request . type ) 1249client : notify ( 'release' ) 1250end , 1251on_exit =function ( code , signal ) 1252eq ( 0 , code , 'exit code' ) 1253eq ( 0 , signal , 'exit signal' ) 1254eq ( 0 , # expected_handlers , 'did not call expected handler' ) 1255end , 1256on_handler =function ( err , _ , ctx ) 1257eq ( table . remove ( expected_handlers ), { err , {} , ctx } , 'expected handler' ) 1258local request =exec_lua ( function () 1259return _G . TEST_RPC_CLIENT . requests [ 2 ] 1260end ) 1261eq ( nil , request ) 1262if ctx . method == 'finish' then 1263client : stop () 1264end 1265end , 1266} 1267end ) 1268 1269it ( 'should clear pending and cancel requests on reply' , function () 1270local expected_handlers ={ 1271{ NIL , {} , { method ='finish' , client_id =1 } } , 1272{ 1273NIL , 1274{} , 1275{ method ='slow_request' , bufnr =1 , client_id =1 , request_id =2 , version =0 } , 1276} , 1277} 1278local client --- @type vim.lsp.Client 1279test_rpc_server { 1280test_name ='check_tracked_requests_cleared' , 1281on_init =function ( _client ) 1282client =_client 1283client : request ( 'slow_request' ) 1284local request =exec_lua ( function () 1285return _G . TEST_RPC_CLIENT . requests [ 2 ] 1286end ) 1287eq ( 'slow_request' , request . method ) 1288eq ( 'pending' , request . type ) 1289client : cancel_request ( 2 ) 1290request =exec_lua ( function () 1291return _G . TEST_RPC_CLIENT . requests [ 2 ] 1292end ) 1293eq ( 'slow_request' , request . method ) 1294eq ( 'cancel' , request . type ) 1295client : notify ( 'release' ) 1296end , 1297on_exit =function ( code , signal ) 1298eq ( 0 , code , 'exit code' ) 1299eq ( 0 , signal , 'exit signal' ) 1300eq ( 0 , # expected_handlers , 'did not call expected handler' ) 1301end , 1302on_handler =function ( err , _ , ctx ) 1303eq ( table . remove ( expected_handlers ), { err , {} , ctx } , 'expected handler' ) 1304if ctx . method == 'slow_request' then 1305local request =exec_lua ( function () 1306return _G . TEST_RPC_CLIENT . requests [ 2 ] 1307end ) 1308eq ( nil , request ) 1309client : notify ( 'finish' ) 1310end 1311if ctx . method == 'finish' then 1312client : stop () 1313end 1314end , 1315} 1316end ) 1317 1318it ( 'request should not be pending for sync responses (in-process LS)' , function () 1319--- @type boolean 1320local pending_request =exec_lua ( function () 1321local function server ( dispatchers ) 1322local closing =false 1323local srv ={} 1324local request_id =0 1325 1326function srv . request ( method , _params , callback , notify_reply_callback ) 1327if method == 'textDocument/formatting' then 1328callback ( nil , {} ) 1329elseif method == 'initialize' then 1330callback ( nil , { 1331capabilities ={ 1332textDocument ={ 1333formatting =true , 1334} , 1335} , 1336} ) 1337elseif method == 'shutdown' then 1338callback ( nil , nil ) 1339end 1340request_id =request_id + 1 1341if notify_reply_callback then 1342notify_reply_callback ( request_id ) 1343end 1344return true , request_id 1345end 1346 1347function srv . notify ( method ) 1348if method == 'exit' then 1349dispatchers . on_exit ( 0 , 15 ) 1350end 1351end 1352function srv . is_closing () 1353return closing 1354end 1355function srv . terminate () 1356closing =true 1357end 1358 1359return srv 1360end 1361 1362local client_id =assert ( vim . lsp . start ( { cmd =server } )) 1363local client =assert ( vim . lsp . get_client_by_id ( client_id )) 1364 1365local ok , request_id =client : request ( 'textDocument/formatting' , {} ) 1366assert ( ok ) 1367 1368local has_pending =client . requests [ request_id ] ~= nil 1369vim . lsp . get_client_by_id ( client_id ): stop () 1370 1371return has_pending 1372end ) 1373 1374eq ( false , pending_request , 'expected no pending requests' ) 1375end ) 1376 1377it ( 'should trigger LspRequest autocmd when requests table changes' , function () 1378local expected_handlers ={ 1379{ NIL , {} , { method ='finish' , client_id =1 } } , 1380{ 1381NIL , 1382{} , 1383{ method ='slow_request' , bufnr =1 , client_id =1 , request_id =2 , version =0 } , 1384} , 1385} 1386local client --- @type vim.lsp.Client 1387test_rpc_server { 1388test_name ='check_tracked_requests_cleared' , 1389on_init =function ( _client ) 1390command ( 'let g:requests = 0' ) 1391command ( 'autocmd LspRequest * let g:requests+=1' ) 1392client =_client 1393client : request ( 'slow_request' ) 1394eq ( 1 , eval ( 'g:requests' )) 1395client : cancel_request ( 2 ) 1396eq ( 2 , eval ( 'g:requests' )) 1397client : notify ( 'release' ) 1398end , 1399on_exit =function ( code , signal ) 1400eq ( 0 , code , 'exit code' ) 1401eq ( 0 , signal , 'exit signal' ) 1402eq ( 0 , # expected_handlers , 'did not call expected handler' ) 1403eq ( 3 , eval ( 'g:requests' )) 1404end , 1405on_handler =function ( err , _ , ctx ) 1406eq ( table . remove ( expected_handlers ), { err , {} , ctx } , 'expected handler' ) 1407if ctx . method == 'slow_request' then 1408client : notify ( 'finish' ) 1409end 1410if ctx . method == 'finish' then 1411client : stop () 1412end 1413end , 1414} 1415end ) 1416 1417it ( 'should not send didOpen if the buffer closes before init' , function () 1418local expected_handlers ={ 1419{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1420{ NIL , {} , { method ='finish' , client_id =1 } } , 1421} 1422local client --- @type vim.lsp.Client 1423test_rpc_server { 1424test_name ='basic_finish' , 1425on_setup =function () 1426exec_lua ( function () 1427_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1428vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1429'testing' , 1430'123' , 1431} ) 1432assert ( _G . TEST_RPC_CLIENT_ID == 1 ) 1433assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1434assert ( vim . lsp . buf_is_attached ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1435vim . cmd ( _G . BUFFER .. 'bwipeout' ) 1436end ) 1437end , 1438on_init =function ( _client ) 1439client =_client 1440local full_kind =exec_lua ( function () 1441return require 'vim.lsp.protocol' . TextDocumentSyncKind . Full 1442end ) 1443eq ( full_kind , client . server_capabilities (). textDocumentSync . change ) 1444eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1445client : notify ( 'finish' ) 1446end , 1447on_exit =function ( code , signal ) 1448eq ( 0 , code , 'exit code' ) 1449eq ( 0 , signal , 'exit signal' ) 1450end , 1451on_handler =function ( err , result , ctx ) 1452eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1453if ctx . method == 'finish' then 1454client : stop () 1455end 1456end , 1457} 1458end ) 1459 1460it ( 'should check the body sent attaching before init' , function () 1461local expected_handlers ={ 1462{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1463{ NIL , {} , { method ='finish' , client_id =1 } } , 1464{ NIL , {} , { method ='start' , client_id =1 } } , 1465} 1466local client --- @type vim.lsp.Client 1467test_rpc_server { 1468test_name ='basic_check_buffer_open' , 1469on_setup =function () 1470exec_lua ( function () 1471_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1472vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1473'testing' , 1474'123' , 1475} ) 1476end ) 1477exec_lua ( function () 1478assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1479end ) 1480end , 1481on_init =function ( _client ) 1482client =_client 1483local full_kind =exec_lua ( function () 1484return require 'vim.lsp.protocol' . TextDocumentSyncKind . Full 1485end ) 1486eq ( full_kind , client . server_capabilities (). textDocumentSync . change ) 1487eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1488exec_lua ( function () 1489assert ( 1490vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID ), 1491'Already attached, returns true' 1492) 1493end ) 1494end , 1495on_exit =function ( code , signal ) 1496eq ( 0 , code , 'exit code' ) 1497eq ( 0 , signal , 'exit signal' ) 1498end , 1499on_handler =function ( err , result , ctx ) 1500if ctx . method == 'start' then 1501client : notify ( 'finish' ) 1502end 1503eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1504if ctx . method == 'finish' then 1505client : stop () 1506end 1507end , 1508} 1509end ) 1510 1511it ( 'should check the body sent attaching after init' , function () 1512local expected_handlers ={ 1513{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1514{ NIL , {} , { method ='finish' , client_id =1 } } , 1515{ NIL , {} , { method ='start' , client_id =1 } } , 1516} 1517local client --- @type vim.lsp.Client 1518test_rpc_server { 1519test_name ='basic_check_buffer_open' , 1520on_setup =function () 1521exec_lua ( function () 1522_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1523vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1524'testing' , 1525'123' , 1526} ) 1527end ) 1528end , 1529on_init =function ( _client ) 1530client =_client 1531local full_kind =exec_lua ( function () 1532return require 'vim.lsp.protocol' . TextDocumentSyncKind . Full 1533end ) 1534eq ( full_kind , client . server_capabilities (). textDocumentSync . change ) 1535eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1536exec_lua ( function () 1537assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1538end ) 1539end , 1540on_exit =function ( code , signal ) 1541eq ( 0 , code , 'exit code' ) 1542eq ( 0 , signal , 'exit signal' ) 1543end , 1544on_handler =function ( err , result , ctx ) 1545if ctx . method == 'start' then 1546client : notify ( 'finish' ) 1547end 1548eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1549if ctx . method == 'finish' then 1550client : stop () 1551end 1552end , 1553} 1554end ) 1555 1556it ( 'should check the body and didChange full' , function () 1557local expected_handlers ={ 1558{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1559{ NIL , {} , { method ='finish' , client_id =1 } } , 1560{ NIL , {} , { method ='start' , client_id =1 } } , 1561} 1562local client --- @type vim.lsp.Client 1563test_rpc_server { 1564test_name ='basic_check_buffer_open_and_change' , 1565on_setup =function () 1566exec_lua ( function () 1567_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1568vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1569'testing' , 1570'123' , 1571} ) 1572end ) 1573end , 1574on_init =function ( _client ) 1575client =_client 1576local full_kind =exec_lua ( function () 1577return require 'vim.lsp.protocol' . TextDocumentSyncKind . Full 1578end ) 1579eq ( full_kind , client . server_capabilities (). textDocumentSync . change ) 1580eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1581exec_lua ( function () 1582assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1583end ) 1584end , 1585on_exit =function ( code , signal ) 1586eq ( 0 , code , 'exit code' ) 1587eq ( 0 , signal , 'exit signal' ) 1588end , 1589on_handler =function ( err , result , ctx ) 1590if ctx . method == 'start' then 1591exec_lua ( function () 1592vim . api . nvim_buf_set_lines ( _G . BUFFER , 1 , 2 , false , { 1593'boop' , 1594} ) 1595end ) 1596client : notify ( 'finish' ) 1597end 1598eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1599if ctx . method == 'finish' then 1600client : stop () 1601end 1602end , 1603} 1604end ) 1605 1606it ( 'should check the body and didChange full with noeol' , function () 1607local expected_handlers ={ 1608{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1609{ NIL , {} , { method ='finish' , client_id =1 } } , 1610{ NIL , {} , { method ='start' , client_id =1 } } , 1611} 1612local client --- @type vim.lsp.Client 1613test_rpc_server { 1614test_name ='basic_check_buffer_open_and_change_noeol' , 1615on_setup =function () 1616exec_lua ( function () 1617_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1618vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1619'testing' , 1620'123' , 1621} ) 1622vim . bo [ _G . BUFFER ]. eol =false 1623end ) 1624end , 1625on_init =function ( _client ) 1626client =_client 1627local full_kind =exec_lua ( function () 1628return require 'vim.lsp.protocol' . TextDocumentSyncKind . Full 1629end ) 1630eq ( full_kind , client . server_capabilities (). textDocumentSync . change ) 1631eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1632exec_lua ( function () 1633assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1634end ) 1635end , 1636on_exit =function ( code , signal ) 1637eq ( 0 , code , 'exit code' ) 1638eq ( 0 , signal , 'exit signal' ) 1639end , 1640on_handler =function ( err , result , ctx ) 1641if ctx . method == 'start' then 1642exec_lua ( function () 1643vim . api . nvim_buf_set_lines ( _G . BUFFER , 1 , 2 , false , { 1644'boop' , 1645} ) 1646end ) 1647client : notify ( 'finish' ) 1648end 1649eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1650if ctx . method == 'finish' then 1651client : stop () 1652end 1653end , 1654} 1655end ) 1656 1657it ( 'should send correct range for inlay hints with noeol' , function () 1658local expected_handlers ={ 1659{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1660{ NIL , {} , { method ='finish' , client_id =1 } } , 1661{ 1662NIL , 1663{} , 1664{ 1665method ='textDocument/inlayHint' , 1666params ={ 1667textDocument ={ 1668uri ='file://' , 1669} , 1670range ={ 1671start ={ line =0 , character =0 } , 1672[ 'end' ] ={ line =1 , character =3 } , 1673} , 1674} , 1675bufnr =2 , 1676client_id =1 , 1677request_id =2 , 1678version =0 , 1679} , 1680} , 1681{ NIL , {} , { method ='start' , client_id =1 } } , 1682} 1683local client --- @type vim.lsp.Client 1684test_rpc_server { 1685test_name ='inlay_hint' , 1686on_setup =function () 1687exec_lua ( function () 1688_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1689vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1690'testing' , 1691'123' , 1692} ) 1693vim . bo [ _G . BUFFER ]. eol =false 1694end ) 1695end , 1696on_init =function ( _client ) 1697client =_client 1698eq ( true , client : supports_method ( 'textDocument/inlayHint' )) 1699exec_lua ( function () 1700assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1701end ) 1702end , 1703on_exit =function ( code , signal ) 1704eq ( 0 , code , 'exit code' ) 1705eq ( 0 , signal , 'exit signal' ) 1706end , 1707on_handler =function ( err , result , ctx ) 1708if ctx . method == 'start' then 1709exec_lua ( function () 1710vim . lsp . inlay_hint . enable ( true , { bufnr =_G . BUFFER } ) 1711end ) 1712end 1713if ctx . method == 'textDocument/inlayHint' then 1714client : notify ( 'finish' ) 1715end 1716eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1717if ctx . method == 'finish' then 1718client : stop () 1719end 1720end , 1721} 1722end ) 1723 1724it ( 'should check the body and didChange incremental' , function () 1725local expected_handlers ={ 1726{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1727{ NIL , {} , { method ='finish' , client_id =1 } } , 1728{ NIL , {} , { method ='start' , client_id =1 } } , 1729} 1730local client --- @type vim.lsp.Client 1731test_rpc_server { 1732test_name ='basic_check_buffer_open_and_change_incremental' , 1733options ={ 1734allow_incremental_sync =true , 1735} , 1736on_setup =function () 1737exec_lua ( function () 1738_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1739vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1740'testing' , 1741'123' , 1742} ) 1743end ) 1744end , 1745on_init =function ( _client ) 1746client =_client 1747local sync_kind =exec_lua ( function () 1748return require 'vim.lsp.protocol' . TextDocumentSyncKind . Incremental 1749end ) 1750eq ( sync_kind , client . server_capabilities (). textDocumentSync . change ) 1751eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1752exec_lua ( function () 1753assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1754end ) 1755end , 1756on_exit =function ( code , signal ) 1757eq ( 0 , code , 'exit code' ) 1758eq ( 0 , signal , 'exit signal' ) 1759end , 1760on_handler =function ( err , result , ctx ) 1761if ctx . method == 'start' then 1762exec_lua ( function () 1763vim . api . nvim_buf_set_lines ( _G . BUFFER , 1 , 2 , false , { 1764'123boop' , 1765} ) 1766end ) 1767client : notify ( 'finish' ) 1768end 1769eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1770if ctx . method == 'finish' then 1771client : stop () 1772end 1773end , 1774} 1775end ) 1776 1777it ( 'should check the body and didChange incremental with debounce' , function () 1778local expected_handlers ={ 1779{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1780{ NIL , {} , { method ='finish' , client_id =1 } } , 1781{ NIL , {} , { method ='start' , client_id =1 } } , 1782} 1783local client --- @type vim.lsp.Client 1784test_rpc_server { 1785test_name ='basic_check_buffer_open_and_change_incremental' , 1786options ={ 1787allow_incremental_sync =true , 1788debounce_text_changes =5 , 1789} , 1790on_setup =function () 1791exec_lua ( function () 1792_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1793vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1794'testing' , 1795'123' , 1796} ) 1797end ) 1798end , 1799on_init =function ( _client ) 1800client =_client 1801local sync_kind =exec_lua ( function () 1802return require 'vim.lsp.protocol' . TextDocumentSyncKind . Incremental 1803end ) 1804eq ( sync_kind , client . server_capabilities (). textDocumentSync . change ) 1805eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1806exec_lua ( function () 1807assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1808end ) 1809end , 1810on_exit =function ( code , signal ) 1811eq ( 0 , code , 'exit code' ) 1812eq ( 0 , signal , 'exit signal' ) 1813end , 1814on_handler =function ( err , result , ctx ) 1815if ctx . method == 'start' then 1816exec_lua ( function () 1817vim . api . nvim_buf_set_lines ( _G . BUFFER , 1 , 2 , false , { 1818'123boop' , 1819} ) 1820end ) 1821client : notify ( 'finish' ) 1822end 1823eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1824if ctx . method == 'finish' then 1825client : stop () 1826end 1827end , 1828} 1829end ) 1830 1831-- TODO(askhan) we don't support full for now, so we can disable these tests. 1832pending ( 'should check the body and didChange incremental normal mode editing' , function () 1833local expected_handlers ={ 1834{ NIL , {} , { method ='shutdown' , bufnr =1 , client_id =1 } } , 1835{ NIL , {} , { method ='finish' , client_id =1 } } , 1836{ NIL , {} , { method ='start' , client_id =1 } } , 1837} 1838local client --- @type vim.lsp.Client 1839test_rpc_server { 1840test_name ='basic_check_buffer_open_and_change_incremental_editing' , 1841on_setup =function () 1842exec_lua ( function () 1843_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1844vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1845'testing' , 1846'123' , 1847} ) 1848end ) 1849end , 1850on_init =function ( _client ) 1851client =_client 1852local sync_kind =exec_lua ( function () 1853return require 'vim.lsp.protocol' . TextDocumentSyncKind . Incremental 1854end ) 1855eq ( sync_kind , client . server_capabilities (). textDocumentSync . change ) 1856eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1857exec_lua ( function () 1858assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1859end ) 1860end , 1861on_exit =function ( code , signal ) 1862eq ( 0 , code , 'exit code' ) 1863eq ( 0 , signal , 'exit signal' ) 1864end , 1865on_handler =function ( err , result , ctx ) 1866if ctx . method == 'start' then 1867n . command ( 'normal! 1Go' ) 1868client : notify ( 'finish' ) 1869end 1870eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1871if ctx . method == 'finish' then 1872client : stop () 1873end 1874end , 1875} 1876end ) 1877 1878it ( 'should check the body and didChange full with 2 changes' , function () 1879local expected_handlers ={ 1880{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1881{ NIL , {} , { method ='finish' , client_id =1 } } , 1882{ NIL , {} , { method ='start' , client_id =1 } } , 1883} 1884local client --- @type vim.lsp.Client 1885test_rpc_server { 1886test_name ='basic_check_buffer_open_and_change_multi' , 1887on_setup =function () 1888exec_lua ( function () 1889_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1890vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1891'testing' , 1892'123' , 1893} ) 1894end ) 1895end , 1896on_init =function ( _client ) 1897client =_client 1898local sync_kind =exec_lua ( function () 1899return require 'vim.lsp.protocol' . TextDocumentSyncKind . Full 1900end ) 1901eq ( sync_kind , client . server_capabilities (). textDocumentSync . change ) 1902eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1903exec_lua ( function () 1904assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1905end ) 1906end , 1907on_exit =function ( code , signal ) 1908eq ( 0 , code , 'exit code' ) 1909eq ( 0 , signal , 'exit signal' ) 1910end , 1911on_handler =function ( err , result , ctx ) 1912if ctx . method == 'start' then 1913exec_lua ( function () 1914vim . api . nvim_buf_set_lines ( _G . BUFFER , 1 , 2 , false , { 1915'321' , 1916} ) 1917vim . api . nvim_buf_set_lines ( _G . BUFFER , 1 , 2 , false , { 1918'boop' , 1919} ) 1920end ) 1921client : notify ( 'finish' ) 1922end 1923eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1924if ctx . method == 'finish' then 1925client : stop () 1926end 1927end , 1928} 1929end ) 1930 1931it ( 'should check the body and didChange full lifecycle' , function () 1932local expected_handlers ={ 1933{ NIL , {} , { method ='shutdown' , client_id =1 } } , 1934{ NIL , {} , { method ='finish' , client_id =1 } } , 1935{ NIL , {} , { method ='start' , client_id =1 } } , 1936} 1937local client --- @type vim.lsp.Client 1938test_rpc_server { 1939test_name ='basic_check_buffer_open_and_change_multi_and_close' , 1940on_setup =function () 1941exec_lua ( function () 1942_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 1943vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 1944'testing' , 1945'123' , 1946} ) 1947end ) 1948end , 1949on_init =function ( _client ) 1950client =_client 1951local sync_kind =exec_lua ( function () 1952return require 'vim.lsp.protocol' . TextDocumentSyncKind . Full 1953end ) 1954eq ( sync_kind , client . server_capabilities (). textDocumentSync . change ) 1955eq ( true , client . server_capabilities (). textDocumentSync . openClose ) 1956exec_lua ( function () 1957assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 1958end ) 1959end , 1960on_exit =function ( code , signal ) 1961eq ( 0 , code , 'exit code' ) 1962eq ( 0 , signal , 'exit signal' ) 1963end , 1964on_handler =function ( err , result , ctx ) 1965if ctx . method == 'start' then 1966exec_lua ( function () 1967vim . api . nvim_buf_set_lines ( _G . BUFFER , 1 , 2 , false , { 1968'321' , 1969} ) 1970vim . api . nvim_buf_set_lines ( _G . BUFFER , 1 , 2 , false , { 1971'boop' , 1972} ) 1973vim . api . nvim_command ( _G . BUFFER .. 'bwipeout' ) 1974end ) 1975client : notify ( 'finish' ) 1976end 1977eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 1978if ctx . method == 'finish' then 1979client : stop () 1980end 1981end , 1982} 1983end ) 1984 1985it ( 'vim.lsp.start when existing client has no workspace_folders' , function () 1986exec_lua ( create_server_definition ) 1987eq ( 1988{ 2 , 'foo' , 'foo' } , 1989exec_lua ( function () 1990local server =_G . _create_server () 1991vim . lsp . start { cmd =server . cmd , name ='foo' } 1992vim . lsp . start { cmd =server . cmd , name ='foo' , root_dir ='bar' } 1993local foos =vim . lsp . get_clients () 1994return { # foos , foos [ 1 ]. name , foos [ 2 ]. name } 1995end ) 1996) 1997end ) 1998end ) 1999 2000describe ( 'parsing tests' , function () 2001local body ='{"jsonrpc":"2.0","id": 1,"method":"demo"}' 2002 2003before_each ( function () 2004exec_lua ( create_tcp_echo_server ) 2005end ) 2006 2007it ( 'should catch error while parsing invalid header' , function () 2008-- No whitespace is allowed between the header field-name and colon. 2009-- See https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 2010local field ='Content-Length : 10 \r\n' 2011exec_lua ( function () 2012_G . _send_msg_to_server ( field .. '\r\n' ) 2013end ) 2014verify_single_notification ( function ( method , args ) ---@param args [string, number] 2015eq ( 'error' , method ) 2016eq ( 1 , args [ 2 ]) 2017matches ( vim . pesc ( 'Content-Length not found in header: ' .. field ) .. '$' , args [ 1 ]) 2018end ) 2019end ) 2020 2021it ( 'value of Content-Length shoud be number' , function () 2022local value ='123 foo' 2023exec_lua ( function () 2024_G . _send_msg_to_server ( 'Content-Length: ' .. value .. '\r\n\r\n' ) 2025end ) 2026verify_single_notification ( function ( method , args ) ---@param args [string, number] 2027eq ( 'error' , method ) 2028eq ( 1 , args [ 2 ]) 2029matches ( 'value of Content%-Length is not number: ' .. value .. '$' , args [ 1 ]) 2030end ) 2031end ) 2032 2033it ( 'field name is case-insensitive' , function () 2034exec_lua ( function () 2035_G . _send_msg_to_server ( 'CONTENT-Length: ' .. # body .. ' \r\n\r\n' .. body ) 2036end ) 2037verify_single_notification ( function ( method , args ) ---@param args [string] 2038eq ( 'body' , method ) 2039eq ( body , args [ 1 ]) 2040end ) 2041end ) 2042 2043it ( "ignore some lines ending with LF that don't contain content-length" , function () 2044exec_lua ( function () 2045_G . _send_msg_to_server ( 2046'foo \n bar\nWARN: no common words.\nContent-Length: ' .. # body .. ' \r\n\r\n' .. body 2047) 2048end ) 2049verify_single_notification ( function ( method , args ) ---@param args [string] 2050eq ( 'body' , method ) 2051eq ( body , args [ 1 ]) 2052end ) 2053end ) 2054 2055it ( 'ignores blank lines before content-length' , function () 2056exec_lua ( function () 2057_G . _send_msg_to_server ( 2058'Info: Parsed 35 declarations\n\nContent-Length: ' .. # body .. ' \r\n\r\n' .. body 2059) 2060end ) 2061verify_single_notification ( function ( method , args ) ---@param args [string] 2062eq ( 'body' , method ) 2063eq ( body , args [ 1 ]) 2064end ) 2065end ) 2066 2067it ( 'skips partial match of "content-length"' , function () 2068exec_lua ( function () 2069_G . _send_msg_to_server ( 'Cont\nContent-Length: ' .. # body .. ' \r\n\r\n' .. body ) 2070end ) 2071verify_single_notification ( function ( method , args ) ---@param args [string] 2072eq ( 'body' , method ) 2073eq ( body , args [ 1 ]) 2074end ) 2075end ) 2076 2077it ( 'should not trim vim.NIL from the end of a list' , function () 2078local expected_handlers ={ 2079{ NIL , {} , { method ='shutdown' , client_id =1 } } , 2080{ NIL , {} , { method ='finish' , client_id =1 } } , 2081{ 2082NIL , 2083{ 2084arguments ={ 'EXTRACT_METHOD' , { metadata ={ field =vim . NIL } } , 3 , 0 , 6123 , NIL } , 2085command ='refactor.perform' , 2086title ='EXTRACT_METHOD' , 2087} , 2088{ method ='workspace/executeCommand' , client_id =1 } , 2089} , 2090{ NIL , {} , { method ='start' , client_id =1 } } , 2091} 2092local client --- @type vim.lsp.Client 2093test_rpc_server { 2094test_name ='decode_nil' , 2095on_setup =function () 2096exec_lua ( function () 2097_G . BUFFER =vim . api . nvim_create_buf ( false , true ) 2098vim . api . nvim_buf_set_lines ( _G . BUFFER , 0 , - 1 , false , { 2099'testing' , 2100'123' , 2101} ) 2102end ) 2103end , 2104on_init =function ( _client ) 2105client =_client 2106exec_lua ( function () 2107assert ( vim . lsp . buf_attach_client ( _G . BUFFER , _G . TEST_RPC_CLIENT_ID )) 2108end ) 2109end , 2110on_exit =function ( code , signal ) 2111eq ( 0 , code , 'exit code' ) 2112eq ( 0 , signal , 'exit signal' ) 2113end , 2114on_handler =function ( err , result , ctx ) 2115eq ( table . remove ( expected_handlers ), { err , result , ctx } , 'expected handler' ) 2116if ctx . method == 'finish' then 2117client : stop () 2118end 2119end , 2120} 2121end ) 2122end ) 2123 2124describe ( 'apply vscode text_edits' , function () 2125it ( 'single replace' , function () 2126insert ( '012345678901234567890123456789' ) 2127apply_text_edits ( { 2128{ 0 , 3 , 0 , 6 , { 'Hello' } } , 2129} ) 2130eq ( { '012Hello678901234567890123456789' } , buf_lines ( 1 )) 2131end ) 2132 2133it ( 'two replaces' , function () 2134insert ( '012345678901234567890123456789' ) 2135apply_text_edits ( { 2136{ 0 , 3 , 0 , 6 , { 'Hello' } } , 2137{ 0 , 6 , 0 , 9 , { 'World' } } , 2138} ) 2139eq ( { '012HelloWorld901234567890123456789' } , buf_lines ( 1 )) 2140end ) 2141 2142it ( 'same start pos insert are kept in order' , function () 2143insert ( '012345678901234567890123456789' ) 2144apply_text_edits ( { 2145{ 0 , 3 , 0 , 3 , { 'World' } } , 2146{ 0 , 3 , 0 , 3 , { 'Hello' } } , 2147} ) 2148eq ( { '012WorldHello345678901234567890123456789' } , buf_lines ( 1 )) 2149end ) 2150 2151it ( 'same start pos insert and replace are kept in order' , function () 2152insert ( '012345678901234567890123456789' ) 2153apply_text_edits ( { 2154{ 0 , 3 , 0 , 3 , { 'World' } } , 2155{ 0 , 3 , 0 , 3 , { 'Hello' } } , 2156{ 0 , 3 , 0 , 8 , { 'No' } } , 2157} ) 2158eq ( { '012WorldHelloNo8901234567890123456789' } , buf_lines ( 1 )) 2159end ) 2160 2161it ( 'multiline' , function () 2162exec_lua ( function () 2163vim . api . nvim_buf_set_lines ( 1 , 0 , 0 , true , { ' {' , ' "foo": "bar"' , ' }' } ) 2164end ) 2165eq ( { ' {' , ' "foo": "bar"' , ' }' , '' } , buf_lines ( 1 )) 2166apply_text_edits ( { 2167{ 0 , 0 , 3 , 0 , { '' } } , 2168{ 3 , 0 , 3 , 0 , { '{\n' } } , 2169{ 3 , 0 , 3 , 0 , { ' "foo": "bar"\n' } } , 2170{ 3 , 0 , 3 , 0 , { '}\n' } } , 2171} ) 2172eq ( { '{' , ' "foo": "bar"' , '}' , '' } , buf_lines ( 1 )) 2173end ) 2174end ) 2175 2176describe ( 'apply_text_edits' , function () 2177local buffer_text ={ 2178'First line of text' , 2179'Second line of text' , 2180'Third line of text' , 2181'Fourth line of text' , 2182'å å ɧ 汉语 ↥ 🤦 🦄' , 2183} 2184 2185before_each ( function () 2186insert ( dedent ( table . concat ( buffer_text , '\n' ))) 2187end ) 2188 2189it ( 'applies simple edits' , function () 2190apply_text_edits ( { 2191{ 0 , 0 , 0 , 0 , { '123' } } , 2192{ 1 , 0 , 1 , 1 , { '2' } } , 2193{ 2 , 0 , 2 , 2 , { '3' } } , 2194{ 3 , 2 , 3 , 4 , { '' } } , 2195} ) 2196eq ( { 2197'123First line of text' , 2198'2econd line of text' , 2199'3ird line of text' , 2200'Foth line of text' , 2201'å å ɧ 汉语 ↥ 🤦 🦄' , 2202} , buf_lines ( 1 )) 2203end ) 2204 2205it ( 'applies complex edits' , function () 2206apply_text_edits ( { 2207{ 0 , 0 , 0 , 0 , { '' , '12' } } , 2208{ 0 , 0 , 0 , 0 , { '3' , 'foo' } } , 2209{ 0 , 1 , 0 , 1 , { 'bar' , '123' } } , 2210{ 0 , # 'First ' , 0 , # 'First line of text' , { 'guy' } } , 2211{ 1 , 0 , 1 , # 'Second' , { 'baz' } } , 2212{ 2 , # 'Th' , 2 , # 'Third' , { 'e next' } } , 2213{ 3 , # '' , 3 , # 'Fourth' , { 'another line of text' , 'before this' } } , 2214{ 3 , # 'Fourth' , 3 , # 'Fourth line of text' , { '!' } } , 2215} ) 2216eq ( { 2217'' , 2218'123' , 2219'fooFbar' , 2220'123irst guy' , 2221'baz line of text' , 2222'The next line of text' , 2223'another line of text' , 2224'before this!' , 2225'å å ɧ 汉语 ↥ 🤦 🦄' , 2226} , buf_lines ( 1 )) 2227end ) 2228 2229it ( 'applies complex edits (reversed range)' , function () 2230apply_text_edits ( { 2231{ 0 , 0 , 0 , 0 , { '' , '12' } } , 2232{ 0 , 0 , 0 , 0 , { '3' , 'foo' } } , 2233{ 0 , 1 , 0 , 1 , { 'bar' , '123' } } , 2234{ 0 , # 'First line of text' , 0 , # 'First ' , { 'guy' } } , 2235{ 1 , # 'Second' , 1 , 0 , { 'baz' } } , 2236{ 2 , # 'Third' , 2 , # 'Th' , { 'e next' } } , 2237{ 3 , # 'Fourth' , 3 , # '' , { 'another line of text' , 'before this' } } , 2238{ 3 , # 'Fourth line of text' , 3 , # 'Fourth' , { '!' } } , 2239} ) 2240eq ( { 2241'' , 2242'123' , 2243'fooFbar' , 2244'123irst guy' , 2245'baz line of text' , 2246'The next line of text' , 2247'another line of text' , 2248'before this!' , 2249'å å ɧ 汉语 ↥ 🤦 🦄' , 2250} , buf_lines ( 1 )) 2251end ) 2252 2253it ( 'applies non-ASCII characters edits' , function () 2254apply_text_edits ( { 2255{ 4 , 3 , 4 , 4 , { 'ä' } } , 2256} ) 2257eq ( { 2258'First line of text' , 2259'Second line of text' , 2260'Third line of text' , 2261'Fourth line of text' , 2262'å ä ɧ 汉语 ↥ 🤦 🦄' , 2263} , buf_lines ( 1 )) 2264end ) 2265 2266it ( 'applies text edits at the end of the document' , function () 2267apply_text_edits ( { 2268{ 5 , 0 , 5 , 0 , 'foobar' } , 2269} ) 2270eq ( { 2271'First line of text' , 2272'Second line of text' , 2273'Third line of text' , 2274'Fourth line of text' , 2275'å å ɧ 汉语 ↥ 🤦 🦄' , 2276'foobar' , 2277} , buf_lines ( 1 )) 2278end ) 2279 2280it ( 'applies multiple text edits at the end of the document' , function () 2281apply_text_edits ( { 2282{ 4 , 0 , 5 , 0 , '' } , 2283{ 5 , 0 , 5 , 0 , 'foobar' } , 2284} ) 2285eq ( { 2286'First line of text' , 2287'Second line of text' , 2288'Third line of text' , 2289'Fourth line of text' , 2290'foobar' , 2291} , buf_lines ( 1 )) 2292end ) 2293 2294it ( 'applies newline-terminated text edits at the end of the document' , function () 2295apply_text_edits ( { 2296{ 5 , 0 , 5 , 0 , 'foobar\n' } , 2297} ) 2298eq ( { 2299'First line of text' , 2300'Second line of text' , 2301'Third line of text' , 2302'Fourth line of text' , 2303'å å ɧ 汉语 ↥ 🤦 🦄' , 2304'foobar' , 2305} , buf_lines ( 1 )) 2306end ) 2307 2308it ( 'it restores marks' , function () 2309eq ( true , api . nvim_buf_set_mark ( 1 , 'a' , 2 , 1 , {} )) 2310apply_text_edits ( { 2311{ 1 , 0 , 2 , 5 , 'foobar' } , 2312{ 4 , 0 , 5 , 0 , 'barfoo' } , 2313} ) 2314eq ( { 2315'First line of text' , 2316'foobar line of text' , 2317'Fourth line of text' , 2318'barfoo' , 2319} , buf_lines ( 1 )) 2320eq ( { 2 , 1 } , api . nvim_buf_get_mark ( 1 , 'a' )) 2321end ) 2322 2323it ( 'it restores marks to last valid col' , function () 2324eq ( true , api . nvim_buf_set_mark ( 1 , 'a' , 2 , 10 , {} )) 2325apply_text_edits ( { 2326{ 1 , 0 , 2 , 15 , 'foobar' } , 2327{ 4 , 0 , 5 , 0 , 'barfoo' } , 2328} ) 2329eq ( { 2330'First line of text' , 2331'foobarext' , 2332'Fourth line of text' , 2333'barfoo' , 2334} , buf_lines ( 1 )) 2335eq ( { 2 , 9 } , api . nvim_buf_get_mark ( 1 , 'a' )) 2336end ) 2337 2338it ( 'it restores marks to last valid line' , function () 2339eq ( true , api . nvim_buf_set_mark ( 1 , 'a' , 4 , 1 , {} )) 2340apply_text_edits ( { 2341{ 1 , 0 , 4 , 5 , 'foobar' } , 2342{ 4 , 0 , 5 , 0 , 'barfoo' } , 2343} ) 2344eq ( { 2345'First line of text' , 2346'foobaro' , 2347} , buf_lines ( 1 )) 2348eq ( { 2 , 1 } , api . nvim_buf_get_mark ( 1 , 'a' )) 2349end ) 2350 2351it ( 'applies edit based on confirmation response' , function () 2352--- @type lsp.AnnotatedTextEdit 2353local edit =make_edit ( 0 , 0 , 5 , 0 , 'foo' ) 2354edit . annotationId ='annotation-id' 2355 2356local function test ( response ) 2357exec_lua ( function () 2358---@diagnostic disable-next-line: duplicate-set-field 2359vim . fn . confirm =function () 2360return response 2361end 2362 2363vim . lsp . util . apply_text_edits ( 2364{ edit } , 23651 , 2366'utf-16' , 2367{ [ 'annotation-id' ] ={ label ='Insert "foo"' , needsConfirmation =true } } 2368) 2369end , { response } ) 2370end 2371 2372test ( 2 ) -- 2 = No 2373eq ( buffer_text , buf_lines ( 1 )) 2374 2375test ( 1 ) -- 1 = Yes 2376eq ( { 'foo' } , buf_lines ( 1 )) 2377end ) 2378 2379describe ( 'cursor position' , function () 2380it ( "don't fix the cursor if the range contains the cursor" , function () 2381api . nvim_win_set_cursor ( 0 , { 2 , 6 } ) 2382apply_text_edits ( { 2383{ 1 , 0 , 1 , 19 , 'Second line of text' } , 2384} ) 2385eq ( { 2386'First line of text' , 2387'Second line of text' , 2388'Third line of text' , 2389'Fourth line of text' , 2390'å å ɧ 汉语 ↥ 🤦 🦄' , 2391} , buf_lines ( 1 )) 2392eq ( { 2 , 6 } , api . nvim_win_get_cursor ( 0 )) 2393end ) 2394 2395it ( 'fix the cursor to the valid col if the content was removed' , function () 2396api . nvim_win_set_cursor ( 0 , { 2 , 6 } ) 2397apply_text_edits ( { 2398{ 1 , 0 , 1 , 6 , '' } , 2399{ 1 , 6 , 1 , 19 , '' } , 2400} ) 2401eq ( { 2402'First line of text' , 2403'' , 2404'Third line of text' , 2405'Fourth line of text' , 2406'å å ɧ 汉语 ↥ 🤦 🦄' , 2407} , buf_lines ( 1 )) 2408eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 2409end ) 2410 2411it ( 'fix the cursor to the valid row if the content was removed' , function () 2412api . nvim_win_set_cursor ( 0 , { 2 , 6 } ) 2413apply_text_edits ( { 2414{ 1 , 0 , 1 , 6 , '' } , 2415{ 0 , 18 , 5 , 0 , '' } , 2416} ) 2417eq ( { 2418'First line of text' , 2419} , buf_lines ( 1 )) 2420eq ( { 1 , 17 } , api . nvim_win_get_cursor ( 0 )) 2421end ) 2422 2423it ( 'fix the cursor row' , function () 2424api . nvim_win_set_cursor ( 0 , { 3 , 0 } ) 2425apply_text_edits ( { 2426{ 1 , 0 , 2 , 0 , '' } , 2427} ) 2428eq ( { 2429'First line of text' , 2430'Third line of text' , 2431'Fourth line of text' , 2432'å å ɧ 汉语 ↥ 🤦 🦄' , 2433} , buf_lines ( 1 )) 2434eq ( { 2 , 0 } , api . nvim_win_get_cursor ( 0 )) 2435end ) 2436 2437it ( 'fix the cursor col' , function () 2438-- append empty last line. See #22636 2439api . nvim_buf_set_lines ( 1 , - 1 , - 1 , true , { '' } ) 2440 2441api . nvim_win_set_cursor ( 0 , { 2 , 11 } ) 2442apply_text_edits ( { 2443{ 1 , 7 , 1 , 11 , '' } , 2444} ) 2445eq ( { 2446'First line of text' , 2447'Second of text' , 2448'Third line of text' , 2449'Fourth line of text' , 2450'å å ɧ 汉语 ↥ 🤦 🦄' , 2451'' , 2452} , buf_lines ( 1 )) 2453eq ( { 2 , 7 } , api . nvim_win_get_cursor ( 0 )) 2454end ) 2455 2456it ( 'fix the cursor row and col' , function () 2457api . nvim_win_set_cursor ( 0 , { 2 , 12 } ) 2458apply_text_edits ( { 2459{ 0 , 11 , 1 , 12 , '' } , 2460} ) 2461eq ( { 2462'First line of text' , 2463'Third line of text' , 2464'Fourth line of text' , 2465'å å ɧ 汉语 ↥ 🤦 🦄' , 2466} , buf_lines ( 1 )) 2467eq ( { 1 , 11 } , api . nvim_win_get_cursor ( 0 )) 2468end ) 2469end ) 2470 2471describe ( 'with LSP end line after what Vim considers to be the end line' , function () 2472it ( 'applies edits when the last linebreak is considered a new line' , function () 2473apply_text_edits ( { 2474{ 0 , 0 , 5 , 0 , { 'All replaced' } } , 2475} ) 2476eq ( { 'All replaced' } , buf_lines ( 1 )) 2477end ) 2478 2479it ( "applies edits when the end line is 2 larger than vim's" , function () 2480apply_text_edits ( { 2481{ 0 , 0 , 6 , 0 , { 'All replaced' } } , 2482} ) 2483eq ( { 'All replaced' } , buf_lines ( 1 )) 2484end ) 2485 2486it ( 'applies edits with a column offset' , function () 2487apply_text_edits ( { 2488{ 0 , 0 , 5 , 2 , { 'All replaced' } } , 2489} ) 2490eq ( { 'All replaced' } , buf_lines ( 1 )) 2491end ) 2492end ) 2493end ) 2494 2495describe ( 'apply_text_edits regression tests for #20116' , function () 2496before_each ( function () 2497insert ( dedent ( [[ 2498Test line one 2499Test line two 21 char]] )) 2500end ) 2501 2502describe ( 'with LSP end column out of bounds and start column at 0' , function () 2503it ( 'applies edits at the end of the buffer' , function () 2504apply_text_edits ( { 2505{ 0 , 0 , 1 , 22 , { '#include "whatever.h"\r\n#include <algorithm>\r' } } , 2506} , 'utf-8' ) 2507eq ( { '#include "whatever.h"' , '#include <algorithm>' } , buf_lines ( 1 )) 2508end ) 2509 2510it ( 'applies edits in the middle of the buffer' , function () 2511apply_text_edits ( { 2512{ 0 , 0 , 0 , 22 , { '#include "whatever.h"\r\n#include <algorithm>\r' } } , 2513} , 'utf-8' ) 2514eq ( 2515{ '#include "whatever.h"' , '#include <algorithm>' , 'Test line two 21 char' } , 2516buf_lines ( 1 ) 2517) 2518end ) 2519end ) 2520 2521describe ( 'with LSP end column out of bounds and start column NOT at 0' , function () 2522it ( 'applies edits at the end of the buffer' , function () 2523apply_text_edits ( { 2524{ 0 , 2 , 1 , 22 , { '#include "whatever.h"\r\n#include <algorithm>\r' } } , 2525} , 'utf-8' ) 2526eq ( { 'Te#include "whatever.h"' , '#include <algorithm>' } , buf_lines ( 1 )) 2527end ) 2528 2529it ( 'applies edits in the middle of the buffer' , function () 2530apply_text_edits ( { 2531{ 0 , 2 , 0 , 22 , { '#include "whatever.h"\r\n#include <algorithm>\r' } } , 2532} , 'utf-8' ) 2533eq ( 2534{ 'Te#include "whatever.h"' , '#include <algorithm>' , 'Test line two 21 char' } , 2535buf_lines ( 1 ) 2536) 2537end ) 2538end ) 2539end ) 2540 2541describe ( 'apply_text_document_edit' , function () 2542local target_bufnr --- @type integer 2543 2544local text_document_edit =function ( editVersion ) 2545return { 2546edits ={ 2547make_edit ( 0 , 0 , 0 , 3 , 'First ↥ 🤦 🦄' ), 2548} , 2549textDocument ={ 2550uri ='file:///fake/uri' , 2551version =editVersion , 2552} , 2553} 2554end 2555 2556before_each ( function () 2557target_bufnr =exec_lua ( function () 2558local bufnr =vim . uri_to_bufnr ( 'file:///fake/uri' ) 2559local lines ={ '1st line of text' , '2nd line of 语text' } 2560vim . api . nvim_buf_set_lines ( bufnr , 0 , 1 , false , lines ) 2561return bufnr 2562end ) 2563end ) 2564 2565it ( 'correctly goes ahead with the edit if all is normal' , function () 2566exec_lua ( function ( text_edit ) 2567vim . lsp . util . apply_text_document_edit ( text_edit , nil , 'utf-16' ) 2568end , text_document_edit ( 5 )) 2569eq ( { 2570'First ↥ 🤦 🦄 line of text' , 2571'2nd line of 语text' , 2572} , buf_lines ( target_bufnr )) 2573end ) 2574 2575it ( 'always accepts edit with version = 0' , function () 2576exec_lua ( function ( text_edit ) 2577vim . lsp . util . buf_versions [ target_bufnr ] =10 2578vim . lsp . util . apply_text_document_edit ( text_edit , nil , 'utf-16' ) 2579end , text_document_edit ( 0 )) 2580eq ( { 2581'First ↥ 🤦 🦄 line of text' , 2582'2nd line of 语text' , 2583} , buf_lines ( target_bufnr )) 2584end ) 2585 2586it ( 'skips the edit if the version of the edit is behind the local buffer ' , function () 2587local apply_edit_mocking_current_version =function ( edit , versionedBuf ) 2588exec_lua ( function () 2589vim . lsp . util . buf_versions [ versionedBuf . bufnr ] =versionedBuf . currentVersion 2590vim . lsp . util . apply_text_document_edit ( edit , nil , 'utf-16' ) 2591end ) 2592end 2593 2594local baseText ={ 2595'1st line of text' , 2596'2nd line of 语text' , 2597} 2598 2599eq ( baseText , buf_lines ( target_bufnr )) 2600 2601-- Apply an edit for an old version, should skip 2602apply_edit_mocking_current_version ( 2603text_document_edit ( 2 ), 2604{ currentVersion =7 , bufnr =target_bufnr } 2605) 2606eq ( baseText , buf_lines ( target_bufnr )) -- no change 2607 2608-- Sanity check that next version to current does apply change 2609apply_edit_mocking_current_version ( 2610text_document_edit ( 8 ), 2611{ currentVersion =7 , bufnr =target_bufnr } 2612) 2613eq ( { 2614'First ↥ 🤦 🦄 line of text' , 2615'2nd line of 语text' , 2616} , buf_lines ( target_bufnr )) 2617end ) 2618end ) 2619 2620describe ( 'workspace_apply_edit' , function () 2621it ( 'workspace/applyEdit returns ApplyWorkspaceEditResponse' , function () 2622local expected_handlers ={ 2623{ NIL , {} , { method ='test' , client_id =1 } } , 2624} 2625test_rpc_server { 2626test_name ='basic_init' , 2627on_init =function ( client , _ ) 2628client : stop () 2629end , 2630-- If the program timed out, then code will be nil. 2631on_exit =function ( code , signal ) 2632eq ( 0 , code , 'exit code' ) 2633eq ( 0 , signal , 'exit signal' ) 2634end , 2635-- Note that NIL must be used here. 2636-- on_handler(err, method, result, client_id) 2637on_handler =function ( ... ) 2638local expected ={ 2639applied =true , 2640failureReason =nil , 2641} 2642eq ( 2643expected , 2644exec_lua ( function () 2645local apply_edit ={ 2646label =nil , 2647edit ={} , 2648} 2649return vim . lsp . handlers [ 'workspace/applyEdit' ]( 2650nil , 2651apply_edit , 2652{ client_id =_G . TEST_RPC_CLIENT_ID } 2653) 2654end ) 2655) 2656eq ( table . remove ( expected_handlers ), { ... } ) 2657end , 2658} 2659end ) 2660end ) 2661 2662describe ( 'apply_workspace_edit' , function () 2663local replace_line_edit =function ( row , new_line , editVersion ) 2664return { 2665edits ={ 2666-- NOTE: This is a hack if you have a line longer than 1000 it won't replace it 2667make_edit ( row , 0 , row , 1000 , new_line ), 2668} , 2669textDocument ={ 2670uri ='file:///fake/uri' , 2671version =editVersion , 2672} , 2673} 2674end 2675 2676-- Some servers send all the edits separately, but with the same version. 2677-- We should not stop applying the edits 2678local make_workspace_edit =function ( changes ) 2679return { 2680documentChanges =changes , 2681} 2682end 2683 2684local target_bufnr --- @type integer 2685local changedtick --- @type integer 2686 2687before_each ( function () 2688exec_lua ( function () 2689target_bufnr =vim . uri_to_bufnr ( 'file:///fake/uri' ) 2690local lines ={ 2691'Original Line #1' , 2692'Original Line #2' , 2693} 2694 2695vim . api . nvim_buf_set_lines ( target_bufnr , 0 , - 1 , false , lines ) 2696 2697local function update_changed_tick () 2698vim . lsp . util . buf_versions [ target_bufnr ] =vim . b [ target_bufnr ]. changedtick 2699end 2700 2701update_changed_tick () 2702vim . api . nvim_buf_attach ( target_bufnr , false , { 2703on_changedtick =update_changed_tick , 2704} ) 2705 2706changedtick =vim . b [ target_bufnr ]. changedtick 2707end ) 2708end ) 2709 2710it ( 'apply_workspace_edit applies a single edit' , function () 2711local new_lines ={ 2712'First Line' , 2713} 2714 2715local edits ={} 2716for row , line in ipairs ( new_lines ) do 2717table . insert ( edits , replace_line_edit ( row - 1 , line , changedtick )) 2718end 2719 2720eq ( 2721{ 2722'First Line' , 2723'Original Line #2' , 2724} , 2725exec_lua ( function ( workspace_edits ) 2726vim . lsp . util . apply_workspace_edit ( workspace_edits , 'utf-16' ) 2727 2728return vim . api . nvim_buf_get_lines ( target_bufnr , 0 , - 1 , false ) 2729end , make_workspace_edit ( edits )) 2730) 2731eq ( true , api . nvim_get_option_value ( 'buflisted' , { buf =target_bufnr } )) 2732end ) 2733 2734it ( 'apply_workspace_edit applies multiple edits' , function () 2735local new_lines ={ 2736'First Line' , 2737'Second Line' , 2738} 2739 2740local edits ={} 2741for row , line in ipairs ( new_lines ) do 2742table . insert ( edits , replace_line_edit ( row - 1 , line , changedtick )) 2743end 2744 2745eq ( 2746new_lines , 2747exec_lua ( function ( workspace_edits ) 2748vim . lsp . util . apply_workspace_edit ( workspace_edits , 'utf-16' ) 2749return vim . api . nvim_buf_get_lines ( target_bufnr , 0 , - 1 , false ) 2750end , make_workspace_edit ( edits )) 2751) 2752end ) 2753 2754it ( 'supports file creation with CreateFile payload' , function () 2755local tmpfile =tmpname ( false ) 2756local uri =exec_lua ( 'return vim.uri_from_fname(...)' , tmpfile ) 2757local edit ={ 2758documentChanges ={ 2759{ 2760kind ='create' , 2761uri =uri , 2762} , 2763} , 2764} 2765exec_lua ( function () 2766vim . lsp . util . apply_workspace_edit ( edit , 'utf-16' ) 2767end ) 2768eq ( true , vim . uv . fs_stat ( tmpfile ) ~= nil ) 2769end ) 2770 2771it ( 2772'supports file creation in folder that needs to be created with CreateFile payload' , 2773function () 2774local tmpfile =tmpname ( false ) .. '/dummy/x/' 2775local uri =exec_lua ( 'return vim.uri_from_fname(...)' , tmpfile ) 2776local edit ={ 2777documentChanges ={ 2778{ 2779kind ='create' , 2780uri =uri , 2781} , 2782} , 2783} 2784exec_lua ( function () 2785vim . lsp . util . apply_workspace_edit ( edit , 'utf-16' ) 2786end ) 2787eq ( true , vim . uv . fs_stat ( tmpfile ) ~= nil ) 2788end 2789) 2790 2791it ( 'createFile does not touch file if it exists and ignoreIfExists is set' , function () 2792local tmpfile =tmpname () 2793write_file ( tmpfile , 'Dummy content' ) 2794local uri =exec_lua ( 'return vim.uri_from_fname(...)' , tmpfile ) 2795local edit ={ 2796documentChanges ={ 2797{ 2798kind ='create' , 2799uri =uri , 2800options ={ 2801ignoreIfExists =true , 2802} , 2803} , 2804} , 2805} 2806exec_lua ( 'vim.lsp.util.apply_workspace_edit(...)' , edit , 'utf-16' ) 2807eq ( true , vim . uv . fs_stat ( tmpfile ) ~= nil ) 2808eq ( 'Dummy content' , read_file ( tmpfile )) 2809end ) 2810 2811it ( 'createFile overrides file if overwrite is set' , function () 2812local tmpfile =tmpname () 2813write_file ( tmpfile , 'Dummy content' ) 2814local uri =exec_lua ( 'return vim.uri_from_fname(...)' , tmpfile ) 2815local edit ={ 2816documentChanges ={ 2817{ 2818kind ='create' , 2819uri =uri , 2820options ={ 2821overwrite =true , 2822ignoreIfExists =true , -- overwrite must win over ignoreIfExists 2823} , 2824} , 2825} , 2826} 2827exec_lua ( 'vim.lsp.util.apply_workspace_edit(...)' , edit , 'utf-16' ) 2828eq ( true , vim . uv . fs_stat ( tmpfile ) ~= nil ) 2829eq ( '' , read_file ( tmpfile )) 2830end ) 2831 2832it ( 'DeleteFile delete file and buffer' , function () 2833local tmpfile =tmpname () 2834write_file ( tmpfile , 'Be gone' ) 2835local uri =exec_lua ( function () 2836local bufnr =vim . fn . bufadd ( tmpfile ) 2837vim . fn . bufload ( bufnr ) 2838return vim . uri_from_fname ( tmpfile ) 2839end ) 2840local edit ={ 2841documentChanges ={ 2842{ 2843kind ='delete' , 2844uri =uri , 2845} , 2846} , 2847} 2848eq ( true , pcall ( exec_lua , 'vim.lsp.util.apply_workspace_edit(...)' , edit , 'utf-16' )) 2849eq ( false , vim . uv . fs_stat ( tmpfile ) ~= nil ) 2850eq ( false , api . nvim_buf_is_loaded ( fn . bufadd ( tmpfile ))) 2851end ) 2852 2853it ( 'DeleteFile fails if file does not exist and ignoreIfNotExists is false' , function () 2854local tmpfile =tmpname ( false ) 2855local uri =exec_lua ( 'return vim.uri_from_fname(...)' , tmpfile ) 2856local edit ={ 2857documentChanges ={ 2858{ 2859kind ='delete' , 2860uri =uri , 2861options ={ 2862ignoreIfNotExists =false , 2863} , 2864} , 2865} , 2866} 2867eq ( false , pcall ( exec_lua , 'vim.lsp.util.apply_workspace_edit(...)' , edit , 'utf-16' )) 2868eq ( false , vim . uv . fs_stat ( tmpfile ) ~= nil ) 2869end ) 2870end ) 2871 2872describe ( 'vim.lsp.formatexpr' , function () 2873---@return string[] 2874local function nvim_buf_get_all_lines () 2875return exec_lua ( function () 2876return vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ) 2877end ) 2878end 2879 2880before_each ( function () 2881exec_lua ( create_server_definition ) 2882exec_lua ( function () 2883vim . bo . formatexpr ='v:lua.vim.lsp.formatexpr()' 2884 2885---@class test.functional.plugin.lsp_spec.formatexpr.start_server.Opts 2886---@field range_formatting_handler? fun(params: lsp.DocumentRangeFormattingParams): lsp.TextEdit[]? 2887---@field formatting_handler? fun(params: lsp.DocumentFormattingParams): lsp.TextEdit[]? 2888 2889local server_number =1 2890---Starts a server which can support textDocument/rangeFormatting and 2891---textDocument/formatting. Providing a handler for either method enables the corresponding 2892---capability. 2893---@param opts test.functional.plugin.lsp_spec.formatexpr.start_server.Opts? 2894function _G . start_server ( opts ) 2895opts =opts or {} 2896local server =_G . _create_server ( { 2897capabilities ={ 2898documentRangeFormattingProvider =opts . range_formatting_handler ~= nil , 2899documentFormattingProvider =opts . formatting_handler ~= nil , 2900} , 2901handlers ={ 2902---@param params lsp.DocumentRangeFormattingParams 2903[ 'textDocument/rangeFormatting' ] =function ( _ , params , callback ) 2904local text_edits =opts . range_formatting_handler ( params ) 2905callback ( nil , text_edits ) 2906end , 2907---@param params lsp.DocumentFormattingParams 2908[ 'textDocument/formatting' ] =function ( _ , params , callback ) 2909local text_edits =opts . formatting_handler ( params ) 2910callback ( nil , text_edits ) 2911end , 2912} , 2913} ) 2914vim . lsp . start ( { name =string . format ( 'server %d' , server_number ), cmd =server . cmd } ) 2915server_number =server_number + 1 2916end 2917end ) 2918end ) 2919 2920it ( 'formats range of lines with textDocument/rangeFormatting' , function () 2921local lines ={ 'line 1' , 'line 2' , 'line 3' , 'line 4' } 2922exec_lua ( function () 2923vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 2924-- This server formats a range starting on line n and ending on line m by replacing the 2925-- whole range with the lines "formatted line $i" where $i goes from n to m. This enables us 2926-- to verify that the correct range is requested for formatting (starts at first character 2927-- of first line and ends on last character of last line). 2928start_server ( { 2929range_formatting_handler =function ( params ) 2930local lines ={} ---@type string[] 2931for i =params . range . start . line + 1 , params . range [ 'end' ]. line + 1 do 2932table . insert ( lines , string . format ( 'formatted line %d' , i )) 2933end 2934return { { range =params . range , newText =table . concat ( lines , '\n' ) } } 2935end , 2936} ) 2937end ) 2938 2939feed ( '2G' ) -- Move to line 2 2940feed ( 'gqj' ) -- Format lines 2 and 3 2941 2942local expected_lines =vim . deepcopy ( lines ) 2943expected_lines [ 2 ] ='formatted line 2' 2944expected_lines [ 3 ] ='formatted line 3' 2945eq ( expected_lines , nvim_buf_get_all_lines ()) 2946end ) 2947 2948it ( 2949'formats whole buffer with textDocument/rangeFormatting when client supports textDocument/rangeFormatting and textDocument/formatting' , 2950function () 2951local range_formatting_new_text ='range formatting result' 2952local formatting_new_text ='formatting result' 2953exec_lua ( function () 2954-- This server formats a range and a whole document by inserting "range formatting result" 2955-- and "formatting result" respectively at the start of the buffer. 2956local first_char ={ line =0 , character =0 } 2957start_server ( { 2958range_formatting_handler =function () 2959return { 2960{ 2961range ={ start =first_char , [ 'end' ] =first_char } , 2962newText =range_formatting_new_text , 2963} , 2964} 2965end , 2966formatting_handler =function () 2967return { 2968{ 2969range ={ start =first_char , [ 'end' ] =first_char } , 2970newText =formatting_new_text , 2971} , 2972} 2973end , 2974} ) 2975end ) 2976 2977feed ( 'gqal' ) -- Format whole buffer 2978 2979eq ( { range_formatting_new_text } , nvim_buf_get_all_lines ()) 2980end 2981) 2982 2983it ( 2984'formats whole buffer with textDocument/formatting when client supports textDocument/formatting but not textDocument/rangeFormatting' , 2985function () 2986local new_text ='formatting result' 2987exec_lua ( function () 2988-- This server formats a whole document by inserting "formatting result" at the start of 2989-- the buffer. 2990start_server ( { 2991formatting_handler =function () 2992local first_char ={ line =0 , character =0 } 2993return { 2994{ 2995range ={ start =first_char , [ 'end' ] =first_char } , 2996newText =new_text , 2997} , 2998} 2999end , 3000} ) 3001end ) 3002 3003feed ( 'gqal' ) -- Format whole buffer 3004 3005eq ( { new_text } , nvim_buf_get_all_lines ()) 3006end 3007) 3008 3009it ( 3010'does nothing when range is not whole buffer and client supports textDocument/formatting but not textDocument/rangeFormatting' , 3011function () 3012local lines ={ 'a b' , 'c d' } 3013local new_text ='formatting result' 3014exec_lua ( function () 3015vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 3016-- Set small textwidth to verify that gq doesn't perform internal formatting (wrap 3017-- lines) on the buffer. 3018vim . bo . textwidth =1 3019-- This server formats a whole document by inserting "formatting result" at the start of 3020-- the buffer. 3021start_server ( { 3022formatting_handler =function () 3023local first_char ={ line =0 , character =0 } 3024return { 3025{ 3026range ={ start =first_char , [ 'end' ] =first_char } , 3027newText =new_text , 3028} , 3029} 3030end , 3031} ) 3032end ) 3033 3034feed ( '2G' ) -- Move to line 2 3035feed ( 'Vgq' ) -- Format line 2 3036 3037eq ( lines , nvim_buf_get_all_lines ()) 3038end 3039) 3040 3041it ( 'applies only one formatting result when multiple clients support formatting' , function () 3042local new_text ='formatting result' 3043exec_lua ( function () 3044-- These servers format a range by inserting "formatting result" at the start of the buffer. 3045-- This enables us to verify that only one client was used, since if both were used then 3046-- both results would have been inserted at the start of the buffer. 3047for _ =1 , 2 do 3048start_server ( { 3049range_formatting_handler =function () 3050local first_char ={ line =0 , character =0 } 3051return { 3052{ 3053range ={ start =first_char , [ 'end' ] =first_char } , 3054newText =new_text , 3055} , 3056} 3057end , 3058} ) 3059end 3060end ) 3061 3062feed ( 'gqal' ) -- Format whole buffer 3063 3064eq ( { new_text } , nvim_buf_get_all_lines ()) 3065end ) 3066 3067it ( 3068'applies first non-null formatting result when multiple clients support formatting' , 3069function () 3070local new_text ='formatting result' 3071exec_lua ( function () 3072-- These servers format a range by returning null if neither of them have been called 3073-- before and inserting "formatting result" otherwise. This decouples the test from the 3074-- (undefined) order that clients are called in. 3075for _ =1 , 2 do 3076start_server ( { 3077range_formatting_handler =function () 3078if not range_formatting_called then 3079_G . range_formatting_called =true 3080return nil 3081else 3082local first_char ={ line =0 , character =0 } 3083return { 3084{ 3085range ={ start =first_char , [ 'end' ] =first_char } , 3086newText =new_text , 3087} , 3088} 3089end 3090end , 3091} ) 3092end 3093end ) 3094 3095feed ( 'gqal' ) -- Format whole buffer 3096 3097eq ( { new_text } , nvim_buf_get_all_lines ()) 3098end 3099) 3100 3101describe ( 'does nothing when' , function () 3102---@type table<string, function> 3103local test_case_setups ={ 3104[ 'no clients are attached' ] =function () end , 3105[ 'all servers return null formatting result' ] =function () 3106start_server ( { 3107range_formatting_handler =function () 3108return nil 3109end , 3110} ) 3111end , 3112[ 'no servers support textDocument/rangeFormatting or textDocument/formatting' ] =function () 3113start_server () 3114end , 3115} 3116 3117for test_case , setup in pairs ( test_case_setups ) do 3118it ( test_case , function () 3119local lines ={ 'a b' } 3120exec_lua ( function () 3121vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , lines ) 3122-- Set small textwidth to verify that gq doesn't perform internal formatting (wrap 3123-- lines) on the buffer. 3124vim . bo . textwidth =1 3125start_server ( { 3126range_formatting_handler =function () 3127return nil 3128end , 3129} ) 3130end ) 3131exec_lua ( setup ) 3132 3133feed ( 'gqal' ) -- Format whole buffer 3134 3135eq ( lines , nvim_buf_get_all_lines ()) 3136end ) 3137end 3138end ) 3139 3140describe ( 'falls back to internal formatting' , function () 3141for _ , mode in ipairs ( { 'i' , 'R' } ) do 3142it ( string . format ( 'in %q mode' , mode ), function () 3143exec_lua ( function () 3144vim . bo . textwidth =1 3145-- This server formats a range by inserting "formatting result" at the start of the 3146-- buffer. 3147start_server ( { 3148range_formatting_handler =function () 3149local first_char ={ line =0 , character =0 } 3150return { 3151{ 3152range ={ start =first_char , [ 'end' ] =first_char } , 3153newText ='formatting result' , 3154} , 3155} 3156end , 3157} ) 3158end ) 3159 3160feed ( mode ) 3161feed ( 'a b' ) 3162 3163eq ( { 'a' , 'b' } , nvim_buf_get_all_lines ()) 3164end ) 3165end 3166end ) 3167end ) 3168 3169describe ( 'vim.lsp.tagfunc' , function () 3170before_each ( function () 3171---@type lsp.Location[] 3172local mock_locations ={ 3173{ 3174range ={ 3175[ 'start' ] ={ line =5 , character =23 } , 3176[ 'end' ] ={ line =10 , character =0 } , 3177} , 3178uri ='test://buf' , 3179} , 3180{ 3181range ={ 3182[ 'start' ] ={ line =42 , character =10 } , 3183[ 'end' ] ={ line =44 , character =0 } , 3184} , 3185uri ='test://another-file' , 3186} , 3187} 3188exec_lua ( create_server_definition ) 3189exec_lua ( function () 3190_G . mock_locations =mock_locations 3191_G . server =_G . _create_server ( { 3192capabilities ={ 3193definitionProvider =true , 3194workspaceSymbolProvider =true , 3195} , 3196handlers ={ 3197[ 'textDocument/definition' ] =function ( _ , _ , callback ) 3198callback ( nil , { _G . mock_locations [ 1 ] } ) 3199end , 3200[ 'workspace/symbol' ] =function ( _ , request , callback ) 3201assert ( request . query == 'foobar' ) 3202callback ( nil , { 3203{ 3204name ='foobar' , 3205kind =13 , ---@type lsp.SymbolKind 3206location =_G . mock_locations [ 1 ], 3207} , 3208{ 3209name ='vim.foobar' , 3210kind =12 , ---@type lsp.SymbolKind 3211location =_G . mock_locations [ 2 ], 3212} , 3213} ) 3214end , 3215} , 3216} ) 3217_G . client_id =vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 3218end ) 3219end ) 3220 3221after_each ( function () 3222exec_lua ( function () 3223vim . lsp . get_client_by_id ( _G . client_id ): stop () 3224end ) 3225end ) 3226 3227it ( 'with flags=c, returns matching tags using textDocument/definition' , function () 3228local result =exec_lua ( function () 3229return vim . lsp . tagfunc ( 'foobar' , 'c' ) 3230end ) 3231eq ( { 3232{ 3233cmd ='/\\%6l\\%1c/' , -- for location (5, 23) 3234filename ='test://buf' , 3235name ='foobar' , 3236} , 3237} , result ) 3238end ) 3239 3240it ( 'without flags=c, returns all matching tags using workspace/symbol' , function () 3241local result =exec_lua ( function () 3242return vim . lsp . tagfunc ( 'foobar' , '' ) 3243end ) 3244eq ( { 3245{ 3246cmd ='/\\%6l\\%1c/' , -- for location (5, 23) 3247filename ='test://buf' , 3248kind ='Variable' , 3249name ='foobar' , 3250} , 3251{ 3252cmd ='/\\%43l\\%1c/' , -- for location (42, 10) 3253filename ='test://another-file' , 3254kind ='Function' , 3255name ='vim.foobar' , 3256} , 3257} , result ) 3258end ) 3259 3260it ( 'with flags including i, returns NIL' , function () 3261exec_lua ( function () 3262local result =vim . lsp . tagfunc ( 'foobar' , 'cir' ) 3263assert ( result == vim . NIL , 'should not issue LSP requests' ) 3264return {} 3265end ) 3266end ) 3267end ) 3268 3269describe ( 'cmd' , function () 3270it ( 'cmd string[] CWD' , function () 3271local root_dir =tmpname ( false ) 3272local cmd_cwd =tmpname ( false ) 3273mkdir ( root_dir ) 3274mkdir ( cmd_cwd ) 3275 3276local cwd =exec_lua ( function () 3277return assert ( vim . uv . cwd ()) 3278end ) 3279 3280local cases ={ 3281{ 3282desc ='cmd_cwd takes precedence' , 3283config ={ name ='cwd-test-cmd-cwd' , root_dir =root_dir , cmd_cwd =cmd_cwd } , 3284expected_cwd =cmd_cwd , 3285} , 3286{ 3287desc ='root_dir is used when cmd_cwd is unset' , 3288config ={ name ='cwd-test-root-dir' , root_dir =root_dir } , 3289expected_cwd =root_dir , 3290} , 3291{ 3292desc ='current cwd is used when cmd_cwd and root_dir are unset' , 3293config ={ name ='cwd-test-current-cwd' } , 3294expected_cwd =cwd , 3295} , 3296} 3297 3298for _ , case in ipairs ( cases ) do 3299local outfile =tmpname ( false ) 3300local config =vim . tbl_extend ( 'force' , { 3301cmd ={ 3302n . nvim_prog , 3303'--clean' , 3304'--headless' , 3305( '+call writefile([getcwd()], %q)' ): format ( outfile ), 3306'+qa!' , 3307} , 3308} , case . config ) 3309exec_lua ( function ( conf ) 3310assert ( vim . lsp . start ( conf , { attach =false } )) 3311end , config ) 3312t . assert_log ( vim . pesc ( case . expected_cwd ), outfile ) 3313end 3314end ) 3315 3316it ( 'connects to lsp server via rpc.connect using ip address' , function () 3317exec_lua ( create_tcp_echo_server ) 3318exec_lua ( function () 3319local port =_G . _create_tcp_server ( '127.0.0.1' ) 3320vim . lsp . start ( { name ='dummy' , cmd =vim . lsp . rpc . connect ( '127.0.0.1' , port ) } ) 3321end ) 3322verify_single_notification ( function ( method , args ) ---@param args [string] 3323eq ( 'body' , method ) 3324eq ( 'initialize' , vim . json . decode ( args [ 1 ]). method ) 3325end ) 3326end ) 3327 3328it ( 'connects to lsp server via rpc.connect using hostname' , function () 3329skip ( is_os ( 'bsd' ), 'issue with host resolution in ci' ) 3330skip ( t . is_arch ( 's390x' ), 'issue with host resolution in ci' ) 3331exec_lua ( create_tcp_echo_server ) 3332exec_lua ( function () 3333local port =_G . _create_tcp_server ( '::1' ) 3334vim . lsp . start ( { name ='dummy' , cmd =vim . lsp . rpc . connect ( 'localhost' , port ) } ) 3335end ) 3336verify_single_notification ( function ( method , args ) ---@param args [string] 3337eq ( 'body' , method ) 3338eq ( 'initialize' , vim . json . decode ( args [ 1 ]). method ) 3339end ) 3340end ) 3341 3342it ( 'can connect to lsp server via pipe or domain_socket' , function () 3343local tmpfile =is_os ( 'win' ) and '\\\\.\\\\pipe\\pipe.test' or tmpname ( false ) 3344local result =exec_lua ( function () 3345local uv =vim . uv 3346local server =assert ( uv . new_pipe ( false )) 3347server : bind ( tmpfile ) 3348local init =nil 3349 3350server : listen ( 127 , function ( err ) 3351assert ( not err , err ) 3352local client =assert ( vim . uv . new_pipe ()) 3353server : accept ( client ) 3354client : read_start ( require ( 'vim.lsp.rpc' ). create_read_loop ( function ( body ) 3355init =body 3356client : close () 3357end )) 3358end ) 3359vim . lsp . start ( { name ='dummy' , cmd =vim . lsp . rpc . connect ( tmpfile ) } ) 3360vim . wait ( 1000 , function () 3361return init ~= nil 3362end ) 3363assert ( init , 'server must receive `initialize` request' ) 3364server : close () 3365server : shutdown () 3366return vim . json . decode ( init ) 3367end ) 3368eq ( 'initialize' , result . method ) 3369end ) 3370 3371it ( 'clean up failed lsp client when rpc.connect fails' , function () 3372exec_lua ( function () 3373local server =assert ( vim . uv . new_tcp ()) 3374server : bind ( '127.0.0.1' , 0 ) 3375local port =server : getsockname (). port 3376server : close () 3377local exited =false 3378vim . lsp . start ( { 3379name ='dummy' , 3380cmd =vim . lsp . rpc . connect ( '127.0.0.1' , port ), 3381on_exit =function () 3382exited =true 3383end , 3384} ) 3385assert ( 3386vim . wait ( 1000 , function () 3387return # vim . lsp . get_clients ( { name ='dummy' , _uninitialized =true } ) == 0 3388end ), 3389'Timed out waiting for errored client to be cleaned up' 3390) 3391assert ( exited ) 3392end ) 3393end ) 3394end ) 3395 3396describe ( 'handlers' , function () 3397it ( 'handler can return false as response' , function () 3398local result =exec_lua ( function () 3399local server =assert ( vim . uv . new_tcp ()) 3400local messages ={} 3401local responses ={} 3402server : bind ( '127.0.0.1' , 0 ) 3403server : listen ( 127 , function ( err ) 3404assert ( not err , err ) 3405local socket =assert ( vim . uv . new_tcp ()) 3406server : accept ( socket ) 3407socket : read_start ( require ( 'vim.lsp.rpc' ). create_read_loop ( function ( body ) 3408local payload =vim . json . decode ( body ) 3409if payload . method then 3410table . insert ( messages , payload . method ) 3411if payload . method == 'initialize' then 3412local msg =vim . json . encode ( { 3413id =payload . id , 3414jsonrpc ='2.0' , 3415result ={ 3416capabilities ={} , 3417} , 3418} ) 3419socket : write ( table . concat ( { 'Content-Length: ' , tostring ( # msg ), '\r\n\r\n' , msg } )) 3420elseif payload . method == 'initialized' then 3421local msg =vim . json . encode ( { 3422id =10 , 3423jsonrpc ='2.0' , 3424method ='dummy' , 3425params ={} , 3426} ) 3427socket : write ( table . concat ( { 'Content-Length: ' , tostring ( # msg ), '\r\n\r\n' , msg } )) 3428end 3429else 3430table . insert ( responses , payload ) 3431socket : close () 3432end 3433end )) 3434end ) 3435local port =server : getsockname (). port 3436local handler_called =false 3437vim . lsp . handlers [ 'dummy' ] =function ( _ , _ ) 3438handler_called =true 3439return false 3440end 3441local client_id = 3442assert ( vim . lsp . start ( { name ='dummy' , cmd =vim . lsp . rpc . connect ( '127.0.0.1' , port ) } )) 3443vim . lsp . get_client_by_id ( client_id ) 3444vim . wait ( 1000 , function () 3445return # messages == 2 and handler_called and # responses == 1 3446end ) 3447server : close () 3448server : shutdown () 3449return { 3450messages =messages , 3451handler_called =handler_called , 3452responses =responses , 3453} 3454end ) 3455local expected ={ 3456messages ={ 'initialize' , 'initialized' } , 3457handler_called =true , 3458responses ={ 3459{ 3460id =10 , 3461jsonrpc ='2.0' , 3462result =false , 3463} , 3464} , 3465} 3466eq ( expected , result ) 3467end ) 3468 3469--- Starts a TCP server that completes initialization, then sends `null_id_payload` after the 3470--- "initialized" notification. 3471--- 3472--- @param null_id_payload string JSON 3473--- @return { on_error_called: table, notification_received: boolean, messages: boolean }. 3474local function test_null_id_response ( null_id_payload ) 3475return exec_lua ( function () 3476local server =assert ( vim . uv . new_tcp ()) 3477local accepted 3478local messages ={} 3479server : bind ( '127.0.0.1' , 0 ) 3480server : listen ( 127 , function ( err ) 3481assert ( not err , err ) 3482accepted =assert ( vim . uv . new_tcp ()) 3483server : accept ( accepted ) 3484accepted : read_start ( require ( 'vim.lsp.rpc' ). create_read_loop ( function ( body ) 3485local payload =vim . json . decode ( body ) 3486if payload . method then 3487table . insert ( messages , payload . method ) 3488if payload . method == 'initialize' then 3489-- Send a valid initialize response first 3490local msg =vim . json . encode ( { 3491id =payload . id , 3492jsonrpc ='2.0' , 3493result ={ 3494capabilities ={} , 3495} , 3496} ) 3497accepted : write ( 3498table . concat ( { 'Content-Length: ' , tostring ( # msg ), '\r\n\r\n' , msg } ) 3499) 3500elseif payload . method == 'initialized' then 3501accepted : write ( table . concat ( { 3502'Content-Length: ' , 3503tostring ( # null_id_payload ), 3504'\r\n\r\n' , 3505null_id_payload , 3506} )) 3507end 3508end 3509end , function () 3510if accepted and not accepted : is_closing () then 3511accepted : close () 3512end 3513end , function () 3514if accepted and not accepted : is_closing () then 3515accepted : close () 3516end 3517end )) 3518end ) 3519local port =server : getsockname (). port 3520local on_error_called =false 3521local notification_received =false 3522local client_id =assert ( vim . lsp . start ( { 3523name ='null-id-test' , 3524cmd =function ( dispatchers ) 3525local notification =dispatchers . notification 3526dispatchers . notification =function ( method , params ) 3527notification_received =true 3528if notification then 3529notification ( method , params ) 3530end 3531end 3532return vim . lsp . rpc . connect ( '127.0.0.1' , port )( dispatchers ) 3533end , 3534on_error =function ( _code , _err ) 3535on_error_called =true 3536end , 3537} )) 3538vim . lsp . get_client_by_id ( client_id ) 3539vim . wait ( 1000 , function () 3540return # messages >= 2 and ( on_error_called or notification_received ) 3541end ) 3542if accepted and not accepted : is_closing () then 3543accepted : close () 3544end 3545server : shutdown () 3546server : close () 3547return { 3548messages =messages , 3549on_error_called =on_error_called , 3550notification_received =notification_received , 3551} 3552end ) 3553end 3554 3555it ( 'null-id in response (JSON-RPC 2.0 parse error) is handled, emits error' , function () 3556local result =test_null_id_response ( 3557'{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}' 3558) 3559eq ( true , result . on_error_called ) 3560eq ( true , # result . messages >= 2 ) 3561end ) 3562 3563it ( 'null-id in response does not misclassify as a notification' , function () 3564-- Sanity check: a real notification (no id) dispatches the handler. 3565local valid =test_null_id_response ( 3566'{"jsonrpc":"2.0","method":"workspace/configuration","params":{"items":[]}}' 3567) 3568eq ( true , valid . notification_received ) 3569 3570local result =test_null_id_response ( 3571-- Error response with null id (parse error per JSON-RPC 2.0 §5) 3572'{"jsonrpc":"2.0","method":"workspace/configuration","params":{"items":[]},"id":null}' 3573) 3574-- Null id must be dispatched as a request, not as a notification. 3575eq ( false , result . notification_received ) 3576end ) 3577end ) 3578 3579describe ( '#dynamic vim.lsp._dynamic' , function () 3580it ( 'supports dynamic registration' , function () 3581local root_dir =tmpname ( false ) 3582mkdir ( root_dir ) 3583local tmpfile =root_dir .. '/dynamic.foo' 3584local file =io . open ( tmpfile , 'w' ) 3585if file then 3586file : close () 3587end 3588 3589exec_lua ( create_server_definition ) 3590local result =exec_lua ( function () 3591local server =_G . _create_server () 3592local client_id =assert ( vim . lsp . start ( { 3593name ='dynamic-test' , 3594cmd =server . cmd , 3595root_dir =root_dir , 3596get_language_id =function () 3597return 'dummy-lang' 3598end , 3599capabilities ={ 3600textDocument ={ 3601formatting ={ 3602dynamicRegistration =true , 3603} , 3604rangeFormatting ={ 3605dynamicRegistration =true , 3606} , 3607} , 3608workspace ={ 3609didChangeWatchedFiles ={ 3610dynamicRegistration =true , 3611} , 3612didChangeConfiguration ={ 3613dynamicRegistration =true , 3614} , 3615fileOperations ={ 3616didCreate ={ 3617dynamicRegistration =true , 3618} , 3619} , 3620textDocumentContent ={ 3621dynamicRegistration =true , 3622} , 3623} , 3624} , 3625} )) 3626 3627vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3628registrations ={ 3629{ 3630id ='formatting' , 3631method ='textDocument/formatting' , 3632registerOptions ={ 3633documentSelector ={ 3634{ 3635pattern =root_dir .. '/*.foo' , 3636} , 3637{ pattern ='**/**.foo' } , 3638} , 3639} , 3640} , 3641} , 3642} , { client_id =client_id } ) 3643 3644vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3645registrations ={ 3646{ 3647id ='range-formatting' , 3648method ='textDocument/rangeFormatting' , 3649registerOptions ={ 3650documentSelector ={ 3651{ 3652language ='dummy-lang' , 3653} , 3654} , 3655} , 3656} , 3657} , 3658} , { client_id =client_id } ) 3659 3660vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3661registrations ={ 3662{ 3663id ='completion' , 3664method ='textDocument/completion' , 3665} , 3666} , 3667} , { client_id =client_id } ) 3668 3669local result ={} 3670local function check ( method , fname , ... ) 3671local bufnr =fname and vim . fn . bufadd ( fname ) or nil 3672local client =assert ( vim . lsp . get_client_by_id ( client_id )) 3673local keys ={ ... } 3674local caps ={} 3675if # keys > 0 then 3676client : _provider_foreach ( method , function ( cap ) 3677table . insert ( caps , vim . tbl_get ( cap , unpack ( keys )) or vim . NIL ) 3678end ) 3679end 3680result [ # result + 1 ] ={ 3681method =method , 3682fname =fname , 3683supported =client : supports_method ( method , bufnr ), 3684cap =# keys > 0 and caps or nil , 3685} 3686end 3687 3688check ( 'textDocument/formatting' ) 3689check ( 'textDocument/formatting' , tmpfile ) 3690check ( 'textDocument/rangeFormatting' ) 3691check ( 'textDocument/rangeFormatting' , tmpfile ) 3692check ( 'textDocument/completion' ) 3693 3694check ( 'workspace/didChangeWatchedFiles' ) 3695check ( 'workspace/didChangeWatchedFiles' , tmpfile ) 3696 3697vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3698registrations ={ 3699{ 3700id ='didChangeWatched' , 3701method ='workspace/didChangeWatchedFiles' , 3702registerOptions ={ 3703watchers ={ 3704{ 3705globPattern ='something' , 3706kind =4 , 3707} , 3708} , 3709} , 3710} , 3711} , 3712} , { client_id =client_id } ) 3713 3714check ( 'workspace/didChangeWatchedFiles' ) 3715check ( 'workspace/didChangeWatchedFiles' , tmpfile ) 3716 3717-- Initial support false 3718check ( 'workspace/diagnostic' ) 3719 3720vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3721registrations ={ 3722{ 3723id ='diag1' , 3724method ='textDocument/diagnostic' , 3725registerOptions ={ 3726identifier ='diag-ident-1' , 3727-- workspaceDiagnostics field omitted 3728} , 3729} , 3730} , 3731} , { client_id =client_id } ) 3732 3733-- Checks after registering without workspaceDiagnostics support 3734-- Returns false 3735check ( 'workspace/diagnostic' , nil , 'identifier' ) 3736 3737vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3738registrations ={ 3739{ 3740id ='diag2' , 3741method ='textDocument/diagnostic' , 3742registerOptions ={ 3743identifier ='diag-ident-2' , 3744workspaceDiagnostics =true , 3745} , 3746} , 3747} , 3748} , { client_id =client_id } ) 3749 3750-- Check after second registration with support 3751-- Returns true 3752check ( 'workspace/diagnostic' , nil , 'identifier' ) 3753 3754vim . lsp . handlers [ 'client/unregisterCapability' ]( nil , { 3755unregisterations ={ 3756{ id ='diag2' , method ='textDocument/diagnostic' } , 3757} , 3758} , { client_id =client_id } ) 3759 3760-- Check after unregistering 3761-- Returns false 3762check ( 'workspace/diagnostic' , nil , 'identifier' ) 3763 3764check ( 'textDocument/codeAction' ) 3765check ( 'codeAction/resolve' ) 3766 3767vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3768registrations ={ 3769{ 3770id ='codeAction' , 3771method ='textDocument/codeAction' , 3772registerOptions ={ 3773resolveProvider =true , 3774} , 3775} , 3776} , 3777} , { client_id =client_id } ) 3778 3779check ( 'textDocument/codeAction' ) 3780check ( 'codeAction/resolve' ) 3781 3782check ( 'workspace/didChangeWorkspaceFolders' ) 3783vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3784registrations ={ 3785{ 3786id ='didChangeWorkspaceFolders-id' , 3787method ='workspace/didChangeWorkspaceFolders' , 3788} , 3789} , 3790} , { client_id =client_id } ) 3791check ( 'workspace/didChangeWorkspaceFolders' ) 3792 3793check ( 'workspace/didChangeConfiguration' ) 3794vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3795registrations ={ 3796{ 3797id ='didChangeConfiguration-id' , 3798method ='workspace/didChangeConfiguration' , 3799registerOptions ={ 3800section ='dummy-section' , 3801} , 3802} , 3803} , 3804} , { client_id =client_id } ) 3805check ( 'workspace/didChangeConfiguration' , nil , 'section' ) 3806 3807check ( 'workspace/didCreateFiles' ) 3808vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3809registrations ={ 3810{ 3811id ='didCreateFiles-id' , 3812method ='workspace/didCreateFiles' , 3813registerOptions ={ 3814label ='did-create-files' , 3815filters ={ 3816{ 3817scheme ='file' , 3818pattern ={ 3819glob ='**/*.foo' , 3820} , 3821} , 3822} , 3823} , 3824} , 3825} , 3826} , { client_id =client_id } ) 3827check ( 'workspace/didCreateFiles' , nil , 'label' ) 3828 3829check ( 'workspace/textDocumentContent' ) 3830vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3831registrations ={ 3832{ 3833id ='textDocumentContent-id' , 3834method ='workspace/textDocumentContent' , 3835registerOptions ={ 3836schemes ={ 'git' , 'untitled' } , 3837} , 3838} , 3839} , 3840} , { client_id =client_id } ) 3841check ( 'workspace/textDocumentContent' , nil , 'schemes' ) 3842 3843vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3844registrations ={ 3845{ 3846id ='unknown-method-id' , 3847method ='unknown-method' , 3848registerOptions ={ 3849some_opt ='unknown-dummy-opt' , 3850} , 3851} , 3852} , 3853} , { client_id =client_id } ) 3854check ( 'unknown-method' , nil , 'some_opt' ) 3855 3856check ( 'unknown-method-2' ) 3857vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 3858registrations ={ 3859{ 3860id ='unknown-method-2-id' , 3861method ='unknown-method-2' , 3862registerOptions ={ 3863documentSelector ={ 3864{ 3865pattern =root_dir .. '/*.foo' , 3866} , 3867} , 3868} , 3869} , 3870} , 3871} , { client_id =client_id } ) 3872check ( 'unknown-method-2' ) 3873check ( 'unknown-method-2' , tmpfile ) 3874 3875return result 3876end ) 3877 3878eq ( 29 , # result ) 3879eq ( { method ='textDocument/formatting' , supported =false } , result [ 1 ]) 3880eq ( { method ='textDocument/formatting' , supported =true , fname =tmpfile } , result [ 2 ]) 3881eq ( { method ='textDocument/rangeFormatting' , supported =true } , result [ 3 ]) 3882eq ( { method ='textDocument/rangeFormatting' , supported =true , fname =tmpfile } , result [ 4 ]) 3883eq ( { method ='textDocument/completion' , supported =false } , result [ 5 ]) 3884eq ( { method ='workspace/didChangeWatchedFiles' , supported =false } , result [ 6 ]) 3885eq ( 3886{ method ='workspace/didChangeWatchedFiles' , supported =false , fname =tmpfile } , 3887result [ 7 ] 3888) 3889eq ( { method ='workspace/didChangeWatchedFiles' , supported =true } , result [ 8 ]) 3890eq ( 3891{ method ='workspace/didChangeWatchedFiles' , supported =true , fname =tmpfile } , 3892result [ 9 ] 3893) 3894eq ( { method ='workspace/diagnostic' , supported =false } , result [ 10 ]) 3895eq ( { method ='workspace/diagnostic' , supported =false , cap ={} } , result [ 11 ]) 3896eq ( { 3897method ='workspace/diagnostic' , 3898supported =true , 3899cap ={ 'diag-ident-2' } , 3900} , result [ 12 ]) 3901eq ( { method ='workspace/diagnostic' , supported =false , cap ={} } , result [ 13 ]) 3902eq ( { method ='textDocument/codeAction' , supported =false } , result [ 14 ]) 3903eq ( { method ='codeAction/resolve' , supported =false } , result [ 15 ]) 3904eq ( { method ='textDocument/codeAction' , supported =true } , result [ 16 ]) 3905eq ( { method ='codeAction/resolve' , supported =true } , result [ 17 ]) 3906eq ( { method ='workspace/didChangeWorkspaceFolders' , supported =false } , result [ 18 ]) 3907eq ( { method ='workspace/didChangeWorkspaceFolders' , supported =true } , result [ 19 ]) 3908eq ( { method ='workspace/didChangeConfiguration' , supported =false } , result [ 20 ]) 3909eq ( 3910{ method ='workspace/didChangeConfiguration' , supported =true , cap ={ 'dummy-section' } } , 3911result [ 21 ] 3912) 3913eq ( { method ='workspace/didCreateFiles' , supported =false } , result [ 22 ]) 3914eq ( 3915{ method ='workspace/didCreateFiles' , supported =true , cap ={ 'did-create-files' } } , 3916result [ 23 ] 3917) 3918eq ( { method ='workspace/textDocumentContent' , supported =false } , result [ 24 ]) 3919eq ( { 3920method ='workspace/textDocumentContent' , 3921supported =true , 3922cap ={ { 'git' , 'untitled' } } , 3923} , result [ 25 ]) 3924eq ( { 3925method ='unknown-method' , 3926supported =true , 3927cap ={ 'unknown-dummy-opt' } , 3928} , result [ 26 ]) 3929eq ( { method ='unknown-method-2' , supported =true } , result [ 27 ]) 3930eq ( { method ='unknown-method-2' , supported =false } , result [ 28 ]) 3931eq ( { method ='unknown-method-2' , supported =true , fname =tmpfile } , result [ 29 ]) 3932end ) 3933 3934it ( 'identifies client dynamic registration capability' , function () 3935exec_lua ( create_server_definition ) 3936local result =exec_lua ( function () 3937local server =_G . _create_server () 3938local client_id =assert ( vim . lsp . start ( { 3939name ='dynamic-test' , 3940cmd =server . cmd , 3941capabilities ={ 3942textDocument ={ 3943formatting ={ 3944dynamicRegistration =true , 3945} , 3946synchronization ={ 3947dynamicRegistration =true , 3948} , 3949diagnostic ={ 3950dynamicRegistration =true , 3951} , 3952} , 3953} , 3954} )) 3955 3956local result ={} 3957local function check ( method ) 3958local client =assert ( vim . lsp . get_client_by_id ( client_id )) 3959result [ # result + 1 ] ={ 3960method =method , 3961supports_reg =client : _supports_registration ( method ), 3962} 3963end 3964 3965check ( 'textDocument/formatting' ) 3966check ( 'textDocument/didSave' ) 3967check ( 'textDocument/didOpen' ) 3968check ( 'textDocument/codeLens' ) 3969check ( 'textDocument/diagnostic' ) 3970check ( 'workspace/diagnostic' ) 3971 3972return result 3973end ) 3974 3975eq ( 6 , # result ) 3976eq ( { method ='textDocument/formatting' , supports_reg =true } , result [ 1 ]) 3977eq ( { method ='textDocument/didSave' , supports_reg =true } , result [ 2 ]) 3978eq ( { method ='textDocument/didOpen' , supports_reg =true } , result [ 3 ]) 3979eq ( { method ='textDocument/codeLens' , supports_reg =false } , result [ 4 ]) 3980eq ( { method ='textDocument/diagnostic' , supports_reg =true } , result [ 5 ]) 3981eq ( { method ='workspace/diagnostic' , supports_reg =true } , result [ 6 ]) 3982end ) 3983 3984it ( 'supports static registration' , function () 3985exec_lua ( create_server_definition ) 3986 3987local client_id =exec_lua ( function () 3988local server =_G . _create_server ( { 3989capabilities ={ 3990colorProvider ={ id ='color-registration' } , 3991diagnosticProvider ={ 3992documentSelector =vim . NIL , 3993interFileDependencies =false , 3994id ='diag-registration' , 3995identifier ='diag-ident-static' , 3996workspaceDiagnostics =true , 3997} , 3998workspace ={ 3999textDocumentContent ={ 4000id ='text-document-content-registration' , 4001schemes ={ 'git' , 'untitled' } , 4002} , 4003} , 4004} , 4005} ) 4006 4007return assert ( vim . lsp . start ( { name ='dynamic-test' , cmd =server . cmd } )) 4008end ) 4009 4010local function sort_method ( tbl ) 4011local result_t =vim . deepcopy ( tbl ) 4012table . sort ( result_t , function ( a , b ) 4013return ( a . method or '' ) < ( b . method or '' ) 4014end ) 4015return result_t 4016end 4017 4018eq ( 4019{ 4020{ 4021id ='color-registration' , 4022method ='textDocument/colorPresentation' , 4023registerOptions ={ id ='color-registration' } , 4024} , 4025{ 4026id ='color-registration' , 4027method ='textDocument/documentColor' , 4028registerOptions ={ id ='color-registration' } , 4029} , 4030} , 4031sort_method ( exec_lua ( function () 4032local client =assert ( vim . lsp . get_client_by_id ( client_id )) 4033return client . dynamic_capabilities : get ( 'colorProvider' ) 4034end )) 4035) 4036 4037eq ( 4038{ 4039{ 4040id ='diag-registration' , 4041method ='textDocument/diagnostic' , 4042registerOptions ={ 4043documentSelector =vim . NIL , 4044interFileDependencies =false , 4045id ='diag-registration' , 4046identifier ='diag-ident-static' , 4047workspaceDiagnostics =true , 4048} , 4049} , 4050} , 4051sort_method ( exec_lua ( function () 4052local client =assert ( vim . lsp . get_client_by_id ( client_id )) 4053return client . dynamic_capabilities : get ( 'diagnosticProvider' ) 4054end )) 4055) 4056 4057eq ( 4058{ 'diag-ident-static' } , 4059exec_lua ( function () 4060local client =assert ( vim . lsp . get_client_by_id ( client_id )) 4061local result ={} 4062client : _provider_foreach ( 'textDocument/diagnostic' , function ( cap ) 4063table . insert ( result , cap . identifier ) 4064end ) 4065return result 4066end ) 4067) 4068 4069eq ( 4070{ 4071{ 4072id ='text-document-content-registration' , 4073method ='workspace/textDocumentContent' , 4074registerOptions ={ 4075id ='text-document-content-registration' , 4076schemes ={ 'git' , 'untitled' } , 4077} , 4078} , 4079} , 4080sort_method ( exec_lua ( function () 4081local client =assert ( vim . lsp . get_client_by_id ( client_id )) 4082return client . dynamic_capabilities : get ( 'workspace.textDocumentContent' ) 4083end )) 4084) 4085 4086eq ( 4087{ { 'git' , 'untitled' } } , 4088exec_lua ( function () 4089local client =assert ( vim . lsp . get_client_by_id ( client_id )) 4090local result ={} 4091client : _provider_foreach ( 'workspace/textDocumentContent' , function ( cap ) 4092table . insert ( result , cap . schemes ) 4093end ) 4094return result 4095end ) 4096) 4097end ) 4098end ) 4099 4100describe ( 'vim.lsp._watchfiles' , function () 4101--- @type integer, integer, integer 4102local created , changed , deleted 4103 4104setup ( function () 4105n . clear () 4106created =exec_lua ( [[return vim.lsp.protocol.FileChangeType.Created]] ) 4107changed =exec_lua ( [[return vim.lsp.protocol.FileChangeType.Changed]] ) 4108deleted =exec_lua ( [[return vim.lsp.protocol.FileChangeType.Deleted]] ) 4109end ) 4110 4111before_each ( function () 4112exec_lua ( create_server_definition ) 4113exec_lua ( function () 4114_G . start_watchfiles_client =function ( root_dir ) 4115local server =_G . _create_server () 4116local client_id =assert ( vim . lsp . start ( { 4117name ='watchfiles-test' , 4118cmd =server . cmd , 4119root_dir =root_dir , 4120capabilities ={ 4121workspace ={ didChangeWatchedFiles ={ dynamicRegistration =true } } , 4122} , 4123} )) 4124 4125local function wait_for_messages ( count ) 4126assert ( 4127vim . wait ( 2500 , function () 4128return # server . messages == count 4129end ), 4130'Timed out waiting for messages: ' .. vim . inspect ( server . messages ) 4131) 4132end 4133 4134wait_for_messages ( 2 ) -- initialize, initialized 4135return assert ( vim . lsp . get_client_by_id ( client_id )), server , wait_for_messages 4136end 4137 4138-- Mock the process so glob compilation, sharing, and event delivery still run. 4139_G . mock_watch =function () 4140local watchers ={} 4141vim . lsp . _watchfiles . _watchfunc =vim . _watch . inotify 4142vim . system =function ( _ , opts ) 4143watchers [ # watchers + 1 ] =opts 4144return { 4145kill =function () 4146assert ( not opts . stopped , 'watcher cancelled twice' ) 4147opts . stopped =true 4148end , 4149} 4150end 4151 4152local function send_event ( path , change_type ) 4153local event =( { 'CREATE' , 'MODIFY' , 'DELETE' } )[ change_type ] 4154for _ , opts in ipairs ( watchers ) do 4155if not opts . stopped then 4156opts . stdout ( 4157nil , 4158vim . fs . dirname ( path ) .. ' ' .. event .. ' ' .. vim . fs . basename ( path ) 4159) 4160end 4161end 4162end 4163 4164return watchers , send_event 4165end 4166end ) 4167end ) 4168 4169local function test_filechanges ( watchfunc , root_change ) 4170it ( 4171string . format ( 4172'sends notifications when files change (watchfunc=%s)%s' , 4173watchfunc , 4174root_change and ( ' after root is ' .. root_change ) or '' 4175), 4176function () 4177if watchfunc == 'inotify' then 4178skip ( is_os ( 'win' ), 'not supported on windows' ) 4179skip ( is_os ( 'mac' ), 'flaky test on mac' ) 4180skip ( 4181not is_ci () and fn . executable ( 'inotifywait' ) == 0 , 4182'inotify-tools not installed and not on CI' 4183) 4184skip ( t . is_arch ( 's390x' ), 'inotifywait not available on s390x CI' ) 4185end 4186 4187if watchfunc == 'watch' then 4188skip ( is_os ( 'mac' ), 'flaky test on mac' ) 4189skip ( 4190is_os ( 'bsd' ), 4191'Stopped working on bsd after 3ca967387c49c754561c3b11a574797504d40f38' 4192) 4193else 4194skip ( 4195is_os ( 'bsd' ), 4196'kqueue only reports events on watched folder itself, not contained files #26110' 4197) 4198end 4199 4200local root_dir =tmpname ( false ) 4201if root_change ~= 'created' then 4202mkdir ( root_dir ) 4203end 4204 4205local result =exec_lua ( function () 4206local logfile =vim . lsp . log . get_filename () 4207vim . lsp . log . set_level ( 'info' ) 4208vim . fn . writefile ( { '' } , logfile ) 4209local notifications ={} 4210vim . notify =function ( message , level ) 4211notifications [ # notifications + 1 ] ={ message , level } 4212end 4213local client , server , wait_for_messages =_G . start_watchfiles_client ( root_dir ) 4214 4215vim . lsp . _watchfiles . _watchfunc =vim . _watch [ watchfunc ] 4216 4217vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 4218registrations ={ 4219{ 4220id ='watchfiles-test-0' , 4221method ='workspace/didChangeWatchedFiles' , 4222registerOptions ={ 4223watchers ={ 4224{ 4225globPattern ='a/**b' , 4226kind =7 , 4227} , 4228{ 4229globPattern ={ 4230baseUri =vim . uri_from_fname ( root_dir ), 4231pattern ='{foo}' , 4232} , 4233kind =7 , 4234} , 4235{ 4236globPattern ='**/watch' , 4237kind =7 , 4238} , 4239} , 4240} , 4241} , 4242} , 4243} , { client_id =client . id } ) 4244 4245if root_change then 4246local method ='workspace/didChangeWatchedFiles' 4247local reg =vim . deepcopy ( client . registrations [ method ][ 1 ]) 4248if root_change == 'replaced' then 4249-- Allocate a different inode before deleting the original directory. 4250vim . fn . mkdir ( root_dir .. '-replacement' ) 4251assert ( vim . uv . fs_rmdir ( root_dir )) 4252assert ( vim . uv . fs_rename ( root_dir .. '-replacement' , root_dir )) 4253else 4254reg . id ='watchfiles-test-missing' 4255client : _register ( { reg } ) 4256client : _unregister ( { { id =reg . id , method =method } } ) 4257vim . fn . mkdir ( root_dir ) 4258end 4259reg . id ='watchfiles-test-1' 4260client : _register ( { reg } ) 4261client : _unregister ( { { id ='watchfiles-test-0' , method =method } } ) 4262end 4263 4264if watchfunc ~= 'watch' then 4265vim . wait ( 100 ) 4266end 4267 4268local path =root_dir .. '/watch' 4269local tmp =vim . fn . tempname () 4270io . open ( tmp , 'w' ): close () 4271vim . uv . fs_rename ( tmp , path ) 4272 4273wait_for_messages ( 3 ) 4274 4275os . remove ( path ) 4276 4277wait_for_messages ( 4 ) 4278 4279client : stop () 4280 4281return { logfile =logfile , messages =server . messages , notifications =notifications } 4282end ) 4283 4284local uri =vim . uri_from_fname ( root_dir .. '/watch' ) 4285local messages =result . messages 4286 4287eq ( 6 , # messages ) 4288 4289eq ( { 4290method ='workspace/didChangeWatchedFiles' , 4291params ={ 4292changes ={ 4293{ 4294type =created , 4295uri =uri , 4296} , 4297} , 4298} , 4299} , messages [ 3 ]) 4300 4301eq ( { 4302method ='workspace/didChangeWatchedFiles' , 4303params ={ 4304changes ={ 4305{ 4306type =deleted , 4307uri =uri , 4308} , 4309} , 4310} , 4311} , messages [ 4 ]) 4312 4313eq ( root_change == 'created' and 3 or 2 , # result . notifications ) 4314for i , pattern in ipairs ( { 'a/**b' , '{foo}' } ) do 4315t . assert_log ( '%[ERROR%].-Invalid glob.-' .. pesc ( pattern ), result . logfile ) 4316local message , level =unpack ( result . notifications [ i ]) 4317eq ( vim . log . levels . ERROR , level ) 4318matches ( 'Invalid glob.-' .. pesc ( pattern ), message ) 4319matches ( pesc ( '(watching ' .. root_dir .. ')' ), message ) 4320end 4321if root_change == 'created' then 4322t . assert_log ( '%[INFO%].-ENOENT:.-' .. pesc ( root_dir ), result . logfile ) 4323eq ( vim . log . levels . INFO , result . notifications [ 3 ][ 2 ]) 4324matches ( 'file watcher failed: ENOENT:.-' .. pesc ( root_dir ), result . notifications [ 3 ][ 1 ]) 4325end 4326end 4327) 4328end 4329 4330test_filechanges ( 'watch' ) 4331test_filechanges ( 'watchdirs' ) 4332test_filechanges ( 'inotify' ) 4333test_filechanges ( 'watchdirs' , 'created' ) 4334test_filechanges ( 'watch' , 'replaced' ) 4335test_filechanges ( 'watchdirs' , 'replaced' ) 4336 4337for _ , stop_client in ipairs ( { false , true } ) do 4338it ( 4339'shares identical registrations until ' .. ( stop_client and 'client stop' or 'unregister' ), 4340function () 4341local root_dir =tmpname ( false ) 4342mkdir ( root_dir ) 4343t . finally ( function () 4344n . rmdir ( root_dir ) 4345end ) 4346local result =exec_lua ( function ( stop_client_ ) 4347local client , server , wait_for_messages =_G . start_watchfiles_client ( root_dir ) 4348 4349local watchers , send_event =_G . mock_watch () 4350-- Repeating an ID must neither restart the watcher nor add an owner. 4351for _ , id in ipairs ( { 'a' , 'a' , 'b' , 'c' , 'c' } ) do 4352client : _register ( { 4353{ 4354id =id , 4355method ='workspace/didChangeWatchedFiles' , 4356registerOptions ={ watchers ={ { globPattern ='**/*.py' } } } , 4357} , 4358} ) 4359end 4360assert ( # watchers == 1 , 'identical registrations must share one watcher' ) 4361 4362client : _unregister ( { { id ='a' , method ='workspace/didChangeWatchedFiles' } } ) 4363if not stop_client_ then 4364client : _unregister ( { { id ='b' , method ='workspace/didChangeWatchedFiles' } } ) 4365end 4366assert ( not watchers [ 1 ]. stopped , 'watcher cancelled while registrations remain' ) 4367send_event ( root_dir .. '/file.py' , vim . _watch . FileChangeType . Created ) 4368send_event ( root_dir .. '/file.txt' , vim . _watch . FileChangeType . Created ) 4369wait_for_messages ( 3 ) 4370if stop_client_ then 4371client : stop ( true ) 4372else 4373client : _unregister ( { { id ='c' , method ='workspace/didChangeWatchedFiles' } } ) 4374end 4375assert ( watchers [ 1 ]. stopped , 'watcher not cancelled after its last registration' ) 4376return server . messages [ 3 ] 4377end , stop_client ) 4378 4379eq ( { 4380method ='workspace/didChangeWatchedFiles' , 4381params ={ 4382changes ={ { uri =vim . uri_from_fname ( root_dir .. '/file.py' ), type =created } } , 4383} , 4384} , result ) 4385end 4386) 4387end 4388 4389it ( 'correctly registers and unregisters' , function () 4390local root_dir ='/some_dir' 4391local result =exec_lua ( function () 4392local client , server , wait_for_messages =_G . start_watchfiles_client ( root_dir ) 4393 4394local _ , send_event =_G . mock_watch () 4395 4396vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 4397registrations ={ 4398{ 4399id ='watchfiles-test-0' , 4400method ='workspace/didChangeWatchedFiles' , 4401registerOptions ={ 4402watchers ={ 4403{ 4404globPattern ='**/*.watch0' , 4405} , 4406} , 4407} , 4408} , 4409} , 4410} , { client_id =client . id } ) 4411 4412send_event ( root_dir .. '/file.watch0' , vim . _watch . FileChangeType . Created ) 4413send_event ( root_dir .. '/file.watch1' , vim . _watch . FileChangeType . Created ) 4414 4415wait_for_messages ( 3 ) 4416 4417vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 4418registrations ={ 4419{ 4420id ='watchfiles-test-1' , 4421method ='workspace/didChangeWatchedFiles' , 4422registerOptions ={ 4423watchers ={ 4424{ 4425globPattern ='**/*.watch1' , 4426} , 4427} , 4428} , 4429} , 4430} , 4431} , { client_id =client . id } ) 4432 4433vim . lsp . handlers [ 'client/unregisterCapability' ]( nil , { 4434unregisterations ={ 4435{ 4436id ='watchfiles-test-0' , 4437method ='workspace/didChangeWatchedFiles' , 4438} , 4439} , 4440} , { client_id =client . id } ) 4441 4442send_event ( root_dir .. '/file.watch0' , vim . _watch . FileChangeType . Created ) 4443send_event ( root_dir .. '/file.watch1' , vim . _watch . FileChangeType . Created ) 4444 4445wait_for_messages ( 4 ) 4446 4447return server . messages 4448end ) 4449 4450local function watched_uri ( fname ) 4451return vim . uri_from_fname ( root_dir .. '/' .. fname ) 4452end 4453 4454eq ( 4 , # result ) 4455eq ( 'workspace/didChangeWatchedFiles' , result [ 3 ]. method ) 4456eq ( { 4457changes ={ 4458{ 4459type =created , 4460uri =watched_uri ( 'file.watch0' ), 4461} , 4462} , 4463} , result [ 3 ]. params ) 4464eq ( 'workspace/didChangeWatchedFiles' , result [ 4 ]. method ) 4465eq ( { 4466changes ={ 4467{ 4468type =created , 4469uri =watched_uri ( 'file.watch1' ), 4470} , 4471} , 4472} , result [ 4 ]. params ) 4473end ) 4474 4475it ( 'correctly handles the registered watch kind with a literal baseUri' , function () 4476local root_dir ='some_dir' 4477local result =exec_lua ( function () 4478local client , server , wait_for_messages =_G . start_watchfiles_client ( root_dir ) 4479 4480local _ , send_event =_G . mock_watch () 4481 4482local protocol =require ( 'vim.lsp.protocol' ) 4483 4484local watchers ={} 4485local max_kind =protocol . WatchKind . Create 4486+ protocol . WatchKind . Change 4487+ protocol . WatchKind . Delete 4488for i =0 , max_kind do 4489table . insert ( watchers , { 4490globPattern ={ 4491baseUri =vim . uri_from_fname ( '/dir[1]' ), 4492pattern ='watch' .. tostring ( i ), 4493} , 4494kind =i , 4495} ) 4496end 4497vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 4498registrations ={ 4499{ 4500id ='watchfiles-test-kind' , 4501method ='workspace/didChangeWatchedFiles' , 4502registerOptions ={ 4503watchers =watchers , 4504} , 4505} , 4506} , 4507} , { client_id =client . id } ) 4508 4509for i =0 , max_kind do 4510send_event ( '/dir1/watch' .. tostring ( i ), vim . _watch . FileChangeType . Created ) 4511local filename ='/dir[1]/watch' .. tostring ( i ) 4512send_event ( filename , vim . _watch . FileChangeType . Created ) 4513send_event ( filename , vim . _watch . FileChangeType . Changed ) 4514send_event ( filename , vim . _watch . FileChangeType . Deleted ) 4515end 4516 4517wait_for_messages ( 3 ) 4518 4519return server . messages 4520end ) 4521 4522local function watched_uri ( fname ) 4523return vim . uri_from_fname ( '/dir[1]/' .. fname ) 4524end 4525 4526eq ( 3 , # result ) 4527eq ( 'workspace/didChangeWatchedFiles' , result [ 3 ]. method ) 4528eq ( { 4529changes ={ 4530{ 4531type =created , 4532uri =watched_uri ( 'watch1' ), 4533} , 4534{ 4535type =changed , 4536uri =watched_uri ( 'watch2' ), 4537} , 4538{ 4539type =created , 4540uri =watched_uri ( 'watch3' ), 4541} , 4542{ 4543type =changed , 4544uri =watched_uri ( 'watch3' ), 4545} , 4546{ 4547type =deleted , 4548uri =watched_uri ( 'watch4' ), 4549} , 4550{ 4551type =created , 4552uri =watched_uri ( 'watch5' ), 4553} , 4554{ 4555type =deleted , 4556uri =watched_uri ( 'watch5' ), 4557} , 4558{ 4559type =changed , 4560uri =watched_uri ( 'watch6' ), 4561} , 4562{ 4563type =deleted , 4564uri =watched_uri ( 'watch6' ), 4565} , 4566{ 4567type =created , 4568uri =watched_uri ( 'watch7' ), 4569} , 4570{ 4571type =changed , 4572uri =watched_uri ( 'watch7' ), 4573} , 4574{ 4575type =deleted , 4576uri =watched_uri ( 'watch7' ), 4577} , 4578} , 4579} , result [ 3 ]. params ) 4580end ) 4581 4582it ( 'prunes duplicate events' , function () 4583local root_dir ='some_dir' 4584local result =exec_lua ( function () 4585local client , server , wait_for_messages =_G . start_watchfiles_client ( root_dir ) 4586 4587local send_event --- @type function 4588require ( 'vim.lsp._watchfiles' ). _watchfunc =function ( _ , _ , callback ) 4589send_event =callback 4590return function () 4591-- noop because this test never stops the watch 4592end 4593end 4594 4595vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 4596registrations ={ 4597{ 4598id ='watchfiles-test-kind' , 4599method ='workspace/didChangeWatchedFiles' , 4600registerOptions ={ 4601watchers ={ 4602{ 4603globPattern ='**/*' , 4604} , 4605} , 4606} , 4607} , 4608} , 4609} , { client_id =client . id } ) 4610 4611send_event ( 'file1' , vim . _watch . FileChangeType . Created ) 4612send_event ( 'file1' , vim . _watch . FileChangeType . Created ) -- pruned 4613send_event ( 'file1' , vim . _watch . FileChangeType . Changed ) 4614send_event ( 'file2' , vim . _watch . FileChangeType . Created ) 4615send_event ( 'file1' , vim . _watch . FileChangeType . Changed ) -- pruned 4616 4617wait_for_messages ( 3 ) 4618 4619return server . messages 4620end ) 4621 4622eq ( 3 , # result ) 4623eq ( 'workspace/didChangeWatchedFiles' , result [ 3 ]. method ) 4624eq ( { 4625changes ={ 4626{ 4627type =created , 4628uri =vim . uri_from_fname ( 'file1' ), 4629} , 4630{ 4631type =changed , 4632uri =vim . uri_from_fname ( 'file1' ), 4633} , 4634{ 4635type =created , 4636uri =vim . uri_from_fname ( 'file2' ), 4637} , 4638} , 4639} , result [ 3 ]. params ) 4640end ) 4641 4642it ( "ignores registrations by servers when the client doesn't advertise support" , function () 4643exec_lua ( function () 4644_G . server =_G . _create_server () 4645require ( 'vim.lsp._watchfiles' ). _watchfunc =function ( _ , _ , _ ) 4646-- Since the registration is ignored, this should not execute and `watching` should stay false 4647_G . watching =true 4648return function () end 4649end 4650end ) 4651 4652local function check_registered ( capabilities ) 4653return exec_lua ( function () 4654_G . watching =false 4655local client_id =assert ( vim . lsp . start ( { 4656name ='watchfiles-test' , 4657cmd =_G . server . cmd , 4658root_dir ='some_dir' , 4659capabilities =capabilities , 4660} , { 4661reuse_client =function () 4662return false 4663end , 4664} )) 4665 4666vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 4667registrations ={ 4668{ 4669id ='watchfiles-test-kind' , 4670method ='workspace/didChangeWatchedFiles' , 4671registerOptions ={ 4672watchers ={ 4673{ 4674globPattern ='**/*' , 4675} , 4676} , 4677} , 4678} , 4679} , 4680} , { client_id =client_id } ) 4681 4682-- Ensure no errors occur when unregistering something that was never really registered. 4683vim . lsp . handlers [ 'client/unregisterCapability' ]( nil , { 4684unregisterations ={ 4685{ 4686id ='watchfiles-test-kind' , 4687method ='workspace/didChangeWatchedFiles' , 4688} , 4689} , 4690} , { client_id =client_id } ) 4691 4692vim . lsp . get_client_by_id ( client_id ): stop ( true ) 4693return _G . watching 4694end ) 4695end 4696 4697eq ( is_os ( 'mac' ) or is_os ( 'win' ), check_registered ( nil )) -- start{_client}() defaults to make_client_capabilities(). 4698eq ( 4699false , 4700check_registered ( { 4701workspace ={ 4702didChangeWatchedFiles ={ 4703dynamicRegistration =false , 4704} , 4705} , 4706} ) 4707) 4708eq ( 4709true , 4710check_registered ( { 4711workspace ={ 4712didChangeWatchedFiles ={ 4713dynamicRegistration =true , 4714} , 4715} , 4716} ) 4717) 4718end ) 4719end ) 4720 4721describe ( 'vim.lsp.config() and vim.lsp.enable()' , function () 4722---@param names string[] 4723local function get_resolved ( names ) 4724return exec_lua ( function ( names_ ) 4725local rv ={} 4726local cs =vim . lsp . _enabled_configs 4727for _ , k in ipairs ( names_ ) do 4728rv [ k ] =not not ( cs [ k ] and cs [ k ]. resolved_config ) 4729end 4730return rv 4731end , names ) 4732end 4733 4734it ( 'merges settings from "*"' , function () 4735eq ( 4736{ 4737name ='foo' , 4738cmd ={ 'foo' } , 4739root_markers ={ '.git' } , 4740} , 4741exec_lua ( function () 4742vim . lsp . config ( '*' , { root_markers ={ '.git' } } ) 4743vim . lsp . config ( 'foo' , { cmd ={ 'foo' } } ) 4744 4745return vim . lsp . config [ 'foo' ] 4746end ) 4747) 4748end ) 4749 4750it ( 'config("bogus") shows a hint' , function () 4751matches ( 4752'hint%: to resolve a config' , 4753pcall_err ( exec_lua , function () 4754vim . ( vim . lsp . config ( 'non-existent-config' )) 4755end ) 4756) 4757end ) 4758 4759it ( 'sets up an autocmd' , function () 4760eq ( 47611 , 4762exec_lua ( function () 4763vim . lsp . config ( 'foo' , { 4764cmd ={ 'foo' } , 4765root_markers ={ '.foorc' } , 4766} ) 4767vim . lsp . enable ( 'foo' ) 4768return # vim . api . nvim_get_autocmds ( { 4769group ='nvim.lsp.enable' , 4770event ='FileType' , 4771} ) 4772end ) 4773) 4774end ) 4775 4776it ( 'handle nil config (some clients may not have a config!)' , function () 4777exec_lua ( create_server_definition ) 4778exec_lua ( function () 4779local server =_G . _create_server () 4780vim . bo . filetype ='lua' 4781-- Attach a client without defining a config. 4782local client_id =vim . lsp . start ( { 4783name ='test_ls' , 4784cmd =function ( dispatchers , config ) 4785_G . test_resolved_root =config . root_dir --[[@type string]] 4786return server . cmd ( dispatchers , config ) 4787end , 4788} , { bufnr =0 } ) 4789 4790local bufnr =vim . api . nvim_get_current_buf () 4791local client =vim . lsp . get_client_by_id ( client_id ) 4792assert ( client . attached_buffers [ bufnr ]) 4793 4794-- Exercise the codepath which had a regression: 4795vim . lsp . enable ( 'test_ls' ) 4796vim . api . nvim_exec_autocmds ( 'FileType' , { buf =bufnr } ) 4797 4798-- enable() does _not_ detach the client since it doesn't actually have a config. 4799-- XXX: otoh, is it confusing to allow `enable("foo")` if there a "foo" _client_ without a "foo" _config_? 4800assert ( client . attached_buffers [ bufnr ]) 4801assert ( client_id == vim . lsp . get_client_by_id ( bufnr ). id ) 4802end ) 4803end ) 4804 4805it ( 'attaches to buffers when they are opened' , function () 4806exec_lua ( create_server_definition ) 4807 4808local tmp1 =t . tmpname ( true ) 4809local tmp2 =t . tmpname ( true ) 4810 4811exec_lua ( function () 4812local server =_G . _create_server ( { 4813handlers ={ 4814initialize =function ( _ , _ , callback ) 4815callback ( nil , { capabilities ={} } ) 4816end , 4817} , 4818} ) 4819 4820vim . lsp . config ( 'foo' , { 4821cmd =server . cmd , 4822filetypes ={ 'foo' } , 4823root_markers ={ '.foorc' } , 4824} ) 4825 4826vim . lsp . config ( 'bar' , { 4827cmd =server . cmd , 4828filetypes ={ 'bar' } , 4829root_markers ={ '.foorc' } , 4830} ) 4831 4832vim . lsp . enable ( 'foo' ) 4833vim . lsp . enable ( 'bar' ) 4834 4835vim . cmd . edit ( tmp1 ) 4836vim . bo . filetype ='foo' 4837_G . foo_buf =vim . api . nvim_get_current_buf () 4838 4839vim . cmd . edit ( tmp2 ) 4840vim . bo . filetype ='bar' 4841_G . bar_buf =vim . api . nvim_get_current_buf () 4842end ) 4843 4844eq ( 4845{ 1 , 'foo' , 1 , 'bar' } , 4846exec_lua ( function () 4847local foos =vim . lsp . get_clients ( { bufnr =assert ( _G . foo_buf ) } ) 4848local bars =vim . lsp . get_clients ( { bufnr =assert ( _G . bar_buf ) } ) 4849return { # foos , foos [ 1 ]. name , # bars , bars [ 1 ]. name } 4850end ) 4851) 4852end ) 4853 4854it ( 'attaches/detaches preexisting buffers' , function () 4855exec_lua ( create_server_definition ) 4856 4857local tmp1 =t . tmpname ( true ) 4858local tmp2 =t . tmpname ( true ) 4859 4860exec_lua ( function () 4861vim . cmd . edit ( tmp1 ) 4862vim . bo . filetype ='foo' 4863_G . foo_buf =vim . api . nvim_get_current_buf () 4864 4865vim . cmd . edit ( tmp2 ) 4866vim . bo . filetype ='bar' 4867_G . bar_buf =vim . api . nvim_get_current_buf () 4868 4869local server =_G . _create_server ( { 4870handlers ={ 4871initialize =function ( _ , _ , callback ) 4872callback ( nil , { capabilities ={} } ) 4873end , 4874} , 4875} ) 4876 4877vim . lsp . config ( 'foo' , { 4878cmd =server . cmd , 4879filetypes ={ 'foo' } , 4880root_markers ={ '.foorc' } , 4881} ) 4882 4883vim . lsp . config ( 'bar' , { 4884cmd =server . cmd , 4885filetypes ={ 'bar' } , 4886root_markers ={ '.foorc' } , 4887} ) 4888 4889vim . lsp . enable ( 'foo' ) 4890vim . lsp . enable ( 'bar' ) 4891end ) 4892 4893eq ( 4894{ 1 , 'foo' , 1 , 'bar' } , 4895exec_lua ( function () 4896local foos =vim . lsp . get_clients ( { bufnr =assert ( _G . foo_buf ) } ) 4897local bars =vim . lsp . get_clients ( { bufnr =assert ( _G . bar_buf ) } ) 4898return { # foos , foos [ 1 ]. name , # bars , bars [ 1 ]. name } 4899end ) 4900) 4901 4902-- Now disable the 'foo' lsp and confirm that it's detached from the buffer it was previous 4903-- attached to. 4904exec_lua ( [[vim.lsp.enable('foo', false)]] ) 4905eq ( 4906{ 0 , 'foo' , 1 , 'bar' } , 4907exec_lua ( function () 4908local foos =vim . lsp . get_clients ( { bufnr =assert ( _G . foo_buf ) } ) 4909local bars =vim . lsp . get_clients ( { bufnr =assert ( _G . bar_buf ) } ) 4910return { # foos , 'foo' , # bars , bars [ 1 ]. name } 4911end ) 4912) 4913end ) 4914 4915it ( 'in first FileType event (on startup)' , function () 4916local tmp =tmpname () 4917write_file ( tmp , string . dump ( create_server_definition )) 4918n . clear ( { 4919args ={ 4920'--cmd' , 4921string . format ( [[lua assert(loadfile(%q))()]] , tmp ), 4922'--cmd' , 4923[[lua _G.server = _G._create_server({ handlers = {initialize = function(_, _, callback) callback(nil, {capabilities = {}}) end} })]] , 4924'--cmd' , 4925[[lua vim.lsp.config('foo', { cmd = _G.server.cmd, filetypes = { 'foo' }, root_markers = { '.foorc' } })]] , 4926'--cmd' , 4927[[au FileType * ++once lua vim.lsp.enable('foo')]] , 4928'-c' , 4929'set ft=foo' , 4930} , 4931} ) 4932 4933eq ( 4934{ 1 , 'foo' } , 4935exec_lua ( function () 4936local foos =vim . lsp . get_clients ( { bufnr =0 } ) 4937return { # foos , ( foos [ 1 ] or {} ). name } 4938end ) 4939) 4940exec_lua ( [[vim.lsp.enable('foo', false)]] ) 4941eq ( 49420 , 4943exec_lua ( function () 4944return # vim . lsp . get_clients ( { bufnr =0 } ) 4945end ) 4946) 4947end ) 4948 4949it ( 'does not attach to buffers more than once if no root_dir' , function () 4950exec_lua ( create_server_definition ) 4951 4952local tmp1 =t . tmpname ( true ) 4953 4954eq ( 49551 , 4956exec_lua ( function () 4957local server =_G . _create_server ( { 4958handlers ={ 4959initialize =function ( _ , _ , callback ) 4960callback ( nil , { capabilities ={} } ) 4961end , 4962} , 4963} ) 4964 4965vim . lsp . config ( 'foo' , { cmd =server . cmd , filetypes ={ 'foo' } } ) 4966vim . lsp . enable ( 'foo' ) 4967 4968vim . cmd . edit ( assert ( tmp1 )) 4969vim . bo . filetype ='foo' 4970vim . bo . filetype ='foo' 4971 4972return # vim . lsp . get_clients ( { bufnr =vim . api . nvim_get_current_buf () } ) 4973end ) 4974) 4975end ) 4976 4977it ( 'async root_dir, cmd(…,config) gets resolved config' , function () 4978exec_lua ( create_server_definition ) 4979 4980local tmp1 =t . tmpname ( true ) 4981exec_lua ( function () 4982local server =_G . _create_server ( { 4983handlers ={ 4984initialize =function ( _ , _ , callback ) 4985callback ( nil , { capabilities ={} } ) 4986end , 4987} , 4988} ) 4989 4990vim . lsp . config ( 'foo' , { 4991cmd =function ( dispatchers , config ) 4992_G . test_resolved_root =config . root_dir --[[@type string]] 4993return server . cmd ( dispatchers , config ) 4994end , 4995filetypes ={ 'foo' } , 4996root_dir =function ( bufnr , cb ) 4997assert ( tmp1 == vim . api . nvim_buf_get_name ( bufnr )) 4998vim . system ( { 'sleep' , '0' } , {} , function () 4999cb ( 'some_dir' ) 5000end ) 5001end , 5002} ) 5003vim . lsp . enable ( 'foo' ) 5004 5005vim . cmd . edit ( assert ( tmp1 )) 5006vim . bo . filetype ='foo' 5007end ) 5008 5009retry ( nil , 1000 , function () 5010eq ( 5011'some_dir' , 5012exec_lua ( function () 5013return vim . lsp . get_clients ( { bufnr =vim . api . nvim_get_current_buf () } )[ 1 ]. root_dir 5014end ) 5015) 5016end ) 5017eq ( 5018'some_dir' , 5019exec_lua ( function () 5020return _G . test_resolved_root 5021end ) 5022) 5023end ) 5024 5025it ( 'starts correct LSP and stops incorrect LSP when filetype changes' , function () 5026exec_lua ( create_server_definition ) 5027 5028local tmp1 =t . tmpname ( true ) 5029 5030exec_lua ( function () 5031local server =_G . _create_server ( { 5032handlers ={ 5033initialize =function ( _ , _ , callback ) 5034callback ( nil , { capabilities ={} } ) 5035end , 5036} , 5037} ) 5038 5039vim . lsp . config ( 'foo' , { 5040cmd =server . cmd , 5041filetypes ={ 'foo' } , 5042root_markers ={ '.foorc' } , 5043} ) 5044 5045vim . lsp . config ( 'bar' , { 5046cmd =server . cmd , 5047filetypes ={ 'bar' } , 5048root_markers ={ '.foorc' } , 5049} ) 5050 5051vim . lsp . enable ( 'foo' ) 5052vim . lsp . enable ( 'bar' ) 5053 5054vim . cmd . edit ( tmp1 ) 5055end ) 5056 5057local count_clients =function () 5058return exec_lua ( function () 5059local foos =vim . lsp . get_clients ( { name ='foo' , bufnr =0 } ) 5060local bars =vim . lsp . get_clients ( { name ='bar' , bufnr =0 } ) 5061return { # foos , 'foo' , # bars , 'bar' } 5062end ) 5063end 5064 5065-- No filetype on the buffer yet, so no LSPs. 5066eq ( { 0 , 'foo' , 0 , 'bar' } , count_clients ()) 5067 5068-- Set the filetype to 'foo', confirm a LSP starts. 5069exec_lua ( [[vim.bo.filetype = 'foo']] ) 5070eq ( { 1 , 'foo' , 0 , 'bar' } , count_clients ()) 5071 5072-- Set the filetype to 'bar', confirm a new LSP starts, and the old one goes away. 5073exec_lua ( [[vim.bo.filetype = 'bar']] ) 5074eq ( { 0 , 'foo' , 1 , 'bar' } , count_clients ()) 5075end ) 5076 5077it ( 'sends didClose and didOpen when languageId changes' , function () 5078exec_lua ( create_server_definition ) 5079 5080local tmp1 =t . tmpname ( true ) 5081 5082exec_lua ( function () 5083_G . server =_G . _create_server ( { 5084handlers ={ 5085initialize =function ( _ , _ , callback ) 5086callback ( nil , { capabilities ={ textDocumentSync ={ openClose =true } } } ) 5087end , 5088} , 5089} ) 5090vim . lsp . config ( 'foo' , { cmd =_G . server . cmd , filetypes ={ 'foo' , 'bar' } } ) 5091vim . lsp . enable ( 'foo' ) 5092vim . cmd . edit ( tmp1 ) 5093end ) 5094 5095local function test_messages () 5096local opens =0 5097local closes =0 5098local msgs =exec_lua ( [[ return _G.server.messages ]] ) 5099local num_clients =exec_lua ( [[ return #vim.lsp.get_clients() ]] ) 5100for _ , msg in ipairs ( msgs ) do 5101opens =opens + ( msg . method == 'textDocument/didOpen' and 1 or 0 ) 5102closes =closes + ( msg . method == 'textDocument/didClose' and 1 or 0 ) 5103end 5104return { opens , 'did_open' , closes , 'did_close' , num_clients , 'clients' } 5105end 5106 5107-- No filetype on the buffer yet. 5108eq ( { 0 , 'did_open' , 0 , 'did_close' , 0 , 'clients' } , test_messages ()) 5109 5110-- Set the filetype to 'foo', confirm didOpen is sent. 5111exec_lua ( [[vim.bo.filetype = 'foo']] ) 5112retry ( nil , 1000 , function () 5113eq ( { 1 , 'did_open' , 0 , 'did_close' , 1 , 'clients' } , test_messages ()) 5114end ) 5115 5116-- Set to anohter lsp-supported filetype 'bar', confirm didClose and didOpen are sent. 5117exec_lua ( [[vim.bo.filetype = 'bar']] ) 5118retry ( nil , 1000 , function () 5119eq ( { 2 , 'did_open' , 1 , 'did_close' , 1 , 'clients' } , test_messages ()) 5120end ) 5121end ) 5122 5123it ( 'validates config on attach' , function () 5124local tmp1 =t . tmpname ( true ) 5125local logfile =exec_lua ( function () 5126return vim . lsp . log . get_filename () 5127end ) 5128 5129exec_lua ( function () 5130vim . fn . writefile ( { '' } , vim . lsp . log . get_filename ()) 5131end ) 5132 5133local function test_cfg ( cfg , err ) 5134exec_lua ( function () 5135vim . lsp . config [ 'foo' ] ={} 5136vim . lsp . config ( 'foo' , cfg ) 5137vim . lsp . enable ( 'foo' ) 5138vim . cmd . edit ( assert ( tmp1 )) 5139vim . bo . filetype ='non.applicable.filetype' 5140end ) 5141 5142-- Assert NO log for non-applicable 'filetype'. #35737 5143if type ( cfg . filetypes ) == 'table' then 5144t . assert_nolog ( err , logfile ) 5145end 5146 5147exec_lua ( function () 5148vim . bo . filetype ='foo' 5149end ) 5150 5151retry ( nil , 1000 , function () 5152t . assert_log ( err , logfile ) 5153end ) 5154end 5155 5156test_cfg ( { 5157filetypes ={ 'foo' } , 5158cmd ={ 'lolling' } , 5159} , 'invalid "foo" config: .* lolling is not executable' ) 5160 5161test_cfg ( { 5162cmd ={ 'cat' } , 5163filetypes =true , 5164} , 'invalid "foo" config: .* filetypes: expected table, got boolean' ) 5165end ) 5166 5167it ( 'does not start without workspace if workspace_required=true' , function () 5168exec_lua ( create_server_definition ) 5169 5170local tmp1 =t . tmpname ( true ) 5171 5172eq ( 5173{ workspace_required =false } , 5174exec_lua ( function () 5175local server =_G . _create_server ( { 5176handlers ={ 5177initialize =function ( _ , _ , callback ) 5178callback ( nil , { capabilities ={} } ) 5179end , 5180} , 5181} ) 5182 5183local ws_required ={ cmd =server . cmd , workspace_required =true , filetypes ={ 'foo' } } 5184local ws_not_required =vim . deepcopy ( ws_required ) 5185ws_not_required . workspace_required =false 5186 5187vim . lsp . config ( 'ws_required' , ws_required ) 5188vim . lsp . config ( 'ws_not_required' , ws_not_required ) 5189vim . lsp . enable ( 'ws_required' ) 5190vim . lsp . enable ( 'ws_not_required' ) 5191 5192vim . cmd . edit ( assert ( tmp1 )) 5193vim . bo . filetype ='foo' 5194 5195local clients =vim . lsp . get_clients ( { bufnr =vim . api . nvim_get_current_buf () } ) 5196assert ( 1 == # clients ) 5197return { workspace_required =clients [ 1 ]. config . workspace_required } 5198end ) 5199) 5200end ) 5201 5202it ( 'does not allow wildcards in config name' , function () 5203local err = 5204'.../lsp.lua:0: name: expected non%-wildcard string, got foo%*%. Info: LSP config name cannot contain wildcard %("%*"%)' 5205 5206matches ( 5207err , 5208pcall_err ( exec_lua , function () 5209local _ =vim . lsp . config [ 'foo*' ] 5210end ) 5211) 5212matches ( 5213err , 5214pcall_err ( exec_lua , function () 5215vim . lsp . config [ 'foo*' ] ={} 5216end ) 5217) 5218matches ( 5219err , 5220pcall_err ( exec_lua , function () 5221vim . lsp . config ( 'foo*' , {} ) 5222end ) 5223) 5224-- Exception for '*' 5225pcall ( exec_lua , function () 5226vim . lsp . config ( '*' , {} ) 5227end ) 5228end ) 5229 5230it ( 'root_markers priority' , function () 5231--- Setup directories for testing 5232-- root/ 5233-- ├── dir_a/ 5234-- │ ├── dir_b/ 5235-- │ │ ├── target 5236-- │ │ └── marker_d 5237-- │ ├── marker_b 5238-- │ └── marker_c 5239-- └── marker_a 5240 5241---@param filepath string 5242local function touch ( filepath ) 5243local file =io . open ( filepath , 'w' ) 5244if file then 5245file : close () 5246end 5247end 5248 5249local tmp_root =tmpname ( false ) 5250local marker_a =tmp_root .. '/marker_a' 5251local dir_a =tmp_root .. '/dir_a' 5252local marker_b =dir_a .. '/marker_b' 5253local marker_c =dir_a .. '/marker_c' 5254local dir_b =dir_a .. '/dir_b' 5255local marker_d =dir_b .. '/marker_d' 5256local target =dir_b .. '/target' 5257 5258mkdir ( tmp_root ) 5259touch ( marker_a ) 5260mkdir ( dir_a ) 5261touch ( marker_b ) 5262touch ( marker_c ) 5263mkdir ( dir_b ) 5264touch ( marker_d ) 5265touch ( target ) 5266 5267exec_lua ( create_server_definition ) 5268exec_lua ( function () 5269_G . _custom_server =_G . _create_server () 5270end ) 5271 5272---@param root_markers (string|string[])[] 5273---@param expected_root_dir string? 5274local function markers_resolve_to ( root_markers , expected_root_dir ) 5275exec_lua ( function () 5276vim . lsp . config [ 'foo' ] ={} 5277vim . lsp . config ( 'foo' , { 5278cmd =_G . _custom_server . cmd , 5279reuse_client =function () 5280return false 5281end , 5282filetypes ={ 'foo' } , 5283root_markers =root_markers , 5284} ) 5285vim . lsp . enable ( 'foo' ) 5286vim . cmd . edit ( target ) 5287vim . bo . filetype ='foo' 5288end ) 5289retry ( nil , 1000 , function () 5290eq ( 5291expected_root_dir , 5292exec_lua ( function () 5293local clients =vim . lsp . get_clients () 5294return clients [ # clients ]. root_dir 5295end ) 5296) 5297end ) 5298end 5299 5300markers_resolve_to ( { 'marker_d' } , dir_b ) 5301markers_resolve_to ( { 'marker_b' } , dir_a ) 5302markers_resolve_to ( { 'marker_c' } , dir_a ) 5303markers_resolve_to ( { 'marker_a' } , tmp_root ) 5304markers_resolve_to ( { 'foo' } , nil ) 5305markers_resolve_to ( { { 'marker_b' , 'marker_a' } , 'marker_d' } , dir_a ) 5306markers_resolve_to ( { 'marker_a' , { 'marker_b' , 'marker_d' } } , tmp_root ) 5307markers_resolve_to ( { 'foo' , { 'bar' , 'baz' } , 'marker_d' } , dir_b ) 5308end ) 5309 5310it ( 'vim.lsp.is_enabled()' , function () 5311exec_lua ( function () 5312vim . lsp . config ( 'foo' , { 5313cmd ={ 'foo' } , 5314root_markers ={ '.foorc' } , 5315} ) 5316end ) 5317 5318-- LSP config defaults to disabled. 5319eq ( false , exec_lua ( [[return vim.lsp.is_enabled('foo')]] )) 5320 5321-- Confirm we can enable it. 5322exec_lua ( [[vim.lsp.enable('foo')]] ) 5323eq ( true , exec_lua ( [[return vim.lsp.is_enabled('foo')]] )) 5324 5325-- And finally, disable it again. 5326exec_lua ( [[vim.lsp.enable('foo', false)]] ) 5327eq ( false , exec_lua ( [[return vim.lsp.is_enabled('foo')]] )) 5328end ) 5329 5330it ( 'vim.lsp.get_configs()' , function () 5331exec_lua ( function () 5332vim . lsp . config ( 'foo' , { 5333cmd ={ 'foo' } , 5334filetypes ={ 'foofile' } , 5335root_markers ={ '.foorc' } , 5336} ) 5337vim . lsp . config ( 'bar' , { 5338cmd ={ 'bar' } , 5339root_markers ={ '.barrc' } , 5340} ) 5341vim . lsp . enable ( 'foo' ) 5342end ) 5343 5344local function names ( configs ) 5345local config_names =vim 5346. iter ( configs ) 5347: map ( function ( config ) 5348return config . name 5349end ) 5350: totable () 5351table . sort ( config_names ) 5352return config_names 5353end 5354 5355eq ( { 'foo' } , names ( exec_lua ( [[return vim.lsp.get_configs { enabled = true }]] ))) 5356-- Does NOT resolve non-enabled configs. 5357eq ( { foo =true , bar =false } , get_resolved ( { 'bar' , 'foo' } )) 5358 5359eq ( { 'bar' } , names ( exec_lua ( [[return vim.lsp.get_configs { enabled = false }]] ))) 5360 5361-- With no filter, return all configs 5362eq ( { 'bar' , 'foo' } , names ( exec_lua ( [[return vim.lsp.get_configs()]] ))) 5363 5364-- Confirm `filetype` works 5365eq ( { 'foo' } , names ( exec_lua ( [[return vim.lsp.get_configs { filetype = 'foofile' }]] ))) 5366 5367-- Confirm filters combine 5368eq ( 5369{ 'foo' } , 5370names ( exec_lua ( [[return vim.lsp.get_configs { filetype = 'foofile', enabled = true }]] )) 5371) 5372eq ( 5373{} , 5374names ( exec_lua ( [[return vim.lsp.get_configs { filetype = 'foofile', enabled = false }]] )) 5375) 5376-- Does NOT resolve non-enabled configs. 5377eq ( { foo =true , bar =false } , get_resolved ( { 'bar' , 'foo' } )) 5378end ) 5379end ) 5380end )