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 3 4local severity_module =require ( 'vim.diagnostic._severity' ) 5 6--- @class (private) vim.diagnostic._store 7local M ={} 8 9-- bufnr -> ns -> Diagnostic[] 10local diagnostic_cache ={} --- @type table<integer,table<integer,vim.Diagnostic[]?>> 11 12local group =api . nvim_create_augroup ( 'nvim.diagnostic.buf_wipeout' ) 13setmetatable ( diagnostic_cache , { 14--- @param t table<integer,vim.Diagnostic[]> 15--- @param bufnr integer 16__index =function ( t , bufnr ) 17assert ( bufnr > 0 , 'Invalid buffer number' ) 18nvim_on ( 'BufWipeout' , group , { buf =bufnr } , function () 19rawset ( t , bufnr , nil ) 20end ) 21t [ bufnr ] ={} 22return t [ bufnr ] 23end , 24} ) 25 26--- @param bufnr integer 27--- @param namespace integer 28--- @param d vim.Diagnostic.Set 29local function norm_diag ( bufnr , namespace , d ) 30vim . validate ( 'diagnostic.lnum' , d . lnum , 'number' ) 31local d1 =d --[[@as vim.Diagnostic]] 32d1 . severity =d . severity and severity_module . to_severity ( d . severity ) 33or vim . diagnostic . severity . ERROR 34d1 . end_lnum =d . end_lnum or d . lnum 35d1 . col =d . col or 0 36d1 . end_col =d . end_col or d . col or 0 37d1 . namespace =namespace 38d1 . bufnr =bufnr 39end 40 41--- Set extmarks at the computed positions of the cached diagnostics of 42--- {bufnr}/{namespace}, and store their ids in diagnostic._extmark_id (used by 43--- get_logical_pos to adjust positions). 44--- 45--- @param bufnr integer 46--- @param namespace integer 47local function set_position_extmarks ( bufnr , namespace ) 48local ns =vim . diagnostic . get_namespace ( namespace ) 49 50if not ns . user_data . location_ns then 51ns . user_data . location_ns = 52api . nvim_create_namespace ( string . format ( 'nvim.%s.diagnostic' , ns . name )) 53end 54 55api . nvim_buf_clear_namespace ( bufnr , ns . user_data . location_ns , 0 , - 1 ) 56 57local lines =api . nvim_buf_get_lines ( bufnr , 0 , - 1 , true ) 58local buf_cache =rawget ( diagnostic_cache , bufnr ) --- @type table<integer,vim.Diagnostic[]?>? 59-- set extmarks at diagnostic locations to preserve logical positions despite text changes 60for _ , diagnostic0 in ipairs ( buf_cache and buf_cache [ namespace ] or {} ) do 61--- @cast diagnostic0 vim.Diagnostic 62local last_row =# lines - 1 63local row =math . max ( 0 , math . min ( diagnostic0 . lnum , last_row )) 64local row_len =# lines [ row + 1 ] 65local col =math . max ( 0 , math . min ( diagnostic0 . col , row_len - 1 )) 66 67local end_row =math . max ( 0 , math . min ( diagnostic0 . end_lnum or row , last_row )) 68local end_row_len =# lines [ end_row + 1 ] 69local end_col =math . max ( 0 , math . min ( diagnostic0 . end_col or col , end_row_len )) 70 71if end_row == row then 72-- avoid starting an extmark beyond end of the line 73if end_col == col then 74end_col =math . min ( end_col + 1 , end_row_len ) 75end 76else 77-- avoid ending an extmark before start of the line 78if end_col == 0 then 79end_row =end_row - 1 80 81local end_line =lines [ end_row + 1 ] 82 83if not end_line then 84error ( 85'Failed to adjust diagnostic position to the end of a previous line. #lines in a buffer: ' 86.. # lines 87.. ', lnum: ' 88.. diagnostic0 . lnum 89.. ', col: ' 90.. diagnostic0 . col 91.. ', end_lnum: ' 92.. diagnostic0 . end_lnum 93.. ', end_col: ' 94.. diagnostic0 . end_col 95) 96end 97 98end_col =# end_line 99end 100end 101 102diagnostic0 . _extmark_id =api . nvim_buf_set_extmark ( bufnr , ns . user_data . location_ns , row , col , { 103end_row =end_row , 104end_col =end_col , 105invalidate =true , 106} ) 107end 108end 109 110-- Positions can only be computed once the buffer is loaded, so set the 111-- extmarks for any buffer with cached diagnostics when it is read (or re-read). 112nvim_on ( 'BufRead' , api . nvim_create_augroup ( 'nvim.diagnostic.buf_load' ), function ( ev ) 113local buf_cache =rawget ( diagnostic_cache , ev . buf ) --- @type table<integer,vim.Diagnostic[]?>? 114if buf_cache then 115for namespace in pairs ( buf_cache ) do 116set_position_extmarks ( ev . buf , namespace ) 117end 118end 119end ) 120 121--- @param bufnr integer? 122--- @param opts vim.diagnostic.GetOpts? 123--- @param clamp boolean 124--- @return vim.Diagnostic[] 125function M . get_diagnostics ( bufnr , opts , clamp ) 126opts =opts or {} 127 128local namespace =opts . namespace 129 130if type ( namespace ) == 'number' then 131namespace ={ namespace } 132end 133 134--- @cast namespace integer[] 135 136--- @type vim.Diagnostic[] 137local diagnostics ={} 138 139-- Memoized results of buf_line_count per bufnr 140--- @type table<integer,integer> 141local buf_line_count =setmetatable ( {} , { 142--- @param t table<integer,integer> 143--- @param k integer 144--- @return integer 145__index =function ( t , k ) 146t [ k ] =api . nvim_buf_line_count ( k ) 147return rawget ( t , k ) 148end , 149} ) 150 151local match_severity =opts . severity and severity_module . severity_predicate ( opts . severity ) 152or function ( _ ) 153return true 154end 155 156--- @param b integer 157--- @param d vim.Diagnostic 158local match_enablement =function ( d , b ) 159if opts . enabled == nil then 160return true 161end 162 163local enabled =vim . diagnostic . is_enabled ( { bufnr =b , ns_id =d . namespace } ) 164 165return ( enabled and opts . enabled ) or ( not enabled and not opts . enabled ) 166end 167 168--- @param b integer 169--- @param d vim.Diagnostic 170local function add ( b , d ) 171if 172match_severity ( d ) 173and match_enablement ( d , b ) 174and ( not opts . lnum or ( opts . lnum >= d . lnum and opts . lnum <= ( d . end_lnum or d . lnum ))) 175then 176if clamp and api . nvim_buf_is_loaded ( b ) then 177local line_count =buf_line_count [ b ] - 1 178if 179d . lnum > line_count 180or d . end_lnum > line_count 181or d . lnum < 0 182or d . end_lnum < 0 183or d . col < 0 184or d . end_col < 0 185then 186d =vim . deepcopy ( d , true ) 187d . lnum =math . max ( math . min ( d . lnum , line_count ), 0 ) 188d . end_lnum =math . max ( math . min ( d . end_lnum , line_count ), 0 ) 189d . col =math . max ( d . col , 0 ) 190d . end_col =math . max ( d . end_col , 0 ) 191end 192end 193table . insert ( diagnostics , d ) 194end 195end 196 197--- @param buf integer 198--- @param diags vim.Diagnostic[] 199local function add_all_diags ( buf , diags ) 200for _ , diagnostic0 in pairs ( diags ) do 201add ( buf , diagnostic0 ) 202end 203end 204 205if not namespace and not bufnr then 206for buf , ns_diags in pairs ( diagnostic_cache ) do 207for _ , diags in pairs ( ns_diags ) do 208add_all_diags ( buf , diags ) 209end 210end 211elseif not namespace then 212bufnr =vim . _resolve_bufnr ( bufnr ) 213for _ , diags in pairs ( diagnostic_cache [ bufnr ]) do 214add_all_diags ( bufnr , diags ) 215end 216elseif bufnr == nil then 217for b , t in pairs ( diagnostic_cache ) do 218for _ , iter_namespace in ipairs ( namespace ) do 219add_all_diags ( b , t [ iter_namespace ] or {} ) 220end 221end 222else 223bufnr =vim . _resolve_bufnr ( bufnr ) 224for _ , iter_namespace in ipairs ( namespace ) do 225add_all_diags ( bufnr , diagnostic_cache [ bufnr ][ iter_namespace ] or {} ) 226end 227end 228 229return diagnostics 230end 231 232--- @return integer[] 233function M . get_bufnrs () 234return vim . tbl_keys ( diagnostic_cache ) 235end 236 237--- @param bufnr integer 238--- @return integer[] 239function M . get_buf_namespaces ( bufnr ) 240return vim . tbl_keys ( diagnostic_cache [ vim . _resolve_bufnr ( bufnr )]) 241end 242 243--- @param namespace integer 244--- @param bufnr integer 245function M . clear ( namespace , bufnr ) 246diagnostic_cache [ vim . _resolve_bufnr ( bufnr )][ namespace ] =nil 247end 248 249--- @param bufnr integer 250function M . drop_buf ( bufnr ) 251diagnostic_cache [ vim . _resolve_bufnr ( bufnr )] =nil 252end 253 254--- Set diagnostics for the given namespace and buffer. 255--- 256--- @param namespace integer The diagnostic namespace 257--- @param bufnr integer Buffer number 258--- @param diagnostics vim.Diagnostic.Set[] 259function M . set ( namespace , bufnr , diagnostics ) 260vim . validate ( 'namespace' , namespace , 'number' ) 261vim . validate ( 'bufnr' , bufnr , 'number' ) 262vim . validate ( 'diagnostics' , diagnostics , vim . islist , 'a list of diagnostics' ) 263 264bufnr =vim . _resolve_bufnr ( bufnr ) 265 266for _ , diagnostic0 in ipairs ( diagnostics ) do 267norm_diag ( bufnr , namespace , diagnostic0 ) 268end 269 270--- @cast diagnostics vim.Diagnostic[] 271 272if vim . tbl_isempty ( diagnostics ) then 273diagnostic_cache [ bufnr ][ namespace ] =nil 274else 275diagnostic_cache [ bufnr ][ namespace ] =diagnostics 276end 277 278if api . nvim_buf_is_loaded ( bufnr ) then 279set_position_extmarks ( bufnr , namespace ) 280end 281end 282 283--- @param bufnr integer? Buffer number to get diagnostics from. Use 0 for 284--- current buffer or nil for all buffers. 285--- @param opts? vim.diagnostic.GetOpts 286--- @return vim.Diagnostic[] : Fields `bufnr`, `end_lnum`, `end_col`, and `severity` 287--- are guaranteed to be present. 288function M . get ( bufnr , opts ) 289vim . validate ( 'bufnr' , bufnr , 'number' , true ) 290vim . validate ( 'opts' , opts , 'table' , true ) 291 292return vim . deepcopy ( M . get_diagnostics ( bufnr , opts , false ), true ) 293end 294 295--- @param bufnr? integer Buffer number to get diagnostics from. Use 0 for 296--- current buffer or nil for all buffers. 297--- @param opts? vim.diagnostic.GetOpts 298--- @return table<integer, integer> : Table with actually present severity values as keys 299--- (see |diagnostic-severity|) and integer counts as values. 300function M . count ( bufnr , opts ) 301vim . validate ( 'bufnr' , bufnr , 'number' , true ) 302vim . validate ( 'opts' , opts , 'table' , true ) 303 304local diagnostics =M . get_diagnostics ( bufnr , opts , false ) 305local count ={} --- @type table<integer,integer> 306for _ , d in ipairs ( diagnostics ) do 307count [ d . severity ] =( count [ d . severity ] or 0 ) + 1 308end 309return count 310end 311 312return M