robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local api , nonnil =vim . api , vim . nonnil 2local shared =require ( 'vim.diagnostic._shared' ) 3local store =require ( 'vim.diagnostic._store' ) 4 5--- @class (private) vim.diagnostic._float 6local M ={} 7 8local float_ns =api . nvim_create_namespace ( 'nvim.diagnostic.float' ) 9 10local severity =vim . diagnostic . severity 11 12--- @type table<vim.diagnostic.Severity, string> 13local floating_highlight_map ={ 14[ severity . ERROR ] ='DiagnosticFloatingError' , 15[ severity . WARN ] ='DiagnosticFloatingWarn' , 16[ severity . INFO ] ='DiagnosticFloatingInfo' , 17[ severity . HINT ] ='DiagnosticFloatingHint' , 18} 19 20--- @param opts vim.diagnostic.Opts.Float 21--- @param bufnr integer 22--- @return vim.diagnostic.Opts.Float, vim.diagnostic.Opts 23local function resolve_float_opts ( opts , bufnr ) 24-- Resolve options with user settings from vim.diagnostic.config 25-- Unlike the other decoration functions (e.g. set_virtual_text, set_signs, etc.) `open_float` 26-- does not have a dedicated table for configuration options; instead, the options are mixed in 27-- with its `opts` table. We create a dedicated options table (`float_opts`) that inherits 28-- missing keys from the global configuration (`global_diagnostic_options.float`), which can 29-- be a table or a function. 30local global_opts =vim . diagnostic . config () 31local float_opts =global_opts . float 32local resolved_float_opts =type ( float_opts ) == 'table' and float_opts 33or ( type ( float_opts ) == 'function' and float_opts ( opts . namespace , bufnr ) or {} ) 34 35return vim . tbl_extend ( 'keep' , opts , resolved_float_opts ), global_opts 36end 37 38--- @param opts vim.diagnostic.Opts.Float? 39--- @return integer? float_bufnr 40--- @return integer? winid 41function M . open ( opts , ... ) 42-- Support old (bufnr, opts) signature 43local bufnr --- @type integer? 44if opts == nil or type ( opts ) == 'number' then 45bufnr =opts --[[@as integer?]] 46opts =... --- @type vim.diagnostic.Opts.Float 47else 48vim . validate ( 'opts' , opts , 'table' , true ) 49end 50 51opts =opts or {} 52bufnr =vim . _resolve_bufnr (( bufnr or opts . bufnr ) --[[@as integer?]] ) 53local global_opts --- @type vim.diagnostic.Opts 54opts , global_opts =resolve_float_opts ( opts , bufnr ) 55 56local scope =( { l ='line' , c ='cursor' , b ='buffer' } )[ opts . scope ] or opts . scope or 'line' 57local lnum , col --- @type integer, integer 58local opts_pos =opts . pos 59if scope == 'line' or scope == 'cursor' then 60if not opts_pos then 61local pos =api . nvim_win_get_cursor ( 0 ) 62lnum =pos [ 1 ] - 1 63col =pos [ 2 ] 64elseif type ( opts_pos ) == 'number' then 65lnum =opts_pos 66elseif type ( opts_pos ) == 'table' then 67lnum , col =opts_pos [ 1 ], opts_pos [ 2 ] 68else 69error ( "Invalid value for option 'pos'" ) 70end 71elseif scope ~= 'buffer' then 72error ( "Invalid value for option 'scope'" ) 73end 74 75local diagnostics =store . get_diagnostics ( bufnr , opts --[[@as vim.diagnostic.GetOpts]] , true ) 76 77if scope == 'line' then 78--- @param diagnostic vim.Diagnostic 79local function line_filter ( diagnostic ) 80local d_lnum , _ , d_end_lnum , d_end_col =shared . get_logical_pos ( diagnostic ) 81return lnum >= d_lnum 82and lnum <= d_end_lnum 83and ( d_lnum == d_end_lnum or lnum ~= d_end_lnum or d_end_col ~= 0 ) 84end 85diagnostics =vim . tbl_filter ( line_filter , diagnostics ) 86elseif scope == 'cursor' then 87-- If `col` is past the end of the line, show if the cursor is on the last char in the line 88local line_length =# api . nvim_buf_get_lines ( bufnr , lnum , lnum + 1 , true )[ 1 ] 89--- @param diagnostic vim.Diagnostic 90local function cursor_filter ( diagnostic ) 91local d_lnum , d_col , d_end_lnum , d_end_col =shared . get_logical_pos ( diagnostic ) 92return lnum >= d_lnum 93and lnum <= d_end_lnum 94and ( lnum ~= d_lnum or col >= math . min ( d_col , line_length - 1 )) 95and (( d_lnum == d_end_lnum and d_col == d_end_col ) or lnum ~= d_end_lnum or col < d_end_col ) 96end 97diagnostics =vim . tbl_filter ( cursor_filter , diagnostics ) 98end 99 100if vim . tbl_isempty ( diagnostics ) then 101return 102end 103 104local severity_sort =nonnil ( opts . severity_sort , global_opts . severity_sort ) 105if severity_sort then 106if type ( severity_sort ) == 'table' and severity_sort . reverse then 107table . sort ( diagnostics , function ( a , b ) 108return shared . diagnostic_cmp ( a , b , 'severity' , true ) 109end ) 110else 111table . sort ( diagnostics , function ( a , b ) 112return shared . diagnostic_cmp ( a , b , 'severity' , false ) 113end ) 114end 115end 116 117local lines ={} --- @type string[] 118local highlights ={} --- @type { hlname: string, prefix?: { length: integer, hlname: string? }, suffix?: { length: integer, hlname: string? } }[] 119local header =nonnil ( opts . header , 'Diagnostics:' ) 120if header then 121vim . validate ( 'header' , header , { 'string' , 'table' } , "'string' or 'table'" ) 122if type ( header ) == 'table' then 123-- Don't insert any lines for an empty string 124if # ( header [ 1 ] or '' ) > 0 then 125lines [ # lines + 1 ] =header [ 1 ] 126highlights [ # highlights + 1 ] ={ hlname =header [ 2 ] or 'Bold' } 127end 128elseif # header > 0 then 129lines [ # lines + 1 ] =header 130highlights [ # highlights + 1 ] ={ hlname ='Bold' } 131end 132end 133 134if opts . format then 135diagnostics =shared . reformat_diagnostics ( opts . format , diagnostics ) 136end 137 138if opts . source and ( opts . source ~= 'if_many' or shared . count_sources ( bufnr ) > 1 ) then 139diagnostics =shared . prefix_source ( diagnostics ) 140end 141 142local prefix_opt =opts . prefix 143or ( scope == 'cursor' and # diagnostics <= 1 ) and '' 144or function ( _ , i ) 145return string . format ( '%d. ' , i ) 146end 147 148local prefix , prefix_hl_group --- @type string?, string? 149if prefix_opt then 150vim . validate ( 151'prefix' , 152prefix_opt , 153{ 'string' , 'table' , 'function' } , 154"'string' or 'table' or 'function'" 155) 156if type ( prefix_opt ) == 'string' then 157prefix , prefix_hl_group =prefix_opt , 'NormalFloat' 158elseif type ( prefix_opt ) == 'table' then 159prefix , prefix_hl_group =prefix_opt [ 1 ] or '' , prefix_opt [ 2 ] or 'NormalFloat' 160end 161end 162 163local suffix_opt =opts . suffix 164or function ( diagnostic ) 165return diagnostic . code and string . format ( ' [%s]' , diagnostic . code ) or '' 166end 167 168local suffix , suffix_hl_group --- @type string?, string? 169if suffix_opt then 170vim . validate ( 171'suffix' , 172suffix_opt , 173{ 'string' , 'table' , 'function' } , 174"'string' or 'table' or 'function'" 175) 176if type ( suffix_opt ) == 'string' then 177suffix , suffix_hl_group =suffix_opt , 'NormalFloat' 178elseif type ( suffix_opt ) == 'table' then 179suffix , suffix_hl_group =suffix_opt [ 1 ] or '' , suffix_opt [ 2 ] or 'NormalFloat' 180end 181end 182 183local related_info_locations ={} --- @type table<integer, lsp.Location> 184for i , diagnostic in ipairs ( diagnostics ) do 185if type ( prefix_opt ) == 'function' then 186local prefix0 , prefix_hl_group0 =prefix_opt ( diagnostic , i , # diagnostics ) 187prefix , prefix_hl_group =prefix0 or '' , prefix_hl_group0 or 'NormalFloat' 188end 189if type ( suffix_opt ) == 'function' then 190local suffix0 , suffix_hl_group0 =suffix_opt ( diagnostic , i , # diagnostics ) 191suffix , suffix_hl_group =suffix0 or '' , suffix_hl_group0 or 'NormalFloat' 192end 193 194local hiname =floating_highlight_map [ diagnostic . severity ] 195local message_lines =vim . split ( diagnostic . message , '\n' ) 196local default_pre =string . rep ( ' ' , # prefix ) 197for j =1 , # message_lines do 198local pre =j == 1 and prefix or default_pre 199local suf =j == # message_lines and suffix or '' 200lines [ # lines + 1 ] =pre .. message_lines [ j ] .. suf 201highlights [ # highlights + 1 ] ={ 202hlname =hiname , 203prefix ={ 204length =j == 1 and # prefix or 0 , 205hlname =prefix_hl_group , 206} , 207suffix ={ 208length =# suf , 209hlname =suffix_hl_group , 210} , 211} 212end 213 214--- @type lsp.DiagnosticRelatedInformation[] 215local related_info =vim . tbl_get ( diagnostic , 'user_data' , 'lsp' , 'relatedInformation' ) or {} 216assert ( related_info ~= vim . NIL , 'server response has invalid (null) relatedInformation' ) 217-- Below the diagnostic, show its LSP related information (if any) in the form of file name and 218-- range, plus description. 219for _ , info in ipairs ( related_info ) do 220local location =info . location 221local file_name =vim . fs . basename ( vim . uri_to_fname ( location . uri )) 222local info_suffix =': ' .. info . message 223related_info_locations [ # lines + 1 ] =location 224lines [ # lines + 1 ] =string . format ( 225'%s%s:%s:%s%s' , 226default_pre , 227file_name , 228location . range . start . line + 1 , 229location . range . start . character + 1 , 230info_suffix 231) 232highlights [ # highlights + 1 ] ={ 233hlname ='@string.special.path' , 234prefix ={ 235length =# default_pre , 236hlname =prefix_hl_group , 237} , 238suffix ={ 239length =# info_suffix , 240hlname ='NormalFloat' , 241} , 242} 243end 244end 245 246-- Used by open_floating_preview to allow the float to be focused 247if not opts . focus_id then 248opts . focus_id =scope 249end 250 251--- @diagnostic disable-next-line: param-type-mismatch 252local float_bufnr , winnr =vim . lsp . util . open_floating_preview ( lines , 'plaintext' , opts ) 253vim . bo [ float_bufnr ]. path =vim . bo [ bufnr ]. path 254 255-- TODO: Handle this generally (like vim.ui.open()), rather than overriding gf. 256vim . keymap . set ( 'n' , 'gf' , function () 257local cursor_row =api . nvim_win_get_cursor ( 0 )[ 1 ] 258local location =related_info_locations [ cursor_row ] 259if location then 260-- Split the window before calling `show_document` so the window doesn't disappear. 261vim . cmd . split () 262vim . lsp . util . show_document ( location , 'utf-16' , { focus =true } ) 263else 264vim . cmd . normal ( { 'gf' , bang =true } ) 265end 266end , { buf =float_bufnr , remap =false } ) 267 268-- The preview may trim empty lines or reuse an older buffer, so highlight ranges 269-- can extend past its contents. Use strict = false to tolerate this. 270for i , hl in ipairs ( highlights ) do 271local line =lines [ i ] 272local prefix_len =hl . prefix and hl . prefix . length or 0 273local suffix_len =hl . suffix and hl . suffix . length or 0 274if hl . prefix and prefix_len > 0 then 275api . nvim_buf_set_extmark ( float_bufnr , float_ns , i - 1 , 0 , { 276hl_group =hl . prefix . hlname , 277end_col =prefix_len , 278strict =false , 279} ) 280end 281api . nvim_buf_set_extmark ( float_bufnr , float_ns , i - 1 , prefix_len , { 282hl_group =hl . hlname , 283end_col =# line - suffix_len , 284strict =false , 285} ) 286if hl . suffix and suffix_len > 0 then 287api . nvim_buf_set_extmark ( float_bufnr , float_ns , i - 1 , # line - suffix_len , { 288hl_group =hl . suffix . hlname , 289end_row =i , 290end_col =0 , 291strict =false , 292} ) 293end 294end 295 296return float_bufnr , winnr 297end 298 299return M