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