health.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. local M = {}
  2. local report_info = vim.health.info
  3. local report_warn = vim.health.warn
  4. local function check_log()
  5. local log = vim.lsp.log
  6. local current_log_level = log.get_level()
  7. local log_level_string = log.levels[current_log_level] ---@type string
  8. report_info(string.format('LSP log level : %s', log_level_string))
  9. if current_log_level < log.levels.WARN then
  10. report_warn(
  11. string.format(
  12. 'Log level %s will cause degraded performance and high disk usage',
  13. log_level_string
  14. )
  15. )
  16. end
  17. local log_path = vim.lsp.get_log_path()
  18. report_info(string.format('Log path: %s', log_path))
  19. local log_file = vim.uv.fs_stat(log_path)
  20. local log_size = log_file and log_file.size or 0
  21. local report_fn = (log_size / 1000000 > 100 and report_warn or report_info)
  22. report_fn(string.format('Log size: %d KB', log_size / 1000))
  23. end
  24. --- @param f function
  25. --- @return string
  26. local function func_tostring(f)
  27. local info = debug.getinfo(f, 'S')
  28. return ('<function %s:%s>'):format(info.source, info.linedefined)
  29. end
  30. local function check_active_clients()
  31. vim.health.start('vim.lsp: Active Clients')
  32. local clients = vim.lsp.get_clients()
  33. if next(clients) then
  34. for _, client in pairs(clients) do
  35. local server_version = vim.tbl_get(client, 'server_info', 'version')
  36. or '? (no serverInfo.version response)'
  37. local cmd ---@type string
  38. local ccmd = client.config.cmd
  39. if type(ccmd) == 'table' then
  40. cmd = vim.inspect(ccmd)
  41. elseif type(ccmd) == 'function' then
  42. cmd = func_tostring(ccmd)
  43. end
  44. local dirs_info ---@type string
  45. if client.workspace_folders and #client.workspace_folders > 1 then
  46. local wfolders = {} --- @type string[]
  47. for _, dir in ipairs(client.workspace_folders) do
  48. wfolders[#wfolders + 1] = dir.name
  49. end
  50. dirs_info = ('- Workspace folders:\n %s'):format(table.concat(wfolders, '\n '))
  51. else
  52. dirs_info = string.format(
  53. '- Root directory: %s',
  54. client.root_dir and vim.fn.fnamemodify(client.root_dir, ':~')
  55. ) or nil
  56. end
  57. report_info(table.concat({
  58. string.format('%s (id: %d)', client.name, client.id),
  59. string.format('- Version: %s', server_version),
  60. dirs_info,
  61. string.format('- Command: %s', cmd),
  62. string.format('- Settings: %s', vim.inspect(client.settings, { newline = '\n ' })),
  63. string.format(
  64. '- Attached buffers: %s',
  65. vim.iter(pairs(client.attached_buffers)):map(tostring):join(', ')
  66. ),
  67. }, '\n'))
  68. end
  69. else
  70. report_info('No active clients')
  71. end
  72. end
  73. local function check_watcher()
  74. vim.health.start('vim.lsp: File Watcher')
  75. -- Only run the check if file watching has been enabled by a client.
  76. local clients = vim.lsp.get_clients()
  77. if
  78. --- @param client vim.lsp.Client
  79. vim.iter(clients):all(function(client)
  80. local has_capability = vim.tbl_get(
  81. client.capabilities,
  82. 'workspace',
  83. 'didChangeWatchedFiles',
  84. 'dynamicRegistration'
  85. )
  86. local has_dynamic_capability =
  87. client.dynamic_capabilities:get(vim.lsp.protocol.Methods.workspace_didChangeWatchedFiles)
  88. return has_capability == nil
  89. or has_dynamic_capability == nil
  90. or client.workspace_folders == nil
  91. end)
  92. then
  93. report_info('file watching "(workspace/didChangeWatchedFiles)" disabled on all clients')
  94. return
  95. end
  96. local watchfunc = vim.lsp._watchfiles._watchfunc
  97. assert(watchfunc)
  98. local watchfunc_name --- @type string
  99. if watchfunc == vim._watch.watch then
  100. watchfunc_name = 'libuv-watch'
  101. elseif watchfunc == vim._watch.watchdirs then
  102. watchfunc_name = 'libuv-watchdirs'
  103. elseif watchfunc == vim._watch.inotifywait then
  104. watchfunc_name = 'inotifywait'
  105. else
  106. local nm = debug.getinfo(watchfunc, 'S').source
  107. watchfunc_name = string.format('Custom (%s)', nm)
  108. end
  109. report_info('File watch backend: ' .. watchfunc_name)
  110. if watchfunc_name == 'libuv-watchdirs' then
  111. report_warn('libuv-watchdirs has known performance issues. Consider installing inotify-tools.')
  112. end
  113. end
  114. local function check_position_encodings()
  115. vim.health.start('vim.lsp: Position Encodings')
  116. local clients = vim.lsp.get_clients()
  117. if next(clients) then
  118. local position_encodings = {} ---@type table<integer, table<string, integer[]>>
  119. for _, client in pairs(clients) do
  120. for bufnr in pairs(client.attached_buffers) do
  121. if not position_encodings[bufnr] then
  122. position_encodings[bufnr] = {}
  123. end
  124. if not position_encodings[bufnr][client.offset_encoding] then
  125. position_encodings[bufnr][client.offset_encoding] = {}
  126. end
  127. table.insert(position_encodings[bufnr][client.offset_encoding], client.id)
  128. end
  129. end
  130. -- Check if any buffers are attached to multiple clients with different position encodings
  131. local buffers = {} ---@type integer[]
  132. for bufnr, encodings in pairs(position_encodings) do
  133. local list = {} ---@type string[]
  134. for k in pairs(encodings) do
  135. list[#list + 1] = k
  136. end
  137. if #list > 1 then
  138. buffers[#buffers + 1] = bufnr
  139. end
  140. end
  141. if #buffers > 0 then
  142. local lines =
  143. { 'Found buffers attached to multiple clients with different position encodings.' }
  144. for _, bufnr in ipairs(buffers) do
  145. local encodings = position_encodings[bufnr]
  146. local parts = {}
  147. for encoding, client_ids in pairs(encodings) do
  148. table.insert(
  149. parts,
  150. string.format('%s (client id(s): %s)', encoding:upper(), table.concat(client_ids, ', '))
  151. )
  152. end
  153. table.insert(lines, string.format('- Buffer %d: %s', bufnr, table.concat(parts, ', ')))
  154. end
  155. report_warn(
  156. table.concat(lines, '\n'),
  157. 'Use the positionEncodings client capability to ensure all clients use the same position encoding'
  158. )
  159. else
  160. report_info('No buffers contain mixed position encodings')
  161. end
  162. else
  163. report_info('No active clients')
  164. end
  165. end
  166. local function check_enabled_configs()
  167. vim.health.start('vim.lsp: Enabled Configurations')
  168. for name in vim.spairs(vim.lsp._enabled_configs) do
  169. local config = vim.lsp.config[name]
  170. local text = {} --- @type string[]
  171. text[#text + 1] = ('%s:'):format(name)
  172. for k, v in
  173. vim.spairs(config --[[@as table<string,any>]])
  174. do
  175. local v_str --- @type string?
  176. if k == 'name' then
  177. v_str = nil
  178. elseif k == 'filetypes' or k == 'root_markers' then
  179. v_str = table.concat(v, ', ')
  180. elseif type(v) == 'function' then
  181. v_str = func_tostring(v)
  182. else
  183. v_str = vim.inspect(v, { newline = '\n ' })
  184. end
  185. if k == 'cmd' and type(v) == 'table' and vim.fn.executable(v[1]) == 0 then
  186. report_warn(("'%s' is not executable. Configuration will not be used."):format(v[1]))
  187. end
  188. if v_str then
  189. text[#text + 1] = ('- %s: %s'):format(k, v_str)
  190. end
  191. end
  192. text[#text + 1] = ''
  193. report_info(table.concat(text, '\n'))
  194. end
  195. end
  196. --- Performs a healthcheck for LSP
  197. function M.check()
  198. check_log()
  199. check_active_clients()
  200. check_enabled_configs()
  201. check_watcher()
  202. check_position_encodings()
  203. end
  204. return M