robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local command =n . command 7local clear =n . clear 8local exec_lua =n . exec_lua 9local eq =t . eq 10local neq =t . neq 11local matches =t . matches 12local retry =t . retry 13local api =n . api 14local pcall_err =t . pcall_err 15local fn =n . fn 16 17describe ( 'vim.diagnostic' , function () 18before_each ( function () 19clear () 20 21exec_lua ( function () 22require ( 'vim.diagnostic' ) 23 24local function make_diagnostic ( msg , lnum , col , end_lnum , end_col , severity , source , code ) 25return { 26lnum =lnum , 27col =col , 28end_lnum =end_lnum , 29end_col =end_col , 30message =msg , 31severity =severity , 32source =source , 33code =code , 34} 35end 36 37function _G . make_error ( msg , lnum , col , end_lnum , end_col , source , code ) 38return make_diagnostic ( 39msg , 40lnum , 41col , 42end_lnum , 43end_col , 44vim . diagnostic . severity . ERROR , 45source , 46code 47) 48end 49 50function _G . make_warning ( msg , lnum , col , end_lnum , end_col , source , code ) 51return make_diagnostic ( 52msg , 53lnum , 54col , 55end_lnum , 56end_col , 57vim . diagnostic . severity . WARN , 58source , 59code 60) 61end 62 63function _G . make_info ( msg , lnum , col , end_lnum , end_col , source , code ) 64return make_diagnostic ( 65msg , 66lnum , 67col , 68end_lnum , 69end_col , 70vim . diagnostic . severity . INFO , 71source , 72code 73) 74end 75 76function _G . make_hint ( msg , lnum , col , end_lnum , end_col , source , code ) 77return make_diagnostic ( 78msg , 79lnum , 80col , 81end_lnum , 82end_col , 83vim . diagnostic . severity . HINT , 84source , 85code 86) 87end 88 89function _G . count_diagnostics ( bufnr , severity , namespace ) 90return # vim . diagnostic . get ( bufnr , { severity =severity , namespace =namespace } ) 91end 92 93function _G . count_extmarks ( bufnr , namespace ) 94local ns =vim . diagnostic . get_namespace ( namespace ) 95local extmarks =0 96if ns . user_data . virt_text_ns then 97extmarks =extmarks 98+ # vim . api . nvim_buf_get_extmarks ( bufnr , ns . user_data . virt_text_ns , 0 , - 1 , {} ) 99end 100if ns . user_data . underline_ns then 101extmarks =extmarks 102+ # vim . api . nvim_buf_get_extmarks ( bufnr , ns . user_data . underline_ns , 0 , - 1 , {} ) 103end 104return extmarks 105end 106 107function _G . get_virt_text_extmarks ( ns ) 108ns =vim . diagnostic . get_namespace ( ns ) 109local virt_text_ns =ns . user_data . virt_text_ns 110return vim . api . nvim_buf_get_extmarks ( 111_G . diagnostic_bufnr , 112virt_text_ns , 1130 , 114- 1 , 115{ details =true } 116) 117end 118 119function _G . get_virt_lines_extmarks ( ns ) 120ns =vim . diagnostic . get_namespace ( ns ) 121local virt_lines_ns =ns . user_data . virt_lines_ns 122return vim . api . nvim_buf_get_extmarks ( 123_G . diagnostic_bufnr , 124virt_lines_ns , 1250 , 126- 1 , 127{ details =true } 128) 129end 130 131---@param ns integer 132function _G . get_underline_extmarks ( ns ) 133---@type integer 134local underline_ns =vim . diagnostic . get_namespace ( ns ). user_data . underline_ns 135return vim . api . nvim_buf_get_extmarks ( 136_G . diagnostic_bufnr , 137underline_ns , 1380 , 139- 1 , 140{ details =true } 141) 142end 143end ) 144 145exec_lua ( function () 146_G . diagnostic_ns =vim . api . nvim_create_namespace ( 'diagnostic_spec' ) 147_G . other_ns =vim . api . nvim_create_namespace ( 'other_namespace' ) 148_G . diagnostic_bufnr =vim . api . nvim_create_buf ( true , false ) 149local lines ={ '1st line of text' , '2nd line of text' , 'wow' , 'cool' , 'more' , 'lines' } 150vim . fn . bufload ( _G . diagnostic_bufnr ) 151vim . api . nvim_buf_set_lines ( _G . diagnostic_bufnr , 0 , 1 , false , lines ) 152end ) 153end ) 154 155it ( 'creates highlight groups' , function () 156command ( 'runtime plugin/diagnostic.vim' ) 157eq ( { 158'DiagnosticDeprecated' , 159'DiagnosticError' , 160'DiagnosticFloatingError' , 161'DiagnosticFloatingHint' , 162'DiagnosticFloatingInfo' , 163'DiagnosticFloatingOk' , 164'DiagnosticFloatingWarn' , 165'DiagnosticHint' , 166'DiagnosticInfo' , 167'DiagnosticOk' , 168'DiagnosticSignError' , 169'DiagnosticSignHint' , 170'DiagnosticSignInfo' , 171'DiagnosticSignOk' , 172'DiagnosticSignWarn' , 173'DiagnosticUnderlineError' , 174'DiagnosticUnderlineHint' , 175'DiagnosticUnderlineInfo' , 176'DiagnosticUnderlineOk' , 177'DiagnosticUnderlineWarn' , 178'DiagnosticUnnecessary' , 179'DiagnosticVirtualLinesError' , 180'DiagnosticVirtualLinesHint' , 181'DiagnosticVirtualLinesInfo' , 182'DiagnosticVirtualLinesOk' , 183'DiagnosticVirtualLinesWarn' , 184'DiagnosticVirtualTextError' , 185'DiagnosticVirtualTextHint' , 186'DiagnosticVirtualTextInfo' , 187'DiagnosticVirtualTextOk' , 188'DiagnosticVirtualTextWarn' , 189'DiagnosticWarn' , 190} , fn . getcompletion ( 'Diagnostic' , 'highlight' )) 191end ) 192 193it ( 'retrieves diagnostics from all buffers and namespaces' , function () 194local result =exec_lua ( function () 195local other_bufnr =vim . api . nvim_create_buf ( true , false ) 196local lines =vim . api . nvim_buf_get_lines ( _G . diagnostic_bufnr , 0 , - 1 , true ) 197vim . api . nvim_buf_set_lines ( other_bufnr , 0 , 1 , false , lines ) 198 199vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 200_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 201_G . make_error ( 'Diagnostic #2' , 2 , 1 , 2 , 1 ), 202} ) 203vim . diagnostic . set ( _G . other_ns , other_bufnr , { 204_G . make_error ( 'Diagnostic #3' , 3 , 1 , 3 , 1 ), 205} ) 206return vim . diagnostic . get () 207end ) 208eq ( 3 , # result ) 209eq ( 2102 , 211exec_lua ( function () 212return # vim . tbl_filter ( function ( d ) 213return d . bufnr == _G . diagnostic_bufnr 214end , result ) 215end ) 216) 217eq ( 'Diagnostic #1' , result [ 1 ]. message ) 218end ) 219 220it ( 'removes diagnostics from the cache when a buffer is removed' , function () 221eq ( 2222 , 223exec_lua ( function () 224vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 225local other_bufnr =vim . fn . bufadd ( 'test | test' ) 226local lines =vim . api . nvim_buf_get_lines ( _G . diagnostic_bufnr , 0 , - 1 , true ) 227vim . api . nvim_buf_set_lines ( other_bufnr , 0 , 1 , false , lines ) 228vim . cmd ( 'bunload! ' .. other_bufnr ) 229 230vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 231_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 232_G . make_error ( 'Diagnostic #2' , 2 , 1 , 2 , 1 ), 233} ) 234vim . diagnostic . set ( _G . diagnostic_ns , other_bufnr , { 235_G . make_error ( 'Diagnostic #3' , 3 , 1 , 3 , 1 ), 236} ) 237vim . api . nvim_set_current_buf ( other_bufnr ) 238vim . opt_local . buflisted =true 239vim . cmd ( 'bwipeout!' ) 240return # vim . diagnostic . get () 241end ) 242) 243eq ( 2442 , 245exec_lua ( function () 246vim . api . nvim_set_current_buf ( _G . diagnostic_bufnr ) 247vim . opt_local . buflisted =false 248return # vim . diagnostic . get () 249end ) 250) 251eq ( 2520 , 253exec_lua ( function () 254vim . cmd ( 'bwipeout!' ) 255return # vim . diagnostic . get () 256end ) 257) 258end ) 259 260it ( 'removes diagnostic from stale cache on reset' , function () 261local diagnostics =exec_lua ( function () 262vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 263_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 264_G . make_error ( 'Diagnostic #2' , 2 , 1 , 2 , 1 ), 265} ) 266vim . fn . bufadd ( 'test | test' ) 267vim . cmd ( 'noautocmd bwipeout! ' .. _G . diagnostic_bufnr ) 268return vim . diagnostic . get ( _G . diagnostic_bufnr ) 269end ) 270eq ( 2 , # diagnostics ) 271diagnostics =exec_lua ( function () 272vim . diagnostic . reset () 273return vim . diagnostic . get () 274end ) 275eq ( 0 , # diagnostics ) 276end ) 277 278it ( 'always returns a copy of diagnostic tables' , function () 279local result =exec_lua ( function () 280vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 281_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 282} ) 283local diag =vim . diagnostic . get () 284diag [ 1 ]. col =10000 285return vim . diagnostic . get ()[ 1 ]. col == 10000 286end ) 287eq ( false , result ) 288end ) 289 290it ( 'resolves buffer number 0 to the current buffer' , function () 291eq ( 2922 , 293exec_lua ( function () 294vim . api . nvim_set_current_buf ( _G . diagnostic_bufnr ) 295vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 296_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 297_G . make_error ( 'Diagnostic #2' , 2 , 1 , 2 , 1 ), 298} ) 299return # vim . diagnostic . get ( 0 ) 300end ) 301) 302end ) 303 304it ( 'saves and count a single error' , function () 305eq ( 3061 , 307exec_lua ( function () 308vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 309_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 310} ) 311return _G . count_diagnostics ( 312_G . diagnostic_bufnr , 313vim . diagnostic . severity . ERROR , 314_G . diagnostic_ns 315) 316end ) 317) 318end ) 319 320it ( 'saves and count multiple errors' , function () 321eq ( 3222 , 323exec_lua ( function () 324vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 325_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 326_G . make_error ( 'Diagnostic #2' , 2 , 1 , 2 , 1 ), 327} ) 328return _G . count_diagnostics ( 329_G . diagnostic_bufnr , 330vim . diagnostic . severity . ERROR , 331_G . diagnostic_ns 332) 333end ) 334) 335end ) 336 337it ( 'saves and count from multiple namespaces' , function () 338eq ( 339{ 1 , 1 , 2 } , 340exec_lua ( function () 341vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 342_G . make_error ( 'Diagnostic From Server 1' , 1 , 1 , 1 , 1 ), 343} ) 344vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , { 345_G . make_error ( 'Diagnostic From Server 2' , 1 , 1 , 1 , 1 ), 346} ) 347return { 348-- First namespace 349_G . count_diagnostics ( 350_G . diagnostic_bufnr , 351vim . diagnostic . severity . ERROR , 352_G . diagnostic_ns 353), 354-- Second namespace 355_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . ERROR , _G . other_ns ), 356-- All namespaces 357_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . ERROR ), 358} 359end ) 360) 361end ) 362 363it ( 'saves and count from multiple namespaces with respect to severity' , function () 364eq ( 365{ 3 , 0 , 3 } , 366exec_lua ( function () 367vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 368_G . make_error ( 'Diagnostic From Server 1:1' , 1 , 1 , 1 , 1 ), 369_G . make_error ( 'Diagnostic From Server 1:2' , 2 , 2 , 2 , 2 ), 370_G . make_error ( 'Diagnostic From Server 1:3' , 2 , 3 , 3 , 2 ), 371} ) 372vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , { 373_G . make_warning ( 'Warning From Server 2' , 3 , 3 , 3 , 3 ), 374} ) 375return { 376-- Namespace 1 377_G . count_diagnostics ( 378_G . diagnostic_bufnr , 379vim . diagnostic . severity . ERROR , 380_G . diagnostic_ns 381), 382-- Namespace 2 383_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . ERROR , _G . other_ns ), 384-- All namespaces 385_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . ERROR ), 386} 387end ) 388) 389end ) 390 391it ( 'handles one namespace clearing highlights while the other still has highlights' , function () 392exec_lua ( function () 393vim . diagnostic . config ( { virtual_text =true } ) 394end ) 395-- 1 Error (1) 396-- 1 Warning (2) 397-- 1 Warning (2) + 1 Warning (1) 398-- 2 highlights and 2 underlines (since error) 399-- 1 highlight + 1 underline 400local all_highlights ={ 1 , 1 , 2 , 4 , 2 } 401eq ( 402all_highlights , 403exec_lua ( function () 404local ns_1_diags ={ 405_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 406_G . make_warning ( 'Warning on Server 1' , 2 , 1 , 2 , 3 ), 407} 408local ns_2_diags ={ 409_G . make_warning ( 'Warning 1' , 2 , 1 , 2 , 3 ), 410} 411 412vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diags ) 413vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diags ) 414 415return { 416_G . count_diagnostics ( 417_G . diagnostic_bufnr , 418vim . diagnostic . severity . ERROR , 419_G . diagnostic_ns 420), 421_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN , _G . other_ns ), 422_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN ), 423_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ), 424_G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ), 425} 426end ) 427) 428 429-- Clear diagnostics from namespace 1, and make sure we have the right amount of stuff for namespace 2 430eq ( 431{ 1 , 1 , 2 , 0 , 2 } , 432exec_lua ( function () 433vim . diagnostic . enable ( false , { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } ) 434return { 435_G . count_diagnostics ( 436_G . diagnostic_bufnr , 437vim . diagnostic . severity . ERROR , 438_G . diagnostic_ns 439), 440_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN , _G . other_ns ), 441_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN ), 442_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ), 443_G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ), 444} 445end ) 446) 447 448-- Show diagnostics from namespace 1 again 449eq ( 450all_highlights , 451exec_lua ( function () 452vim . diagnostic . enable ( true , { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } ) 453return { 454_G . count_diagnostics ( 455_G . diagnostic_bufnr , 456vim . diagnostic . severity . ERROR , 457_G . diagnostic_ns 458), 459_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN , _G . other_ns ), 460_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN ), 461_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ), 462_G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ), 463} 464end ) 465) 466end ) 467 468it ( 'does not display diagnostics when disabled' , function () 469exec_lua ( function () 470vim . diagnostic . config ( { virtual_text =true } ) 471end ) 472 473eq ( 474{ 0 , 2 } , 475exec_lua ( function () 476local ns_1_diags ={ 477_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 478_G . make_warning ( 'Warning on Server 1' , 2 , 1 , 2 , 3 ), 479} 480local ns_2_diags ={ 481_G . make_warning ( 'Warning 1' , 2 , 1 , 2 , 3 ), 482} 483 484vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diags ) 485vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diags ) 486 487vim . diagnostic . enable ( false , { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } ) 488 489return { 490_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ), 491_G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ), 492} 493end ) 494) 495 496eq ( 497{ 4 , 0 } , 498exec_lua ( function () 499vim . diagnostic . enable ( true , { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } ) 500vim . diagnostic . enable ( false , { bufnr =_G . diagnostic_bufnr , ns_id =_G . other_ns } ) 501 502return { 503_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ), 504_G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ), 505} 506end ) 507) 508end ) 509 510describe ( 'show() and hide()' , function () 511it ( 'works' , function () 512local result =exec_lua ( function () 513local other_bufnr =vim . api . nvim_create_buf ( true , false ) 514 515vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 516 517local result ={} 518 519vim . diagnostic . config ( { underline =false , virtual_text =true } ) 520 521local ns_1_diags ={ 522_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 523_G . make_warning ( 'Warning on Server 1' , 2 , 1 , 2 , 5 ), 524} 525local ns_2_diags ={ 526_G . make_warning ( 'Warning 1' , 2 , 1 , 2 , 5 ), 527} 528local other_buffer_diags ={ 529_G . make_info ( 'This is interesting' , 0 , 0 , 0 , 0 ), 530} 531 532vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diags ) 533vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diags ) 534vim . diagnostic . set ( _G . diagnostic_ns , other_bufnr , other_buffer_diags ) 535 536-- All buffers and namespaces 537table . insert ( 538result , 539_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 540+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 541+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 542) 543 544-- Hide one namespace 545vim . diagnostic . hide ( _G . diagnostic_ns ) 546table . insert ( 547result , 548_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 549+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 550+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 551) 552 553-- Show one namespace 554vim . diagnostic . show ( _G . diagnostic_ns ) 555table . insert ( 556result , 557_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 558+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 559+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 560) 561 562-- Hide one buffer 563vim . diagnostic . hide ( nil , other_bufnr ) 564table . insert ( 565result , 566_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 567+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 568+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 569) 570 571-- Hide everything 572vim . diagnostic . hide () 573table . insert ( 574result , 575_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 576+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 577+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 578) 579 580-- Show one buffer 581vim . diagnostic . show ( nil , _G . diagnostic_bufnr ) 582table . insert ( 583result , 584_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 585+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 586+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 587) 588 589return result 590end ) 591 592eq ( 4 , result [ 1 ]) 593eq ( 1 , result [ 2 ]) 594eq ( 4 , result [ 3 ]) 595eq ( 3 , result [ 4 ]) 596eq ( 0 , result [ 5 ]) 597eq ( 3 , result [ 6 ]) 598end ) 599 600it ( "doesn't error after bwipeout on buffer" , function () 601exec_lua ( function () 602vim . diagnostic . set ( 603_G . diagnostic_ns , 604_G . diagnostic_bufnr , 605{ { message ='' , lnum =0 , end_lnum =0 , col =0 , end_col =0 } } 606) 607vim . cmd ( 'bwipeout! ' .. _G . diagnostic_bufnr ) 608 609vim . diagnostic . show ( _G . diagnostic_ns ) 610vim . diagnostic . hide ( _G . diagnostic_ns ) 611end ) 612end ) 613 614it ( 'handles diagnostics without extmark_id' , function () 615exec_lua ( function () 616vim . diagnostic . config ( { virtual_text =true } ) 617 618vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 619_G . make_error ( 'Error message' , 0 , 0 , 0 , 5 ), 620} ) 621 622local diags =vim . diagnostic . get ( _G . diagnostic_bufnr , { namespace =_G . diagnostic_ns } ) 623diags [ 1 ]. _extmark_id =nil 624 625vim . diagnostic . show ( _G . diagnostic_ns , _G . diagnostic_bufnr , diags ) 626end ) 627end ) 628end ) 629 630describe ( 'enable() and disable()' , function () 631it ( 'validation' , function () 632matches ( 'expected boolean, got table' , pcall_err ( exec_lua , [[vim.diagnostic.enable({})]] )) 633matches ( 634'filter: expected table, got string' , 635pcall_err ( exec_lua , [[vim.diagnostic.enable(false, '')]] ) 636) 637matches ( 638'Invalid buffer id: 42' , 639pcall_err ( exec_lua , [[vim.diagnostic.enable(true, { bufnr = 42 })]] ) 640) 641matches ( 642'expected boolean, got number' , 643pcall_err ( exec_lua , [[vim.diagnostic.enable(42, {})]] ) 644) 645matches ( 'expected boolean, got table' , pcall_err ( exec_lua , [[vim.diagnostic.enable({}, 42)]] )) 646end ) 647 648it ( 'without arguments' , function () 649local result =exec_lua ( function () 650vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 651 652local result ={} 653 654vim . diagnostic . config ( { underline =false , virtual_text =true } ) 655 656local ns_1_diags ={ 657_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 658_G . make_warning ( 'Warning on Server 1' , 2 , 1 , 2 , 5 ), 659} 660local ns_2_diags ={ 661_G . make_warning ( 'Warning 1' , 2 , 1 , 2 , 5 ), 662} 663 664vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diags ) 665vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diags ) 666 667table . insert ( 668result , 669_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 670+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 671) 672 673vim . diagnostic . enable ( false ) 674 675table . insert ( 676result , 677_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 678+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 679) 680 681-- Create a new buffer 682local other_bufnr =vim . api . nvim_create_buf ( true , false ) 683local other_buffer_diags ={ 684_G . make_info ( 'This is interesting' , 0 , 0 , 0 , 0 ), 685} 686 687vim . diagnostic . set ( _G . diagnostic_ns , other_bufnr , other_buffer_diags ) 688 689table . insert ( 690result , 691_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 692+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 693+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 694) 695 696vim . diagnostic . enable () 697 698table . insert ( 699result , 700_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 701+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 702+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 703) 704 705return result 706end ) 707 708eq ( 3 , result [ 1 ]) 709eq ( 0 , result [ 2 ]) 710eq ( 0 , result [ 3 ]) 711eq ( 4 , result [ 4 ]) 712end ) 713 714it ( 'with buffer argument' , function () 715local result =exec_lua ( function () 716local other_bufnr =vim . api . nvim_create_buf ( true , false ) 717 718vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 719 720local result ={} 721 722vim . diagnostic . config ( { underline =false , virtual_text =true } ) 723 724local ns_1_diags ={ 725_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 726_G . make_warning ( 'Warning on Server 1' , 2 , 1 , 2 , 5 ), 727} 728local ns_2_diags ={ 729_G . make_warning ( 'Warning 1' , 2 , 1 , 2 , 5 ), 730} 731local other_buffer_diags ={ 732_G . make_info ( 'This is interesting' , 0 , 0 , 0 , 0 ), 733} 734 735vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diags ) 736vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diags ) 737vim . diagnostic . set ( _G . diagnostic_ns , other_bufnr , other_buffer_diags ) 738 739table . insert ( 740result , 741_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 742+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 743+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 744) 745 746vim . diagnostic . enable ( false , { bufnr =_G . diagnostic_bufnr } ) 747 748table . insert ( 749result , 750_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 751+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 752+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 753) 754 755vim . diagnostic . enable ( true , { bufnr =_G . diagnostic_bufnr } ) 756 757table . insert ( 758result , 759_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 760+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 761+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 762) 763 764vim . diagnostic . enable ( false , { bufnr =other_bufnr } ) 765 766table . insert ( 767result , 768_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 769+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 770+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 771) 772 773return result 774end ) 775 776eq ( 4 , result [ 1 ]) 777eq ( 1 , result [ 2 ]) 778eq ( 4 , result [ 3 ]) 779eq ( 3 , result [ 4 ]) 780end ) 781 782it ( 'with a namespace argument' , function () 783local result =exec_lua ( function () 784vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 785 786local result ={} 787 788vim . diagnostic . config ( { underline =false , virtual_text =true } ) 789 790local ns_1_diags ={ 791_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 792_G . make_warning ( 'Warning on Server 1' , 2 , 1 , 2 , 5 ), 793} 794local ns_2_diags ={ 795_G . make_warning ( 'Warning 1' , 2 , 1 , 2 , 5 ), 796} 797 798vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diags ) 799vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diags ) 800 801table . insert ( 802result , 803_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 804+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 805) 806 807vim . diagnostic . enable ( false , { ns_id =_G . diagnostic_ns } ) 808 809table . insert ( 810result , 811_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 812+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 813) 814 815vim . diagnostic . enable ( true , { ns_id =_G . diagnostic_ns } ) 816 817table . insert ( 818result , 819_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 820+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 821) 822 823vim . diagnostic . enable ( false , { ns_id =_G . other_ns } ) 824 825table . insert ( 826result , 827_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 828+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 829) 830 831return result 832end ) 833 834eq ( 3 , result [ 1 ]) 835eq ( 1 , result [ 2 ]) 836eq ( 3 , result [ 3 ]) 837eq ( 2 , result [ 4 ]) 838end ) 839 840--- @return table 841local function test_enable () 842return exec_lua ( function () 843local other_bufnr =vim . api . nvim_create_buf ( true , false ) 844 845vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 846 847local result ={} 848 849vim . diagnostic . config ( { underline =false , virtual_text =true } ) 850 851local ns_1_diags ={ 852_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 853_G . make_warning ( 'Warning on Server 1' , 2 , 1 , 2 , 5 ), 854} 855local ns_2_diags ={ 856_G . make_warning ( 'Warning 1' , 2 , 1 , 2 , 5 ), 857} 858local other_buffer_diags ={ 859_G . make_info ( 'This is interesting' , 0 , 0 , 0 , 0 ), 860} 861 862vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diags ) 863vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diags ) 864vim . diagnostic . set ( _G . diagnostic_ns , other_bufnr , other_buffer_diags ) 865 866table . insert ( 867result , 868_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 869+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 870+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 871) 872 873vim . diagnostic . enable ( false , { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } ) 874 875table . insert ( 876result , 877_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 878+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 879+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 880) 881 882vim . diagnostic . enable ( false , { bufnr =_G . diagnostic_bufnr , ns_id =_G . other_ns } ) 883 884table . insert ( 885result , 886_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 887+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 888+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 889) 890 891vim . diagnostic . enable ( true , { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } ) 892 893table . insert ( 894result , 895_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 896+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 897+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 898) 899 900-- Should have no effect 901vim . diagnostic . enable ( false , { bufnr =other_bufnr , ns_id =_G . other_ns } ) 902 903table . insert ( 904result , 905_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 906+ _G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ) 907+ _G . count_extmarks ( other_bufnr , _G . diagnostic_ns ) 908) 909 910return result 911end ) 912end 913 914it ( 'with both buffer and namespace arguments' , function () 915local result =test_enable () 916eq ( 4 , result [ 1 ]) 917eq ( 2 , result [ 2 ]) 918eq ( 1 , result [ 3 ]) 919eq ( 3 , result [ 4 ]) 920eq ( 3 , result [ 5 ]) 921end ) 922end ) 923 924describe ( 'reset()' , function () 925it ( 'diagnostic count is 0 and displayed diagnostics are 0 after call' , function () 926exec_lua ( function () 927vim . diagnostic . config ( { virtual_text =true } ) 928end ) 929 930-- 1 Error (1) 931-- 1 Warning (2) 932-- 1 Warning (2) + 1 Warning (1) 933-- 2 highlights and 2 underlines (since error) 934-- 1 highlight + 1 underline 935local all_highlights ={ 1 , 1 , 2 , 4 , 2 } 936eq ( 937all_highlights , 938exec_lua ( function () 939local ns_1_diags ={ 940_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 941_G . make_warning ( 'Warning on Server 1' , 2 , 1 , 2 , 3 ), 942} 943local ns_2_diags ={ 944_G . make_warning ( 'Warning 1' , 2 , 1 , 2 , 3 ), 945} 946 947vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diags ) 948vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diags ) 949 950return { 951_G . count_diagnostics ( 952_G . diagnostic_bufnr , 953vim . diagnostic . severity . ERROR , 954_G . diagnostic_ns 955), 956_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN , _G . other_ns ), 957_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN ), 958_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ), 959_G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ), 960} 961end ) 962) 963 964-- Reset diagnostics from namespace 1 965exec_lua ( [[ vim.diagnostic.reset( _G.diagnostic_ns) ]] ) 966 967-- Make sure we have the right diagnostic count 968eq ( 969{ 0 , 1 , 1 , 0 , 2 } , 970exec_lua ( function () 971local diagnostic_count ={} 972vim . wait ( 100 , function () 973diagnostic_count ={ 974_G . count_diagnostics ( 975_G . diagnostic_bufnr , 976vim . diagnostic . severity . ERROR , 977_G . diagnostic_ns 978), 979_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN , _G . other_ns ), 980_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN ), 981_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ), 982_G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ), 983} 984end ) 985return diagnostic_count 986end ) 987) 988 989-- Reset diagnostics from namespace 2 990exec_lua ( [[ vim.diagnostic.reset(_G.other_ns) ]] ) 991 992-- Make sure we have the right diagnostic count 993eq ( 994{ 0 , 0 , 0 , 0 , 0 } , 995exec_lua ( function () 996local diagnostic_count ={} 997vim . wait ( 100 , function () 998diagnostic_count ={ 999_G . count_diagnostics ( 1000_G . diagnostic_bufnr , 1001vim . diagnostic . severity . ERROR , 1002_G . diagnostic_ns 1003), 1004_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN , _G . other_ns ), 1005_G . count_diagnostics ( _G . diagnostic_bufnr , vim . diagnostic . severity . WARN ), 1006_G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ), 1007_G . count_extmarks ( _G . diagnostic_bufnr , _G . other_ns ), 1008} 1009end ) 1010return diagnostic_count 1011end ) 1012) 1013end ) 1014 1015it ( "doesn't error after bwipeout called on buffer" , function () 1016exec_lua ( function () 1017vim . diagnostic . set ( 1018_G . diagnostic_ns , 1019_G . diagnostic_bufnr , 1020{ { message ='' , lnum =0 , end_lnum =0 , col =0 , end_col =0 } } 1021) 1022vim . cmd ( 'bwipeout! ' .. _G . diagnostic_bufnr ) 1023 1024vim . diagnostic . reset ( _G . diagnostic_ns ) 1025end ) 1026end ) 1027end ) 1028 1029describe ( 'get_next()' , function () 1030it ( 'can find the next pos with only one namespace' , function () 1031eq ( 1032{ 1 , 1 } , 1033exec_lua ( function () 1034vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1035_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 1036} ) 1037vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1038local next =vim . diagnostic . get_next () 1039return { next . lnum , next . col } 1040end ) 1041) 1042end ) 1043 1044it ( 'can find next pos with two errors' , function () 1045eq ( 1046{ 4 , 4 } , 1047exec_lua ( function () 1048vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1049_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 1050_G . make_error ( 'Diagnostic #2' , 4 , 4 , 4 , 4 ), 1051} ) 1052vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1053vim . api . nvim_win_set_cursor ( 0 , { 3 , 1 } ) 1054local next =vim . diagnostic . get_next ( { namespace =_G . diagnostic_ns } ) 1055return { next . lnum , next . col } 1056end ) 1057) 1058end ) 1059 1060it ( 'can cycle when position is past error' , function () 1061eq ( 1062{ 1 , 1 } , 1063exec_lua ( function () 1064vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1065_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 1066} ) 1067vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1068vim . api . nvim_win_set_cursor ( 0 , { 3 , 1 } ) 1069local next =vim . diagnostic . get_next ( { namespace =_G . diagnostic_ns } ) 1070return { next . lnum , next . col } 1071end ) 1072) 1073end ) 1074 1075it ( 'will not cycle when wrap is off' , function () 1076eq ( 1077nil , 1078exec_lua ( function () 1079vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1080_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 1081} ) 1082vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1083vim . api . nvim_win_set_cursor ( 0 , { 3 , 1 } ) 1084local next =vim . diagnostic . get_next ( { namespace =_G . diagnostic_ns , wrap =false } ) 1085return next 1086end ) 1087) 1088end ) 1089 1090it ( 'can cycle even from the last line' , function () 1091eq ( 1092{ 4 , 4 } , 1093exec_lua ( function () 1094vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1095_G . make_error ( 'Diagnostic #2' , 4 , 4 , 4 , 4 ), 1096} ) 1097vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1098vim . api . nvim_win_set_cursor ( 0 , { vim . api . nvim_buf_line_count ( 0 ), 1 } ) 1099local prev =vim . diagnostic . get_prev ( { namespace =_G . diagnostic_ns } ) 1100return { prev . lnum , prev . col } 1101end ) 1102) 1103end ) 1104 1105it ( 'works with diagnostics past the end of the line #16349' , function () 1106eq ( 1107{ 4 , 0 } , 1108exec_lua ( function () 1109vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1110_G . make_error ( 'Diagnostic #1' , 3 , 9001 , 3 , 9001 ), 1111_G . make_error ( 'Diagnostic #2' , 4 , 0 , 4 , 0 ), 1112} ) 1113vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1114vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 1115vim . diagnostic . jump ( { count =1 } ) 1116local next =vim . diagnostic . get_next ( { namespace =_G . diagnostic_ns } ) 1117return { next . lnum , next . col } 1118end ) 1119) 1120end ) 1121 1122it ( 'works with diagnostics before the start of the line' , function () 1123eq ( 1124{ 4 , 0 } , 1125exec_lua ( function () 1126vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1127_G . make_error ( 'Diagnostic #1' , 3 , 9001 , 3 , 9001 ), 1128_G . make_error ( 'Diagnostic #2' , 4 , - 1 , 4 , - 1 ), 1129} ) 1130vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1131vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 1132vim . diagnostic . jump ( { count =1 } ) 1133local next =vim . diagnostic . get_next ( { namespace =_G . diagnostic_ns } ) 1134return { next . lnum , next . col } 1135end ) 1136) 1137end ) 1138 1139it ( 'jumps to diagnostic with highest severity' , function () 1140exec_lua ( function () 1141vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1142_G . make_info ( 'Info' , 1 , 0 , 1 , 1 ), 1143_G . make_error ( 'Error' , 2 , 0 , 2 , 1 ), 1144_G . make_warning ( 'Warning' , 3 , 0 , 3 , 1 ), 1145_G . make_error ( 'Error' , 4 , 0 , 4 , 1 ), 1146} ) 1147 1148vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1149vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1150end ) 1151 1152eq ( 1153{ 3 , 0 } , 1154exec_lua ( function () 1155vim . diagnostic . jump ( { count =1 , _highest =true } ) 1156return vim . api . nvim_win_get_cursor ( 0 ) 1157end ) 1158) 1159 1160eq ( 1161{ 5 , 0 } , 1162exec_lua ( function () 1163vim . diagnostic . jump ( { count =1 , _highest =true } ) 1164return vim . api . nvim_win_get_cursor ( 0 ) 1165end ) 1166) 1167 1168exec_lua ( function () 1169vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1170_G . make_info ( 'Info' , 1 , 0 , 1 , 1 ), 1171_G . make_hint ( 'Hint' , 2 , 0 , 2 , 1 ), 1172_G . make_warning ( 'Warning' , 3 , 0 , 3 , 1 ), 1173_G . make_hint ( 'Hint' , 4 , 0 , 4 , 1 ), 1174_G . make_warning ( 'Warning' , 5 , 0 , 5 , 1 ), 1175} ) 1176 1177vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1178vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1179end ) 1180 1181eq ( 1182{ 4 , 0 } , 1183exec_lua ( function () 1184vim . diagnostic . jump ( { count =1 , _highest =true } ) 1185return vim . api . nvim_win_get_cursor ( 0 ) 1186end ) 1187) 1188 1189eq ( 1190{ 6 , 0 } , 1191exec_lua ( function () 1192vim . diagnostic . jump ( { count =1 , _highest =true } ) 1193return vim . api . nvim_win_get_cursor ( 0 ) 1194end ) 1195) 1196end ) 1197 1198it ( 'jumps to next diagnostic if severity is non-nil' , function () 1199exec_lua ( function () 1200vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1201_G . make_info ( 'Info' , 1 , 0 , 1 , 1 ), 1202_G . make_error ( 'Error' , 2 , 0 , 2 , 1 ), 1203_G . make_warning ( 'Warning' , 3 , 0 , 3 , 1 ), 1204_G . make_error ( 'Error' , 4 , 0 , 4 , 1 ), 1205} ) 1206 1207vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1208vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1209end ) 1210 1211eq ( 1212{ 2 , 0 } , 1213exec_lua ( function () 1214vim . diagnostic . jump ( { count =1 } ) 1215return vim . api . nvim_win_get_cursor ( 0 ) 1216end ) 1217) 1218 1219eq ( 1220{ 3 , 0 } , 1221exec_lua ( function () 1222vim . diagnostic . jump ( { count =1 } ) 1223return vim . api . nvim_win_get_cursor ( 0 ) 1224end ) 1225) 1226 1227eq ( 1228{ 4 , 0 } , 1229exec_lua ( function () 1230vim . diagnostic . jump ( { count =1 } ) 1231return vim . api . nvim_win_get_cursor ( 0 ) 1232end ) 1233) 1234end ) 1235end ) 1236 1237describe ( 'get_prev()' , function () 1238it ( 'can find the previous diagnostic with only one namespace' , function () 1239eq ( 1240{ 1 , 1 } , 1241exec_lua ( function () 1242vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1243_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 1244} ) 1245vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1246vim . api . nvim_win_set_cursor ( 0 , { 3 , 1 } ) 1247local prev =vim . diagnostic . get_prev () 1248return { prev . lnum , prev . col } 1249end ) 1250) 1251end ) 1252 1253it ( 'can find the previous diagnostic with two errors' , function () 1254eq ( 1255{ 1 , 1 } , 1256exec_lua ( function () 1257vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1258_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 1259_G . make_error ( 'Diagnostic #2' , 4 , 4 , 4 , 4 ), 1260} ) 1261vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1262vim . api . nvim_win_set_cursor ( 0 , { 3 , 1 } ) 1263local prev =vim . diagnostic . get_prev ( { namespace =_G . diagnostic_ns } ) 1264return { prev . lnum , prev . col } 1265end ) 1266) 1267end ) 1268 1269it ( 'can cycle when position is past error' , function () 1270eq ( 1271{ 4 , 4 } , 1272exec_lua ( function () 1273vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1274_G . make_error ( 'Diagnostic #2' , 4 , 4 , 4 , 4 ), 1275} ) 1276vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1277vim . api . nvim_win_set_cursor ( 0 , { 3 , 1 } ) 1278local prev =vim . diagnostic . get_prev ( { namespace =_G . diagnostic_ns } ) 1279return { prev . lnum , prev . col } 1280end ) 1281) 1282end ) 1283 1284it ( 'respects wrap parameter' , function () 1285eq ( 1286nil , 1287exec_lua ( function () 1288vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1289_G . make_error ( 'Diagnostic #2' , 4 , 4 , 4 , 4 ), 1290} ) 1291vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1292vim . api . nvim_win_set_cursor ( 0 , { 3 , 1 } ) 1293local prev =vim . diagnostic . get_prev ( { namespace =_G . diagnostic_ns , wrap =false } ) 1294return prev 1295end ) 1296) 1297end ) 1298 1299it ( 'works on blank line #28397' , function () 1300eq ( 1301{ 0 , 2 } , 1302exec_lua ( function () 1303local test_bufnr =vim . api . nvim_create_buf ( true , false ) 1304vim . api . nvim_buf_set_lines ( test_bufnr , 0 , - 1 , false , { 1305'first line' , 1306'' , 1307'' , 1308'end line' , 1309} ) 1310vim . diagnostic . set ( _G . diagnostic_ns , test_bufnr , { 1311_G . make_info ( 'Diagnostic #1' , 0 , 2 , 0 , 2 ), 1312_G . make_info ( 'Diagnostic #2' , 2 , 0 , 2 , 0 ), 1313_G . make_info ( 'Diagnostic #3' , 2 , 0 , 2 , 0 ), 1314} ) 1315vim . api . nvim_win_set_buf ( 0 , test_bufnr ) 1316vim . api . nvim_win_set_cursor ( 0 , { 3 , 0 } ) 1317return vim . diagnostic . get_prev_pos { namespace =_G . diagnostic_ns } 1318end ) 1319) 1320end ) 1321end ) 1322 1323describe ( 'jump()' , function () 1324before_each ( function () 1325exec_lua ( function () 1326vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1327_G . make_error ( 'Diagnostic #1' , 0 , 0 , 0 , 2 ), 1328_G . make_error ( 'Diagnostic #2' , 1 , 1 , 1 , 4 ), 1329_G . make_warning ( 'Diagnostic #3' , 2 , - 1 , 2 , - 1 ), 1330_G . make_info ( 'Diagnostic #4' , 3 , 0 , 3 , 3 ), 1331} ) 1332vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 1333end ) 1334end ) 1335 1336it ( 'can move forward' , function () 1337eq ( 1338{ 2 , 1 } , 1339exec_lua ( function () 1340vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1341vim . diagnostic . jump ( { count =1 } ) 1342return vim . api . nvim_win_get_cursor ( 0 ) 1343end ) 1344) 1345 1346eq ( 1347{ 4 , 0 } , 1348exec_lua ( function () 1349vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1350vim . diagnostic . jump ( { count =3 } ) 1351return vim . api . nvim_win_get_cursor ( 0 ) 1352end ) 1353) 1354 1355eq ( 1356{ 4 , 0 } , 1357exec_lua ( function () 1358vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1359vim . diagnostic . jump ( { count =math . huge , wrap =false } ) 1360return vim . api . nvim_win_get_cursor ( 0 ) 1361end ) 1362) 1363end ) 1364 1365it ( 'can move backward' , function () 1366eq ( 1367{ 3 , 0 } , 1368exec_lua ( function () 1369vim . api . nvim_win_set_cursor ( 0 , { 4 , 0 } ) 1370vim . diagnostic . jump ( { count =- 1 } ) 1371return vim . api . nvim_win_get_cursor ( 0 ) 1372end ) 1373) 1374 1375eq ( 1376{ 1 , 0 } , 1377exec_lua ( function () 1378vim . api . nvim_win_set_cursor ( 0 , { 4 , 0 } ) 1379vim . diagnostic . jump ( { count =- 3 } ) 1380return vim . api . nvim_win_get_cursor ( 0 ) 1381end ) 1382) 1383 1384eq ( 1385{ 1 , 0 } , 1386exec_lua ( function () 1387vim . api . nvim_win_set_cursor ( 0 , { 4 , 0 } ) 1388vim . diagnostic . jump ( { count =- math . huge , wrap =false } ) 1389return vim . api . nvim_win_get_cursor ( 0 ) 1390end ) 1391) 1392end ) 1393 1394it ( 'can filter by severity' , function () 1395eq ( 1396{ 3 , 0 } , 1397exec_lua ( function () 1398vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1399vim . diagnostic . jump ( { count =1 , severity =vim . diagnostic . severity . WARN } ) 1400return vim . api . nvim_win_get_cursor ( 0 ) 1401end ) 1402) 1403 1404eq ( 1405{ 3 , 0 } , 1406exec_lua ( function () 1407vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1408vim . diagnostic . jump ( { count =9999 , severity =vim . diagnostic . severity . WARN } ) 1409return vim . api . nvim_win_get_cursor ( 0 ) 1410end ) 1411) 1412end ) 1413 1414it ( 'can wrap' , function () 1415eq ( 1416{ 1 , 0 } , 1417exec_lua ( function () 1418vim . api . nvim_win_set_cursor ( 0 , { 4 , 0 } ) 1419vim . diagnostic . jump ( { count =1 , wrap =true } ) 1420return vim . api . nvim_win_get_cursor ( 0 ) 1421end ) 1422) 1423 1424eq ( 1425{ 4 , 0 } , 1426exec_lua ( function () 1427vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1428vim . diagnostic . jump ( { count =- 1 , wrap =true } ) 1429return vim . api . nvim_win_get_cursor ( 0 ) 1430end ) 1431) 1432end ) 1433 1434it ( 'supports on_jump() handler' , function () 1435exec_lua ( function () 1436_G . jumped =false 1437 1438vim . diagnostic . jump ( { 1439count =1 , 1440on_jump =function () 1441_G . jumped =true 1442end , 1443} ) 1444end ) 1445 1446retry ( nil , nil , function () 1447eq ( true , exec_lua ( 'return _G.jumped' )) 1448end ) 1449end ) 1450 1451describe ( 'after inserting text before diagnostic position' , function () 1452before_each ( function () 1453api . nvim_buf_set_text ( 0 , 3 , 0 , 3 , 0 , { 'new line' , 'new ' } ) 1454end ) 1455 1456it ( 'finds next diagnostic at a logical location' , function () 1457eq ( 1458{ 5 , 4 } , 1459exec_lua ( function () 1460vim . api . nvim_win_set_cursor ( 0 , { 3 , 1 } ) 1461vim . diagnostic . jump ( { count =1 } ) 1462return vim . api . nvim_win_get_cursor ( 0 ) 1463end ) 1464) 1465end ) 1466 1467it ( 'finds previous diagnostic at a logical location' , function () 1468eq ( 1469{ 5 , 4 } , 1470exec_lua ( function () 1471vim . api . nvim_win_set_cursor ( 0 , { 6 , 4 } ) 1472vim . diagnostic . jump ( { count =- 1 } ) 1473return vim . api . nvim_win_get_cursor ( 0 ) 1474end ) 1475) 1476end ) 1477end ) 1478 1479describe ( 'if diagnostic is set after last character in line' , function () 1480before_each ( function () 1481exec_lua ( function () 1482vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1483_G . make_error ( 'Diagnostic #1' , 2 , 3 , 3 , 4 ), 1484} ) 1485end ) 1486end ) 1487 1488it ( 'finds next diagnostic at the end of the line' , function () 1489eq ( 1490{ 3 , 2 } , 1491exec_lua ( function () 1492vim . api . nvim_win_set_cursor ( 0 , { 3 , 0 } ) 1493vim . diagnostic . jump ( { count =1 } ) 1494return vim . api . nvim_win_get_cursor ( 0 ) 1495end ) 1496) 1497end ) 1498 1499it ( 'finds previous diagnostic at the end of the line' , function () 1500eq ( 1501{ 3 , 2 } , 1502exec_lua ( function () 1503vim . api . nvim_win_set_cursor ( 0 , { 4 , 2 } ) 1504vim . diagnostic . jump ( { count =- 1 } ) 1505return vim . api . nvim_win_get_cursor ( 0 ) 1506end ) 1507) 1508end ) 1509end ) 1510 1511describe ( 'after entire text range with a diagnostic was deleted' , function () 1512before_each ( function () 1513api . nvim_buf_set_text ( 0 , 1 , 1 , 1 , 4 , {} ) 1514end ) 1515 1516it ( 'does not find next diagnostic inside the deleted range' , function () 1517eq ( 1518{ 3 , 0 } , 1519exec_lua ( function () 1520vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 1521vim . diagnostic . jump ( { count =1 } ) 1522return vim . api . nvim_win_get_cursor ( 0 ) 1523end ) 1524) 1525end ) 1526 1527it ( 'does not find previous diagnostic inside the deleted range' , function () 1528eq ( 1529{ 1 , 0 } , 1530exec_lua ( function () 1531vim . api . nvim_win_set_cursor ( 0 , { 3 , 0 } ) 1532vim . diagnostic . jump ( { count =- 1 } ) 1533return vim . api . nvim_win_get_cursor ( 0 ) 1534end ) 1535) 1536end ) 1537end ) 1538end ) 1539 1540describe ( 'get()' , function () 1541it ( 'returns an empty table when no diagnostics are present' , function () 1542eq ( 1543{} , 1544exec_lua [[return vim.diagnostic.get( _G.diagnostic_bufnr, {namespace=diagnostic_ns})]] 1545) 1546end ) 1547 1548it ( 'returns all diagnostics when no severity is supplied' , function () 1549eq ( 15502 , 1551exec_lua ( function () 1552vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1553_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 1554_G . make_warning ( 'Warning on Server 1' , 1 , 1 , 2 , 3 ), 1555} ) 1556 1557return # vim . diagnostic . get ( _G . diagnostic_bufnr ) 1558end ) 1559) 1560end ) 1561 1562it ( 'returns only requested diagnostics when severity range is supplied' , function () 1563eq ( 1564{ 2 , 3 , 2 } , 1565exec_lua ( function () 1566vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1567_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 1568_G . make_warning ( 'Warning on Server 1' , 1 , 1 , 2 , 3 ), 1569_G . make_info ( 'Ignored information' , 1 , 1 , 2 , 3 ), 1570_G . make_hint ( "Here's a hint" , 1 , 1 , 2 , 3 ), 1571} ) 1572 1573return { 1574# vim . diagnostic . get ( 1575_G . diagnostic_bufnr , 1576{ severity ={ min =vim . diagnostic . severity . WARN } } 1577), 1578# vim . diagnostic . get ( 1579_G . diagnostic_bufnr , 1580{ severity ={ max =vim . diagnostic . severity . WARN } } 1581), 1582# vim . diagnostic . get ( _G . diagnostic_bufnr , { 1583severity ={ 1584min =vim . diagnostic . severity . INFO , 1585max =vim . diagnostic . severity . WARN , 1586} , 1587} ), 1588} 1589end ) 1590) 1591end ) 1592 1593it ( 'returns only requested diagnostics when severities are supplied' , function () 1594eq ( 1595{ 1 , 1 , 2 } , 1596exec_lua ( function () 1597vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1598_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 1599_G . make_warning ( 'Warning on Server 1' , 1 , 1 , 2 , 3 ), 1600_G . make_info ( 'Ignored information' , 1 , 1 , 2 , 3 ), 1601_G . make_hint ( "Here's a hint" , 1 , 1 , 2 , 3 ), 1602} ) 1603 1604return { 1605# vim . diagnostic . get ( 1606_G . diagnostic_bufnr , 1607{ severity ={ vim . diagnostic . severity . WARN } } 1608), 1609# vim . diagnostic . get ( 1610_G . diagnostic_bufnr , 1611{ severity ={ vim . diagnostic . severity . ERROR } } 1612), 1613# vim . diagnostic . get ( _G . diagnostic_bufnr , { 1614severity ={ 1615vim . diagnostic . severity . INFO , 1616vim . diagnostic . severity . WARN , 1617} , 1618} ), 1619} 1620end ) 1621) 1622end ) 1623 1624it ( 'allows filtering by line' , function () 1625eq ( 16262 , 1627exec_lua ( function () 1628vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1629_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 1630_G . make_warning ( 'Warning on Server 1' , 1 , 1 , 2 , 3 ), 1631_G . make_info ( 'Ignored information' , 1 , 1 , 2 , 3 ), 1632_G . make_error ( 'Error On Other Line' , 3 , 1 , 3 , 5 ), 1633} ) 1634 1635return # vim . diagnostic . get ( _G . diagnostic_bufnr , { lnum =2 } ) 1636end ) 1637) 1638end ) 1639 1640it ( 'allows filtering by enablement' , function () 1641eq ( 1642{ 3 , 1 , 2 } , 1643exec_lua ( function () 1644vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1645_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 1646} ) 1647vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , { 1648_G . make_error ( 'Error 2' , 1 , 1 , 1 , 5 ), 1649_G . make_error ( 'Error 3' , 3 , 1 , 3 , 5 ), 1650} ) 1651 1652vim . diagnostic . enable ( false , { ns_id =_G . other_ns } ) 1653 1654return { 1655# vim . diagnostic . get ( _G . diagnostic_bufnr ), 1656# vim . diagnostic . get ( _G . diagnostic_bufnr , { enabled =true } ), 1657# vim . diagnostic . get ( _G . diagnostic_bufnr , { enabled =false } ), 1658} 1659end ) 1660) 1661end ) 1662end ) 1663 1664describe ( 'count' , function () 1665it ( 'returns actually present severity counts' , function () 1666eq ( 1667exec_lua ( function () 1668return { 1669[ vim . diagnostic . severity . ERROR ] =4 , 1670[ vim . diagnostic . severity . WARN ] =3 , 1671[ vim . diagnostic . severity . INFO ] =2 , 1672[ vim . diagnostic . severity . HINT ] =1 , 1673} 1674end ), 1675exec_lua ( function () 1676vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1677_G . make_error ( 'Error 1' , 1 , 1 , 1 , 2 ), 1678_G . make_error ( 'Error 2' , 1 , 3 , 1 , 4 ), 1679_G . make_error ( 'Error 3' , 1 , 5 , 1 , 6 ), 1680_G . make_error ( 'Error 4' , 1 , 7 , 1 , 8 ), 1681_G . make_warning ( 'Warning 1' , 2 , 1 , 2 , 2 ), 1682_G . make_warning ( 'Warning 2' , 2 , 3 , 2 , 4 ), 1683_G . make_warning ( 'Warning 3' , 2 , 5 , 2 , 6 ), 1684_G . make_info ( 'Info 1' , 3 , 1 , 3 , 2 ), 1685_G . make_info ( 'Info 2' , 3 , 3 , 3 , 4 ), 1686_G . make_hint ( 'Hint 1' , 4 , 1 , 4 , 2 ), 1687} ) 1688return vim . diagnostic . count ( _G . diagnostic_bufnr ) 1689end ) 1690) 1691eq ( 1692exec_lua ( function () 1693return { 1694[ vim . diagnostic . severity . ERROR ] =2 , 1695[ vim . diagnostic . severity . INFO ] =1 , 1696} 1697end ), 1698exec_lua ( function () 1699vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1700_G . make_error ( 'Error 1' , 1 , 1 , 1 , 2 ), 1701_G . make_error ( 'Error 2' , 1 , 3 , 1 , 4 ), 1702_G . make_info ( 'Info 1' , 3 , 1 , 3 , 2 ), 1703} ) 1704return vim . diagnostic . count ( _G . diagnostic_bufnr ) 1705end ) 1706) 1707end ) 1708 1709it ( 'returns only requested diagnostics count when severity range is supplied' , function () 1710eq ( 1711exec_lua ( function () 1712return { 1713{ [ vim . diagnostic . severity . ERROR ] =1 , [ vim . diagnostic . severity . WARN ] =1 } , 1714{ 1715[ vim . diagnostic . severity . WARN ] =1 , 1716[ vim . diagnostic . severity . INFO ] =1 , 1717[ vim . diagnostic . severity . HINT ] =1 , 1718} , 1719{ [ vim . diagnostic . severity . WARN ] =1 , [ vim . diagnostic . severity . INFO ] =1 } , 1720} 1721end ), 1722exec_lua ( function () 1723vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1724_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 1725_G . make_warning ( 'Warning on Server 1' , 1 , 1 , 2 , 3 ), 1726_G . make_info ( 'Ignored information' , 1 , 1 , 2 , 3 ), 1727_G . make_hint ( "Here's a hint" , 1 , 1 , 2 , 3 ), 1728} ) 1729 1730return { 1731vim . diagnostic . count ( 1732_G . diagnostic_bufnr , 1733{ severity ={ min =vim . diagnostic . severity . WARN } } 1734), 1735vim . diagnostic . count ( 1736_G . diagnostic_bufnr , 1737{ severity ={ max =vim . diagnostic . severity . WARN } } 1738), 1739vim . diagnostic . count ( _G . diagnostic_bufnr , { 1740severity ={ 1741min =vim . diagnostic . severity . INFO , 1742max =vim . diagnostic . severity . WARN , 1743} , 1744} ), 1745} 1746end ) 1747) 1748end ) 1749 1750it ( 'returns only requested diagnostics when severities are supplied' , function () 1751eq ( 1752exec_lua ( function () 1753return { 1754{ [ vim . diagnostic . severity . WARN ] =1 } , 1755{ [ vim . diagnostic . severity . ERROR ] =1 } , 1756{ [ vim . diagnostic . severity . WARN ] =1 , [ vim . diagnostic . severity . INFO ] =1 } , 1757} 1758end ), 1759exec_lua ( function () 1760vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1761_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 1762_G . make_warning ( 'Warning on Server 1' , 1 , 1 , 2 , 3 ), 1763_G . make_info ( 'Ignored information' , 1 , 1 , 2 , 3 ), 1764_G . make_hint ( "Here's a hint" , 1 , 1 , 2 , 3 ), 1765} ) 1766 1767return { 1768vim . diagnostic . count ( 1769_G . diagnostic_bufnr , 1770{ severity ={ vim . diagnostic . severity . WARN } } 1771), 1772vim . diagnostic . count ( 1773_G . diagnostic_bufnr , 1774{ severity ={ vim . diagnostic . severity . ERROR } } 1775), 1776vim . diagnostic . count ( _G . diagnostic_bufnr , { 1777severity ={ 1778vim . diagnostic . severity . INFO , 1779vim . diagnostic . severity . WARN , 1780} , 1781} ), 1782} 1783end ) 1784) 1785end ) 1786 1787it ( 'allows filtering by line' , function () 1788eq ( 1789exec_lua ( function () 1790return { 1791[ vim . diagnostic . severity . WARN ] =1 , 1792[ vim . diagnostic . severity . INFO ] =1 , 1793} 1794end ), 1795exec_lua ( function () 1796vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1797_G . make_error ( 'Error 1' , 1 , 1 , 1 , 5 ), 1798_G . make_warning ( 'Warning on Server 1' , 1 , 1 , 2 , 3 ), 1799_G . make_info ( 'Ignored information' , 1 , 1 , 2 , 3 ), 1800_G . make_error ( 'Error On Other Line' , 3 , 1 , 3 , 5 ), 1801} ) 1802 1803return vim . diagnostic . count ( _G . diagnostic_bufnr , { lnum =2 } ) 1804end ) 1805) 1806end ) 1807end ) 1808 1809describe ( 'config()' , function () 1810it ( 'works with global, namespace, and ephemeral options' , function () 1811eq ( 18121 , 1813exec_lua ( function () 1814vim . diagnostic . config ( { 1815virtual_text =false , 1816} ) 1817 1818vim . diagnostic . config ( { 1819virtual_text =true , 1820underline =false , 1821} , _G . diagnostic_ns ) 1822 1823vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1824_G . make_error ( 'Some Error' , 4 , 4 , 4 , 4 ), 1825} ) 1826 1827return _G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 1828end ) 1829) 1830 1831eq ( 18321 , 1833exec_lua ( function () 1834vim . diagnostic . config ( { 1835virtual_text =false , 1836} ) 1837 1838vim . diagnostic . config ( { 1839virtual_text =false , 1840underline =false , 1841} , _G . diagnostic_ns ) 1842 1843vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1844_G . make_error ( 'Some Error' , 4 , 4 , 4 , 4 ), 1845} , { virtual_text =true } ) 1846 1847return _G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 1848end ) 1849) 1850 1851eq ( 18520 , 1853exec_lua ( function () 1854vim . diagnostic . config ( { 1855virtual_text =false , 1856} ) 1857 1858vim . diagnostic . config ( { 1859virtual_text ={ severity =vim . diagnostic . severity . ERROR } , 1860underline =false , 1861} , _G . diagnostic_ns ) 1862 1863vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1864_G . make_warning ( 'Some Warning' , 4 , 4 , 4 , 4 ), 1865} , { virtual_text =true } ) 1866 1867return _G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 1868end ) 1869) 1870 1871eq ( 18721 , 1873exec_lua ( function () 1874vim . diagnostic . config ( { 1875virtual_text =false , 1876} ) 1877 1878vim . diagnostic . config ( { 1879virtual_text ={ severity =vim . diagnostic . severity . ERROR } , 1880underline =false , 1881} , _G . diagnostic_ns ) 1882 1883vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1884_G . make_warning ( 'Some Warning' , 4 , 4 , 4 , 4 ), 1885} , { 1886virtual_text ={} , -- An empty table uses default values 1887} ) 1888 1889return _G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 1890end ) 1891) 1892end ) 1893 1894it ( 'can use functions for config values' , function () 1895exec_lua ( function () 1896vim . diagnostic . config ( { 1897virtual_text =function () 1898return true 1899end , 1900} , _G . diagnostic_ns ) 1901vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1902_G . make_error ( 'Delayed Diagnostic' , 4 , 4 , 4 , 4 ), 1903} ) 1904end ) 1905 1906eq ( 19071 , 1908exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 1909) 1910eq ( 2 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 1911 1912-- Now, don't enable virtual text. 1913-- We should have one less extmark displayed. 1914exec_lua ( function () 1915vim . diagnostic . config ( { 1916virtual_text =function () 1917return false 1918end , 1919} , _G . diagnostic_ns ) 1920end ) 1921 1922eq ( 19231 , 1924exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 1925) 1926eq ( 1 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 1927end ) 1928 1929it ( 'allows filtering by severity' , function () 1930local get_extmark_count_with_severity =function ( min_severity ) 1931return exec_lua ( function () 1932vim . diagnostic . config ( { 1933underline =false , 1934virtual_text ={ 1935severity ={ min =min_severity } , 1936} , 1937} ) 1938 1939vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1940_G . make_warning ( 'Delayed Diagnostic' , 4 , 4 , 4 , 4 ), 1941} ) 1942 1943return _G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 1944end ) 1945end 1946 1947-- No messages with Error or higher 1948eq ( 0 , get_extmark_count_with_severity ( 'ERROR' )) 1949 1950-- But now we don't filter it 1951eq ( 1 , get_extmark_count_with_severity ( 'WARN' )) 1952eq ( 1 , get_extmark_count_with_severity ( 'HINT' )) 1953end ) 1954 1955it ( 'allows sorting by severity' , function () 1956exec_lua ( function () 1957vim . diagnostic . config ( { 1958underline =false , 1959signs =true , 1960virtual_text =true , 1961} ) 1962 1963vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 1964_G . make_warning ( 'Warning' , 4 , 4 , 4 , 4 ), 1965_G . make_error ( 'Error' , 4 , 4 , 4 , 4 ), 1966_G . make_info ( 'Info' , 4 , 4 , 4 , 4 ), 1967} ) 1968 1969function _G . get_highest_underline_hl ( severity_sort ) 1970vim . diagnostic . config ( { 1971underline =true , 1972severity_sort =severity_sort , 1973} ) 1974 1975local extmarks =_G . get_underline_extmarks ( _G . diagnostic_ns ) 1976 1977table . sort ( extmarks , function ( a , b ) 1978return a [ 4 ]. priority > b [ 4 ]. priority 1979end ) 1980 1981return extmarks [ 1 ][ 4 ]. hl_group 1982end 1983 1984function _G . get_virt_text_and_signs ( severity_sort ) 1985vim . diagnostic . config ( { 1986severity_sort =severity_sort , 1987} ) 1988 1989local virt_text =_G . get_virt_text_extmarks ( _G . diagnostic_ns )[ 1 ][ 4 ]. virt_text 1990 1991local virt_texts ={} 1992for i =2 , # virt_text - 1 do 1993table . insert ( virt_texts , ( string . gsub ( virt_text [ i ][ 2 ], 'DiagnosticVirtualText' , '' ))) 1994end 1995 1996local ns =vim . diagnostic . get_namespace ( _G . diagnostic_ns ) 1997local sign_ns =ns . user_data . sign_ns 1998local signs ={} 1999local all_signs =vim . api . nvim_buf_get_extmarks ( 2000_G . diagnostic_bufnr , 2001sign_ns , 20020 , 2003- 1 , 2004{ type ='sign' , details =true } 2005) 2006table . sort ( all_signs , function ( a , b ) 2007return a [ 1 ] > b [ 1 ] 2008end ) 2009 2010for _ , v in ipairs ( all_signs ) do 2011local s =v [ 4 ]. sign_hl_group : gsub ( 'DiagnosticSign' , '' ) 2012if not vim . tbl_contains ( signs , s ) then 2013signs [ # signs + 1 ] =s 2014end 2015end 2016 2017return { virt_texts , signs } 2018end 2019end ) 2020 2021local result =exec_lua [[return _G.get_virt_text_and_signs(false)]] 2022 2023-- Virt texts are defined lowest priority to highest, signs from 2024-- highest to lowest 2025eq ( { 'Warn' , 'Error' , 'Info' } , result [ 1 ]) 2026eq ( { 'Info' , 'Error' , 'Warn' } , result [ 2 ]) 2027 2028result =exec_lua [[return _G.get_virt_text_and_signs(true)]] 2029eq ( { 'Info' , 'Warn' , 'Error' } , result [ 1 ]) 2030eq ( { 'Error' , 'Warn' , 'Info' } , result [ 2 ]) 2031 2032result =exec_lua [[return _G.get_virt_text_and_signs({ reverse = true })]] 2033eq ( { 'Error' , 'Warn' , 'Info' } , result [ 1 ]) 2034eq ( { 'Info' , 'Warn' , 'Error' } , result [ 2 ]) 2035 2036local underline_hl =exec_lua [[return _G.get_highest_underline_hl(true)]] 2037eq ( 'DiagnosticUnderlineError' , underline_hl ) 2038 2039underline_hl =exec_lua [[return _G.get_highest_underline_hl({ reverse = true })]] 2040eq ( 'DiagnosticUnderlineInfo' , underline_hl ) 2041end ) 2042 2043it ( 2044'shows deprecated and unnecessary highlights in addition to severity-based highlights' , 2045function () 2046local screen =Screen . new ( 50 , 3 ) 2047screen : set_default_attr_ids ( { 2048--- DiagnosticUnderlineError + DiagnosticUnnecessary + DiagnosticDeprecated combined 2049[ 1 ] ={ 2050background =Screen . colors . Red1 , 2051strikethrough =true , 2052underline =true , 2053special =Screen . colors . Red1 , 2054bold =true , 2055} , 2056} ) 2057 2058command ( 'hi DiagnosticUnderlineError guibg=Red gui=underline guisp=Red' ) 2059command ( 'hi DiagnosticUnnecessary gui=bold' ) 2060command ( 'hi DiagnosticDeprecated gui=strikethrough' ) 2061 2062exec_lua ( function () 2063vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 2064vim . diagnostic . config ( { 2065signs =false , 2066} ) 2067 2068local diagnostic =_G . make_error ( 'Some error' , 0 , 0 , 0 , 3 , 'source x' ) 2069diagnostic . _tags ={ 2070deprecated =true , 2071unnecessary =true , 2072} 2073vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { diagnostic } ) 2074end ) 2075 2076screen : expect ( [[ 2077{1:^1st} line of text | 20782nd line of text | 2079| 2080]] ) 2081end 2082) 2083 2084it ( 'can show diagnostic sources in virtual text' , function () 2085local result =exec_lua ( function () 2086local diagnostics ={ 2087_G . make_error ( 'Some error' , 0 , 0 , 0 , 0 , 'source x' ), 2088} 2089 2090vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics , { 2091underline =false , 2092virtual_text ={ 2093prefix ='' , 2094source ='always' , 2095} , 2096} ) 2097 2098local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2099local virt_text =extmarks [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ] 2100return virt_text 2101end ) 2102eq ( ' source x: Some error' , result ) 2103 2104result =exec_lua ( function () 2105vim . diagnostic . config ( { 2106underline =false , 2107virtual_text ={ 2108prefix ='' , 2109source ='if_many' , 2110} , 2111} , _G . diagnostic_ns ) 2112 2113local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2114local virt_text =extmarks [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ] 2115return virt_text 2116end ) 2117eq ( ' Some error' , result ) 2118 2119result =exec_lua ( function () 2120local diagnostics ={ 2121_G . make_error ( 'Some error' , 0 , 0 , 0 , 0 , 'source x' ), 2122_G . make_error ( 'Another error' , 1 , 1 , 1 , 1 , 'source y' ), 2123} 2124 2125vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics , { 2126underline =false , 2127virtual_text ={ 2128prefix ='' , 2129source ='if_many' , 2130} , 2131} ) 2132 2133local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2134local virt_text ={ extmarks [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ], extmarks [ 2 ][ 4 ]. virt_text [ 3 ][ 1 ] } 2135return virt_text 2136end ) 2137eq ( ' source x: Some error' , result [ 1 ]) 2138eq ( ' source y: Another error' , result [ 2 ]) 2139end ) 2140 2141it ( 'supports a format function for diagnostic messages' , function () 2142local result =exec_lua ( function () 2143vim . diagnostic . config ( { 2144underline =false , 2145virtual_text ={ 2146prefix ='' , 2147format =function ( diagnostic ) 2148if diagnostic . severity == vim . diagnostic . severity . ERROR then 2149return string . format ( '🔥 %s' , diagnostic . message ) 2150end 2151return string . format ( '👀 %s' , diagnostic . message ) 2152end , 2153} , 2154} ) 2155 2156vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2157_G . make_warning ( 'Warning' , 0 , 0 , 0 , 0 ), 2158_G . make_error ( 'Error' , 1 , 0 , 1 , 0 ), 2159} ) 2160 2161local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2162return { extmarks [ 1 ][ 4 ]. virt_text , extmarks [ 2 ][ 4 ]. virt_text } 2163end ) 2164eq ( ' 👀 Warning' , result [ 1 ][ 3 ][ 1 ]) 2165eq ( ' 🔥 Error' , result [ 2 ][ 3 ][ 1 ]) 2166end ) 2167 2168it ( 'includes source for formatted diagnostics' , function () 2169local result =exec_lua ( function () 2170vim . diagnostic . config ( { 2171underline =false , 2172virtual_text ={ 2173prefix ='' , 2174source ='always' , 2175format =function ( diagnostic ) 2176if diagnostic . severity == vim . diagnostic . severity . ERROR then 2177return string . format ( '🔥 %s' , diagnostic . message ) 2178end 2179return string . format ( '👀 %s' , diagnostic . message ) 2180end , 2181} , 2182} ) 2183 2184vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2185_G . make_warning ( 'Warning' , 0 , 0 , 0 , 0 , 'some_linter' ), 2186_G . make_error ( 'Error' , 1 , 0 , 1 , 0 , 'another_linter' ), 2187} ) 2188 2189local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2190return { extmarks [ 1 ][ 4 ]. virt_text , extmarks [ 2 ][ 4 ]. virt_text } 2191end ) 2192eq ( ' some_linter: 👀 Warning' , result [ 1 ][ 3 ][ 1 ]) 2193eq ( ' another_linter: 🔥 Error' , result [ 2 ][ 3 ][ 1 ]) 2194end ) 2195 2196it ( 'can add a prefix to virtual text' , function () 2197eq ( 2198'E Some error' , 2199exec_lua ( function () 2200local diagnostics ={ 2201_G . make_error ( 'Some error' , 0 , 0 , 0 , 0 ), 2202} 2203 2204vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics , { 2205underline =false , 2206virtual_text ={ 2207prefix ='E' , 2208suffix ='' , 2209} , 2210} ) 2211 2212local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2213local prefix =extmarks [ 1 ][ 4 ]. virt_text [ 2 ][ 1 ] 2214local message =extmarks [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ] 2215return prefix .. message 2216end ) 2217) 2218 2219eq ( 2220'[(1/1) err-code] Some error' , 2221exec_lua ( function () 2222local diagnostics ={ 2223_G . make_error ( 'Some error' , 0 , 0 , 0 , 0 , nil , 'err-code' ), 2224} 2225 2226vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics , { 2227underline =false , 2228virtual_text ={ 2229prefix =function ( diag , i , total ) 2230return string . format ( '[(%d/%d) %s]' , i , total , diag . code ) 2231end , 2232suffix ='' , 2233} , 2234} ) 2235 2236local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2237local prefix =extmarks [ 1 ][ 4 ]. virt_text [ 2 ][ 1 ] 2238local message =extmarks [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ] 2239return prefix .. message 2240end ) 2241) 2242end ) 2243 2244it ( 'can add a suffix to virtual text' , function () 2245eq ( 2246' Some error ✘' , 2247exec_lua ( function () 2248local diagnostics ={ 2249_G . make_error ( 'Some error' , 0 , 0 , 0 , 0 ), 2250} 2251 2252vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics , { 2253underline =false , 2254virtual_text ={ 2255prefix ='' , 2256suffix =' ✘' , 2257} , 2258} ) 2259 2260local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2261local virt_text =extmarks [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ] 2262return virt_text 2263end ) 2264) 2265 2266eq ( 2267' Some error [err-code]' , 2268exec_lua ( function () 2269local diagnostics ={ 2270_G . make_error ( 'Some error' , 0 , 0 , 0 , 0 , nil , 'err-code' ), 2271} 2272 2273vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics , { 2274underline =false , 2275virtual_text ={ 2276prefix ='' , 2277suffix =function ( diag ) 2278return string . format ( ' [%s]' , diag . code ) 2279end , 2280} , 2281} ) 2282 2283local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2284local virt_text =extmarks [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ] 2285return virt_text 2286end ) 2287) 2288end ) 2289 2290it ( 'can filter diagnostics by returning nil when formatting' , function () 2291local result =exec_lua ( function () 2292vim . diagnostic . config { 2293virtual_text ={ 2294format =function ( diagnostic ) 2295if diagnostic . code == 'foo_err' then 2296return nil 2297end 2298return diagnostic . message 2299end , 2300} , 2301} 2302 2303vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2304_G . make_error ( 'An error here!' , 0 , 0 , 0 , 0 , 'foo_server' , 'foo_err' ), 2305_G . make_error ( 'An error there!' , 1 , 1 , 1 , 1 , 'bar_server' , 'bar_err' ), 2306} ) 2307 2308local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2309return extmarks 2310end ) 2311 2312eq ( 1 , # result ) 2313eq ( ' An error there!' , result [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ]) 2314end ) 2315 2316it ( 'can only show virtual_text for the current line' , function () 2317local result =exec_lua ( function () 2318vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 2319 2320vim . diagnostic . config ( { virtual_text ={ current_line =true } } ) 2321 2322vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2323_G . make_error ( 'Error here!' , 0 , 0 , 0 , 0 , 'foo_server' ), 2324_G . make_error ( 'Another error there!' , 1 , 0 , 1 , 0 , 'foo_server' ), 2325} ) 2326 2327local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2328return extmarks 2329end ) 2330 2331eq ( 1 , # result ) 2332eq ( ' Error here!' , result [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ]) 2333end ) 2334 2335it ( 'can hide virtual_text for the current line' , function () 2336local result =exec_lua ( function () 2337vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 2338 2339vim . diagnostic . config ( { virtual_text ={ current_line =false } } ) 2340 2341vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2342_G . make_error ( 'Error here!' , 0 , 0 , 0 , 0 , 'foo_server' ), 2343_G . make_error ( 'Another error there!' , 1 , 0 , 1 , 0 , 'foo_server' ), 2344} ) 2345 2346local extmarks =_G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2347return extmarks 2348end ) 2349 2350eq ( 1 , # result ) 2351eq ( ' Another error there!' , result [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ]) 2352end ) 2353 2354it ( 'only renders virtual_text diagnostics within buffer length' , function () 2355local result =exec_lua ( function () 2356vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 2357 2358vim . diagnostic . config ( { virtual_text ={ current_line =false } } ) 2359 2360vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2361_G . make_error ( 'Hidden Error here!' , 0 , 0 , 0 , 0 , 'foo_server' ), 2362_G . make_error ( 'First Error here!' , 1 , 0 , 1 , 0 , 'foo_server' ), 2363_G . make_error ( 'Second Error here!' , 2 , 0 , 2 , 0 , 'foo_server' ), 2364_G . make_error ( 'First Ignored Error here!' , 3 , 0 , 3 , 0 , 'foo_server' ), 2365_G . make_error ( 'Second Ignored Error here!' , 6 , 0 , 6 , 0 , 'foo_server' ), 2366_G . make_error ( 'Third Ignored Error here!' , 8 , 0 , 8 , 0 , 'foo_server' ), 2367} ) 2368 2369vim . api . nvim_buf_set_lines ( _G . diagnostic_bufnr , 2 , 5 , false , {} ) 2370vim . api . nvim_exec_autocmds ( 'CursorHold' , { buf =_G . diagnostic_bufnr } ) 2371return _G . get_virt_text_extmarks ( _G . diagnostic_ns ) 2372end ) 2373 2374eq ( 2 , # result ) 2375eq ( ' First Error here!' , result [ 1 ][ 4 ]. virt_text [ 3 ][ 1 ]) 2376eq ( ' Second Error here!' , result [ 2 ][ 4 ]. virt_text [ 3 ][ 1 ]) 2377end ) 2378end ) 2379 2380describe ( 'handlers.virtual_lines' , function () 2381it ( 'includes diagnostic code and message' , function () 2382local result =exec_lua ( function () 2383vim . diagnostic . config ( { virtual_lines =true } ) 2384 2385vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2386_G . make_error ( 'Missed symbol `,`' , 0 , 0 , 0 , 0 , 'lua_ls' , 'miss-symbol' ), 2387} ) 2388 2389local extmarks =_G . get_virt_lines_extmarks ( _G . diagnostic_ns ) 2390return extmarks 2391end ) 2392 2393eq ( 1 , # result ) 2394eq ( 'auto' , result [ 1 ][ 4 ]. virt_lines_overflow ) 2395eq ( 'miss-symbol: Missed symbol `,`' , result [ 1 ][ 4 ]. virt_lines [ 1 ][ 3 ][ 1 ]) 2396end ) 2397 2398it ( 'adds space to the left of the diagnostic' , function () 2399local error_offset =5 2400local result =exec_lua ( function () 2401vim . diagnostic . config ( { virtual_lines =true } ) 2402 2403vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2404_G . make_error ( 'Error here!' , 0 , error_offset , 0 , error_offset , 'foo_server' ), 2405} ) 2406 2407local extmarks =_G . get_virt_lines_extmarks ( _G . diagnostic_ns ) 2408return extmarks [ 1 ][ 4 ]. virt_lines 2409end ) 2410 2411eq ( error_offset , result [ 1 ][ 1 ][ 1 ]: len ()) 2412end ) 2413 2414it ( 'highlights diagnostics in multiple lines by default #38685' , function () 2415local result =exec_lua ( function () 2416vim . diagnostic . config ( { virtual_lines =true } ) 2417 2418vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2419_G . make_error ( 'Error here!' , 0 , 0 , 0 , 0 , 'foo_server' ), 2420_G . make_error ( 'Another error there!' , 1 , 0 , 3 , 0 , 'foo_server' ), 2421} ) 2422 2423local extmarks =_G . get_virt_lines_extmarks ( _G . diagnostic_ns ) 2424return extmarks 2425end ) 2426 2427eq ( 2 , # result ) 2428eq ( 'Error here!' , result [ 1 ][ 4 ]. virt_lines [ 1 ][ 3 ][ 1 ]) 2429eq ( 'Another error there!' , result [ 2 ][ 4 ]. virt_lines [ 1 ][ 3 ][ 1 ]) 2430eq ( 0 , result [ 1 ][ 2 ]) 2431eq ( 3 , result [ 2 ][ 2 ]) 2432end ) 2433 2434it ( 'highlights multiple diagnostics in a single line by default' , function () 2435local result =exec_lua ( function () 2436vim . api . nvim_buf_set_lines ( 2437_G . diagnostic_bufnr , 24380 , 2439- 1 , 2440false , 2441{ 'def foo(x: int, /, y: str, *, z: float) -> None: ...' } 2442) 2443 2444vim . diagnostic . config ( { virtual_lines =true } ) 2445 2446vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2447_G . make_error ( 'Error here!' , 0 , 8 , 0 , 9 , 'foo_server' ), 2448_G . make_error ( 'Another error there!' , 0 , 19 , 0 , 20 , 'foo_server' ), 2449_G . make_error ( 'And another one!' , 0 , 30 , 0 , 31 , 'foo_server' ), 2450} ) 2451 2452local extmarks =_G . get_virt_lines_extmarks ( _G . diagnostic_ns ) 2453return extmarks 2454end ) 2455 2456--[[ 2457|def foo(x: int, /, y: str, *, z: float) -> None: ... 2458| │ │ └──── And another one! 2459| │ └──── Another error there! 2460| └──── Error here! 2461| ^ col=8 2462| ^ col=19 2463| ^ col=30 2464 246511 cols between each diagnostic after the first one (10 spaces + |) 2466]] 2467 2468eq ( 1 , # result ) 2469local virt_lines =result [ 1 ][ 4 ]. virt_lines 2470eq ( 8 , virt_lines [ 1 ][ 1 ][ 1 ]: len ()) -- first space 2471eq ( 10 , virt_lines [ 1 ][ 3 ][ 1 ]: len ()) -- second space 2472eq ( 10 , virt_lines [ 1 ][ 5 ][ 1 ]: len ()) -- third space 2473eq ( 'And another one!' , virt_lines [ 1 ][ 7 ][ 1 ]) 2474eq ( 8 , virt_lines [ 2 ][ 1 ][ 1 ]: len ()) -- first space 2475eq ( 10 , virt_lines [ 2 ][ 3 ][ 1 ]: len ()) -- second space 2476eq ( 'Another error there!' , virt_lines [ 2 ][ 5 ][ 1 ]) 2477eq ( 8 , virt_lines [ 3 ][ 1 ][ 1 ]: len ()) -- first space 2478eq ( 'Error here!' , virt_lines [ 3 ][ 3 ][ 1 ]) 2479end ) 2480 2481it ( 'highlights overlapping diagnostics on a single line' , function () 2482local result =exec_lua ( function () 2483vim . diagnostic . config ( { virtual_lines =true } ) 2484 2485vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2486_G . make_error ( 'Error here!' , 0 , 10 , 0 , 11 , 'foo_server' ), 2487_G . make_error ( 'Another error here!' , 0 , 10 , 0 , 11 , 'foo_server' ), 2488} ) 2489 2490local extmarks =_G . get_virt_lines_extmarks ( _G . diagnostic_ns ) 2491return extmarks 2492end ) 2493 2494--[[ 2495|1234567890x 2496| ├──── Another error here! 2497| └──── Error here! 2498]] 2499 2500eq ( 1 , # result ) 2501local virt_lines =result [ 1 ][ 4 ]. virt_lines 2502eq ( 10 , virt_lines [ 1 ][ 1 ][ 1 ]: len ()) -- first space 2503eq ( '├──── ' , virt_lines [ 1 ][ 2 ][ 1 ]) 2504eq ( 'Another error here!' , virt_lines [ 1 ][ 3 ][ 1 ]) 2505eq ( 10 , virt_lines [ 2 ][ 1 ][ 1 ]: len ()) -- second space 2506eq ( '└──── ' , virt_lines [ 2 ][ 2 ][ 1 ]) 2507eq ( 'Error here!' , virt_lines [ 2 ][ 3 ][ 1 ]) 2508end ) 2509 2510it ( 'handles multi-line diagnostic message' , function () 2511local result =exec_lua ( function () 2512vim . diagnostic . config ( { virtual_lines =true } ) 2513 2514vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2515_G . make_error ( 'Error here!\ngot another line' , 0 , 10 , 0 , 11 , 'foo_server' ), 2516} ) 2517 2518local extmarks =_G . get_virt_lines_extmarks ( _G . diagnostic_ns ) 2519return extmarks 2520end ) 2521 2522--[[ 2523|1234567890x 2524| └──── Error here! 2525| got another line 2526]] 2527 2528eq ( 1 , # result ) 2529local virt_lines =result [ 1 ][ 4 ]. virt_lines 2530eq ( 10 , virt_lines [ 1 ][ 1 ][ 1 ]: len ()) -- first space 2531eq ( '└──── ' , virt_lines [ 1 ][ 2 ][ 1 ]) 2532eq ( 'Error here!' , virt_lines [ 1 ][ 3 ][ 1 ]) 2533eq ( 10 , virt_lines [ 2 ][ 1 ][ 1 ]: len ()) -- second line space 2534eq ( 6 , virt_lines [ 2 ][ 2 ][ 1 ]: len ()) -- extra padding 2535eq ( 'got another line' , virt_lines [ 2 ][ 3 ][ 1 ]) 2536end ) 2537 2538it ( 'can highlight diagnostics only in the current line' , function () 2539local result =exec_lua ( function () 2540vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 2541 2542vim . diagnostic . config ( { virtual_lines ={ current_line =true } } ) 2543 2544vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2545_G . make_error ( 'Error here!' , 0 , 0 , 0 , 0 , 'foo_server' ), 2546_G . make_error ( 'Another error there!' , 1 , 0 , 1 , 0 , 'foo_server' ), 2547} ) 2548 2549vim . api . nvim_exec_autocmds ( 'CursorHold' , { buf =_G . diagnostic_bufnr } ) 2550local extmarks =_G . get_virt_lines_extmarks ( _G . diagnostic_ns ) 2551return extmarks 2552end ) 2553 2554eq ( 1 , # result ) 2555eq ( 'Error here!' , result [ 1 ][ 4 ]. virt_lines [ 1 ][ 3 ][ 1 ]) 2556end ) 2557 2558it ( 'supports a format function for diagnostic messages' , function () 2559local result =exec_lua ( function () 2560vim . diagnostic . config ( { 2561virtual_lines ={ 2562format =function () 2563return 'Error here!' 2564end , 2565} , 2566} ) 2567vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2568_G . make_error ( 'Invalid syntax' , 0 , 0 , 0 , 0 ), 2569} ) 2570local extmarks =_G . get_virt_lines_extmarks ( _G . diagnostic_ns ) 2571return extmarks [ 1 ][ 4 ]. virt_lines 2572end ) 2573eq ( 'Error here!' , result [ 1 ][ 3 ][ 1 ]) 2574end ) 2575 2576it ( 'sorts by severity with stable tiebreaker #37137' , function () 2577local result =exec_lua ( function () 2578vim . diagnostic . config ( { severity_sort =true , virtual_lines ={ current_line =true } } ) 2579local m =100 2580local diagnostics ={ 2581{ end_col =m , lnum =0 , message ='a' , severity =2 } , 2582{ end_col =m , lnum =0 , message ='b' , severity =2 } , 2583{ end_col =m , lnum =0 , message ='c' , severity =2 } , 2584{ end_col =m , lnum =2 , message ='d' , severity =2 } , 2585{ end_col =m , lnum =2 , message ='e' , severity =2 } , 2586{ end_col =m , lnum =2 , message ='f' , severity =2 } , 2587} 2588vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics , {} ) 2589vim . diagnostic . show ( _G . diagnostic_ns , _G . diagnostic_bufnr ) 2590vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 2591vim . api . nvim_exec_autocmds ( 'CursorHold' , { buf =_G . diagnostic_bufnr } ) 2592local extmarks =_G . get_virt_lines_extmarks ( _G . diagnostic_ns ) 2593local result ={} 2594for _ , d in ipairs ( extmarks [ 1 ][ 4 ]. virt_lines ) do 2595table . insert ( result , d [ 3 ][ 1 ]) 2596end 2597return result 2598end ) 2599eq ( { 'c' , 'b' , 'a' } , result ) 2600end ) 2601end ) 2602 2603describe ( 'set()' , function () 2604it ( 'validation' , function () 2605matches ( 2606'expected a list of diagnostics' , 2607pcall_err ( exec_lua , [[vim.diagnostic.set(1, 0, {lnum = 1, col = 2})]] ) 2608) 2609end ) 2610 2611it ( 'does not accumulate BufRead autocmds for unloaded buffers' , function () 2612local counts =exec_lua ( function () 2613local bufnr =vim . fn . bufadd ( vim . fn . tempname ()) 2614local function count_autocmds () 2615return # vim . api . nvim_get_autocmds ( { event ='BufRead' , buffer =bufnr } ) 2616end 2617vim . diagnostic . set ( _G . diagnostic_ns , bufnr , { _G . make_error ( 'Error 1' , 0 , 0 , 0 , 0 ) } ) 2618local first =count_autocmds () 2619for i =2 , 5 do 2620vim . diagnostic . set ( _G . diagnostic_ns , bufnr , { _G . make_error ( 'Error ' .. i , 0 , 0 , 0 , 0 ) } ) 2621end 2622return { first =first , last =count_autocmds () } 2623end ) 2624eq ( counts . first , counts . last ) 2625end ) 2626 2627it ( 'computes positions from the last set() when the buffer loads' , function () 2628eq ( 2629{ autocmds =0 , extmarks ={ { 2 , 1 } } } , 2630exec_lua ( function () 2631local path =vim . fn . tempname () 2632vim . fn . writefile ( { 'one' , 'two' , 'three' } , path ) 2633local bufnr =vim . fn . bufadd ( path ) 2634vim . diagnostic . set ( _G . diagnostic_ns , bufnr , { _G . make_error ( 'Old' , 0 , 0 , 0 , 0 ) } ) 2635vim . diagnostic . set ( _G . diagnostic_ns , bufnr , { _G . make_error ( 'New' , 2 , 1 , 2 , 2 ) } ) 2636vim . fn . bufload ( bufnr ) 2637local location_ns =vim . diagnostic . get_namespace ( _G . diagnostic_ns ). user_data . location_ns 2638local extmarks ={} --- @type [integer, integer][] 2639for _ , m in ipairs ( vim . api . nvim_buf_get_extmarks ( bufnr , location_ns , 0 , - 1 , {} )) do 2640extmarks [ # extmarks + 1 ] ={ m [ 2 ], m [ 3 ] } 2641end 2642return { 2643autocmds =# vim . api . nvim_get_autocmds ( { event ='BufRead' , buffer =bufnr } ), 2644extmarks =extmarks , 2645} 2646end ) 2647) 2648end ) 2649 2650it ( 'can perform updates after insert_leave' , function () 2651exec_lua ( function () 2652vim . diagnostic . config ( { virtual_text =true } ) 2653vim . api . nvim_set_current_buf ( _G . diagnostic_bufnr ) 2654end ) 2655 2656api . nvim_input ( 'o' ) 2657eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2658 2659-- Save the diagnostics 2660exec_lua ( function () 2661vim . diagnostic . config ( { 2662update_in_insert =false , 2663} ) 2664vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2665_G . make_error ( 'Delayed Diagnostic' , 4 , 4 , 4 , 4 ), 2666} ) 2667end ) 2668 2669-- No diagnostics displayed yet. 2670eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2671eq ( 26721 , 2673exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 2674) 2675eq ( 0 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 2676 2677api . nvim_input ( '<esc>' ) 2678eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 2679 2680eq ( 26811 , 2682exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 2683) 2684eq ( 2 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 2685end ) 2686 2687it ( 'does not perform updates when not needed' , function () 2688exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]] 2689api . nvim_input ( 'o' ) 2690eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2691 2692-- Save the diagnostics 2693exec_lua ( function () 2694vim . diagnostic . config ( { 2695update_in_insert =false , 2696virtual_text =true , 2697} ) 2698 2699_G . DisplayCount =0 2700local set_virtual_text =vim . diagnostic . handlers . virtual_text . show 2701vim . diagnostic . handlers . virtual_text . show =function ( ... ) 2702_G . DisplayCount =_G . DisplayCount + 1 2703return set_virtual_text ( ... ) 2704end 2705 2706vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2707_G . make_error ( 'Delayed Diagnostic' , 4 , 4 , 4 , 4 ), 2708} ) 2709end ) 2710 2711-- No diagnostics displayed yet. 2712eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2713eq ( 27141 , 2715exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 2716) 2717eq ( 0 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 2718eq ( 0 , exec_lua [[return _G.DisplayCount]] ) 2719 2720api . nvim_input ( '<esc>' ) 2721eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 2722 2723eq ( 27241 , 2725exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 2726) 2727eq ( 2 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 2728eq ( 1 , exec_lua [[return _G.DisplayCount]] ) 2729 2730-- Go in and out of insert mode one more time. 2731api . nvim_input ( 'o' ) 2732eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2733 2734api . nvim_input ( '<esc>' ) 2735eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 2736 2737-- Should not have set the virtual text again. 2738eq ( 1 , exec_lua [[return _G.DisplayCount]] ) 2739end ) 2740 2741it ( 'never sets virtual text, in combination with insert leave' , function () 2742exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]] 2743api . nvim_input ( 'o' ) 2744eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2745 2746-- Save the diagnostics 2747exec_lua ( function () 2748vim . diagnostic . config ( { 2749update_in_insert =false , 2750virtual_text =false , 2751} ) 2752 2753_G . DisplayCount =0 2754local set_virtual_text =vim . diagnostic . handlers . virtual_text . show 2755vim . diagnostic . handlers . virtual_text . show =function ( ... ) 2756_G . DisplayCount =_G . DisplayCount + 1 2757return set_virtual_text ( ... ) 2758end 2759 2760vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2761_G . make_error ( 'Delayed Diagnostic' , 4 , 4 , 4 , 4 ), 2762} ) 2763end ) 2764 2765-- No diagnostics displayed yet. 2766eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2767eq ( 27681 , 2769exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 2770) 2771eq ( 0 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 2772eq ( 0 , exec_lua [[return _G.DisplayCount]] ) 2773 2774api . nvim_input ( '<esc>' ) 2775eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 2776 2777eq ( 27781 , 2779exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 2780) 2781eq ( 1 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 2782eq ( 0 , exec_lua [[return _G.DisplayCount]] ) 2783 2784-- Go in and out of insert mode one more time. 2785api . nvim_input ( 'o' ) 2786eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2787 2788api . nvim_input ( '<esc>' ) 2789eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 2790 2791-- Should not have set the virtual text still. 2792eq ( 0 , exec_lua [[return _G.DisplayCount]] ) 2793end ) 2794 2795it ( 'can perform updates while in insert mode, if desired' , function () 2796exec_lua ( function () 2797vim . diagnostic . config ( { virtual_text =true } ) 2798vim . api . nvim_set_current_buf ( _G . diagnostic_bufnr ) 2799end ) 2800api . nvim_input ( 'o' ) 2801eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2802 2803-- Save the diagnostics 2804exec_lua ( function () 2805vim . diagnostic . config ( { 2806update_in_insert =true , 2807} ) 2808 2809vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2810_G . make_error ( 'Delayed Diagnostic' , 4 , 4 , 4 , 4 ), 2811} ) 2812end ) 2813 2814-- Diagnostics are displayed, because the user wanted them that way! 2815eq ( { mode ='i' , blocking =false } , api . nvim_get_mode ()) 2816eq ( 28171 , 2818exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 2819) 2820eq ( 2 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 2821 2822api . nvim_input ( '<esc>' ) 2823eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 2824 2825eq ( 28261 , 2827exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]] 2828) 2829eq ( 2 , exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]] ) 2830end ) 2831 2832it ( 'can set diagnostics without displaying them' , function () 2833exec_lua ( function () 2834vim . diagnostic . config ( { virtual_text =true } ) 2835end ) 2836 2837eq ( 28380 , 2839exec_lua ( function () 2840vim . diagnostic . enable ( false , { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } ) 2841vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2842_G . make_error ( 'Diagnostic From Server 1:1' , 1 , 1 , 1 , 1 ), 2843} ) 2844return _G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 2845end ) 2846) 2847 2848eq ( 28492 , 2850exec_lua ( function () 2851vim . diagnostic . enable ( true , { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } ) 2852return _G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 2853end ) 2854) 2855end ) 2856 2857it ( 'can set display options' , function () 2858eq ( 28590 , 2860exec_lua ( function () 2861vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2862_G . make_error ( 'Diagnostic From Server 1:1' , 1 , 1 , 1 , 1 ), 2863} , { virtual_text =false , underline =false } ) 2864return _G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 2865end ) 2866) 2867 2868eq ( 28691 , 2870exec_lua ( function () 2871vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 2872_G . make_error ( 'Diagnostic From Server 1:1' , 1 , 1 , 1 , 1 ), 2873} , { virtual_text =true , underline =false } ) 2874return _G . count_extmarks ( _G . diagnostic_bufnr , _G . diagnostic_ns ) 2875end ) 2876) 2877end ) 2878 2879it ( 'sets and clears signs #26193 #26555' , function () 2880do 2881local result =exec_lua ( function () 2882vim . diagnostic . config ( { 2883signs =true , 2884} ) 2885 2886local diagnostics ={ 2887_G . make_error ( 'Error' , 1 , 1 , 1 , 2 ), 2888_G . make_warning ( 'Warning' , 3 , 3 , 3 , 3 ), 2889} 2890 2891vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 2892 2893local ns =vim . diagnostic . get_namespace ( _G . diagnostic_ns ) 2894local sign_ns =ns . user_data . sign_ns 2895 2896local signs =vim . api . nvim_buf_get_extmarks ( 2897_G . diagnostic_bufnr , 2898sign_ns , 28990 , 2900- 1 , 2901{ type ='sign' , details =true } 2902) 2903local result ={} 2904for _ , s in ipairs ( signs ) do 2905result [ # result + 1 ] ={ lnum =s [ 2 ] + 1 , name =s [ 4 ]. sign_hl_group } 2906end 2907return result 2908end ) 2909 2910eq ( { 2 , 'DiagnosticSignError' } , { result [ 1 ]. lnum , result [ 1 ]. name } ) 2911eq ( { 4 , 'DiagnosticSignWarn' } , { result [ 2 ]. lnum , result [ 2 ]. name } ) 2912end 2913 2914do 2915local result =exec_lua ( function () 2916vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , {} ) 2917 2918local ns =vim . diagnostic . get_namespace ( _G . diagnostic_ns ) 2919local sign_ns =ns . user_data . sign_ns 2920 2921return vim . api . nvim_buf_get_extmarks ( 2922_G . diagnostic_bufnr , 2923sign_ns , 29240 , 2925- 1 , 2926{ type ='sign' , details =true } 2927) 2928end ) 2929 2930eq ( {} , result ) 2931end 2932end ) 2933 2934it ( 'always passes a table to DiagnosticChanged autocommand' , function () 2935local result =exec_lua ( function () 2936local changed_diags --- @type vim.Diagnostic[]? 2937vim . api . nvim_create_autocmd ( 'DiagnosticChanged' , { 2938buf =_G . diagnostic_bufnr , 2939callback =function ( ev ) 2940--- @type vim.Diagnostic[] 2941changed_diags =ev . data . diagnostics 2942end , 2943} ) 2944vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , {} ) 2945return changed_diags 2946end ) 2947eq ( 'table' , type ( result )) 2948eq ( 0 , # result ) 2949end ) 2950end ) 2951 2952describe ( 'open_float()' , function () 2953it ( 'can display a header' , function () 2954eq ( 2955{ 'Diagnostics:' , '1. Syntax error' } , 2956exec_lua ( function () 2957local diagnostics ={ 2958_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 2959} 2960vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 2961vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 2962local float_bufnr , winnr =vim . diagnostic . open_float () 2963local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 2964vim . api . nvim_win_close ( winnr , true ) 2965return lines 2966end ) 2967) 2968 2969eq ( 2970{ "We're no strangers to love..." , '1. Syntax error' } , 2971exec_lua ( function () 2972local diagnostics ={ 2973_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 2974} 2975vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 2976vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 2977local float_bufnr , winnr = 2978vim . diagnostic . open_float ( { header ="We're no strangers to love..." } ) 2979local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 2980vim . api . nvim_win_close ( winnr , true ) 2981return lines 2982end ) 2983) 2984 2985eq ( 2986{ 'You know the rules' , '1. Syntax error' } , 2987exec_lua ( function () 2988local diagnostics ={ 2989_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 2990} 2991vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 2992vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 2993local float_bufnr , winnr = 2994vim . diagnostic . open_float ( { header ={ 'You know the rules' , 'Search' } } ) 2995local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 2996vim . api . nvim_win_close ( winnr , true ) 2997return lines 2998end ) 2999) 3000end ) 3001 3002it ( 'can show diagnostics from the whole buffer' , function () 3003eq ( 3004{ '1. Syntax error' , '2. Some warning' } , 3005exec_lua ( function () 3006local diagnostics ={ 3007_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3008_G . make_warning ( 'Some warning' , 1 , 1 , 1 , 3 ), 3009} 3010vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3011vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3012local float_bufnr , winnr =vim . diagnostic . open_float ( { header =false , scope ='buffer' } ) 3013local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3014vim . api . nvim_win_close ( winnr , true ) 3015return lines 3016end ) 3017) 3018end ) 3019 3020it ( 'can show diagnostics from a single line' , function () 3021-- Using cursor position 3022eq ( 3023{ '1. Some warning' } , 3024exec_lua ( function () 3025local diagnostics ={ 3026_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3027_G . make_warning ( 'Some warning' , 1 , 1 , 1 , 3 ), 3028} 3029vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3030vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3031vim . api . nvim_win_set_cursor ( 0 , { 2 , 1 } ) 3032local float_bufnr , winnr =vim . diagnostic . open_float ( { header =false } ) 3033local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3034vim . api . nvim_win_close ( winnr , true ) 3035return lines 3036end ) 3037) 3038 3039-- With specified position 3040eq ( 3041{ '1. Some warning' } , 3042exec_lua ( function () 3043local diagnostics ={ 3044_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3045_G . make_warning ( 'Some warning' , 1 , 1 , 1 , 3 ), 3046} 3047vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3048vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3049vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 3050local float_bufnr , winnr =vim . diagnostic . open_float ( { header =false , pos =1 } ) 3051local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3052vim . api . nvim_win_close ( winnr , true ) 3053return lines 3054end ) 3055) 3056 3057-- End position is exclusive 3058eq ( 3059nil , 3060exec_lua ( function () 3061local diagnostics ={ 3062_G . make_error ( 'Syntax error' , 1 , 1 , 2 , 0 ), 3063} 3064vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3065vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3066vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 3067local _ , winnr =vim . diagnostic . open_float ( 0 , { header =false , pos ={ 2 , 0 } } ) 3068return winnr 3069end ) 3070) 3071 3072-- Works when width == 0 3073eq ( 3074{ '1. Syntax error' } , 3075exec_lua ( function () 3076local diagnostics ={ 3077_G . make_error ( 'Syntax error' , 2 , 0 , 2 , 0 ), 3078} 3079vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3080vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3081vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 3082local float_bufnr , winnr = 3083vim . diagnostic . open_float ( 0 , { header =false , pos ={ 2 , 1 } } ) 3084local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3085vim . api . nvim_win_close ( winnr , true ) 3086return lines 3087end ) 3088) 3089end ) 3090 3091it ( 'can show diagnostics from a specific position' , function () 3092-- Using cursor position 3093eq ( 3094{ 'Syntax error' } , 3095exec_lua ( function () 3096local diagnostics ={ 3097_G . make_error ( 'Syntax error' , 1 , 1 , 1 , 3 ), 3098_G . make_warning ( 'Some warning' , 1 , 3 , 1 , 4 ), 3099} 3100vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3101vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3102vim . api . nvim_win_set_cursor ( 0 , { 2 , 2 } ) 3103local float_bufnr , winnr =vim . diagnostic . open_float ( { header =false , scope ='cursor' } ) 3104local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3105vim . api . nvim_win_close ( winnr , true ) 3106return lines 3107end ) 3108) 3109 3110-- With specified position 3111eq ( 3112{ 'Some warning' } , 3113exec_lua ( function () 3114local diagnostics ={ 3115_G . make_error ( 'Syntax error' , 1 , 1 , 1 , 3 ), 3116_G . make_warning ( 'Some warning' , 1 , 3 , 1 , 4 ), 3117} 3118vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3119vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3120vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 3121local float_bufnr , winnr = 3122vim . diagnostic . open_float ( { header =false , scope ='cursor' , pos ={ 1 , 3 } } ) 3123local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3124vim . api . nvim_win_close ( winnr , true ) 3125return lines 3126end ) 3127) 3128 3129-- With column position past the end of the line. #16062 3130eq ( 3131{ 'Syntax error' } , 3132exec_lua ( function () 3133local first_line_len =# vim . api . nvim_buf_get_lines ( _G . diagnostic_bufnr , 0 , 1 , true )[ 1 ] 3134local diagnostics ={ 3135_G . make_error ( 'Syntax error' , 0 , first_line_len + 1 , 1 , 0 ), 3136} 3137vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3138vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3139vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 3140local float_bufnr , winnr =vim . diagnostic . open_float ( { 3141header =false , 3142scope ='cursor' , 3143pos ={ 0 , first_line_len - 1 } , 3144} ) 3145local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3146vim . api . nvim_win_close ( winnr , true ) 3147return lines 3148end ) 3149) 3150 3151-- End position is exclusive 3152eq ( 3153nil , 3154exec_lua ( function () 3155local diagnostics ={ 3156_G . make_error ( 'Syntax error' , 1 , 1 , 1 , 3 ), 3157} 3158vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3159vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3160vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 3161local _ , winnr = 3162vim . diagnostic . open_float ( 0 , { header =false , scope ='cursor' , pos ={ 1 , 3 } } ) 3163return winnr 3164end ) 3165) 3166 3167-- Works when width == 0 3168eq ( 3169{ 'Syntax error' } , 3170exec_lua ( function () 3171local diagnostics ={ 3172_G . make_error ( 'Syntax error' , 2 , 0 , 2 , 0 ), 3173} 3174vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3175vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3176vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 3177local float_bufnr , winnr = 3178vim . diagnostic . open_float ( { header =false , scope ='cursor' , pos ={ 2 , 0 } } ) 3179local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3180vim . api . nvim_win_close ( winnr , true ) 3181return lines 3182end ) 3183) 3184end ) 3185 3186it ( 3187'creates floating window and returns float bufnr and winnr if current line contains diagnostics' , 3188function () 3189-- Two lines: 3190-- Diagnostic: 3191-- 1. <msg> 3192eq ( 31932 , 3194exec_lua ( function () 3195local diagnostics ={ 3196_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3197} 3198vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3199vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3200local float_bufnr , winnr =vim . diagnostic . open_float ( _G . diagnostic_bufnr ) 3201local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3202vim . api . nvim_win_close ( winnr , true ) 3203return # lines 3204end ) 3205) 3206end 3207) 3208 3209it ( 'only reports diagnostics from the current buffer when bufnr is omitted #15710' , function () 3210eq ( 32112 , 3212exec_lua ( function () 3213local other_bufnr =vim . api . nvim_create_buf ( true , false ) 3214local buf_1_diagnostics ={ 3215_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3216} 3217local buf_2_diagnostics ={ 3218_G . make_warning ( 'Some warning' , 0 , 1 , 0 , 3 ), 3219} 3220vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3221vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , buf_1_diagnostics ) 3222vim . diagnostic . set ( _G . other_ns , other_bufnr , buf_2_diagnostics ) 3223local float_bufnr , winnr =vim . diagnostic . open_float () 3224local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3225vim . api . nvim_win_close ( winnr , true ) 3226return # lines 3227end ) 3228) 3229end ) 3230 3231it ( 'allows filtering by namespace' , function () 3232eq ( 3233{ 'Diagnostics:' , '1. Syntax error' } , 3234exec_lua ( function () 3235local ns_1_diagnostics ={ 3236_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3237} 3238local ns_2_diagnostics ={ 3239_G . make_warning ( 'Some warning' , 0 , 1 , 0 , 3 ), 3240} 3241vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3242vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diagnostics ) 3243vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diagnostics ) 3244local float_bufnr , winnr = 3245vim . diagnostic . open_float ( _G . diagnostic_bufnr , { namespace =_G . diagnostic_ns } ) 3246local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3247vim . api . nvim_win_close ( winnr , true ) 3248return lines 3249end ) 3250) 3251end ) 3252 3253it ( 'allows filtering by multiple namespaces' , function () 3254eq ( 3255{ 'Diagnostics:' , '1. Syntax error' , '2. Some warning' } , 3256exec_lua ( function () 3257local ns_1_diagnostics ={ 3258_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3259} 3260local ns_2_diagnostics ={ 3261_G . make_warning ( 'Some warning' , 0 , 1 , 0 , 3 ), 3262} 3263vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3264vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , ns_1_diagnostics ) 3265vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , ns_2_diagnostics ) 3266local float_bufnr , winnr =vim . diagnostic . open_float ( 3267_G . diagnostic_bufnr , 3268{ namespace ={ _G . diagnostic_ns , _G . other_ns } } 3269) 3270local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3271vim . api . nvim_win_close ( winnr , true ) 3272return lines 3273end ) 3274) 3275end ) 3276 3277it ( 'creates floating window without header and supports function-based float config' , function () 3278-- One line (since no header): 3279-- 1. <msg> 3280eq ( 3281{ 1 , '╭' , '┌' } , 3282exec_lua ( function () 3283local diagnostics ={ 3284_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3285} 3286vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3287vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3288local float_bufnr , winnr = 3289vim . diagnostic . open_float ( _G . diagnostic_bufnr , { header =false } ) 3290local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3291vim . api . nvim_win_close ( winnr , true ) 3292vim . diagnostic . config ( { 3293float =function ( _ , bufnr ) 3294return { border =bufnr == _G . diagnostic_bufnr and 'rounded' or 'single' } 3295end , 3296} ) 3297_ , winnr =vim . diagnostic . open_float () 3298local border1 =vim . api . nvim_win_get_config ( winnr ). border 3299vim . api . nvim_win_close ( winnr , true ) 3300 3301local other_bufnr =vim . api . nvim_create_buf ( true , true ) 3302vim . api . nvim_win_set_buf ( 0 , other_bufnr ) 3303vim . diagnostic . set ( _G . diagnostic_ns , other_bufnr , diagnostics ) 3304local _ , winnr1 =vim . diagnostic . open_float () 3305local border2 =vim . api . nvim_win_get_config ( winnr1 ). border 3306vim . api . nvim_win_close ( winnr1 , true ) 3307return { # lines , border1 [ 1 ], border2 [ 1 ] } 3308end ) 3309) 3310end ) 3311 3312it ( 'clamps diagnostic line numbers within the valid range' , function () 3313eq ( 33141 , 3315exec_lua ( function () 3316local diagnostics ={ 3317_G . make_error ( 'Syntax error' , 6 , 0 , 6 , 0 ), 3318} 3319vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3320vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3321local float_bufnr , winnr = 3322vim . diagnostic . open_float ( _G . diagnostic_bufnr , { header =false , pos =5 } ) 3323local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3324vim . api . nvim_win_close ( winnr , true ) 3325return # lines 3326end ) 3327) 3328end ) 3329 3330it ( 'can show diagnostic source' , function () 3331exec_lua [[vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)]] 3332 3333eq ( 3334{ '1. Syntax error' } , 3335exec_lua ( function () 3336local diagnostics ={ 3337_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 , 'source x' ), 3338} 3339vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3340local float_bufnr , winnr =vim . diagnostic . open_float ( _G . diagnostic_bufnr , { 3341header =false , 3342source ='if_many' , 3343} ) 3344local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3345vim . api . nvim_win_close ( winnr , true ) 3346return lines 3347end ) 3348) 3349 3350eq ( 3351{ '1. source x: Syntax error' } , 3352exec_lua ( function () 3353local float_bufnr , winnr =vim . diagnostic . open_float ( _G . diagnostic_bufnr , { 3354header =false , 3355source ='always' , 3356} ) 3357local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3358vim . api . nvim_win_close ( winnr , true ) 3359return lines 3360end ) 3361) 3362 3363eq ( 3364{ '1. source x: Syntax error' , '2. source y: Another error' } , 3365exec_lua ( function () 3366local diagnostics ={ 3367_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 , 'source x' ), 3368_G . make_error ( 'Another error' , 0 , 1 , 0 , 3 , 'source y' ), 3369} 3370vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3371local float_bufnr , winnr =vim . diagnostic . open_float ( _G . diagnostic_bufnr , { 3372header =false , 3373source ='if_many' , 3374} ) 3375local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3376vim . api . nvim_win_close ( winnr , true ) 3377return lines 3378end ) 3379) 3380end ) 3381 3382it ( 'respects severity_sort' , function () 3383exec_lua [[vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)]] 3384 3385eq ( 3386{ '1. Syntax error' , '2. Info' , '3. Error' , '4. Warning' } , 3387exec_lua ( function () 3388local diagnostics ={ 3389_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3390_G . make_info ( 'Info' , 0 , 3 , 0 , 4 ), 3391_G . make_error ( 'Error' , 0 , 2 , 0 , 2 ), 3392_G . make_warning ( 'Warning' , 0 , 0 , 0 , 1 ), 3393} 3394 3395vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3396 3397vim . diagnostic . config ( { severity_sort =false } ) 3398 3399local float_bufnr , winnr = 3400vim . diagnostic . open_float ( _G . diagnostic_bufnr , { header =false } ) 3401local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3402vim . api . nvim_win_close ( winnr , true ) 3403return lines 3404end ) 3405) 3406 3407eq ( 3408{ '1. Syntax error' , '2. Error' , '3. Warning' , '4. Info' } , 3409exec_lua ( function () 3410vim . diagnostic . config ( { severity_sort =true } ) 3411local float_bufnr , winnr = 3412vim . diagnostic . open_float ( _G . diagnostic_bufnr , { header =false } ) 3413local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3414vim . api . nvim_win_close ( winnr , true ) 3415return lines 3416end ) 3417) 3418 3419eq ( 3420{ '1. Info' , '2. Warning' , '3. Error' , '4. Syntax error' } , 3421exec_lua ( function () 3422vim . diagnostic . config ( { severity_sort ={ reverse =true } } ) 3423local float_bufnr , winnr = 3424vim . diagnostic . open_float ( _G . diagnostic_bufnr , { header =false } ) 3425local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3426vim . api . nvim_win_close ( winnr , true ) 3427return lines 3428end ) 3429) 3430end ) 3431 3432it ( 'can filter by severity' , function () 3433local count_diagnostics_with_severity =function ( min_severity , max_severity ) 3434return exec_lua ( function () 3435vim . diagnostic . config ( { 3436float ={ 3437severity ={ min =min_severity , max =max_severity } , 3438} , 3439} ) 3440 3441vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 3442_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3443_G . make_info ( 'Info' , 0 , 3 , 0 , 4 ), 3444_G . make_error ( 'Error' , 0 , 2 , 0 , 2 ), 3445_G . make_warning ( 'Warning' , 0 , 0 , 0 , 1 ), 3446} ) 3447 3448local float_bufnr , winnr = 3449vim . diagnostic . open_float ( _G . diagnostic_bufnr , { header =false } ) 3450if not float_bufnr then 3451return 0 3452end 3453 3454local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3455vim . api . nvim_win_close ( winnr , true ) 3456return # lines 3457end ) 3458end 3459 3460eq ( 2 , count_diagnostics_with_severity ( 'ERROR' )) 3461eq ( 3 , count_diagnostics_with_severity ( 'WARN' )) 3462eq ( 1 , count_diagnostics_with_severity ( 'WARN' , 'WARN' )) 3463eq ( 4 , count_diagnostics_with_severity ( 'HINT' )) 3464eq ( 0 , count_diagnostics_with_severity ( 'HINT' , 'HINT' )) 3465end ) 3466 3467it ( 'can add a prefix to diagnostics' , function () 3468-- Default is to add a number 3469eq ( 3470{ '1. Syntax error' , '2. Some warning' } , 3471exec_lua ( function () 3472local diagnostics ={ 3473_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3474_G . make_warning ( 'Some warning' , 1 , 1 , 1 , 3 ), 3475} 3476vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3477vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3478local float_bufnr , winnr =vim . diagnostic . open_float ( { header =false , scope ='buffer' } ) 3479local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3480vim . api . nvim_win_close ( winnr , true ) 3481return lines 3482end ) 3483) 3484 3485eq ( 3486{ 'Syntax error' , 'Some warning' } , 3487exec_lua ( function () 3488local diagnostics ={ 3489_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3490_G . make_warning ( 'Some warning' , 1 , 1 , 1 , 3 ), 3491} 3492vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3493vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3494local float_bufnr , winnr = 3495vim . diagnostic . open_float ( { header =false , scope ='buffer' , prefix ='' } ) 3496local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3497vim . api . nvim_win_close ( winnr , true ) 3498return lines 3499end ) 3500) 3501 3502eq ( 3503{ '1. Syntax error' , '2. Some warning' } , 3504exec_lua ( function () 3505local diagnostics ={ 3506_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3507_G . make_warning ( 'Some warning' , 0 , 1 , 0 , 3 ), 3508} 3509vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3510vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3511local float_bufnr , winnr =vim . diagnostic . open_float ( { 3512header =false , 3513prefix =function ( _ , i , total ) 3514-- Only show a number if there is more than one diagnostic 3515if total > 1 then 3516return string . format ( '%d. ' , i ) 3517end 3518return '' 3519end , 3520} ) 3521local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3522vim . api . nvim_win_close ( winnr , true ) 3523return lines 3524end ) 3525) 3526 3527eq ( 3528{ 'Syntax error' } , 3529exec_lua ( function () 3530local diagnostics ={ 3531_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3532} 3533vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3534vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3535local float_bufnr , winnr =vim . diagnostic . open_float ( { 3536header =false , 3537prefix =function ( _ , i , total ) 3538-- Only show a number if there is more than one diagnostic 3539if total > 1 then 3540return string . format ( '%d. ' , i ) 3541end 3542return '' 3543end , 3544} ) 3545local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3546vim . api . nvim_win_close ( winnr , true ) 3547return lines 3548end ) 3549) 3550 3551eq ( 3552'.../_float.lua:0: prefix: expected string|table|function, got number' , 3553pcall_err ( exec_lua , [[ vim.diagnostic.open_float({ prefix = 42 }) ]] ) 3554) 3555end ) 3556 3557it ( 'can add a suffix to diagnostics' , function () 3558-- Default is to render the diagnostic error code 3559eq ( 3560{ '1. Syntax error [code-x]' , '2. Some warning [code-y]' } , 3561exec_lua ( function () 3562local diagnostics ={ 3563_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 , nil , 'code-x' ), 3564_G . make_warning ( 'Some warning' , 1 , 1 , 1 , 3 , nil , 'code-y' ), 3565} 3566vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3567vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3568local float_bufnr , winnr =vim . diagnostic . open_float ( { header =false , scope ='buffer' } ) 3569local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3570vim . api . nvim_win_close ( winnr , true ) 3571return lines 3572end ) 3573) 3574 3575eq ( 3576{ '1. Syntax error' , '2. Some warning' } , 3577exec_lua ( function () 3578local diagnostics ={ 3579_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 , nil , 'code-x' ), 3580_G . make_warning ( 'Some warning' , 1 , 1 , 1 , 3 , nil , 'code-y' ), 3581} 3582vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3583vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3584local float_bufnr , winnr = 3585vim . diagnostic . open_float ( { header =false , scope ='buffer' , suffix ='' } ) 3586local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3587vim . api . nvim_win_close ( winnr , true ) 3588return lines 3589end ) 3590) 3591 3592-- Suffix is rendered on the last line of a multiline diagnostic 3593eq ( 3594{ '1. Syntax error' , ' More context [code-x]' } , 3595exec_lua ( function () 3596local diagnostics ={ 3597_G . make_error ( 'Syntax error\nMore context' , 0 , 1 , 0 , 3 , nil , 'code-x' ), 3598} 3599vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3600vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3601local float_bufnr , winnr =vim . diagnostic . open_float ( { header =false , scope ='buffer' } ) 3602local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3603vim . api . nvim_win_close ( winnr , true ) 3604return lines 3605end ) 3606) 3607 3608eq ( 3609'.../_float.lua:0: suffix: expected string|table|function, got number' , 3610pcall_err ( exec_lua , [[ vim.diagnostic.open_float({ suffix = 42 }) ]] ) 3611) 3612end ) 3613 3614it ( 'can add LSP related information to a diagnostic' , function () 3615local related_info_uri ='file:///fake/uri' 3616 3617-- Populate the related info buffer. 3618exec_lua ( function () 3619local fake_bufnr =vim . uri_to_bufnr ( related_info_uri ) 3620vim . fn . bufload ( fake_bufnr ) 3621vim . api . nvim_buf_set_lines ( fake_bufnr , 0 , 1 , false , { 3622'O, the Pelican,' , 3623'so smoothly doth he crest.' , 3624'a wind god!' , 3625} ) 3626vim . api . nvim_win_set_buf ( 0 , fake_bufnr ) 3627end ) 3628 3629-- Displays related info. 3630eq ( 3631{ 3632'1. Some warning' , 3633' uri:2:1: Some extra info' , 3634' uri:3:4: Some more extra info' , 3635} , 3636exec_lua ( function () 3637---@type vim.Diagnostic 3638local diagnostic =_G . make_warning ( 'Some warning' , 1 , 1 , 1 , 3 ) 3639diagnostic . user_data ={ 3640-- Related information comes from LSP user_data 3641lsp ={ 3642relatedInformation ={ 3643{ 3644message ='Some extra info' , 3645location ={ 3646uri =related_info_uri , 3647range ={ 3648start ={ 3649line =1 , 3650character =0 , 3651} , 3652[ 'end' ] ={ 3653line =1 , 3654character =1 , 3655} , 3656} , 3657} , 3658} , 3659{ 3660message ='Some more extra info' , 3661location ={ 3662uri =related_info_uri , 3663range ={ 3664start ={ 3665line =2 , 3666character =3 , 3667} , 3668[ 'end' ] ={ 3669line =4 , 3670character =5 , 3671} , 3672} , 3673} , 3674} , 3675} , 3676} , 3677} 3678vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3679vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { diagnostic } ) 3680local float_bufnr , winnr =vim . diagnostic . open_float ( { header =false , scope ='buffer' } ) 3681local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3682-- Put the cursor on a line with related info 3683vim . api . nvim_tabpage_set_win ( 0 , winnr ) 3684vim . api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 3685return lines 3686end ) 3687) 3688 3689-- Jumps to related info. 3690eq ( 3691'so smoothly doth he crest.' , 3692exec_lua ( function () 3693vim . cmd . norm ( 'gf' ) 3694vim . wait ( 20 , function () end ) 3695return vim . api . nvim_get_current_line () 3696end ) 3697) 3698end ) 3699 3700it ( 'errors when LSP relatedInformation is null' , function () 3701matches ( 3702'server response has invalid %(null%) relatedInformation' , 3703pcall_err ( 3704exec_lua , 3705[[ 3706local diagnostic = _G.make_warning('Some warning', 1, 1, 1, 3) 3707diagnostic.user_data = { lsp = { relatedInformation = vim.NIL } } 3708vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr) 3709vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { diagnostic }) 3710vim.diagnostic.open_float({ header = false, scope = 'buffer' }) 3711]] 3712) 3713) 3714end ) 3715 3716it ( 'works with the old signature' , function () 3717eq ( 3718{ '1. Syntax error' } , 3719exec_lua ( function () 3720local diagnostics ={ 3721_G . make_error ( 'Syntax error' , 0 , 1 , 0 , 3 ), 3722} 3723vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3724vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3725local float_bufnr , winnr =vim . diagnostic . open_float ( 0 , { header =false } ) 3726local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3727vim . api . nvim_win_close ( winnr , true ) 3728return lines 3729end ) 3730) 3731end ) 3732 3733it ( 'works for multi-line diagnostics #21949' , function () 3734-- create diagnostic 3735exec_lua ( function () 3736local diagnostics ={ 3737_G . make_error ( 'Error in two lines lnum is 1 and end_lnum is 2' , 1 , 1 , 2 , 3 ), 3738} 3739vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3740vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , diagnostics ) 3741end ) 3742 3743-- open float failed non diagnostic lnum 3744eq ( 3745nil , 3746exec_lua ( function () 3747vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 3748local _ , winnr =vim . diagnostic . open_float ( 0 , { header =false } ) 3749return winnr 3750end ) 3751) 3752eq ( 3753nil , 3754exec_lua ( function () 3755vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 3756local _ , winnr =vim . diagnostic . open_float ( 0 , { header =false , scope ='cursor' } ) 3757return winnr 3758end ) 3759) 3760 3761-- can open a float window on lnum 1 3762eq ( 3763{ '1. Error in two lines lnum is 1 and end_lnum is 2' } , 3764exec_lua ( function () 3765vim . api . nvim_win_set_cursor ( 0 , { 2 , 0 } ) 3766local float_bufnr , winnr =vim . diagnostic . open_float ( 0 , { header =false } ) 3767local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3768vim . api . nvim_win_close ( winnr , true ) 3769return lines 3770end ) 3771) 3772 3773-- can open a cursor-scoped float window on lnum 1 3774eq ( 3775{ 'Error in two lines lnum is 1 and end_lnum is 2' } , 3776exec_lua ( function () 3777vim . api . nvim_win_set_cursor ( 0 , { 2 , 1 } ) 3778local float_bufnr , winnr = 3779vim . diagnostic . open_float ( 0 , { header =false , scope ='cursor' } ) 3780local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3781vim . api . nvim_win_close ( winnr , true ) 3782return lines 3783end ) 3784) 3785 3786-- can open a float window on end_lnum 2 3787eq ( 3788{ '1. Error in two lines lnum is 1 and end_lnum is 2' } , 3789exec_lua ( function () 3790vim . api . nvim_win_set_cursor ( 0 , { 3 , 0 } ) 3791local float_bufnr , winnr =vim . diagnostic . open_float ( 0 , { header =false } ) 3792local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3793vim . api . nvim_win_close ( winnr , true ) 3794return lines 3795end ) 3796) 3797 3798-- can open a cursor-scoped float window on end_lnum 2 3799eq ( 3800{ 'Error in two lines lnum is 1 and end_lnum is 2' } , 3801exec_lua ( function () 3802vim . api . nvim_win_set_cursor ( 0 , { 3 , 2 } ) 3803local float_bufnr , winnr = 3804vim . diagnostic . open_float ( 0 , { header =false , scope ='cursor' } ) 3805local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3806vim . api . nvim_win_close ( winnr , true ) 3807return lines 3808end ) 3809) 3810end ) 3811 3812it ( 'shows diagnostics at their logical locations after text changes before' , function () 3813exec_lua ( function () 3814vim . api . nvim_set_current_buf ( _G . diagnostic_bufnr ) 3815 3816vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 3817_G . make_error ( 'Diagnostic #1' , 1 , 4 , 1 , 7 ), 3818_G . make_error ( 'Diagnostic #2' , 3 , 0 , 3 , 3 ), 3819} ) 3820end ) 3821 3822api . nvim_buf_set_text ( 0 , 3 , 0 , 3 , 0 , { 'new line' , 'new ' } ) 3823 3824eq ( 3825{ 'Diagnostic #1' } , 3826exec_lua ( function () 3827vim . api . nvim_win_set_cursor ( 0 , { 2 , 4 } ) 3828local float_bufnr , winnr =vim . diagnostic . open_float ( { header ='' , scope ='cursor' } ) 3829local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3830vim . api . nvim_win_close ( winnr , true ) 3831return lines 3832end ) 3833) 3834 3835eq ( 3836{ 'Diagnostic #2' } , 3837exec_lua ( function () 3838vim . api . nvim_win_set_cursor ( 0 , { 5 , 4 } ) 3839local float_bufnr , winnr =vim . diagnostic . open_float ( { header ='' , scope ='cursor' } ) 3840local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3841vim . api . nvim_win_close ( winnr , true ) 3842return lines 3843end ) 3844) 3845end ) 3846 3847it ( 'shows diagnostics at their logical locations after text changes inside' , function () 3848exec_lua ( function () 3849vim . api . nvim_set_current_buf ( _G . diagnostic_bufnr ) 3850 3851vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 3852_G . make_error ( 'Diagnostic #1' , 1 , 0 , 1 , 7 ), 3853} ) 3854end ) 3855 3856api . nvim_buf_set_text ( 0 , 1 , 4 , 1 , 4 , { 'new ' } ) 3857 3858eq ( 3859{ 'Diagnostic #1' } , 3860exec_lua ( function () 3861vim . api . nvim_win_set_cursor ( 0 , { 2 , 10 } ) 3862local float_bufnr , winnr =vim . diagnostic . open_float ( { header ='' , scope ='cursor' } ) 3863local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3864vim . api . nvim_win_close ( winnr , true ) 3865return lines 3866end ) 3867) 3868end ) 3869 3870it ( 3871'shows diagnostics at the end of the line if diagnostic is set after last character in line' , 3872function () 3873exec_lua ( function () 3874vim . api . nvim_set_current_buf ( _G . diagnostic_bufnr ) 3875 3876vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 3877_G . make_error ( 'Diagnostic #1' , 2 , 3 , 3 , 4 ), 3878} ) 3879end ) 3880 3881eq ( 3882{ 'Diagnostic #1' } , 3883exec_lua ( function () 3884vim . api . nvim_win_set_cursor ( 0 , { 3 , 2 } ) 3885local float_bufnr , winnr =vim . diagnostic . open_float ( { header ='' , scope ='cursor' } ) 3886local lines =vim . api . nvim_buf_get_lines ( float_bufnr , 0 , - 1 , false ) 3887vim . api . nvim_win_close ( winnr , true ) 3888return lines 3889end ) 3890) 3891end 3892) 3893end ) 3894 3895describe ( 'setloclist()' , function () 3896it ( 'sets diagnostics in lnum order' , function () 3897local loc_list =exec_lua ( function () 3898vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3899 3900vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 3901_G . make_error ( 'Farther Diagnostic' , 4 , 4 , 4 , 4 ), 3902_G . make_error ( 'Lower Diagnostic' , 1 , 1 , 1 , 1 ), 3903} ) 3904 3905vim . diagnostic . setloclist () 3906 3907return vim . fn . getloclist ( 0 ) 3908end ) 3909 3910assert ( loc_list [ 1 ]. lnum < loc_list [ 2 ]. lnum ) 3911end ) 3912 3913it ( 'sets diagnostics in lnum order, regardless of namespace' , function () 3914local loc_list =exec_lua ( function () 3915vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3916 3917vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 3918_G . make_error ( 'Lower Diagnostic' , 1 , 1 , 1 , 1 ), 3919} ) 3920 3921vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , { 3922_G . make_warning ( 'Farther Diagnostic' , 4 , 4 , 4 , 4 ), 3923} ) 3924 3925vim . diagnostic . setloclist () 3926 3927return vim . fn . getloclist ( 0 ) 3928end ) 3929 3930assert ( loc_list [ 1 ]. lnum < loc_list [ 2 ]. lnum ) 3931end ) 3932 3933it ( 'sets diagnostics from the specified namespaces' , function () 3934local loc_list =exec_lua ( function () 3935vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3936 3937vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 3938_G . make_error ( 'Error here!' , 1 , 1 , 1 , 1 ), 3939} ) 3940vim . diagnostic . set ( _G . other_ns , _G . diagnostic_bufnr , { 3941_G . make_warning ( 'Error there!' , 2 , 2 , 2 , 2 ), 3942} ) 3943 3944vim . diagnostic . setloclist ( { namespace ={ _G . diagnostic_ns } } ) 3945 3946return vim . fn . getloclist ( 0 ) 3947end ) 3948 3949eq ( 1 , # loc_list ) 3950eq ( 'Error here!' , loc_list [ 1 ]. text ) 3951end ) 3952 3953it ( 'supports format function' , function () 3954local loc_list =exec_lua ( function () 3955vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3956 3957vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 3958_G . make_error ( 'Error' , 1 , 1 , 1 , 1 , 'foo_ls' ), 3959_G . make_error ( 'Another error' , 2 , 2 , 2 , 2 , 'foo_ls' ), 3960} ) 3961 3962vim . diagnostic . setloclist ( { 3963format =function ( diagnostic ) 3964if diagnostic . lnum > 1 then 3965return nil 3966end 3967 3968return string . format ( '%s: %s' , diagnostic . source , diagnostic . message ) 3969end , 3970} ) 3971 3972return vim . fn . getloclist ( 0 ) 3973end ) 3974 3975eq ( 1 , # loc_list ) 3976eq ( 'foo_ls: Error' , loc_list [ 1 ]. text ) 3977end ) 3978end ) 3979 3980describe ( 'setqflist()' , function () 3981it ( 'updates existing diagnostics quickfix if one already exists' , function () 3982local result =exec_lua ( function () 3983vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 3984 3985vim . fn . setqflist ( {} , ' ' , { title ='Diagnostics' } ) 3986local diagnostics_qf_id =vim . fn . getqflist ( { id =0 } ). id 3987 3988vim . diagnostic . setqflist ( { title ='Diagnostics' } ) 3989local qf_id =vim . fn . getqflist ( { id =0 , nr ='$' } ). id 3990 3991return { diagnostics_qf_id , qf_id } 3992end ) 3993 3994eq ( result [ 1 ], result [ 2 ]) 3995end ) 3996 3997it ( 'navigates to existing diagnostics quickfix if one already exists and open=true' , function () 3998local result =exec_lua ( function () 3999vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 4000 4001vim . fn . setqflist ( {} , ' ' , { title ='Diagnostics' } ) 4002local diagnostics_qf_id =vim . fn . getqflist ( { id =0 } ). id 4003 4004vim . fn . setqflist ( {} , ' ' , { title ='Other' } ) 4005 4006vim . diagnostic . setqflist ( { title ='Diagnostics' , open =true } ) 4007local qf_id =vim . fn . getqflist ( { id =0 } ). id 4008 4009return { diagnostics_qf_id , qf_id } 4010end ) 4011 4012eq ( result [ 1 ], result [ 2 ]) 4013end ) 4014 4015it ( 'sets new diagnostics quickfix as active when open=true' , function () 4016local result =exec_lua ( function () 4017vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 4018 4019vim . fn . setqflist ( {} , ' ' , { title ='Other' } ) 4020local other_qf_id =vim . fn . getqflist ( { id =0 } ). id 4021 4022vim . diagnostic . setqflist ( { title ='Diagnostics' , open =true } ) 4023local qf_id =vim . fn . getqflist ( { id =0 } ). id 4024 4025return { other_qf_id , qf_id } 4026end ) 4027 4028neq ( result [ 1 ], result [ 2 ]) 4029end ) 4030 4031it ( 'opens quickfix window when open=true' , function () 4032local qf_winid =exec_lua ( function () 4033vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 4034 4035vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 4036_G . make_error ( 'Error' , 1 , 1 , 1 , 1 ), 4037} ) 4038 4039vim . diagnostic . setqflist ( { open =true } ) 4040 4041return vim . fn . getqflist ( { winid =0 } ). winid 4042end ) 4043 4044neq ( 0 , qf_winid ) 4045end ) 4046 4047it ( 'supports format function' , function () 4048local qf_list =exec_lua ( function () 4049vim . api . nvim_win_set_buf ( 0 , _G . diagnostic_bufnr ) 4050 4051vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 4052_G . make_error ( 'Error' , 1 , 1 , 1 , 1 , 'foo_ls' ), 4053_G . make_error ( 'Another error' , 2 , 2 , 2 , 2 , 'foo_ls' ), 4054} ) 4055 4056vim . diagnostic . setqflist ( { 4057format =function ( diagnostic ) 4058if diagnostic . lnum > 1 then 4059return nil 4060end 4061 4062return string . format ( '%s: %s' , diagnostic . source , diagnostic . message ) 4063end , 4064} ) 4065 4066return vim . fn . getqflist () 4067end ) 4068 4069eq ( 1 , # qf_list ) 4070eq ( 'foo_ls: Error' , qf_list [ 1 ]. text ) 4071end ) 4072end ) 4073 4074describe ( 'match()' , function () 4075it ( 'matches a string' , function () 4076local msg ='ERROR: george.txt:19:84:Two plus two equals five' 4077local diagnostic ={ 4078severity =exec_lua [[return vim.diagnostic.severity.ERROR]] , 4079lnum =18 , 4080col =83 , 4081end_lnum =18 , 4082end_col =83 , 4083message ='Two plus two equals five' , 4084} 4085eq ( 4086diagnostic , 4087exec_lua ( function () 4088return vim . diagnostic . match ( 4089msg , 4090'^(%w+): [^:]+:(%d+):(%d+):(.+)$' , 4091{ 'severity' , 'lnum' , 'col' , 'message' } 4092) 4093end ) 4094) 4095end ) 4096 4097it ( 'returns nil if the pattern fails to match' , function () 4098eq ( 4099nil , 4100exec_lua ( function () 4101local msg ='The answer to life, the universe, and everything is' 4102return vim . diagnostic . match ( msg , 'This definitely will not match' , {} ) 4103end ) 4104) 4105end ) 4106 4107it ( 'respects default values' , function () 4108local msg ='anna.txt:1:Happy families are all alike' 4109local diagnostic ={ 4110severity =exec_lua [[return vim.diagnostic.severity.INFO]] , 4111lnum =0 , 4112col =0 , 4113end_lnum =0 , 4114end_col =0 , 4115message ='Happy families are all alike' , 4116} 4117eq ( 4118diagnostic , 4119exec_lua ( function () 4120return vim . diagnostic . match ( 4121msg , 4122'^[^:]+:(%d+):(.+)$' , 4123{ 'lnum' , 'message' } , 4124nil , 4125{ severity =vim . diagnostic . severity . INFO } 4126) 4127end ) 4128) 4129end ) 4130 4131it ( 'accepts a severity map' , function () 4132local msg ='46:FATAL:Et tu, Brute?' 4133local diagnostic ={ 4134severity =exec_lua [[return vim.diagnostic.severity.ERROR]] , 4135lnum =45 , 4136col =0 , 4137end_lnum =45 , 4138end_col =0 , 4139message ='Et tu, Brute?' , 4140} 4141eq ( 4142diagnostic , 4143exec_lua ( function () 4144return vim . diagnostic . match ( 4145msg , 4146'^(%d+):(%w+):(.+)$' , 4147{ 'lnum' , 'severity' , 'message' } , 4148{ FATAL =vim . diagnostic . severity . ERROR } 4149) 4150end ) 4151) 4152end ) 4153end ) 4154 4155describe ( 'toqflist(), fromqflist()' , function () 4156it ( 'works' , function () 4157local result =exec_lua ( function () 4158vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 4159_G . make_error ( 'Error 1' , 0 , 1 , 0 , 1 ), 4160_G . make_error ( 'Error 2' , 1 , 1 , 1 , 1 ), 4161_G . make_warning ( 'Warning' , 2 , 2 , 2 , 2 ), 4162} ) 4163 4164local diagnostics =vim . diagnostic . get ( _G . diagnostic_bufnr ) 4165vim . fn . setqflist ( vim . diagnostic . toqflist ( diagnostics )) 4166local list =vim . fn . getqflist () 4167local new_diagnostics =vim . diagnostic . fromqflist ( list ) 4168 4169-- Remove extra properties not present in the return value of fromlist() 4170for _ , v in ipairs ( diagnostics ) do 4171v . _extmark_id =nil 4172v . namespace =nil 4173end 4174 4175return { diagnostics , new_diagnostics } 4176end ) 4177eq ( result [ 1 ], result [ 2 ]) 4178end ) 4179 4180it ( 'uses the start position when quickfix coordinates are absent' , function () 4181eq ( 4182{ 4183{ 4184lnum =0 , 4185col =0 , 4186end_lnum =0 , 4187end_col =0 , 4188severity =vim . diagnostic . severity . ERROR , 4189message ='missing coordinates' , 4190} , 4191{ 4192lnum =3 , 4193col =2 , 4194end_lnum =3 , 4195end_col =2 , 4196severity =vim . diagnostic . severity . ERROR , 4197message ='missing end coordinates' , 4198} , 4199} , 4200exec_lua ( function () 4201return vim . diagnostic . fromqflist ( { 4202{ valid =1 , nr =0 , text ='missing coordinates' } , 4203{ valid =1 , nr =0 , lnum =4 , col =3 , text ='missing end coordinates' } , 4204} ) 4205end ) 4206) 4207end ) 4208 4209it ( 'merge_lines=true merges continuation lines' , function () 4210local function get_fromqflist ( merge_lines ) 4211return exec_lua ( function ( merge_lines_ ) 4212local qflist ={ 4213{ 4214bufnr =1 , 4215lnum =10 , 4216col =5 , 4217end_lnum =10 , 4218end_col =10 , 4219text ='error: [GHC-83865]' , 4220type ='E' , 4221nr =0 , 4222valid =1 , 4223} , 4224{ 4225bufnr =1 , 4226lnum =0 , 4227col =0 , 4228end_lnum =0 , 4229end_col =0 , 4230text =" Couldn't match expected type" , 4231type ='' , 4232nr =0 , 4233valid =0 , 4234} , 4235{ 4236bufnr =1 , 4237lnum =0 , 4238col =0 , 4239end_lnum =0 , 4240end_col =0 , 4241text =' with actual type' , 4242type ='' , 4243nr =0 , 4244valid =0 , 4245} , 4246{ 4247bufnr =1 , 4248lnum =20 , 4249col =1 , 4250end_lnum =20 , 4251end_col =5 , 4252text ='warning: unused' , 4253type ='W' , 4254nr =0 , 4255valid =1 , 4256} , 4257} 4258return vim . diagnostic . fromqflist ( qflist , { merge_lines =merge_lines_ } ) 4259end , merge_lines ) 4260end 4261 4262-- merge_lines=true 4263local result =get_fromqflist ( true ) 4264eq ( 2 , # result ) 4265eq ( 4266"error: [GHC-83865]\n Couldn't match expected type\n with actual type" , 4267result [ 1 ]. message 4268) 4269eq ( 'warning: unused' , result [ 2 ]. message ) 4270 4271-- merge_lines=false 4272result =get_fromqflist ( false ) 4273eq ( 2 , # result ) 4274eq ( 'error: [GHC-83865]' , result [ 1 ]. message ) 4275end ) 4276end ) 4277 4278describe ( 'status()' , function () 4279it ( 'returns empty string if no diagnostics' , function () 4280local result =exec_lua ( function () 4281vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , {} ) 4282return vim . diagnostic . status () 4283end ) 4284 4285eq ( '' , result ) 4286end ) 4287 4288it ( 'returns count for each diagnostic kind' , function () 4289local result =exec_lua ( function () 4290vim . diagnostic . set ( _G . diagnostic_ns , 0 , { 4291_G . make_error ( 'Error 1' , 0 , 1 , 0 , 1 ), 4292 4293_G . make_warning ( 'Warning 1' , 2 , 2 , 2 , 2 ), 4294_G . make_warning ( 'Warning 2' , 2 , 2 , 2 , 2 ), 4295 4296_G . make_info ( 'Info 1' , 3 , 3 , 3 , 3 ), 4297_G . make_info ( 'Info 2' , 3 , 3 , 3 , 3 ), 4298_G . make_info ( 'Info 3' , 3 , 3 , 3 , 3 ), 4299 4300_G . make_hint ( 'Hint 1' , 4 , 4 , 4 , 4 ), 4301_G . make_hint ( 'Hint 2' , 4 , 4 , 4 , 4 ), 4302_G . make_hint ( 'Hint 3' , 4 , 4 , 4 , 4 ), 4303_G . make_hint ( 'Hint 4' , 4 , 4 , 4 , 4 ), 4304} ) 4305return vim . diagnostic . status () 4306end ) 4307 4308eq ( 4309'%#(%$DiagnosticSignError$E:1%#) %#(%$DiagnosticSignWarn$W:2%#) %#(%$DiagnosticSignInfo$I:3%#) %#(%$DiagnosticSignHint$H:4%#)' , 4310result 4311) 4312 4313exec_lua ( 'vim.cmd.enew()' ) 4314 4315-- Empty diagnostics for a buffer without diagnostics 4316eq ( 4317'' , 4318exec_lua ( function () 4319return vim . diagnostic . status () 4320end ) 4321) 4322end ) 4323 4324it ( 'uses text from diagnostic.config().signs.text[severity]' , function () 4325local result =exec_lua ( function () 4326vim . diagnostic . config ( { 4327signs ={ 4328text ={ 4329[ vim . diagnostic . severity . ERROR ] ='⨯' , 4330[ vim . diagnostic . severity . WARN ] ='⚠︎' , 4331} , 4332} , 4333} ) 4334 4335vim . diagnostic . set ( _G . diagnostic_ns , 0 , { 4336_G . make_error ( 'Error 1' , 0 , 1 , 0 , 1 ), 4337_G . make_warning ( 'Warning 1' , 2 , 2 , 2 , 2 ), 4338} ) 4339 4340return vim . diagnostic . status () 4341end ) 4342 4343eq ( '%#(%$DiagnosticSignError$⨯:1%#) %#(%$DiagnosticSignWarn$⚠︎:1%#)' , result ) 4344end ) 4345 4346it ( 'works when signs are disabled' , function () 4347local result =exec_lua ( function () 4348vim . diagnostic . config ( { signs =false } ) 4349 4350vim . diagnostic . set ( _G . diagnostic_ns , 0 , { 4351_G . make_error ( 'Error 1' , 0 , 1 , 0 , 1 ), 4352_G . make_warning ( 'Warning 1' , 2 , 2 , 2 , 2 ), 4353} ) 4354 4355return vim . diagnostic . status () 4356end ) 4357 4358eq ( '%#(%$DiagnosticSignError$E:1%#) %#(%$DiagnosticSignWarn$W:1%#)' , result ) 4359end ) 4360 4361it ( 'uses format function diagnostic.config().status.format' , function () 4362local result =exec_lua ( function () 4363local signs ={ 4364[ vim . diagnostic . severity . ERROR ] ='EE' , 4365[ vim . diagnostic . severity . WARN ] ='WW' , 4366[ vim . diagnostic . severity . INFO ] ='II' , 4367[ vim . diagnostic . severity . HINT ] ='HH' , 4368} 4369local hl_map ={ 4370[ vim . diagnostic . severity . ERROR ] ='ERROR' , 4371[ vim . diagnostic . severity . WARN ] ='WARN' , 4372[ vim . diagnostic . severity . INFO ] ='INFO' , 4373[ vim . diagnostic . severity . HINT ] ='HINT' , 4374} 4375vim . diagnostic . config ( { 4376status ={ 4377format =function ( counts ) 4378local items ={} 4379for severity , sign in ipairs ( signs ) do 4380local count =counts [ severity ] or 0 4381local hl =hl_map [ severity ] 4382table . insert ( items , ( '%%#%s#%s %s' ): format ( hl , sign , count )) 4383end 4384return table . concat ( items , ' ' ) 4385end , 4386} , 4387} ) 4388 4389vim . diagnostic . set ( _G . diagnostic_ns , 0 , { 4390_G . make_error ( 'Error 1' , 0 , 1 , 0 , 1 ), 4391_G . make_warning ( 'Warning 1' , 2 , 2 , 2 , 2 ), 4392} ) 4393 4394return vim . diagnostic . status () 4395end ) 4396 4397eq ( '%#(%#ERROR#EE 1 %#WARN#WW 1 %#INFO#II 0 %#HINT#HH 0%#)' , result ) 4398end ) 4399end ) 4400 4401describe ( 'handlers' , function () 4402it ( 'checks that a new handler is a table' , function () 4403matches ( 4404[[.*handler: expected table, got string.*]] , 4405pcall_err ( exec_lua , [[ vim.diagnostic.handlers.foo = "bar" ]] ) 4406) 4407matches ( 4408[[.*handler: expected table, got function.*]] , 4409pcall_err ( exec_lua , [[ vim.diagnostic.handlers.foo = function() end ]] ) 4410) 4411end ) 4412 4413it ( 'can add new handlers' , function () 4414eq ( 4415true , 4416exec_lua ( function () 4417local handler_called =false 4418vim . diagnostic . handlers . test ={ 4419show =function ( namespace , bufnr , diagnostics , opts ) 4420assert ( namespace == _G . diagnostic_ns ) 4421assert ( bufnr == _G . diagnostic_bufnr ) 4422assert ( # diagnostics == 1 ) 4423assert ( opts . test . some_opt == 42 ) 4424handler_called =true 4425end , 4426} 4427 4428vim . diagnostic . config ( { test ={ some_opt =42 } } ) 4429vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 4430_G . make_warning ( 'Warning' , 0 , 0 , 0 , 0 ), 4431} ) 4432return handler_called 4433end ) 4434) 4435end ) 4436 4437it ( 'can disable handlers by setting the corresponding option to false' , function () 4438eq ( 4439false , 4440exec_lua ( function () 4441local handler_called =false 4442vim . diagnostic . handlers . test ={ 4443show =function ( _ , _ , _ , _ ) 4444handler_called =true 4445end , 4446} 4447 4448vim . diagnostic . config ( { test =false } ) 4449vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 4450_G . make_warning ( 'Warning' , 0 , 0 , 0 , 0 ), 4451} ) 4452return handler_called 4453end ) 4454) 4455end ) 4456 4457it ( "always calls a handler's hide function if defined" , function () 4458eq ( 4459{ false , true } , 4460exec_lua ( function () 4461local hide_called =false 4462local show_called =false 4463vim . diagnostic . handlers . test ={ 4464show =function ( _ , _ , _ , _ ) 4465show_called =true 4466end , 4467hide =function ( namespace , bufnr ) 4468assert ( namespace == _G . diagnostic_ns ) 4469assert ( bufnr == _G . diagnostic_bufnr ) 4470hide_called =true 4471end , 4472} 4473 4474vim . diagnostic . config ( { test =false } ) 4475vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 4476_G . make_warning ( 'Warning' , 0 , 0 , 0 , 0 ), 4477} ) 4478vim . diagnostic . hide ( _G . diagnostic_ns , _G . diagnostic_bufnr ) 4479return { show_called , hide_called } 4480end ) 4481) 4482end ) 4483 4484it ( 'triggers the autocommand when diagnostics are set' , function () 4485eq ( 4486{ true , true } , 4487exec_lua ( function () 4488-- Set a different buffer as current to test that <abuf> is being set properly in 4489-- DiagnosticChanged callbacks 4490local tmp =vim . api . nvim_create_buf ( false , true ) 4491vim . api . nvim_set_current_buf ( tmp ) 4492 4493local triggered ={} 4494vim . api . nvim_create_autocmd ( 'DiagnosticChanged' , { 4495callback =function ( ev ) 4496triggered ={ ev . buf , # ev . data . diagnostics } 4497end , 4498} ) 4499vim . api . nvim_buf_set_name ( _G . diagnostic_bufnr , 'test | test' ) 4500vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 4501_G . make_error ( 'Diagnostic' , 0 , 0 , 0 , 0 ), 4502} ) 4503return { 4504triggered [ 1 ] == _G . diagnostic_bufnr , 4505triggered [ 2 ] == 1 , 4506} 4507end ) 4508) 4509end ) 4510 4511it ( 'triggers the autocommand when diagnostics are cleared' , function () 4512eq ( 4513true , 4514exec_lua ( function () 4515local tmp =vim . api . nvim_create_buf ( false , true ) 4516vim . api . nvim_set_current_buf ( tmp ) 4517vim . g . diagnostic_autocmd_triggered =0 4518vim . cmd ( 4519'autocmd DiagnosticChanged * let g:diagnostic_autocmd_triggered = +expand("<abuf>")' 4520) 4521vim . api . nvim_buf_set_name ( _G . diagnostic_bufnr , 'test | test' ) 4522vim . diagnostic . reset ( _G . diagnostic_ns , _G . diagnostic_bufnr ) 4523return vim . g . diagnostic_autocmd_triggered == _G . diagnostic_bufnr 4524end ) 4525) 4526end ) 4527 4528it ( 'is_enabled' , function () 4529eq ( 4530{ false , false , false , false , false } , 4531exec_lua ( function () 4532vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 4533_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 4534} ) 4535vim . api . nvim_set_current_buf ( _G . diagnostic_bufnr ) 4536vim . diagnostic . enable ( false ) 4537return { 4538vim . diagnostic . is_enabled (), 4539vim . diagnostic . is_enabled { bufnr =0 } , 4540vim . diagnostic . is_enabled { bufnr =_G . diagnostic_bufnr } , 4541vim . diagnostic . is_enabled { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } , 4542vim . diagnostic . is_enabled { bufnr =0 , ns_id =_G . diagnostic_ns } , 4543} 4544end ) 4545) 4546 4547eq ( 4548{ true , true , true , true , true } , 4549exec_lua ( function () 4550vim . diagnostic . enable () 4551return { 4552vim . diagnostic . is_enabled (), 4553vim . diagnostic . is_enabled { bufnr =0 } , 4554vim . diagnostic . is_enabled { bufnr =_G . diagnostic_bufnr } , 4555vim . diagnostic . is_enabled { bufnr =_G . diagnostic_bufnr , ns_id =_G . diagnostic_ns } , 4556vim . diagnostic . is_enabled { bufnr =0 , ns_id =_G . diagnostic_ns } , 4557} 4558end ) 4559) 4560end ) 4561 4562it ( 'does not redraw for buffer not in window' , function () 4563local did_status =exec_lua ( function () 4564_G . Status =function () 4565_G . did_status =( _G . did_status or 0 ) + 1 4566end 4567vim . o . laststatus , vim . o . statusline =2 , '%!v:lua._G.Status()' 4568vim . diagnostic . set ( _G . diagnostic_ns , _G . diagnostic_bufnr , { 4569_G . make_error ( 'Diagnostic #1' , 1 , 1 , 1 , 1 ), 4570} ) 4571return _G . did_status 4572end ) 4573eq ( nil , did_status ) 4574end ) 4575end ) 4576end )