robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local severity =vim . diagnostic . severity 2 3--- @class (private) vim.diagnostic._severity 4local M ={} 5 6--- @param value string|vim.diagnostic.Severity? 7--- @return vim.diagnostic.Severity? 8function M . to_severity ( value ) 9if type ( value ) == 'string' then 10local ret =severity [ value : upper ()] --[[@as vim.diagnostic.Severity?]] 11if not ret then 12error (( 'Invalid severity: %s' ): format ( value )) 13end 14return ret 15end 16 17return value --[[@as vim.diagnostic.Severity?]] 18end 19 20--- @param filter vim.diagnostic.SeverityFilter 21--- @return fun(d: vim.Diagnostic):boolean 22function M . severity_predicate ( filter ) 23if type ( filter ) ~= 'table' then 24--- @cast filter vim.diagnostic.Severity 25local severity0 =M . to_severity ( filter ) 26--- @param d vim.Diagnostic 27return function ( d ) 28return d . severity == severity0 29end 30end 31 32--- @diagnostic disable-next-line: undefined-field 33if filter . min or filter . max then 34--- @cast filter {min:vim.diagnostic.Severity,max:vim.diagnostic.Severity} 35local min_severity =M . to_severity ( filter . min ) or severity . HINT 36local max_severity =M . to_severity ( filter . max ) or severity . ERROR 37 38--- @param d vim.Diagnostic 39return function ( d ) 40return d . severity <= min_severity and d . severity >= max_severity 41end 42end 43 44--- @cast filter vim.diagnostic.Severity[] 45local severities ={} --- @type table<vim.diagnostic.Severity,true> 46for _ , s in ipairs ( filter ) do 47severities [ assert ( M . to_severity ( s ))] =true 48end 49 50--- @param d vim.Diagnostic 51return function ( d ) 52return severities [ d . severity ] 53end 54end 55 56--- @param filter vim.diagnostic.SeverityFilter? 57--- @param diagnostics vim.Diagnostic[] 58--- @return vim.Diagnostic[] 59function M . filter_by_severity ( filter , diagnostics ) 60if not filter then 61return diagnostics 62end 63 64return vim . tbl_filter ( M . severity_predicate ( filter ), diagnostics ) 65end 66 67--- Parse a diagnostic from a string. 68--- 69--- @param str string String to parse diagnostics from. 70--- @param pat string Lua pattern with capture groups. 71--- @param groups string[] List of fields in a |vim.Diagnostic.Set| structure to associate with captures from {pat}. 72--- @param severity_map table? A table mapping the severity field from {groups} with an item from |vim.diagnostic.severity|. 73--- @param defaults table? Table of default values for any fields not listed in {groups}. 74--- @return vim.Diagnostic.Set? 75function M . match ( str , pat , groups , severity_map , defaults ) 76vim . validate ( 'str' , str , 'string' ) 77vim . validate ( 'pat' , pat , 'string' ) 78vim . validate ( 'groups' , groups , 'table' ) 79vim . validate ( 'severity_map' , severity_map , 'table' , true ) 80vim . validate ( 'defaults' , defaults , 'table' , true ) 81 82--- @type table<string,vim.diagnostic.Severity> 83severity_map =severity_map or severity 84 85local matches ={ str : match ( pat ) } --- @type any[] 86if vim . tbl_isempty ( matches ) then 87return 88end 89 90local diagnostic ={} --- @type table<string,any> 91for i , match in ipairs ( matches ) do 92local field =groups [ i ] 93if field == 'severity' then 94diagnostic [ field ] =severity_map [ match ] 95elseif field == 'lnum' or field == 'end_lnum' or field == 'col' or field == 'end_col' then 96diagnostic [ field ] =vim . _assert_integer ( match ) - 1 97elseif field then 98diagnostic [ field ] =match 99end 100end 101 102diagnostic =vim . tbl_extend ( 'keep' , diagnostic , defaults or {} ) --- @type vim.Diagnostic.Set 103diagnostic . severity =diagnostic . severity or severity . ERROR 104diagnostic . col =diagnostic . col or 0 105diagnostic . end_lnum =diagnostic . end_lnum or diagnostic . lnum 106diagnostic . end_col =diagnostic . end_col or diagnostic . col 107return diagnostic 108end 109 110return M