robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
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 =t . describe , t . it , t . before_each , t . after_each 7local clear =n . clear 8local exec_lua =n . exec_lua 9local eq =t . eq 10local neq =t . neq 11local matches =t . matches 12local pcall_err =t . pcall_err 13 14local create_server_definition =t_lsp . create_server_definition 15 16describe ( 'vim.lsp.diagnostic' , function () 17local fake_uri --- @type string 18local client_id --- @type integer 19local diagnostic_bufnr --- @type integer 20 21before_each ( function () 22clear { env ={ 23NVIM_LUA_NOTRACK ='1' , 24VIMRUNTIME =os . getenv 'VIMRUNTIME' , 25} } 26 27exec_lua ( function () 28require ( 'vim.lsp' ) 29 30_G . make_range =function ( x1 , y1 , x2 , y2 ) 31return { start ={ line =x1 , character =y1 } , [ 'end' ] ={ line =x2 , character =y2 } } 32end 33 34_G . make_error =function ( msg , x1 , y1 , x2 , y2 ) 35return { 36range =_G . make_range ( x1 , y1 , x2 , y2 ), 37message =msg , 38severity =1 , 39} 40end 41 42_G . make_warning =function ( msg , x1 , y1 , x2 , y2 ) 43return { 44range =_G . make_range ( x1 , y1 , x2 , y2 ), 45message =msg , 46severity =2 , 47} 48end 49 50_G . make_information =function ( msg , x1 , y1 , x2 , y2 ) 51return { 52range =_G . make_range ( x1 , y1 , x2 , y2 ), 53message =msg , 54severity =3 , 55} 56end 57 58function _G . get_extmarks ( bufnr , client_id0 ) 59local namespace =vim . lsp . diagnostic . get_namespace ( client_id0 ) 60local ns =vim . diagnostic . get_namespace ( namespace ) 61local extmarks ={} 62if ns . user_data . virt_text_ns then 63for _ , e in 64pairs ( 65vim . api . nvim_buf_get_extmarks ( 66bufnr , 67ns . user_data . virt_text_ns , 680 , 69- 1 , 70{ details =true } 71) 72) 73do 74table . insert ( extmarks , e ) 75end 76end 77if ns . user_data . underline_ns then 78for _ , e in 79pairs ( 80vim . api . nvim_buf_get_extmarks ( 81bufnr , 82ns . user_data . underline_ns , 830 , 84- 1 , 85{ details =true } 86) 87) 88do 89table . insert ( extmarks , e ) 90end 91end 92return extmarks 93end 94end ) 95 96fake_uri ='file:///fake/uri' 97 98exec_lua ( function () 99diagnostic_bufnr =vim . uri_to_bufnr ( fake_uri ) 100local lines ={ '1st line' , '2nd line of text' , 'wow' , 'cool' , 'more' , 'lines' } 101vim . fn . bufload ( diagnostic_bufnr ) 102vim . api . nvim_buf_set_lines ( diagnostic_bufnr , 0 , 1 , false , lines ) 103vim . api . nvim_win_set_buf ( 0 , diagnostic_bufnr ) 104end ) 105end ) 106 107describe ( 'vim.lsp.diagnostic.on_publish_diagnostics' , function () 108before_each ( function () 109exec_lua ( create_server_definition ) 110exec_lua ( function () 111_G . requests =0 112_G . server =_G . _create_server ( { 113capabilities ={ 114diagnosticProvider ={} , 115} , 116} ) 117 118client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } )) 119end ) 120end ) 121 122after_each ( function () 123exec_lua ( function () 124local client =vim . lsp . get_client_by_id ( client_id ) 125if client then 126client : stop () 127end 128 129vim . api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 130end ) 131end ) 132 133it ( 'correctly handles UTF-16 offsets' , function () 134local line ='All 💼 and no 🎉 makes Jack a dull 👦' 135local result =exec_lua ( function () 136vim . api . nvim_buf_set_lines ( diagnostic_bufnr , 0 , - 1 , false , { line } ) 137 138vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 139uri =fake_uri , 140diagnostics ={ 141_G . make_error ( 'UTF-16 Diagnostic' , 0 , 7 , 0 , 8 ), 142} , 143} , { client_id =client_id } ) 144 145local diags =vim . diagnostic . get ( diagnostic_bufnr ) 146return diags 147end ) 148eq ( 1 , # result ) 149eq ( 150exec_lua ( function () 151return vim . str_byteindex ( line , 'utf-16' , 7 ) 152end ), 153result [ 1 ]. col 154) 155eq ( 156exec_lua ( function () 157return vim . str_byteindex ( line , 'utf-16' , 8 ) 158end ), 159result [ 1 ]. end_col 160) 161end ) 162 163it ( 'does not create buffer on empty diagnostics' , function () 164-- No buffer is created without diagnostics 165eq ( 166- 1 , 167exec_lua ( function () 168vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 169uri ='file:///fake/uri2' , 170diagnostics ={} , 171} , { client_id =client_id } ) 172return vim . fn . bufnr ( vim . uri_to_fname ( 'file:///fake/uri2' )) 173end ) 174) 175 176-- Create buffer on diagnostics 177neq ( 178- 1 , 179exec_lua ( function () 180vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 181uri ='file:///fake/uri2' , 182diagnostics ={ 183_G . make_error ( 'Diagnostic' , 0 , 0 , 0 , 0 ), 184} , 185} , { client_id =client_id } ) 186return vim . fn . bufnr ( vim . uri_to_fname ( 'file:///fake/uri2' )) 187end ) 188) 189eq ( 1901 , 191exec_lua ( function () 192return # vim . diagnostic . get ( _G . bufnr ) 193end ) 194) 195 196-- Clear diagnostics after buffer was created 197neq ( 198- 1 , 199exec_lua ( function () 200vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 201uri ='file:///fake/uri2' , 202diagnostics ={} , 203} , { client_id =client_id } ) 204return vim . fn . bufnr ( vim . uri_to_fname ( 'file:///fake/uri2' )) 205end ) 206) 207eq ( 2080 , 209exec_lua ( function () 210return # vim . diagnostic . get ( _G . bufnr ) 211end ) 212) 213end ) 214 215it ( 'clears diagnostics for the client namespace on empty publish' , function () 216local before_clear , after_clear =exec_lua ( function () 217local ns =vim . lsp . diagnostic . get_namespace ( client_id ) 218 219-- Publish diagnostics 220vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 221uri =fake_uri , 222diagnostics ={ 223_G . make_error ( 'Diagnostic' , 0 , 0 , 0 , 0 ), 224} , 225} , { client_id =client_id } ) 226 227local before_clear =vim . diagnostic . get ( diagnostic_bufnr , { namespace =ns } ) 228 229-- Publish empty diagnostics 230vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 231uri =fake_uri , 232diagnostics ={} , 233} , { client_id =client_id } ) 234 235local after_clear =vim . diagnostic . get ( diagnostic_bufnr , { namespace =ns } ) 236 237return before_clear , after_clear 238end ) 239 240eq ( 1 , # before_clear ) 241eq ( 0 , # after_clear ) 242end ) 243 244it ( 'clears diagnostics when buffer is deleted' , function () 245local before_delete , after_delete =exec_lua ( function () 246local ns =vim . lsp . diagnostic . get_namespace ( client_id ) 247 248-- Publish diagnostics 249vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 250uri =fake_uri , 251diagnostics ={ 252_G . make_error ( 'Diagnostic' , 0 , 0 , 0 , 0 ), 253} , 254} , { client_id =client_id } ) 255 256local before_delete =vim . diagnostic . get ( diagnostic_bufnr , { namespace =ns } ) 257 258-- Avoid deleting the currently displayed buffer (Windows teardown edge case) 259local scratch =vim . api . nvim_create_buf ( false , true ) 260vim . api . nvim_win_set_buf ( 0 , scratch ) 261 262-- Delete the buffer deterministically 263vim . api . nvim_buf_delete ( diagnostic_bufnr , { force =true } ) 264 265-- Query remaining diagnostics via valid access path 266-- (deleted buffer cannot be queried directly) 267local after_delete =vim . diagnostic . get ( nil , { namespace =ns } ) 268 269return before_delete , after_delete 270end ) 271 272eq ( 1 , # before_delete ) 273eq ( 0 , # after_delete ) 274end ) 275 276it ( 'clears diagnostics when client is detached' , function () 277exec_lua ( function () 278vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 279uri =fake_uri , 280diagnostics ={ 281_G . make_error ( 'Diagnostic' , 0 , 0 , 0 , 0 ), 282} , 283} , { client_id =client_id } ) 284end ) 285 286eq ( 2871 , 288exec_lua ( function () 289local ns =vim . lsp . diagnostic . get_namespace ( client_id ) 290return # vim . diagnostic . get ( diagnostic_bufnr , { namespace =ns } ) 291end ) 292) 293 294exec_lua ( function () 295vim . lsp . get_client_by_id ( client_id ): stop () 296end ) 297 298eq ( 2990 , 300exec_lua ( function () 301local ns =vim . lsp . diagnostic . get_namespace ( client_id ) 302return # vim . diagnostic . get ( diagnostic_bufnr , { namespace =ns } ) 303end ) 304) 305end ) 306 307it ( 'errors when diagnostic tags is null' , function () 308matches ( 309'server response has invalid %(null%) tags' , 310pcall_err ( exec_lua , function () 311vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 312uri =fake_uri , 313diagnostics ={ 314vim . tbl_extend ( 'force' , _G . make_error ( 'Diagnostic' , 0 , 0 , 0 , 0 ), { tags =vim . NIL } ), 315} , 316} , { client_id =client_id } ) 317end ) 318) 319end ) 320end ) 321 322describe ( 'vim.lsp.diagnostic.on_diagnostic' , function () 323before_each ( function () 324exec_lua ( create_server_definition ) 325exec_lua ( function () 326_G . requests =0 327_G . server =_G . _create_server ( { 328capabilities ={ 329diagnosticProvider ={ 330documentSelector =vim . NIL , 331interFileDependencies =false , 332workspaceDiagnostics =false , 333} , 334} , 335handlers ={ 336[ 'textDocument/diagnostic' ] =function ( _ , params ) 337_G . params =params 338_G . requests =_G . requests + 1 339end , 340} , 341} ) 342 343function _G . get_extmarks ( bufnr , client_id0 ) 344local namespace =vim . lsp . diagnostic . get_namespace ( client_id0 , true ) 345local ns =vim . diagnostic . get_namespace ( namespace ) 346local extmarks ={} 347if ns . user_data . virt_text_ns then 348for _ , e in 349pairs ( 350vim . api . nvim_buf_get_extmarks ( 351bufnr , 352ns . user_data . virt_text_ns , 3530 , 354- 1 , 355{ details =true } 356) 357) 358do 359table . insert ( extmarks , e ) 360end 361end 362if ns . user_data . underline_ns then 363for _ , e in 364pairs ( 365vim . api . nvim_buf_get_extmarks ( 366bufnr , 367ns . user_data . underline_ns , 3680 , 369- 1 , 370{ details =true } 371) 372) 373do 374table . insert ( extmarks , e ) 375end 376end 377return extmarks 378end 379 380client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } )) 381end ) 382end ) 383 384it ( 'adds diagnostics to vim.diagnostics' , function () 385local diags =exec_lua ( function () 386vim . lsp . diagnostic . on_diagnostic ( nil , { 387kind ='full' , 388items ={ 389_G . make_error ( 'Pull Diagnostic' , 4 , 4 , 4 , 4 ), 390} , 391} , { 392params ={ 393textDocument ={ uri =fake_uri } , 394} , 395uri =fake_uri , 396client_id =client_id , 397bufnr =diagnostic_bufnr , 398} , {} ) 399 400return vim . diagnostic . get ( diagnostic_bufnr ) 401end ) 402eq ( 1 , # diags ) 403eq ( 'Pull Diagnostic' , diags [ 1 ]. message ) 404end ) 405 406it ( 'preserves push diagnostics when pull diagnostics are empty' , function () 407local push_ns_count , pull_ns_count , all_diags_count , push_ns , pull_ns =exec_lua ( function () 408vim . lsp . diagnostic . on_publish_diagnostics ( nil , { 409uri =fake_uri , 410diagnostics ={ 411_G . make_error ( 'Push Diagnostic' , 0 , 0 , 0 , 0 ), 412} , 413} , { client_id =client_id } ) 414 415vim . lsp . diagnostic . on_diagnostic ( nil , { 416kind ='full' , 417items ={} , 418} , { 419params ={ 420textDocument ={ uri =fake_uri } , 421} , 422uri =fake_uri , 423client_id =client_id , 424bufnr =diagnostic_bufnr , 425} , {} ) 426 427local push_ns =vim . lsp . diagnostic . get_namespace ( client_id , false ) 428local pull_ns =vim . lsp . diagnostic . get_namespace ( client_id , true ) 429 430return # vim . diagnostic . get ( diagnostic_bufnr , { namespace =push_ns } ), 431# vim . diagnostic . get ( diagnostic_bufnr , { namespace =pull_ns } ), 432# vim . diagnostic . get ( diagnostic_bufnr ), 433push_ns , 434pull_ns 435end ) 436 437eq ( 1 , push_ns_count ) 438eq ( 0 , pull_ns_count ) 439eq ( 1 , all_diags_count ) 440neq ( push_ns , pull_ns ) 441end ) 442 443it ( 'uses pull_id to isolate pull diagnostic namespaces' , function () 444local first_count , second_count , total_count , first_ns , second_ns =exec_lua ( function () 445vim . lsp . diagnostic . on_diagnostic ( nil , { 446kind ='full' , 447items ={ 448_G . make_error ( 'Pull Diagnostic A' , 0 , 0 , 0 , 0 ), 449} , 450} , { 451params ={ 452identifier ='provider-a' , 453textDocument ={ uri =fake_uri } , 454} , 455uri =fake_uri , 456client_id =client_id , 457bufnr =diagnostic_bufnr , 458} , {} ) 459 460vim . lsp . diagnostic . on_diagnostic ( nil , { 461kind ='full' , 462items ={} , 463} , { 464params ={ 465identifier ='provider-b' , 466textDocument ={ uri =fake_uri } , 467} , 468uri =fake_uri , 469client_id =client_id , 470bufnr =diagnostic_bufnr , 471} , {} ) 472 473local first_ns =vim . lsp . diagnostic . get_namespace ( client_id , true , 'provider-a' ) 474local second_ns =vim . lsp . diagnostic . get_namespace ( client_id , true , 'provider-b' ) 475 476return # vim . diagnostic . get ( diagnostic_bufnr , { namespace =first_ns } ), 477# vim . diagnostic . get ( diagnostic_bufnr , { namespace =second_ns } ), 478# vim . diagnostic . get ( diagnostic_bufnr ), 479first_ns , 480second_ns 481end ) 482 483eq ( 1 , first_count ) 484eq ( 0 , second_count ) 485eq ( 1 , total_count ) 486neq ( first_ns , second_ns ) 487end ) 488 489it ( 'handles multiline diagnostic ranges #33782' , function () 490local diags =exec_lua ( function () 491vim . lsp . diagnostic . on_diagnostic ( nil , { 492kind ='full' , 493items ={ 494_G . make_error ( 'Pull Diagnostic' , 0 , 6 , 1 , 10 ), 495} , 496} , { 497params ={ 498textDocument ={ uri =fake_uri } , 499} , 500uri =fake_uri , 501client_id =client_id , 502bufnr =diagnostic_bufnr , 503} , {} ) 504 505return vim . diagnostic . get ( diagnostic_bufnr ) 506end ) 507local lines =exec_lua ( function () 508return vim . api . nvim_buf_get_lines ( diagnostic_bufnr , 0 , - 1 , false ) 509end ) 510-- This test case must be run over a multiline diagnostic in which the start line is shorter 511-- than the end line, and the end_col exceeds the start line's length. 512eq ( 8 , # lines [ 1 ]) 513eq ( 16 , # lines [ 2 ]) 514eq ( 1 , # diags ) 515eq ( 6 , diags [ 1 ]. col ) 516eq ( 10 , diags [ 1 ]. end_col ) 517end ) 518 519it ( 'severity defaults to error if missing' , function () 520---@type vim.Diagnostic[] 521local diagnostics =exec_lua ( function () 522vim . lsp . diagnostic . on_diagnostic ( nil , { 523kind ='full' , 524items ={ 525{ 526range =_G . make_range ( 4 , 4 , 4 , 4 ), 527message ='bad!' , 528} , 529} , 530} , { 531params ={ 532textDocument ={ uri =fake_uri } , 533} , 534uri =fake_uri , 535client_id =client_id , 536bufnr =diagnostic_bufnr , 537} , {} ) 538return vim . diagnostic . get ( diagnostic_bufnr ) 539end ) 540eq ( 1 , # diagnostics ) 541eq ( 1 , diagnostics [ 1 ]. severity ) 542end ) 543 544it ( 'clears pull diagnostics when client detaches' , function () 545exec_lua ( function () 546vim . lsp . diagnostic . on_diagnostic ( nil , { 547kind ='full' , 548items ={ 549_G . make_error ( 'Pull Diagnostic' , 4 , 4 , 4 , 4 ), 550} , 551} , { 552params ={ 553textDocument ={ uri =fake_uri } , 554} , 555uri =fake_uri , 556client_id =client_id , 557bufnr =diagnostic_bufnr , 558} , {} ) 559end ) 560 561eq ( 5621 , 563exec_lua ( function () 564return # vim . diagnostic . get ( diagnostic_bufnr ) 565end ) 566) 567 568exec_lua ( function () 569vim . lsp . get_client_by_id ( client_id ): stop () 570end ) 571 572eq ( 5730 , 574exec_lua ( function () 575return # vim . diagnostic . get ( diagnostic_bufnr ) 576end ) 577) 578end ) 579 580it ( 'keeps diagnostics when one client detaches and others still are attached' , function () 581exec_lua ( function () 582_G . client_id2 =assert ( vim . lsp . start ( { name ='dummy2' , cmd =_G . server . cmd } )) 583 584vim . lsp . diagnostic . on_diagnostic ( nil , { 585kind ='full' , 586items ={ 587_G . make_error ( 'Pull Diagnostic' , 4 , 4 , 4 , 4 ), 588} , 589} , { 590params ={ 591textDocument ={ uri =fake_uri } , 592} , 593uri =fake_uri , 594client_id =client_id , 595bufnr =diagnostic_bufnr , 596} , {} ) 597end ) 598 599eq ( 6001 , 601exec_lua ( function () 602return # vim . diagnostic . get ( diagnostic_bufnr ) 603end ) 604) 605 606exec_lua ( function () 607vim . lsp . get_client_by_id ( _G . client_id2 ): stop () 608end ) 609 610eq ( 6111 , 612exec_lua ( function () 613return # vim . diagnostic . get ( diagnostic_bufnr ) 614end ) 615) 616end ) 617 618it ( 'handles server cancellation' , function () 619eq ( 6202 , 621exec_lua ( function () 622vim . lsp . diagnostic . on_diagnostic ( { 623code =vim . lsp . protocol . ErrorCodes . ServerCancelled , 624-- Empty data defaults to retriggering request 625data ={} , 626message ='' , 627} , {} , { 628method ='textDocument/diagnostic' , 629client_id =client_id , 630bufnr =diagnostic_bufnr , 631} ) 632 633return _G . requests 634end ) 635) 636 637eq ( 6383 , 639exec_lua ( function () 640vim . lsp . diagnostic . on_diagnostic ( { 641code =vim . lsp . protocol . ErrorCodes . ServerCancelled , 642data ={ retriggerRequest =true } , 643message ='' , 644} , {} , { 645method ='textDocument/diagnostic' , 646client_id =client_id , 647bufnr =diagnostic_bufnr , 648} ) 649 650return _G . requests 651end ) 652) 653 654eq ( 6553 , 656exec_lua ( function () 657vim . lsp . diagnostic . on_diagnostic ( { 658code =vim . lsp . protocol . ErrorCodes . ServerCancelled , 659data ={ retriggerRequest =false } , 660message ='' , 661} , {} , { 662method ='textDocument/diagnostic' , 663client_id =client_id , 664bufnr =diagnostic_bufnr , 665} ) 666 667return _G . requests 668end ) 669) 670end ) 671 672it ( 'supports dynamic registration' , function () 673exec_lua ( create_server_definition ) 674exec_lua ( function () 675_G . server2 =_G . _create_server ( { 676diagnosticProvider ={ 677documentSelector =vim . NIL , 678} , 679handlers ={ 680[ 'textDocument/diagnostic' ] =function ( _ , _ , callback ) 681callback ( nil , { 682kind ='full' , 683items ={ 684_G . make_error ( 'Dynamic Diagnostic' , 4 , 4 , 4 , 4 ), 685} , 686} ) 687end , 688} , 689} ) 690 691local client_id2 =assert ( vim . lsp . start ( { name ='dummy2' , cmd =_G . server2 . cmd } )) 692 693vim . lsp . handlers [ 'client/registerCapability' ]( nil , { 694registrations ={ 695{ id ='diagnostic' , method ='textDocument/diagnostic' } , 696} , 697} , { client_id =client_id2 , method ='client/registerCapability' } ) 698end ) 699 700eq ( 7011 , 702exec_lua ( function () 703return # vim . diagnostic . get ( diagnostic_bufnr ) 704end ) 705) 706end ) 707 708it ( 'requests with the `previousResultId`' , function () 709-- Full reports 710eq ( 711'dummy_server' , 712exec_lua ( function () 713vim . lsp . diagnostic . on_diagnostic ( nil , { 714kind ='full' , 715resultId ='dummy_server' , 716items ={ 717_G . make_error ( 'Pull Diagnostic' , 4 , 4 , 4 , 4 ), 718} , 719} , { 720method ='textDocument/diagnostic' , 721params ={ 722textDocument ={ uri =fake_uri } , 723} , 724client_id =client_id , 725bufnr =diagnostic_bufnr , 726} ) 727vim . api . nvim_exec_autocmds ( 'LspNotify' , { 728buf =diagnostic_bufnr , 729data ={ 730method ='textDocument/didChange' , 731client_id =client_id , 732} , 733} ) 734return _G . params . previousResultId 735end ) 736) 737 738-- Unchanged reports 739eq ( 740'squidward' , 741exec_lua ( function () 742vim . lsp . diagnostic . on_diagnostic ( nil , { 743kind ='unchanged' , 744resultId ='squidward' , 745} , { 746method ='textDocument/diagnostic' , 747params ={ 748textDocument ={ uri =fake_uri } , 749} , 750client_id =client_id , 751bufnr =diagnostic_bufnr , 752} ) 753vim . api . nvim_exec_autocmds ( 'LspNotify' , { 754buf =diagnostic_bufnr , 755data ={ 756method ='textDocument/didChange' , 757client_id =client_id , 758} , 759} ) 760return _G . params . previousResultId 761end ) 762) 763end ) 764 765it ( 'handles relatedDocuments diagnostics' , function () 766local fake_uri_2 ='file:///fake/uri2' 767---@type vim.Diagnostic[], vim.Diagnostic[], string? 768local diagnostics , related_diagnostics , relatedPreviousResultId =exec_lua ( function () 769local second_buf =vim . uri_to_bufnr ( fake_uri_2 ) 770vim . fn . bufload ( second_buf ) 771 772-- Attach the client to both buffers. 773vim . api . nvim_win_set_buf ( 0 , second_buf ) 774vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } ) 775 776vim . lsp . diagnostic . on_diagnostic ( nil , { 777kind ='full' , 778relatedDocuments ={ 779[ fake_uri_2 ] ={ 780kind ='full' , 781resultId ='spongebob' , 782items ={ 783{ 784range =_G . make_range ( 4 , 4 , 4 , 4 ), 785message ='related bad!' , 786} , 787} , 788} , 789} , 790items ={} , 791} , { 792params ={ 793textDocument ={ uri =fake_uri } , 794} , 795uri =fake_uri , 796client_id =client_id , 797bufnr =diagnostic_bufnr , 798} , {} ) 799 800vim . api . nvim_exec_autocmds ( 'LspNotify' , { 801buf =second_buf , 802data ={ 803method ='textDocument/didChange' , 804client_id =client_id , 805} , 806} ) 807 808return vim . diagnostic . get ( diagnostic_bufnr ), 809vim . diagnostic . get ( second_buf ), 810_G . params . previousResultId 811end ) 812eq ( 0 , # diagnostics ) 813eq ( 1 , # related_diagnostics ) 814eq ( 'related bad!' , related_diagnostics [ 1 ]. message ) 815eq ( 'spongebob' , relatedPreviousResultId ) 816end ) 817end ) 818 819describe ( 'vim.lsp.diagnostic.on_refresh' , function () 820it ( 'refreshes diagnostics on server-to-client request' , function () 821exec_lua ( create_server_definition ) 822exec_lua ( function () 823_G . requests =0 824_G . server =_G . _create_server ( { 825capabilities ={ 826diagnosticProvider ={ 827documentSelector =vim . NIL , 828interFileDependencies =false , 829workspaceDiagnostics =false , 830} , 831} , 832handlers ={ 833[ 'textDocument/diagnostic' ] =function ( _ , _ , callback ) 834_G . requests =_G . requests + 1 835callback ( nil , { 836kind ='full' , 837items ={ 838_G . make_warning ( 'Pull Diagnostic' , 4 , 4 , 4 , 4 ), 839} , 840} ) 841end , 842} , 843} ) 844client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } )) 845end ) 846 847local diags =exec_lua ( function () 848vim . lsp . diagnostic . on_diagnostic ( nil , { 849kind ='full' , 850items ={ 851_G . make_error ( 'Pull Diagnostic' , 4 , 4 , 4 , 4 ), 852} , 853} , { 854params ={ 855textDocument ={ uri =fake_uri } , 856} , 857uri =fake_uri , 858client_id =client_id , 859bufnr =diagnostic_bufnr , 860} , {} ) 861 862return vim . diagnostic . get ( diagnostic_bufnr ) 863end ) 864eq ( 1 , # diags ) 865eq ( 1 , diags [ 1 ]. severity ) 866 867local requests , refreshed_diags =exec_lua ( function () 868vim . lsp . diagnostic . on_refresh ( nil , nil , { 869method ='workspace/diagnostic/refresh' , 870client_id =client_id , 871} ) 872 873return _G . requests , vim . diagnostic . get ( diagnostic_bufnr ) 874end ) 875eq ( 2 , requests ) 876eq ( 1 , # refreshed_diags ) 877eq ( 2 , refreshed_diags [ 1 ]. severity ) 878end ) 879 880it ( 'refreshes workspace diagnostics' , function () 881local fake_uri_3 ='file:///fake/uri3' 882exec_lua ( create_server_definition ) 883exec_lua ( function () 884_G . workspace_requests =0 885_G . doc_requests =0 886_G . server =_G . _create_server ( { 887capabilities ={ 888diagnosticProvider ={ 889documentSelector =vim . NIL , 890interFileDependencies =false , 891workspaceDiagnostics =true , 892} , 893} , 894handlers ={ 895[ 'textDocument/diagnostic' ] =function ( _ , _ , callback ) 896_G . doc_requests =_G . doc_requests + 1 897callback ( nil , { 898kind ='full' , 899items ={ 900_G . make_warning ( 'Refreshed Diagnostic' , 4 , 4 , 4 , 4 ), 901} , 902} ) 903end , 904[ 'workspace/diagnostic' ] =function ( _ , _ , callback ) 905_G . workspace_requests =_G . workspace_requests + 1 906callback ( nil , { 907items ={ 908{ 909kind ='full' , 910uri =fake_uri_3 , 911items ={ 912_G . make_error ( 'Workspace Diagnostic' , 4 , 4 , 4 , 4 ), 913} , 914} , 915} , 916} ) 917end , 918} , 919} ) 920client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =_G . server . cmd } )) 921end ) 922 923-- Seed the opened buffer with a document diagnostic so on_refresh must 924-- re-request it even when workspace diagnostics are supported. 925exec_lua ( function () 926vim . lsp . diagnostic . on_diagnostic ( nil , { 927kind ='full' , 928items ={ 929_G . make_error ( 'Initial Diagnostic' , 4 , 4 , 4 , 4 ), 930} , 931} , { 932params ={ 933textDocument ={ uri =fake_uri } , 934} , 935uri =fake_uri , 936client_id =client_id , 937bufnr =diagnostic_bufnr , 938} , {} ) 939end ) 940 941eq ( 9421 , 943exec_lua ( function () 944return # vim . diagnostic . get () 945end ) 946) 947 948eq ( 949{ vim . NIL } , 950exec_lua ( function () 951local client =vim . lsp . get_client_by_id ( client_id ) 952assert ( client ) 953local result ={} 954client : _provider_foreach ( 'workspace/diagnostic' , function ( cap ) 955table . insert ( result , cap . identifier or vim . NIL ) 956end ) 957return result 958end ) 959) 960 961exec_lua ( function () 962_G . doc_requests =0 963end ) 964 965local workspace_requests , doc_requests , diags , opened_diags , workspace_diags =exec_lua ( 966function () 967vim . lsp . diagnostic . on_refresh ( nil , nil , { 968method ='workspace/diagnostic/refresh' , 969client_id =client_id , 970} ) 971 972return _G . workspace_requests , 973_G . doc_requests , 974vim . diagnostic . get (), 975vim . diagnostic . get ( diagnostic_bufnr ), 976vim . diagnostic . get ( vim . uri_to_bufnr ( fake_uri_3 )) 977end 978) 979eq ( 1 , workspace_requests ) 980eq ( 1 , doc_requests ) 981eq ( 2 , # diags ) 982eq ( 1 , # opened_diags ) 983eq ( 2 , opened_diags [ 1 ]. severity ) 984eq ( 1 , # workspace_diags ) 985eq ( 1 , workspace_diags [ 1 ]. severity ) 986end ) 987end ) 988end )