robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local api =vim . api 2 3local store =require ( 'vim.diagnostic._store' ) 4 5--- @class (private) vim.diagnostic._shared 6local M ={} 7 8--- @param bufnr integer 9--- @return integer 10function M . count_sources ( bufnr ) 11local count =0 12local seen ={} --- @type table<string, true> 13for _ , diagnostic in ipairs ( store . get_diagnostics ( bufnr , nil , false )) do 14local source =diagnostic . source 15if source and not seen [ source ] then 16seen [ source ] =true 17count =count + 1 18end 19end 20return count 21end 22 23--- @param diagnostics vim.Diagnostic[] 24--- @return vim.Diagnostic[] 25function M . prefix_source ( diagnostics ) 26--- @param diagnostic vim.Diagnostic 27return vim . tbl_map ( function ( diagnostic ) 28if not diagnostic . source then 29return diagnostic 30end 31 32local copied =vim . deepcopy ( diagnostic , true ) 33copied . message =string . format ( '%s: %s' , diagnostic . source , diagnostic . message ) 34return copied 35end , diagnostics ) 36end 37 38--- Get a position based on an extmark referenced by `_extmark_id` field 39--- @param diagnostic vim.Diagnostic 40--- @return integer lnum 41--- @return integer col 42--- @return integer end_lnum 43--- @return integer end_col 44--- @return boolean valid 45function M . get_logical_pos ( diagnostic ) 46if not diagnostic . _extmark_id then 47return diagnostic . lnum , diagnostic . col , diagnostic . end_lnum , diagnostic . end_col , true 48end 49 50local ns =vim . diagnostic . get_namespace ( assert ( diagnostic . namespace )) 51local extmark =api . nvim_buf_get_extmark_by_id ( 52diagnostic . bufnr , 53ns . user_data . location_ns , 54diagnostic . _extmark_id , 55{ details =true } 56) 57if next ( extmark ) == nil then 58return diagnostic . lnum , diagnostic . col , diagnostic . end_lnum , diagnostic . end_col , true 59end 60 61-- Diagnostic extmarks always have an end position. 62---@cast extmark [integer, integer, {end_row: integer, end_col: integer, invalid?: boolean}] 63return extmark [ 1 ], extmark [ 2 ], extmark [ 3 ]. end_row , extmark [ 3 ]. end_col , not extmark [ 3 ]. invalid 64end 65 66--- @param diagnostics vim.Diagnostic[]? 67--- @param use_logical_pos boolean 68--- @return table<integer, vim.Diagnostic[]> 69function M . diagnostic_lines ( diagnostics , use_logical_pos ) 70if not diagnostics then 71return {} 72end 73 74local diagnostics_by_line ={} --- @type table<integer, vim.Diagnostic[]> 75for _ , diagnostic in ipairs ( diagnostics ) do 76local lnum --- @type integer 77local valid --- @type boolean 78 79if use_logical_pos then 80lnum , _ , _ , _ , valid =M . get_logical_pos ( diagnostic ) 81else 82lnum , valid =diagnostic . lnum , true 83end 84 85if valid then 86local line_diagnostics =diagnostics_by_line [ lnum ] 87if not line_diagnostics then 88line_diagnostics ={} 89diagnostics_by_line [ lnum ] =line_diagnostics 90end 91line_diagnostics [ # line_diagnostics + 1 ] =diagnostic 92end 93end 94return diagnostics_by_line 95end 96 97--- @param diagnostics table<integer, vim.Diagnostic[]> 98--- @return vim.Diagnostic[] 99function M . diagnostics_at_cursor ( diagnostics ) 100local lnum =api . nvim_win_get_cursor ( 0 )[ 1 ] - 1 101 102if diagnostics [ lnum ] ~= nil then 103return diagnostics [ lnum ] 104end 105 106local cursor_diagnostics ={} --- @type vim.Diagnostic[] 107for _ , line_diags in pairs ( diagnostics ) do 108for _ , diagnostic in ipairs ( line_diags ) do 109if diagnostic . end_lnum and lnum >= diagnostic . lnum and lnum <= diagnostic . end_lnum then 110cursor_diagnostics [ # cursor_diagnostics + 1 ] =diagnostic 111end 112end 113end 114return cursor_diagnostics 115end 116 117--- @param a vim.Diagnostic 118--- @param b vim.Diagnostic 119--- @param primary_key string 120--- @param reverse boolean 121--- @param col_fn? fun(diagnostic: vim.Diagnostic): integer 122--- @return boolean 123function M . diagnostic_cmp ( a , b , primary_key , reverse , col_fn ) 124local a_val , b_val --- @type integer, integer 125if col_fn then 126a_val , b_val =col_fn ( a ), col_fn ( b ) 127else 128a_val =a [ primary_key ] --[[@as integer]] 129b_val =b [ primary_key ] --[[@as integer]] 130end 131 132--- @param x integer 133--- @param y integer 134local function cmp ( x , y ) 135if reverse then 136return x > y 137end 138return x < y 139end 140 141if a_val ~= b_val then 142return cmp ( a_val , b_val ) 143end 144if a . lnum ~= b . lnum then 145return cmp ( a . lnum , b . lnum ) 146end 147if a . col ~= b . col then 148return cmp ( a . col , b . col ) 149end 150if a . end_lnum ~= b . end_lnum then 151return cmp ( a . end_lnum , b . end_lnum ) 152end 153if a . end_col ~= b . end_col then 154return cmp ( a . end_col , b . end_col ) 155end 156 157return cmp ( a . _extmark_id or 0 , b . _extmark_id or 0 ) 158end 159 160--- @param format fun(diagnostic: vim.Diagnostic): string? 161--- @param diagnostics vim.Diagnostic[] 162--- @return vim.Diagnostic[] 163function M . reformat_diagnostics ( format , diagnostics ) 164vim . validate ( 'format' , format , 'function' ) 165vim . validate ( 'diagnostics' , diagnostics , vim . islist , 'a list of diagnostics' ) 166 167local formatted ={} --- @type vim.Diagnostic[] 168for _ , diagnostic in ipairs ( diagnostics ) do 169local message =format ( diagnostic ) 170if message ~= nil then 171local formatted_diagnostic =vim . deepcopy ( diagnostic , true ) 172formatted_diagnostic . message =message 173formatted [ # formatted + 1 ] =formatted_diagnostic 174end 175end 176return formatted 177end 178 179return M