robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local M ={} 2 3local report_info =vim . health . info 4local report_warn =vim . health . warn 5 6--- Buffer the user was in when they ran `:checkhealth`. 7--- @type integer? 8local cur_bufnr =- 1 9 10--- Decorates a buffer-id in a report line, so it is easy for the user to see (and for us to highlight). 11--- @param bufnr integer 12--- @return string 13local function decor_curbuf ( bufnr ) 14if bufnr == cur_bufnr and vim . api . nvim_buf_is_valid ( bufnr ) then 15return ( '%d (current buffer, %q)' ): format ( 16bufnr , 17vim . fs . basename ( vim . api . nvim_buf_get_name ( bufnr )) 18) 19end 20return tostring ( bufnr ) 21end 22 23local function check_log () 24local log =vim . lsp . log 25local current_log_level =log . get_level () 26local log_level_string =assert ( log . levels [ current_log_level ]) 27report_info ( string . format ( 'LSP log level: %s' , log_level_string )) 28 29if current_log_level < log . levels . WARN then 30report_warn ( 31string . format ( 32'Log level %s will cause degraded performance and high disk usage' , 33log_level_string 34) 35) 36end 37 38local log_path =log . get_filename () 39report_info ( string . format ( 'Log path: %s' , log_path )) 40 41local log_file =vim . uv . fs_stat ( log_path ) 42local log_size =log_file and log_file . size or 0 43 44local report_fn =( log_size / 1000000 > 100 and report_warn or report_info ) 45report_fn ( string . format ( 'Log size: %d KB' , log_size / 1000 )) 46end 47 48local function check_active_features () 49vim . health . start ( 'vim.lsp: Active Features' ) 50if not next ( vim . lsp . get_clients ()) then 51report_info ( 'No active clients' ) 52return 53end 54 55for capname , capability in vim . spairs ( vim . lsp . _capability . all ) do 56---@type string[] 57local buf_infos ={} 58for bufnr , instance in pairs ( capability . active ) do 59local client_info =vim 60. iter ( pairs ( instance . client_state )) 61: map ( function ( client_id ) 62local client =vim . lsp . get_client_by_id ( client_id ) 63if client then 64return string . format ( '%s (id: %d)' , client . name , client . id ) 65else 66return string . format ( 'unknown (id: %d)' , client_id ) 67end 68end ) 69: join ( ', ' ) 70if client_info == '' then 71client_info ='No supported client attached' 72end 73 74buf_infos [ # buf_infos + 1 ] =( ' [%s]: %s' ): format ( decor_curbuf ( bufnr ), client_info ) 75end 76 77report_info ( table . concat ( { 78capname , 79'- Active buffers:' , 80vim . tbl_isempty ( buf_infos ) and ' (none)' or string . format ( table . concat ( buf_infos , '\n' )), 81} , '\n' )) 82end 83end 84 85--- @param f function 86--- @return string 87local function func_tostring ( f ) 88local info =assert ( debug . getinfo ( f , 'S' )) 89return ( '<function %s:%s>' ): format ( info . source , info . linedefined ) 90end 91 92local function check_active_clients () 93vim . health . start ( 'vim.lsp: Active Clients' ) 94local clients =vim . lsp . get_clients () 95if next ( clients ) then 96for _ , client in pairs ( clients ) do 97local server_version =vim . tbl_get ( client , 'server_info' , 'version' ) 98or '? (no serverInfo.version response)' 99local cmd ---@type string 100local ccmd =client . config . cmd 101if type ( ccmd ) == 'table' then 102cmd =vim . inspect ( ccmd ) 103elseif type ( ccmd ) == 'function' then 104cmd =func_tostring ( ccmd ) 105end 106local dirs_info ---@type string 107if client . workspace_folders and # client . workspace_folders > 1 then 108local wfolders ={} --- @type string[] 109for _ , dir in ipairs ( client . workspace_folders ) do 110wfolders [ # wfolders + 1 ] =dir . name 111end 112dirs_info =( '- Workspace folders:\n %s' ): format ( table . concat ( wfolders , '\n ' )) 113else 114dirs_info =string . format ( 115'- Root directory: %s' , 116-- vim.fs.relpath does not prepend '~/' while fnamemodify does 117client . root_dir and vim . fn . fnamemodify ( client . root_dir , ':~' ) 118) or nil 119end 120report_info ( table . concat ( { 121string . format ( '%s (id: %d)' , client . name , client . id ), 122( '- Attached buffers: %s' ): format ( 123vim . iter ( pairs ( client . attached_buffers )): map ( decor_curbuf ): join ( ', ' ) 124), 125string . format ( '- Version: %s' , server_version ), 126dirs_info , 127string . format ( '- Command: %s' , cmd ), 128string . format ( '- Settings: %s' , vim . inspect ( client . settings , { newline ='\n ' } )), 129} , '\n' )) 130end 131else 132report_info ( 'No active clients' ) 133end 134end 135 136-- Non-LSP filewatchers are also checked in: runtime/lua/vim/health/health.lua 137local function check_watcher () 138-- Only run the check if file watching has been enabled by a client. 139local clients =vim . lsp . get_clients () 140if 141--- @param client vim.lsp.Client 142vim . iter ( clients ): all ( function ( client ) 143local has_capability =vim . tbl_get ( 144client . capabilities , 145'workspace' , 146'didChangeWatchedFiles' , 147'dynamicRegistration' 148) 149local has_dynamic_capability = 150client . dynamic_capabilities : get ( 'workspace/didChangeWatchedFiles' ) 151return has_capability == nil 152or has_dynamic_capability == nil 153or client . workspace_folders == nil 154end ) 155then 156report_info ( 'Filewatchers "(workspace/didChangeWatchedFiles)" disabled on all clients' ) 157return 158end 159 160local watchfunc =vim . lsp . _watchfiles . _watchfunc 161local watchfunc_name --- @type string 162if watchfunc == vim . _watch . watch then 163watchfunc_name ='libuv-watch' 164elseif watchfunc == vim . _watch . watchdirs then 165watchfunc_name ='libuv-watchdirs' 166elseif watchfunc == vim . _watch . inotify then 167watchfunc_name ='inotify' 168else 169local info =assert ( debug . getinfo ( watchfunc , 'S' )) 170watchfunc_name =string . format ( 'Custom (%s)' , info . source ) 171end 172 173report_info ( 'Filewatch backend: ' .. watchfunc_name ) 174if watchfunc_name == 'libuv-watchdirs' then 175report_warn ( 'libuv-watchdirs has known performance issues. Consider installing inotify-tools.' ) 176end 177end 178 179local function check_position_encodings () 180local clients =vim . lsp . get_clients () 181if not next ( clients ) then 182report_info ( 'Position encodings: No active clients' ) 183else 184local position_encodings ={} ---@type table<integer, table<string, integer[]>> 185for _ , client in pairs ( clients ) do 186for bufnr in pairs ( client . attached_buffers ) do 187if not position_encodings [ bufnr ] then 188position_encodings [ bufnr ] ={} 189end 190if not position_encodings [ bufnr ][ client . offset_encoding ] then 191position_encodings [ bufnr ][ client . offset_encoding ] ={} 192end 193table . insert ( position_encodings [ bufnr ][ client . offset_encoding ], client . id ) 194end 195end 196 197-- Check if any buffers are attached to multiple clients with different position encodings 198local buffers ={} ---@type integer[] 199for bufnr , encodings in pairs ( position_encodings ) do 200local list ={} ---@type string[] 201for k in pairs ( encodings ) do 202list [ # list + 1 ] =k 203end 204 205if # list > 1 then 206buffers [ # buffers + 1 ] =bufnr 207end 208end 209 210if # buffers > 0 then 211local lines ={ 212'Position encodings: Found buffers attached to multiple clients with different position encodings.' , 213} 214for _ , bufnr in ipairs ( buffers ) do 215local encodings =position_encodings [ bufnr ] 216local parts ={} 217for encoding , client_ids in pairs ( encodings ) do 218table . insert ( 219parts , 220string . format ( '%s (client id(s): %s)' , encoding : upper (), table . concat ( client_ids , ', ' )) 221) 222end 223table . insert ( lines , string . format ( '- Buffer %d: %s' , bufnr , table . concat ( parts , ', ' ))) 224end 225report_warn ( 226table . concat ( lines , '\n' ), 227'Use the positionEncodings client capability to ensure all clients use the same position encoding' 228) 229else 230report_info ( 'Position encodings: No buffers contain mixed position encodings' ) 231end 232end 233end 234 235local function check_enabled_configs () 236vim . health . start ( 'vim.lsp: Enabled Configurations' ) 237 238local known_filetypes =vim . filetype . _get_known_filetypes () 239 240for name in vim . spairs ( vim . lsp . _enabled_configs ) do 241local config =vim . lsp . config [ name ] 242local text ={} --- @type string[] 243text [ # text + 1 ] =( '%s:' ): format ( name ) 244if not config then 245report_warn ( 246( "'%s' config not found. Ensure that vim.lsp.config('%s') was called." ): format ( name , name ) 247) 248else 249for k , v in 250vim . spairs ( config --[[@as table<string,any>]] ) 251do 252local v_str --- @type string? 253if k == 'name' then 254v_str =nil 255elseif k == 'filetypes' then 256v_str =table . concat ( v , ', ' ) 257elseif type ( v ) == 'function' then 258v_str =func_tostring ( v ) 259else 260v_str =vim . inspect ( v , { newline ='\n ' } ) 261end 262 263if k == 'cmd' and type ( v ) == 'table' and vim . fn . executable ( v [ 1 ]) == 0 then 264report_warn (( "'%s' is not executable. Configuration will not be used." ): format ( v [ 1 ])) 265end 266 267if k == 'filetypes' and type ( v ) == 'table' then 268for _ , filetype in 269ipairs ( v --[[@as string[] ]] ) 270do 271if not known_filetypes [ filetype ] then 272report_warn ( 273( "Unknown filetype '%s' (Hint: filename extension != filetype)." ): format ( filetype ) 274) 275end 276end 277end 278 279if v_str then 280text [ # text + 1 ] =( '- %s: %s' ): format ( k , v_str ) 281end 282end 283end 284text [ # text + 1 ] ='' 285report_info ( table . concat ( text , '\n' )) 286end 287end 288 289--- Performs a healthcheck for LSP 290function M . check () 291cur_bufnr =vim . fn . bufnr ( '#' ) 292 293check_log () 294check_watcher () 295check_position_encodings () 296check_active_features () 297check_active_clients () 298check_enabled_configs () 299 300--- Highlight the "current buffer" token emitted by `decor_curbuf`. 301vim . api . nvim_create_autocmd ( 'FileType' , { 302pattern ='checkhealth' , 303once =true , 304callback =function () 305vim . cmd ( [[ 306silent! syntax clear healthLspCurBuf 307syntax match healthLspCurBuf /\d\+ (current buffer[^)]*)/ 308highlight default link healthLspCurBuf Visual 309]] ) 310end , 311} ) 312end 313 314return M