robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local api =vim . api 2local nvim_on =require ( 'vim._core.util' ). nvim_on 3local diagnostic =vim . diagnostic 4local diagnostic_shared =require ( 'vim.diagnostic._shared' ) 5 6local severity =diagnostic . severity 7 8--- @class vim.diagnostic.Handler 9--- @field show? fun(namespace: integer, bufnr: integer, diagnostics: vim.Diagnostic[], opts?: vim.diagnostic.OptsResolved) 10--- @field hide? fun(namespace:integer, bufnr:integer) 11 12--- @class (private) vim.diagnostic._handlers._extmark : vim.api.keyset.get_extmark_item 13--- @field [1] integer extmark_id 14--- @field [2] integer row 15--- @field [3] integer col 16--- @field [4] vim.api.keyset.extmark_details 17 18local M ={} 19 20-- Default diagnostic highlights 21--- @type table<vim.diagnostic.Severity, string> 22local severity_names ={ 23[ severity . ERROR ] ='Error' , 24[ severity . WARN ] ='Warn' , 25[ severity . INFO ] ='Info' , 26[ severity . HINT ] ='Hint' , 27} 28 29--- @param base_name string 30--- @return table<vim.diagnostic.Severity, string> 31local function make_highlight_map ( base_name ) 32local result ={} --- @type table<vim.diagnostic.Severity, string> 33 34for level , name in pairs ( severity_names ) do 35result [ level ] =( 'Diagnostic%s%s' ): format ( base_name , name ) 36end 37 38return result 39end 40 41local sign_highlight_map =make_highlight_map ( 'Sign' ) 42local underline_highlight_map =make_highlight_map ( 'Underline' ) 43local virtual_text_highlight_map =make_highlight_map ( 'VirtualText' ) 44local virtual_lines_highlight_map =make_highlight_map ( 'VirtualLines' ) 45 46-- Metatable that automatically creates an empty table when assigning to a missing key 47local bufnr_and_namespace_cacher_mt ={ 48--- @param t table<integer, table> 49--- @param bufnr integer 50--- @return table 51__index =function ( t , bufnr ) 52assert ( bufnr > 0 , 'Invalid buffer number' ) 53t [ bufnr ] ={} 54return t [ bufnr ] 55end , 56} 57 58--- @type table<integer, table<integer, vim.diagnostic._handlers._extmark[]>> 59local diagnostic_cache_extmarks =setmetatable ( {} , bufnr_and_namespace_cacher_mt ) 60 61--- @type table<integer, true> 62local diagnostic_attached_buffers ={} 63 64--- @param bufnr integer 65--- @param last integer 66local function restore_extmarks ( bufnr , last ) 67for ns , extmarks in pairs ( diagnostic_cache_extmarks [ bufnr ]) do 68local extmarks_current =api . nvim_buf_get_extmarks ( bufnr , ns , 0 , - 1 , { details =true } ) 69local found ={} --- @type table<integer, true> 70 71for _ , extmark in ipairs ( extmarks_current ) do 72-- nvim_buf_set_lines will move any extmark to the line after the last 73-- nvim_buf_set_text will move any extmark to the last line 74if extmark [ 2 ] ~= last + 1 then 75found [ extmark [ 1 ]] =true 76end 77end 78 79for _ , extmark in ipairs ( extmarks ) do 80if not found [ extmark [ 1 ]] then 81local opts =extmark [ 4 ] --[[@as vim.api.keyset.set_extmark]] 82opts . id =extmark [ 1 ] 83pcall ( api . nvim_buf_set_extmark , bufnr , ns , extmark [ 2 ], extmark [ 3 ], opts ) 84end 85end 86end 87end 88 89--- @param namespace integer 90--- @param bufnr? integer 91local function save_extmarks ( namespace , bufnr ) 92bufnr =vim . _resolve_bufnr ( bufnr ) 93 94if not diagnostic_attached_buffers [ bufnr ] then 95api . nvim_buf_attach ( bufnr , false , { 96on_lines =function ( _ , _ , _ , _ , _ , last ) 97restore_extmarks ( bufnr , last - 1 ) 98end , 99on_detach =function () 100diagnostic_cache_extmarks [ bufnr ] =nil 101end , 102} ) 103diagnostic_attached_buffers [ bufnr ] =true 104end 105 106diagnostic_cache_extmarks [ bufnr ][ namespace ] = 107api . nvim_buf_get_extmarks ( bufnr , namespace , 0 , - 1 , { details =true } ) 108end 109 110--- @param bufnr integer 111--- @param namespace integer 112local function clear_extmarks ( bufnr , namespace ) 113diagnostic_cache_extmarks [ bufnr ][ namespace ] ={} 114if api . nvim_buf_is_valid ( bufnr ) then 115api . nvim_buf_clear_namespace ( bufnr , namespace , 0 , - 1 ) 116end 117end 118 119--- @param bufnr integer 120--- @param fn fun() 121--- @return integer? 122local function once_buf_loaded ( bufnr , fn ) 123if api . nvim_buf_is_loaded ( bufnr ) then 124fn () 125else 126return nvim_on ( 'BufRead' , nil , { buf =bufnr , once =true } , function () 127fn () 128end ) 129end 130end 131 132--- @param autocmd_key string 133--- @param ns vim.diagnostic.NS 134local function cleanup_show_autocmd ( autocmd_key , ns ) 135if ns . user_data [ autocmd_key ] then 136api . nvim_del_autocmd ( ns . user_data [ autocmd_key ]) 137--- @type integer? 138ns . user_data [ autocmd_key ] =nil 139end 140end 141 142--- @param autocmd_key string 143--- @param ns vim.diagnostic.NS 144--- @param bufnr integer 145--- @param fn fun() 146local function show_once_loaded ( autocmd_key , ns , bufnr , fn ) 147cleanup_show_autocmd ( autocmd_key , ns ) 148 149--- @type integer? 150ns . user_data [ autocmd_key ] =once_buf_loaded ( bufnr , function () 151--- @type integer? 152ns . user_data [ autocmd_key ] =nil 153fn () 154end ) 155end 156 157--- @param priority integer 158--- @param opts? { severity_sort?: boolean|{reverse?:boolean} } 159--- @return fun(severity: vim.diagnostic.Severity): integer 160local function severity_to_extmark_priority ( priority , opts ) 161opts =opts or {} 162if opts . severity_sort then 163if type ( opts . severity_sort ) == 'table' and opts . severity_sort . reverse then 164return function ( level ) 165return priority + ( level - severity . ERROR ) 166end 167end 168 169return function ( level ) 170return priority + ( severity . HINT - level ) 171end 172end 173 174return function () 175return priority 176end 177end 178 179M . signs ={} 180 181--- @param namespace integer 182--- @param bufnr integer 183--- @param diagnostics vim.Diagnostic[] 184--- @param opts? vim.diagnostic.OptsResolved 185function M . signs . show ( namespace , bufnr , diagnostics , opts ) 186vim . validate ( 'namespace' , namespace , 'number' ) 187vim . validate ( 'bufnr' , bufnr , 'number' ) 188vim . validate ( 'diagnostics' , diagnostics , vim . islist , 'a list of diagnostics' ) 189vim . validate ( 'opts' , opts , 'table' , true ) 190vim . validate ( 'opts.signs' , ( opts and opts or {} ). signs , 'table' , true ) 191 192bufnr =vim . _resolve_bufnr ( bufnr ) 193 194local sopts =opts and opts . signs or {} 195local ns =diagnostic . get_namespace ( namespace ) 196show_once_loaded ( 'sign_show_autocmd' , ns , bufnr , function () 197-- 10 is the default sign priority when none is explicitly specified 198local priority =sopts . priority or 10 199local get_priority =severity_to_extmark_priority ( priority , opts ) 200 201if not ns . user_data . sign_ns then 202ns . user_data . sign_ns = 203api . nvim_create_namespace ( string . format ( 'nvim.%s.diagnostic.signs' , ns . name )) 204end 205 206local text ={} --- @type table<vim.diagnostic.Severity|string, string> 207for level in pairs ( severity ) do 208if sopts . text and sopts . text [ level ] then 209text [ level ] =sopts . text [ level ] 210elseif type ( level ) == 'string' and not text [ level ] then 211text [ level ] =level : sub ( 1 , 1 ): upper () 212end 213end 214 215local numhl =sopts . numhl or {} 216local linehl =sopts . linehl or {} 217local line_count =api . nvim_buf_line_count ( bufnr ) 218 219for _ , diagnostic0 in ipairs ( diagnostics ) do 220if diagnostic0 . lnum <= line_count then 221local severity_name =( 222severity --[[@as table<vim.diagnostic.Severity, vim.diagnostic.SeverityName>]] 223)[ diagnostic0 . severity ] 224api . nvim_buf_set_extmark ( bufnr , ns . user_data . sign_ns , diagnostic0 . lnum , 0 , { 225sign_text =text [ diagnostic0 . severity ] or text [ severity_name ] or 'U' , 226sign_hl_group =sign_highlight_map [ diagnostic0 . severity ], 227number_hl_group =numhl [ diagnostic0 . severity ], 228line_hl_group =linehl [ diagnostic0 . severity ], 229priority =get_priority ( diagnostic0 . severity ), 230} ) 231end 232end 233end ) 234end 235 236--- @param namespace integer 237--- @param bufnr integer 238function M . signs . hide ( namespace , bufnr ) 239local ns =diagnostic . get_namespace ( namespace ) 240cleanup_show_autocmd ( 'sign_show_autocmd' , ns ) 241if ns . user_data . sign_ns and api . nvim_buf_is_valid ( bufnr ) then 242api . nvim_buf_clear_namespace ( bufnr , ns . user_data . sign_ns , 0 , - 1 ) 243end 244end 245 246M . underline ={} 247 248--- @param namespace integer 249--- @param bufnr integer 250--- @param diagnostics vim.Diagnostic[] 251--- @param opts? vim.diagnostic.OptsResolved 252function M . underline . show ( namespace , bufnr , diagnostics , opts ) 253vim . validate ( 'namespace' , namespace , 'number' ) 254vim . validate ( 'bufnr' , bufnr , 'number' ) 255vim . validate ( 'diagnostics' , diagnostics , vim . islist , 'a list of diagnostics' ) 256vim . validate ( 'opts' , opts , 'table' , true ) 257 258bufnr =vim . _resolve_bufnr ( bufnr ) 259 260local ns =diagnostic . get_namespace ( namespace ) 261show_once_loaded ( 'underline_show_autocmd' , ns , bufnr , function () 262if not ns . user_data . underline_ns then 263ns . user_data . underline_ns = 264api . nvim_create_namespace ( string . format ( 'nvim.%s.diagnostic.underline' , ns . name )) 265end 266 267local underline_ns =ns . user_data . underline_ns 268local get_priority =severity_to_extmark_priority ( vim . hl . priorities . diagnostics , opts ) 269 270-- skip diagnostics with an out-of-range lnum. 271local line_count =api . nvim_buf_line_count ( bufnr ) 272 273for _ , diagnostic0 in ipairs ( diagnostics ) do 274if diagnostic0 . lnum < line_count then 275local higroups ={ underline_highlight_map [ diagnostic0 . severity ] } 276 277if diagnostic0 . _tags then 278if diagnostic0 . _tags . unnecessary then 279table . insert ( higroups , 'DiagnosticUnnecessary' ) 280end 281if diagnostic0 . _tags . deprecated then 282table . insert ( higroups , 'DiagnosticDeprecated' ) 283end 284end 285 286local lines = 287api . nvim_buf_get_lines ( diagnostic0 . bufnr , diagnostic0 . lnum , diagnostic0 . lnum + 1 , true ) 288 289vim . hl . range ( 290bufnr , 291underline_ns , 292higroups , 293{ diagnostic0 . lnum , math . min ( diagnostic0 . col , # lines [ 1 ] - 1 ) } , 294{ diagnostic0 . end_lnum , diagnostic0 . end_col } , 295{ priority =get_priority ( diagnostic0 . severity ) } 296) 297end 298end 299 300save_extmarks ( underline_ns , bufnr ) 301end ) 302end 303 304--- @param namespace integer 305--- @param bufnr integer 306function M . underline . hide ( namespace , bufnr ) 307local ns =diagnostic . get_namespace ( namespace ) 308cleanup_show_autocmd ( 'underline_show_autocmd' , ns ) 309if ns . user_data . underline_ns then 310clear_extmarks ( bufnr , ns . user_data . underline_ns ) 311end 312end 313 314--- @param line_diags table<integer, vim.Diagnostic> 315--- @param opts vim.diagnostic.Opts.VirtualText 316--- @return [string, any][]? 317local function get_virt_text_chunks ( line_diags , opts ) 318if # line_diags == 0 then 319return 320end 321 322opts =opts or {} 323local prefix =opts . prefix or '■' 324local suffix =opts . suffix or '' 325local spacing =opts . spacing or 4 326 327-- Create a little more space between virtual text and contents 328local virt_texts ={ { string . rep ( ' ' , spacing ) } } 329 330for i =1 , # line_diags do 331local resolved_prefix =prefix 332if type ( prefix ) == 'function' then 333resolved_prefix =prefix ( line_diags [ i ], i , # line_diags ) or '' 334end 335 336table . insert ( 337virt_texts , 338{ resolved_prefix , virtual_text_highlight_map [ line_diags [ i ]. severity ] } 339) 340end 341 342local last =line_diags [ # line_diags ] 343-- TODO(tjdevries): Allow different servers to be shown first somehow? 344-- TODO(tjdevries): Display server name associated with these? 345if last . message then 346if type ( suffix ) == 'function' then 347suffix =suffix ( last ) or '' 348end 349 350table . insert ( virt_texts , { 351string . format ( ' %s%s' , last . message : gsub ( '\r' , '' ): gsub ( '\n' , ' ' ), suffix ), 352virtual_text_highlight_map [ last . severity ], 353} ) 354 355return virt_texts 356end 357end 358 359--- @param namespace integer 360--- @param bufnr integer 361--- @param diagnostics table<integer, vim.Diagnostic[]> 362--- @param opts vim.diagnostic.Opts.VirtualText 363local function render_virtual_text ( namespace , bufnr , diagnostics , opts ) 364local lnum =api . nvim_win_get_cursor ( 0 )[ 1 ] - 1 365local buf_len =api . nvim_buf_line_count ( bufnr ) 366api . nvim_buf_clear_namespace ( bufnr , namespace , 0 , - 1 ) 367 368--- @param line integer 369--- @return boolean 370local function should_render ( line ) 371if 372line >= buf_len 373or ( opts . current_line == true and line ~= lnum ) 374or ( opts . current_line == false and line == lnum ) 375then 376return false 377end 378 379return true 380end 381 382for line , line_diagnostics in pairs ( diagnostics ) do 383if should_render ( line ) then 384local virt_texts =get_virt_text_chunks ( line_diagnostics , opts ) 385if virt_texts then 386api . nvim_buf_set_extmark ( bufnr , namespace , line , 0 , { 387hl_mode =opts . hl_mode or 'combine' , 388virt_text =virt_texts , 389virt_text_pos =opts . virt_text_pos , 390virt_text_hide =opts . virt_text_hide , 391virt_text_win_col =opts . virt_text_win_col , 392} ) 393end 394end 395end 396end 397 398---Allows to perform actions **only** within CursorHold. 399---@param buf integer 400---@param augroup integer 401---@param on_hold fun() # action to be made on CursorHold 402---@param on_move fun() # action to be made on CursorMoved, usually a cleanup after `on_hold` 403local function on_line_hold ( buf , augroup , on_hold , on_move ) 404local hold_line =- 1 405 406nvim_on ( 'CursorHold' , augroup , { buf =buf } , function () 407on_hold () 408 409local current_line =api . nvim_win_get_cursor ( 0 )[ 1 ] 410if current_line == hold_line then 411return 412end 413 414hold_line =current_line 415 416-- CursorHold can fire multiple times if LSP still resolves diagnostics. 417api . nvim_clear_autocmds ( { event ='CursorMoved' , buf =buf , group =augroup } ) 418nvim_on ( 'CursorMoved' , augroup , { buf =buf } , function () 419local move_line =api . nvim_win_get_cursor ( 0 )[ 1 ] 420if move_line ~= hold_line then 421on_move () 422hold_line =- 1 423return true 424end 425end ) 426end ) 427-- When first loading a buffer CursorHold is emitted before this fn is executed. 428-- So retriggering it once to have proper initial render. 429api . nvim_exec_autocmds ( 'CursorHold' , { buf =buf , group =augroup } ) 430end 431 432M . virtual_text ={} 433 434--- @param namespace integer 435--- @param bufnr integer 436--- @param diagnostics vim.Diagnostic[] 437--- @param opts? vim.diagnostic.OptsResolved 438function M . virtual_text . show ( namespace , bufnr , diagnostics , opts ) 439vim . validate ( 'namespace' , namespace , 'number' ) 440vim . validate ( 'bufnr' , bufnr , 'number' ) 441vim . validate ( 'diagnostics' , diagnostics , vim . islist , 'a list of diagnostics' ) 442vim . validate ( 'opts' , opts , 'table' , true ) 443 444bufnr =vim . _resolve_bufnr ( bufnr ) 445local vopts =opts and opts . virtual_text or {} 446 447local ns =diagnostic . get_namespace ( namespace ) 448show_once_loaded ( 'virtual_text_show_autocmd' , ns , bufnr , function () 449if vopts . format then 450diagnostics =diagnostic_shared . reformat_diagnostics ( vopts . format , diagnostics ) 451end 452 453if 454vopts . source and ( vopts . source ~= 'if_many' or diagnostic_shared . count_sources ( bufnr ) > 1 ) 455then 456diagnostics =diagnostic_shared . prefix_source ( diagnostics ) 457end 458 459if not ns . user_data . virt_text_ns then 460ns . user_data . virt_text_ns = 461api . nvim_create_namespace ( string . format ( 'nvim.%s.diagnostic.virtual_text' , ns . name )) 462end 463if not ns . user_data . virt_text_augroup then 464ns . user_data . virt_text_augroup =api . nvim_create_augroup ( 465string . format ( 'nvim.%s.diagnostic.virt_text' , ns . name ), 466{ clear =true } 467) 468end 469 470api . nvim_clear_autocmds ( { group =ns . user_data . virt_text_augroup , buf =bufnr } ) 471 472local line_diagnostics =diagnostic_shared . diagnostic_lines ( diagnostics , true ) 473 474if vopts . current_line ~= nil then 475on_line_hold ( bufnr , ns . user_data . virt_text_augroup , function () 476render_virtual_text ( ns . user_data . virt_text_ns , bufnr , line_diagnostics , vopts ) 477end , function () 478if vopts . current_line then 479clear_extmarks ( bufnr , ns . user_data . virt_text_ns ) 480else 481local saved_current_line =vopts . current_line 482vopts . current_line =nil 483render_virtual_text ( ns . user_data . virt_text_ns , bufnr , line_diagnostics , vopts ) 484vopts . current_line =saved_current_line 485end 486end ) 487else 488render_virtual_text ( ns . user_data . virt_text_ns , bufnr , line_diagnostics , vopts ) 489end 490 491save_extmarks ( ns . user_data . virt_text_ns , bufnr ) 492end ) 493end 494 495--- @param namespace integer 496--- @param bufnr integer 497function M . virtual_text . hide ( namespace , bufnr ) 498local ns =diagnostic . get_namespace ( namespace ) 499cleanup_show_autocmd ( 'virtual_text_show_autocmd' , ns ) 500if ns . user_data . virt_text_ns then 501clear_extmarks ( bufnr , ns . user_data . virt_text_ns ) 502if api . nvim_buf_is_valid ( bufnr ) then 503api . nvim_clear_autocmds ( { group =ns . user_data . virt_text_augroup , buf =bufnr } ) 504end 505end 506end 507 508--- @param bufnr integer 509--- @param lnum integer 510--- @param start_col integer 511--- @param end_col integer 512--- @return integer 513local function distance_between_cols ( bufnr , lnum , start_col , end_col ) 514return api . nvim_buf_call ( bufnr , function () 515local s =vim . fn . virtcol ( { lnum + 1 , start_col } ) 516local e =vim . fn . virtcol ( { lnum + 1 , end_col + 1 } ) 517return e - 1 - s 518end ) 519end 520 521--- @param namespace integer 522--- @param bufnr integer 523--- @param diagnostics vim.Diagnostic[] 524--- @param opts vim.diagnostic.Opts.VirtualLines 525local function render_virtual_lines ( namespace , bufnr , diagnostics , opts ) 526table . sort ( diagnostics , function ( d1 , d2 ) 527return diagnostic_shared . diagnostic_cmp ( d1 , d2 , 'lnum' , false ) 528end ) 529 530api . nvim_buf_clear_namespace ( bufnr , namespace , 0 , - 1 ) 531 532if not next ( diagnostics ) then 533return 534end 535 536-- This loop reads each line, putting them into stacks with some extra data since 537-- rendering each line requires understanding what is beneath it. 538local ElementType ={ Space =1 , Diagnostic =2 , Overlap =3 , Blank =4 } --- @enum ElementType 539--- @type table<integer, [ElementType, string|vim.diagnostic.Severity|vim.Diagnostic][]> 540local line_stacks ={} 541--- @type table<integer, integer> 542local line_anchor ={} 543local prev_lnum =- 1 544local prev_col =0 545 546for _ , diag in ipairs ( diagnostics ) do 547if not line_stacks [ diag . lnum ] then 548line_stacks [ diag . lnum ] ={} 549end 550 551local stack =line_stacks [ diag . lnum ] 552local end_lnum =diag . end_lnum or diag . lnum 553if not line_anchor [ diag . lnum ] or end_lnum > line_anchor [ diag . lnum ] then 554line_anchor [ diag . lnum ] =end_lnum 555end 556 557if diag . lnum ~= prev_lnum then 558table . insert ( stack , { 559ElementType . Space , 560string . rep ( ' ' , distance_between_cols ( bufnr , diag . lnum , 0 , diag . col )), 561} ) 562elseif diag . col ~= prev_col then 563table . insert ( stack , { 564ElementType . Space , 565-- +1 because indexing starts at 0 in one API but at 1 in the other. 566string . rep ( ' ' , distance_between_cols ( bufnr , diag . lnum , prev_col + 1 , diag . col )), 567} ) 568else 569table . insert ( stack , { ElementType . Overlap , diag . severity } ) 570end 571 572if diag . message : find ( '^%s*$' ) then 573table . insert ( stack , { ElementType . Blank , diag } ) 574else 575table . insert ( stack , { ElementType . Diagnostic , diag } ) 576end 577 578prev_lnum , prev_col =diag . lnum , diag . col 579end 580 581local chars ={ 582cross ='┼' , 583horizontal ='─' , 584horizontal_up ='┴' , 585up_right ='└' , 586vertical ='│' , 587vertical_right ='├' , 588} 589 590for lnum , stack in pairs ( line_stacks ) do 591local virt_lines ={} 592 593-- Note that we read in the order opposite to insertion. 594for i =# stack , 1 , - 1 do 595if stack [ i ][ 1 ] == ElementType . Diagnostic then 596local diagnostic0 =stack [ i ][ 2 ] --[[@as vim.Diagnostic]] 597local left ={} --- @type [string, string] 598local overlap =false 599local multi =false 600 601-- Iterate the stack for this line to find elements on the left. 602for j =1 , i - 1 do 603local element =assert ( stack [ j ]) 604local element_type , data =element [ 1 ], element [ 2 ] 605if element_type == ElementType . Space then 606if multi then 607--- @cast data string 608table . insert ( left , { 609string . rep ( chars . horizontal , data : len ()), 610virtual_lines_highlight_map [ diagnostic0 . severity ], 611} ) 612else 613table . insert ( left , { data , '' } ) 614end 615elseif element_type == ElementType . Diagnostic then 616-- If an overlap follows this line, don't add an extra column. 617if assert ( stack [ j + 1 ])[ 1 ] ~= ElementType . Overlap then 618table . insert ( left , { chars . vertical , virtual_lines_highlight_map [ data . severity ] } ) 619end 620overlap =false 621elseif element_type == ElementType . Blank then 622if multi then 623table . insert ( 624left , 625{ chars . horizontal_up , virtual_lines_highlight_map [ data . severity ] } 626) 627else 628table . insert ( left , { chars . up_right , virtual_lines_highlight_map [ data . severity ] } ) 629end 630multi =true 631elseif element_type == ElementType . Overlap then 632overlap =true 633end 634end 635 636local center_char --- @type string 637if overlap and multi then 638center_char =chars . cross 639elseif overlap then 640center_char =chars . vertical_right 641elseif multi then 642center_char =chars . horizontal_up 643else 644center_char =chars . up_right 645end 646 647local center ={ 648{ 649string . format ( '%s%s' , center_char , string . rep ( chars . horizontal , 4 ) .. ' ' ), 650virtual_lines_highlight_map [ diagnostic0 . severity ], 651} , 652} 653 654-- We can draw on the left side if and only if: 655-- a. Is the last one stacked this line. 656-- b. Has enough space on the left. 657-- c. Is just one line. 658-- d. Is not an overlap. 659for msg_line in diagnostic0 . message : gmatch ( '([^\n]+)' ) do 660local vline ={} 661vim . list_extend ( vline , left ) 662vim . list_extend ( vline , center ) 663vim . list_extend ( vline , { 664{ msg_line , virtual_lines_highlight_map [ diagnostic0 . severity ] } , 665} ) 666 667table . insert ( virt_lines , vline ) 668 669-- Special-case for continuation lines: 670if overlap then 671center ={ 672{ chars . vertical , virtual_lines_highlight_map [ diagnostic0 . severity ] } , 673{ ' ' , '' } , 674} 675else 676center ={ { ' ' , '' } } 677end 678end 679end 680end 681 682api . nvim_buf_set_extmark ( bufnr , namespace , line_anchor [ lnum ] or lnum , 0 , { 683virt_lines_overflow =opts . overflow or 'auto' , 684virt_lines =virt_lines , 685} ) 686end 687end 688 689--- @param diagnostic0 vim.Diagnostic 690--- @return string 691local function format_virtual_lines ( diagnostic0 ) 692if diagnostic0 . code then 693return string . format ( '%s: %s' , diagnostic0 . code , diagnostic0 . message ) 694end 695 696return diagnostic0 . message 697end 698 699M . virtual_lines ={} 700 701--- @param namespace integer 702--- @param bufnr integer 703--- @param diagnostics vim.Diagnostic[] 704--- @param opts? vim.diagnostic.OptsResolved 705function M . virtual_lines . show ( namespace , bufnr , diagnostics , opts ) 706vim . validate ( 'namespace' , namespace , 'number' ) 707vim . validate ( 'bufnr' , bufnr , 'number' ) 708vim . validate ( 'diagnostics' , diagnostics , vim . islist , 'a list of diagnostics' ) 709vim . validate ( 'opts' , opts , 'table' , true ) 710 711bufnr =vim . _resolve_bufnr ( bufnr ) 712local vopts =opts and opts . virtual_lines or {} 713 714local ns =diagnostic . get_namespace ( namespace ) 715show_once_loaded ( 'virtual_lines_show_autocmd' , ns , bufnr , function () 716if not ns . user_data . virt_lines_ns then 717ns . user_data . virt_lines_ns = 718api . nvim_create_namespace ( string . format ( 'nvim.%s.diagnostic.virtual_lines' , ns . name )) 719end 720if not ns . user_data . virt_lines_augroup then 721ns . user_data . virt_lines_augroup =api . nvim_create_augroup ( 722string . format ( 'nvim.%s.diagnostic.virt_lines' , ns . name ), 723{ clear =true } 724) 725end 726 727api . nvim_clear_autocmds ( { group =ns . user_data . virt_lines_augroup , buf =bufnr } ) 728 729diagnostics = 730diagnostic_shared . reformat_diagnostics ( vopts . format or format_virtual_lines , diagnostics ) 731 732if vopts . current_line == true then 733-- Create a mapping from line -> diagnostics so that we can quickly get the 734-- diagnostics we need when the cursor line doesn't change. 735local line_diagnostics =diagnostic_shared . diagnostic_lines ( diagnostics , true ) 736on_line_hold ( bufnr , ns . user_data . virt_lines_augroup , function () 737render_virtual_lines ( 738ns . user_data . virt_lines_ns , 739bufnr , 740diagnostic_shared . diagnostics_at_cursor ( line_diagnostics ), 741vopts 742) 743end , function () 744clear_extmarks ( bufnr , ns . user_data . virt_lines_ns ) 745end ) 746else 747render_virtual_lines ( ns . user_data . virt_lines_ns , bufnr , diagnostics , vopts ) 748end 749 750save_extmarks ( ns . user_data . virt_lines_ns , bufnr ) 751end ) 752end 753 754--- @param namespace integer 755--- @param bufnr integer 756function M . virtual_lines . hide ( namespace , bufnr ) 757local ns =diagnostic . get_namespace ( namespace ) 758cleanup_show_autocmd ( 'virtual_lines_show_autocmd' , ns ) 759if ns . user_data . virt_lines_ns then 760clear_extmarks ( bufnr , ns . user_data . virt_lines_ns ) 761if api . nvim_buf_is_valid ( bufnr ) then 762api . nvim_clear_autocmds ( { group =ns . user_data . virt_lines_augroup , buf =bufnr } ) 763end 764end 765end 766 767return M