robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local store =require ( 'vim.diagnostic._store' ) 2 3--- @class vim.diagnostic.OptsResolved 4--- @field float false|vim.diagnostic.Opts.Float 5--- @field update_in_insert boolean 6--- @field underline false|vim.diagnostic.Opts.Underline 7--- @field virtual_text false|vim.diagnostic.Opts.VirtualText 8--- @field virtual_lines false|vim.diagnostic.Opts.VirtualLines 9--- @field signs false|vim.diagnostic.Opts.Signs 10--- @field severity_sort false|{reverse?:boolean} 11 12--- @class (private) vim.diagnostic._config 13local M ={} 14 15--- @type vim.diagnostic.Opts 16local global_diagnostic_options ={ 17signs =true , 18underline =true , 19virtual_text =false , 20virtual_lines =false , 21float =true , 22update_in_insert =false , 23severity_sort =false , 24jump ={ 25-- Wrap around buffer 26wrap =true , 27} , 28} 29 30--- @param name 'signs'|'underline'|'virtual_text'|'virtual_lines'|'float' 31function M . enable_handler ( name ) 32if global_diagnostic_options [ name ] == nil then 33global_diagnostic_options [ name ] =true 34end 35end 36 37--- @param option string 38--- @param namespace integer? 39--- @return table 40local function enabled_value ( option , namespace ) 41local ns =namespace and vim . diagnostic . get_namespace ( namespace ) or {} 42if ns . opts and type ( ns . opts [ option ]) == 'table' then 43return ns . opts [ option ] 44end 45 46local global_opt =global_diagnostic_options [ option ] 47if type ( global_opt ) == 'table' then 48return global_opt 49end 50 51return {} 52end 53 54--- @param option string 55--- @param value any? 56--- @param namespace integer? 57--- @param bufnr integer 58--- @return any 59local function resolve_optional_value ( option , value , namespace , bufnr ) 60if not value then 61return false 62elseif value == true then 63return enabled_value ( option , namespace ) 64elseif type ( value ) == 'function' then 65local val =value ( namespace , bufnr ) --- @type any 66if val == true then 67return enabled_value ( option , namespace ) 68else 69return val 70end 71elseif type ( value ) == 'table' then 72return value 73end 74error ( 'Unexpected option type: ' .. vim . inspect ( value )) 75end 76 77--- @param opts vim.diagnostic.Opts? 78--- @param namespace integer? 79--- @param bufnr integer 80--- @return vim.diagnostic.OptsResolved 81function M . get_resolved_options ( opts , namespace , bufnr ) 82local ns =namespace and vim . diagnostic . get_namespace ( namespace ) or {} 83-- Do not use tbl_deep_extend so that an empty table can be used to reset to default values 84local resolved =vim . tbl_extend ( 'keep' , opts or {} , ns . opts or {} , global_diagnostic_options ) --- @type table<string,any> 85for k in pairs ( global_diagnostic_options ) do 86if resolved [ k ] ~= nil then 87resolved [ k ] =resolve_optional_value ( k , resolved [ k ], namespace , bufnr ) 88end 89end 90return resolved --[[@as vim.diagnostic.OptsResolved]] 91end 92 93--- @param opts vim.diagnostic.Opts? When omitted or `nil`, retrieve the current 94--- configuration. Otherwise, a configuration table (see |vim.diagnostic.Opts|). 95--- @param namespace integer? Update the options for the given namespace. 96--- When omitted, update the global diagnostic options. 97--- @return vim.diagnostic.Opts? : Current diagnostic config if {opts} is omitted. 98--- @overload fun(opts?: nil, namespace?: integer): vim.diagnostic.Opts 99function M . config ( opts , namespace ) 100vim . validate ( 'opts' , opts , 'table' , true ) 101vim . validate ( 'namespace' , namespace , 'number' , true ) 102 103local t --- @type vim.diagnostic.Opts 104if namespace then 105local ns =vim . diagnostic . get_namespace ( namespace ) 106t =ns . opts 107else 108t =global_diagnostic_options 109end 110 111if not opts then 112-- Return current config 113return vim . deepcopy ( t , true ) 114end 115 116local jump_opts =opts . jump --[[@as vim.diagnostic.JumpOpts1]] 117if jump_opts and jump_opts . float ~= nil then --- @diagnostic disable-line 118vim . deprecate ( 'opts.jump.float' , 'opts.jump.on_jump' , '0.14' ) 119 120local float_opts =jump_opts . float 121if float_opts then 122float_opts =type ( float_opts ) == 'table' and float_opts or {} 123 124jump_opts . on_jump =function ( _ , bufnr ) 125vim . diagnostic . open_float ( vim . tbl_extend ( 'keep' , float_opts , { 126bufnr =bufnr , 127scope ='cursor' , 128focus =false , 129} )) 130end 131end 132 133opts . jump . float =nil --- @diagnostic disable-line 134end 135 136for k , v in 137pairs ( opts --[[@as table<any,any>]] ) 138do 139t [ k ] =v 140end 141 142if namespace then 143for _ , bufnr in ipairs ( store . get_bufnrs ()) do 144local namespaces =store . get_buf_namespaces ( bufnr ) 145if vim . list_contains ( namespaces , namespace ) then 146vim . diagnostic . show ( namespace , bufnr ) 147end 148end 149else 150for _ , bufnr in ipairs ( store . get_bufnrs ()) do 151for _ , ns in ipairs ( store . get_buf_namespaces ( bufnr )) do 152vim . diagnostic . show ( ns , bufnr ) 153end 154end 155end 156end 157 158return M