robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
dec3c6fa34
master
1local protocol =require 'vim.lsp.protocol' 2 3local pid =vim . uv . os_getpid () 4local stdin ='' 5vim . fn . stdioopen ( { 6on_stdin =function ( _ , data , _ ) 7stdin =stdin .. table . concat ( data , '\n' ) 8end , 9} ) 10 11-- Logs to $NVIM_LOG_FILE. 12-- 13-- TODO(justinmk): remove after https://github.com/neovim/neovim/pull/7062 14local function log ( loglevel , area , msg ) 15vim . fn . writefile ( 16{ string . format ( '%d %s %s: %s' , pid , loglevel , area , msg ) } , 17vim . env . NVIM_LOG_FILE , 18'a' 19) 20end 21 22local function message_parts ( sep , ... ) 23local parts ={} 24for i =1 , select ( '#' , ... ) do 25local arg =select ( i , ... ) 26if arg ~= nil then 27table . insert ( parts , arg ) 28end 29end 30return table . concat ( parts , sep ) 31end 32 33-- Assert utility methods 34 35local function assert_eq ( a , b , ... ) 36if not vim . deep_equal ( a , b ) then 37error ( 38message_parts ( 39': ' , 40... , 41'assert_eq failed' , 42string . format ( 43'left == %q, right == %q' , 44table . concat ( vim . split ( vim . inspect ( a ), '\n' ), '' ), 45table . concat ( vim . split ( vim . inspect ( b ), '\n' ), '' ) 46) 47) 48) 49end 50end 51 52local function format_message_with_content_length ( encoded_message ) 53return table . concat { 54'Content-Length: ' , 55tostring ( # encoded_message ), 56'\r\n\r\n' , 57encoded_message , 58} 59end 60 61local function read_line () 62vim . wait ( math . huge , function () 63return stdin : find ( '\n' ) ~= nil 64end , 1 ) 65local eol =assert ( stdin : find ( '\n' )) 66local line =stdin : sub ( 1 , eol - 1 ) 67stdin =stdin : sub ( eol + 1 ) 68return line 69end 70 71--- @param len integer 72local function read_len ( len ) 73vim . wait ( math . huge , function () 74return stdin : len () >= len 75end , 1 ) 76local content =stdin : sub ( 1 , len ) 77stdin =stdin : sub ( len + 1 ) 78return content 79end 80 81local function read_message () 82local line =read_line () 83local length =line : lower (): match ( 'content%-length:%s*(%d+)' ) 84return vim . json . decode ( read_len ( 2 + length ): sub ( 2 )) 85end 86 87local function send ( payload ) 88io . stdout : write ( format_message_with_content_length ( vim . json . encode ( payload ))) 89end 90 91local function respond ( id , err , result ) 92assert ( type ( id ) == 'number' , 'id must be a number' ) 93send { jsonrpc ='2.0' , id =id , error =err , result =result } 94end 95 96local function notify ( method , params ) 97assert ( type ( method ) == 'string' , 'method must be a string' ) 98send { method =method , params =params or {} } 99end 100 101local function expect_notification ( method , params , ... ) 102local message =read_message () 103assert_eq ( method , message . method , ... , 'expect_notification' , 'method' ) 104if params then 105assert_eq ( params , message . params , ... , 'expect_notification' , method , 'params' ) 106assert_eq ( 107{ jsonrpc ='2.0' , method =method , params =params } , 108message , 109... , 110'expect_notification' , 111'message' 112) 113end 114end 115 116local function expect_request ( method , handler , ... ) 117local req =read_message () 118assert_eq ( method , req . method , ... , 'expect_request' , 'method' ) 119local err , result =handler ( req . params ) 120respond ( req . id , err , result ) 121end 122 123io . stderr : setvbuf ( 'no' ) 124 125local function skeleton ( config ) 126local on_init =assert ( config . on_init ) 127local body =assert ( config . body ) 128expect_request ( 'initialize' , function ( params ) 129return nil , on_init ( params ) 130end ) 131expect_notification ( 'initialized' , {} ) 132body () 133expect_request ( 'shutdown' , function () 134return nil , {} 135end ) 136expect_notification ( 'exit' , nil ) 137end 138 139-- The actual tests. 140 141local tests ={} 142 143function tests . basic_init () 144skeleton { 145on_init =function ( params ) 146assert_eq ( params . workDoneToken , '1' ) 147return { 148capabilities ={ 149textDocumentSync =protocol . TextDocumentSyncKind . None , 150} , 151} 152end , 153body =function () 154notify ( 'test' ) 155end , 156} 157end 158 159function tests . basic_init_did_change_configuration () 160skeleton ( { 161on_init =function ( _ ) 162return { 163capabilities ={} , 164} 165end , 166body =function () 167expect_notification ( 'workspace/didChangeConfiguration' , { settings ={ dummy =1 } } ) 168end , 169} ) 170end 171 172function tests . check_workspace_configuration () 173skeleton { 174on_init =function ( _params ) 175return { capabilities ={} } 176end , 177body =function () 178notify ( 'start' ) 179notify ( 'workspace/configuration' , { 180items ={ 181{ section ='testSetting1' } , 182{ section ='testSetting2' } , 183{ section ='test.Setting3' } , 184{ section ='test.Setting4' } , 185{} , 186{ section ='' } , 187} , 188} ) 189local all ={ 190testSetting1 =true , 191testSetting2 =false , 192test ={ Setting3 ='nested' } , 193} 194expect_notification ( 'workspace/configuration' , { true , false , 'nested' , vim . NIL , all , all } ) 195notify ( 'shutdown' ) 196end , 197} 198end 199 200function tests . prepare_rename_nil () 201skeleton { 202on_init =function () 203return { 204capabilities ={ 205renameProvider ={ 206prepareProvider =true , 207} , 208} , 209} 210end , 211body =function () 212notify ( 'start' ) 213expect_request ( 'textDocument/prepareRename' , function () 214return {} , nil 215end ) 216notify ( 'shutdown' ) 217end , 218} 219end 220 221function tests . prepare_rename_placeholder () 222skeleton { 223on_init =function () 224return { 225capabilities ={ 226renameProvider ={ 227prepareProvider =true , 228} , 229} , 230} 231end , 232body =function () 233notify ( 'start' ) 234expect_request ( 'textDocument/prepareRename' , function () 235return nil , { placeholder ='placeholder' } 236end ) 237expect_request ( 'textDocument/rename' , function ( params ) 238assert_eq ( params . newName , 'renameto' ) 239return {} , nil 240end ) 241notify ( 'shutdown' ) 242end , 243} 244end 245 246function tests . prepare_rename_range () 247skeleton { 248on_init =function () 249return { 250capabilities ={ 251renameProvider ={ 252prepareProvider =true , 253} , 254} , 255} 256end , 257body =function () 258notify ( 'start' ) 259expect_request ( 'textDocument/prepareRename' , function () 260return nil , 261{ 262start ={ line =1 , character =8 } , 263[ 'end' ] ={ line =1 , character =12 } , 264} 265end ) 266expect_request ( 'textDocument/rename' , function ( params ) 267assert_eq ( params . newName , 'renameto' ) 268return {} , nil 269end ) 270notify ( 'shutdown' ) 271end , 272} 273end 274 275function tests . prepare_rename_error () 276skeleton { 277on_init =function () 278return { 279capabilities ={ 280renameProvider ={ 281prepareProvider =true , 282} , 283} , 284} 285end , 286body =function () 287notify ( 'start' ) 288expect_request ( 'textDocument/prepareRename' , function () 289return {} , nil 290end ) 291notify ( 'shutdown' ) 292end , 293} 294end 295 296function tests . basic_check_capabilities () 297skeleton { 298on_init =function ( params ) 299local expected_capabilities =protocol . make_client_capabilities () 300assert_eq ( params . capabilities , expected_capabilities ) 301return { 302capabilities ={ 303textDocumentSync =protocol . TextDocumentSyncKind . Full , 304codeLensProvider =false , 305} , 306} 307end , 308body =function () end , 309} 310end 311 312function tests . text_document_save_did_open () 313skeleton { 314on_init =function () 315return { 316capabilities ={ 317textDocumentSync ={ 318save =true , 319} , 320} , 321} 322end , 323body =function () 324notify ( 'start' ) 325expect_notification ( 'textDocument/didClose' ) 326expect_notification ( 'textDocument/didOpen' ) 327expect_notification ( 'textDocument/didSave' ) 328notify ( 'shutdown' ) 329end , 330} 331end 332 333function tests . text_document_sync_save_bool () 334skeleton { 335on_init =function () 336return { 337capabilities ={ 338textDocumentSync ={ 339save =true , 340} , 341} , 342} 343end , 344body =function () 345notify ( 'start' ) 346expect_notification ( 'textDocument/didSave' , { textDocument ={ uri ='file://' } } ) 347notify ( 'shutdown' ) 348end , 349} 350end 351 352function tests . text_document_sync_save_includeText () 353skeleton { 354on_init =function () 355return { 356capabilities ={ 357textDocumentSync ={ 358save ={ 359includeText =true , 360} , 361} , 362} , 363} 364end , 365body =function () 366notify ( 'start' ) 367expect_notification ( 'textDocument/didSave' , { 368textDocument ={ 369uri ='file://' , 370} , 371text ='help me\n' , 372} ) 373notify ( 'shutdown' ) 374end , 375} 376end 377 378function tests . capabilities_for_client_supports_method () 379skeleton { 380on_init =function ( params ) 381local expected_capabilities =protocol . make_client_capabilities () 382assert_eq ( params . capabilities , expected_capabilities ) 383return { 384capabilities ={ 385textDocumentSync =protocol . TextDocumentSyncKind . Full , 386completionProvider =true , 387hoverProvider =true , 388renameProvider =false , 389definitionProvider =false , 390referencesProvider =false , 391codeLensProvider ={ resolveProvider =true } , 392} , 393} 394end , 395body =function () end , 396} 397end 398 399function tests . check_forward_request_cancelled () 400skeleton { 401on_init =function ( _ ) 402return { capabilities ={} } 403end , 404body =function () 405expect_request ( 'error_code_test' , function () 406return { code =- 32800 } , nil , { method ='error_code_test' , client_id =1 } 407end ) 408notify ( 'finish' ) 409end , 410} 411end 412 413function tests . check_forward_content_modified () 414skeleton { 415on_init =function ( _ ) 416return { capabilities ={} } 417end , 418body =function () 419expect_request ( 'error_code_test' , function () 420return { code =- 32801 } , nil , { method ='error_code_test' , client_id =1 } 421end ) 422expect_notification ( 'finish' ) 423notify ( 'finish' ) 424end , 425} 426end 427 428function tests . check_forward_server_cancelled () 429skeleton { 430on_init =function () 431return { capabilities ={} } 432end , 433body =function () 434expect_request ( 'error_code_test' , function () 435return { code =- 32802 } , nil , { method ='error_code_test' , client_id =1 } 436end ) 437expect_notification ( 'finish' ) 438notify ( 'finish' ) 439end , 440} 441end 442 443function tests . check_pending_request_tracked () 444skeleton { 445on_init =function ( _ ) 446return { capabilities ={} } 447end , 448body =function () 449local msg =read_message () 450assert_eq ( 'slow_request' , msg . method ) 451expect_notification ( 'release' ) 452respond ( msg . id , nil , {} ) 453expect_notification ( 'finish' ) 454notify ( 'finish' ) 455end , 456} 457end 458 459function tests . check_cancel_request_tracked () 460skeleton { 461on_init =function ( _ ) 462return { capabilities ={} } 463end , 464body =function () 465local msg =read_message () 466assert_eq ( 'slow_request' , msg . method ) 467expect_notification ( '$/cancelRequest' , { id =msg . id } ) 468expect_notification ( 'release' ) 469respond ( msg . id , { code =- 32800 } , nil ) 470notify ( 'finish' ) 471end , 472} 473end 474 475function tests . check_tracked_requests_cleared () 476skeleton { 477on_init =function ( _ ) 478return { capabilities ={} } 479end , 480body =function () 481local msg =read_message () 482assert_eq ( 'slow_request' , msg . method ) 483expect_notification ( '$/cancelRequest' , { id =msg . id } ) 484expect_notification ( 'release' ) 485respond ( msg . id , nil , {} ) 486expect_notification ( 'finish' ) 487notify ( 'finish' ) 488end , 489} 490end 491 492function tests . basic_finish () 493skeleton { 494on_init =function ( params ) 495local expected_capabilities =protocol . make_client_capabilities () 496assert_eq ( params . capabilities , expected_capabilities ) 497return { 498capabilities ={ 499textDocumentSync =protocol . TextDocumentSyncKind . Full , 500} , 501} 502end , 503body =function () 504expect_notification ( 'finish' ) 505notify ( 'finish' ) 506end , 507} 508end 509 510function tests . basic_check_buffer_open () 511skeleton { 512on_init =function ( params ) 513local expected_capabilities =protocol . make_client_capabilities () 514assert_eq ( params . capabilities , expected_capabilities ) 515return { 516capabilities ={ 517textDocumentSync =protocol . TextDocumentSyncKind . Full , 518} , 519} 520end , 521body =function () 522notify ( 'start' ) 523expect_notification ( 'textDocument/didOpen' , { 524textDocument ={ 525languageId ='' , 526text =table . concat ( { 'testing' , '123' } , '\n' ) .. '\n' , 527uri ='file://' , 528version =0 , 529} , 530} ) 531expect_notification ( 'finish' ) 532notify ( 'finish' ) 533end , 534} 535end 536 537function tests . basic_check_buffer_open_and_change () 538skeleton { 539on_init =function ( params ) 540local expected_capabilities =protocol . make_client_capabilities () 541assert_eq ( params . capabilities , expected_capabilities ) 542return { 543capabilities ={ 544textDocumentSync =protocol . TextDocumentSyncKind . Full , 545} , 546} 547end , 548body =function () 549notify ( 'start' ) 550expect_notification ( 'textDocument/didOpen' , { 551textDocument ={ 552languageId ='' , 553text =table . concat ( { 'testing' , '123' } , '\n' ) .. '\n' , 554uri ='file://' , 555version =0 , 556} , 557} ) 558expect_notification ( 'textDocument/didChange' , { 559textDocument ={ 560uri ='file://' , 561version =3 , 562} , 563contentChanges ={ 564{ text =table . concat ( { 'testing' , 'boop' } , '\n' ) .. '\n' } , 565} , 566} ) 567expect_notification ( 'finish' ) 568notify ( 'finish' ) 569end , 570} 571end 572 573function tests . basic_check_buffer_open_and_change_noeol () 574skeleton { 575on_init =function ( params ) 576local expected_capabilities =protocol . make_client_capabilities () 577assert_eq ( params . capabilities , expected_capabilities ) 578return { 579capabilities ={ 580textDocumentSync =protocol . TextDocumentSyncKind . Full , 581} , 582} 583end , 584body =function () 585notify ( 'start' ) 586expect_notification ( 'textDocument/didOpen' , { 587textDocument ={ 588languageId ='' , 589text =table . concat ( { 'testing' , '123' } , '\n' ), 590uri ='file://' , 591version =0 , 592} , 593} ) 594expect_notification ( 'textDocument/didChange' , { 595textDocument ={ 596uri ='file://' , 597version =3 , 598} , 599contentChanges ={ 600{ text =table . concat ( { 'testing' , 'boop' } , '\n' ) } , 601} , 602} ) 603expect_notification ( 'finish' ) 604notify ( 'finish' ) 605end , 606} 607end 608function tests . basic_check_buffer_open_and_change_multi () 609skeleton { 610on_init =function ( params ) 611local expected_capabilities =protocol . make_client_capabilities () 612assert_eq ( params . capabilities , expected_capabilities ) 613return { 614capabilities ={ 615textDocumentSync =protocol . TextDocumentSyncKind . Full , 616} , 617} 618end , 619body =function () 620notify ( 'start' ) 621expect_notification ( 'textDocument/didOpen' , { 622textDocument ={ 623languageId ='' , 624text =table . concat ( { 'testing' , '123' } , '\n' ) .. '\n' , 625uri ='file://' , 626version =0 , 627} , 628} ) 629expect_notification ( 'textDocument/didChange' , { 630textDocument ={ 631uri ='file://' , 632version =3 , 633} , 634contentChanges ={ 635{ text =table . concat ( { 'testing' , '321' } , '\n' ) .. '\n' } , 636} , 637} ) 638expect_notification ( 'textDocument/didChange' , { 639textDocument ={ 640uri ='file://' , 641version =4 , 642} , 643contentChanges ={ 644{ text =table . concat ( { 'testing' , 'boop' } , '\n' ) .. '\n' } , 645} , 646} ) 647expect_notification ( 'finish' ) 648notify ( 'finish' ) 649end , 650} 651end 652 653function tests . basic_check_buffer_open_and_change_multi_and_close () 654skeleton { 655on_init =function ( params ) 656local expected_capabilities =protocol . make_client_capabilities () 657assert_eq ( params . capabilities , expected_capabilities ) 658return { 659capabilities ={ 660textDocumentSync =protocol . TextDocumentSyncKind . Full , 661} , 662} 663end , 664body =function () 665notify ( 'start' ) 666expect_notification ( 'textDocument/didOpen' , { 667textDocument ={ 668languageId ='' , 669text =table . concat ( { 'testing' , '123' } , '\n' ) .. '\n' , 670uri ='file://' , 671version =0 , 672} , 673} ) 674expect_notification ( 'textDocument/didChange' , { 675textDocument ={ 676uri ='file://' , 677version =3 , 678} , 679contentChanges ={ 680{ text =table . concat ( { 'testing' , '321' } , '\n' ) .. '\n' } , 681} , 682} ) 683expect_notification ( 'textDocument/didChange' , { 684textDocument ={ 685uri ='file://' , 686version =4 , 687} , 688contentChanges ={ 689{ text =table . concat ( { 'testing' , 'boop' } , '\n' ) .. '\n' } , 690} , 691} ) 692expect_notification ( 'textDocument/didClose' , { 693textDocument ={ 694uri ='file://' , 695} , 696} ) 697expect_notification ( 'finish' ) 698notify ( 'finish' ) 699end , 700} 701end 702 703function tests . basic_check_buffer_open_and_change_incremental () 704skeleton { 705on_init =function ( params ) 706local expected_capabilities =protocol . make_client_capabilities () 707assert_eq ( params . capabilities , expected_capabilities ) 708return { 709capabilities ={ 710textDocumentSync ={ 711openClose =true , 712change =protocol . TextDocumentSyncKind . Incremental , 713willSave =true , 714willSaveWaitUntil =true , 715save ={ 716includeText =true , 717} , 718} , 719} , 720} 721end , 722body =function () 723notify ( 'start' ) 724expect_notification ( 'textDocument/didOpen' , { 725textDocument ={ 726languageId ='' , 727text =table . concat ( { 'testing' , '123' } , '\n' ) .. '\n' , 728uri ='file://' , 729version =0 , 730} , 731} ) 732expect_notification ( 'textDocument/didChange' , { 733textDocument ={ 734uri ='file://' , 735version =3 , 736} , 737contentChanges ={ 738{ 739range ={ 740start ={ line =1 , character =3 } , 741[ 'end' ] ={ line =1 , character =3 } , 742} , 743rangeLength =0 , 744text ='boop' , 745} , 746} , 747} ) 748expect_notification ( 'finish' ) 749notify ( 'finish' ) 750end , 751} 752end 753 754function tests . basic_check_buffer_open_and_change_incremental_editing () 755skeleton { 756on_init =function ( params ) 757local expected_capabilities =protocol . make_client_capabilities () 758assert_eq ( params . capabilities , expected_capabilities ) 759return { 760capabilities ={ 761textDocumentSync =protocol . TextDocumentSyncKind . Incremental , 762} , 763} 764end , 765body =function () 766notify ( 'start' ) 767expect_notification ( 'textDocument/didOpen' , { 768textDocument ={ 769languageId ='' , 770text =table . concat ( { 'testing' , '123' } , '\n' ), 771uri ='file://' , 772version =0 , 773} , 774} ) 775expect_notification ( 'textDocument/didChange' , { 776textDocument ={ 777uri ='file://' , 778version =3 , 779} , 780contentChanges ={ 781{ 782range ={ 783start ={ line =0 , character =0 } , 784[ 'end' ] ={ line =1 , character =0 } , 785} , 786rangeLength =4 , 787text ='testing\n\n' , 788} , 789} , 790} ) 791expect_notification ( 'finish' ) 792notify ( 'finish' ) 793end , 794} 795end 796 797function tests . decode_nil () 798skeleton { 799on_init =function ( _ ) 800return { capabilities ={} } 801end , 802body =function () 803notify ( 'start' ) 804notify ( 'workspace/executeCommand' , { 805arguments ={ 'EXTRACT_METHOD' , { metadata ={ field =vim . NIL } } , 3 , 0 , 6123 , vim . NIL } , 806command ='refactor.perform' , 807title ='EXTRACT_METHOD' , 808} ) 809notify ( 'finish' ) 810end , 811} 812end 813 814function tests . code_action_with_resolve () 815skeleton { 816on_init =function () 817return { 818capabilities ={ 819codeActionProvider ={ 820resolveProvider =true , 821} , 822} , 823} 824end , 825body =function () 826notify ( 'start' ) 827local cmd ={ title ='Action 1' } 828expect_request ( 'textDocument/codeAction' , function () 829return nil , { cmd } 830end ) 831expect_request ( 'codeAction/resolve' , function () 832return nil , 833{ 834title ='Action 1' , 835command ={ 836title ='Command 1' , 837command ='dummy1' , 838} , 839} 840end ) 841notify ( 'shutdown' ) 842end , 843} 844end 845 846function tests . code_action_server_side_command () 847skeleton ( { 848on_init =function () 849return { 850capabilities ={ 851codeActionProvider ={ 852resolveProvider =false , 853} , 854executeCommandProvider ={ 855commands ={ 'dummy1' } , 856} , 857} , 858} 859end , 860body =function () 861notify ( 'start' ) 862local cmd ={ 863title ='Command 1' , 864command ='dummy1' , 865} 866expect_request ( 'textDocument/codeAction' , function () 867return nil , { cmd } 868end ) 869expect_request ( 'workspace/executeCommand' , function () 870return nil , cmd 871end ) 872notify ( 'shutdown' ) 873end , 874} ) 875end 876 877function tests . code_action_filter () 878skeleton { 879on_init =function () 880return { 881capabilities ={ 882codeActionProvider ={ 883resolveProvider =false , 884} , 885} , 886} 887end , 888body =function () 889notify ( 'start' ) 890local action ={ 891title ='Action 1' , 892command ='command' , 893} 894local preferred_action ={ 895title ='Action 2' , 896isPreferred =true , 897command ='preferred_command' , 898} 899local type_annotate_action ={ 900title ='Action 3' , 901kind ='type-annotate' , 902command ='type_annotate_command' , 903} 904local type_annotate_foo_action ={ 905title ='Action 4' , 906kind ='type-annotate.foo' , 907command ='type_annotate_foo_command' , 908} 909expect_request ( 'textDocument/codeAction' , function () 910return nil , { action , preferred_action , type_annotate_action , type_annotate_foo_action } 911end ) 912expect_request ( 'textDocument/codeAction' , function () 913return nil , { action , preferred_action , type_annotate_action , type_annotate_foo_action } 914end ) 915notify ( 'shutdown' ) 916end , 917} 918end 919 920function tests . clientside_commands () 921skeleton { 922on_init =function () 923return { 924capabilities ={} , 925} 926end , 927body =function () 928notify ( 'start' ) 929notify ( 'shutdown' ) 930end , 931} 932end 933 934function tests . codelens_refresh_lock () 935skeleton { 936on_init =function () 937return { 938capabilities ={ 939codeLensProvider ={ resolveProvider =true } , 940} , 941} 942end , 943body =function () 944notify ( 'start' ) 945expect_request ( 'textDocument/codeLens' , function () 946return { code =- 32002 , message ='ServerNotInitialized' } , nil 947end ) 948expect_request ( 'textDocument/codeLens' , function () 949local lenses ={ 950{ 951range ={ 952start ={ line =0 , character =0 } , 953[ 'end' ] ={ line =0 , character =3 } , 954} , 955command ={ title ='Lens1' , command ='Dummy' } , 956} , 957} 958return nil , lenses 959end ) 960expect_request ( 'textDocument/codeLens' , function () 961local lenses ={ 962{ 963range ={ 964start ={ line =0 , character =0 } , 965[ 'end' ] ={ line =0 , character =3 } , 966} , 967command ={ title ='Lens2' , command ='Dummy' } , 968} , 969} 970return nil , lenses 971end ) 972notify ( 'shutdown' ) 973end , 974} 975end 976 977function tests . basic_formatting () 978skeleton { 979on_init =function () 980return { 981capabilities ={ 982documentFormattingProvider =true , 983} , 984} 985end , 986body =function () 987notify ( 'start' ) 988expect_request ( 'textDocument/formatting' , function () 989return nil , {} 990end ) 991notify ( 'shutdown' ) 992end , 993} 994end 995 996function tests . range_formatting () 997skeleton { 998on_init =function () 999return { 1000capabilities ={ 1001documentFormattingProvider =true , 1002documentRangeFormattingProvider =true , 1003} , 1004} 1005end , 1006body =function () 1007notify ( 'start' ) 1008expect_request ( 'textDocument/rangeFormatting' , function () 1009return nil , {} 1010end ) 1011notify ( 'shutdown' ) 1012end , 1013} 1014end 1015 1016function tests . ranges_formatting () 1017skeleton { 1018on_init =function () 1019return { 1020capabilities ={ 1021documentFormattingProvider =true , 1022documentRangeFormattingProvider ={ 1023rangesSupport =true , 1024} , 1025} , 1026} 1027end , 1028body =function () 1029notify ( 'start' ) 1030expect_request ( 'textDocument/rangesFormatting' , function () 1031return nil , {} 1032end ) 1033notify ( 'shutdown' ) 1034end , 1035} 1036end 1037 1038function tests . set_defaults_all_capabilities () 1039skeleton { 1040on_init =function ( _ ) 1041return { 1042capabilities ={ 1043definitionProvider =true , 1044completionProvider =true , 1045documentRangeFormattingProvider =true , 1046hoverProvider =true , 1047} , 1048} 1049end , 1050body =function () 1051notify ( 'test' ) 1052end , 1053} 1054end 1055 1056function tests . inlay_hint () 1057skeleton { 1058on_init =function ( params ) 1059local expected_capabilities =protocol . make_client_capabilities () 1060assert_eq ( params . capabilities , expected_capabilities ) 1061return { 1062capabilities ={ 1063inlayHintProvider =true , 1064} , 1065} 1066end , 1067body =function () 1068notify ( 'start' ) 1069expect_request ( 'textDocument/inlayHint' , function () 1070return nil , {} 1071end ) 1072expect_notification ( 'finish' ) 1073notify ( 'finish' ) 1074end , 1075} 1076end 1077 1078-- Tests will be indexed by test_name 1079local test_name =arg [ 1 ] 1080local timeout =tonumber ( arg [ 2 ]) 1081assert ( type ( test_name ) == 'string' , 'test_name must be specified as first arg.' ) 1082 1083local kill_timer =vim . defer_fn ( function () 1084log ( 'ERROR' , 'LSP' , 'TIMEOUT' ) 1085io . stderr : write ( 'TIMEOUT' ) 1086os . exit ( 100 ) 1087end , timeout or 1e3 ) 1088 1089-- Close the timer on exit (deadly signal or :cquit) to avoid delay with ASAN/TSAN. 1090vim . api . nvim_create_autocmd ( 'VimLeave' , { 1091callback =function () 1092kill_timer : stop () 1093kill_timer : close () 1094end , 1095} ) 1096 1097local status , err =pcall ( assert ( tests [ test_name ], 'Test not found' )) 1098if not status then 1099log ( 'ERROR' , 'LSP' , tostring ( err )) 1100io . stderr : write ( err ) 1101vim . cmd [[101cquit]] 1102end