health.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. --- @brief
  2. ---<pre>help
  3. --- vim.health is a minimal framework to help users troubleshoot configuration and
  4. --- any other environment conditions that a plugin might care about. Nvim ships
  5. --- with healthchecks for configuration, performance, python support, ruby
  6. --- support, clipboard support, and more.
  7. ---
  8. --- To run all healthchecks, use: >vim
  9. ---
  10. --- :checkhealth
  11. --- <
  12. --- Plugin authors are encouraged to write new healthchecks. |health-dev|
  13. ---
  14. --- COMMANDS *health-commands*
  15. ---
  16. --- *:che* *:checkhealth*
  17. --- :che[ckhealth] Run all healthchecks.
  18. --- *E5009*
  19. --- Nvim depends on |$VIMRUNTIME|, 'runtimepath' and 'packpath' to
  20. --- find the standard "runtime files" for syntax highlighting,
  21. --- filetype-specific behavior, and standard plugins (including
  22. --- :checkhealth). If the runtime files cannot be found then
  23. --- those features will not work.
  24. ---
  25. --- :che[ckhealth] {plugins}
  26. --- Run healthcheck(s) for one or more plugins. E.g. to run only
  27. --- the standard Nvim healthcheck: >vim
  28. --- :checkhealth vim.health
  29. --- <
  30. --- To run the healthchecks for the "foo" and "bar" plugins
  31. --- (assuming they are on 'runtimepath' and they have implemented
  32. --- the Lua `require("foo.health").check()` interface): >vim
  33. --- :checkhealth foo bar
  34. --- <
  35. --- To run healthchecks for Lua submodules, use dot notation or
  36. --- "*" to refer to all submodules. For example Nvim provides
  37. --- `vim.lsp` and `vim.treesitter`: >vim
  38. --- :checkhealth vim.lsp vim.treesitter
  39. --- :checkhealth vim*
  40. --- <
  41. ---
  42. --- USAGE *health-usage*
  43. ---
  44. --- Local mappings in the healthcheck buffer:
  45. ---
  46. --- q Closes the window.
  47. ---
  48. --- Global configuration:
  49. ---
  50. --- *g:health*
  51. --- g:health Dictionary with the following optional keys:
  52. --- - `style` (`'float'|nil`) Set to "float" to display :checkhealth in
  53. --- a floating window instead of the default behavior.
  54. ---
  55. --- Example: >lua
  56. --- vim.g.health = { style = 'float' }
  57. ---
  58. --- --------------------------------------------------------------------------------
  59. --- Create a healthcheck *health-dev*
  60. ---
  61. --- Healthchecks are functions that check the user environment, configuration, or
  62. --- any other prerequisites that a plugin cares about. Nvim ships with
  63. --- healthchecks in:
  64. --- - $VIMRUNTIME/autoload/health/
  65. --- - $VIMRUNTIME/lua/vim/lsp/health.lua
  66. --- - $VIMRUNTIME/lua/vim/treesitter/health.lua
  67. --- - and more...
  68. ---
  69. --- To add a new healthcheck for your own plugin, simply create a "health.lua"
  70. --- module on 'runtimepath' that returns a table with a "check()" function. Then
  71. --- |:checkhealth| will automatically find and invoke the function.
  72. ---
  73. --- For example if your plugin is named "foo", define your healthcheck module at
  74. --- one of these locations (on 'runtimepath'):
  75. --- - lua/foo/health/init.lua
  76. --- - lua/foo/health.lua
  77. ---
  78. --- If your plugin also provides a submodule named "bar" for which you want
  79. --- a separate healthcheck, define the healthcheck at one of these locations:
  80. --- - lua/foo/bar/health/init.lua
  81. --- - lua/foo/bar/health.lua
  82. ---
  83. --- All such health modules must return a Lua table containing a `check()`
  84. --- function.
  85. ---
  86. --- Copy this sample code into `lua/foo/health.lua`, replacing "foo" in the path
  87. --- with your plugin name: >lua
  88. ---
  89. --- local M = {}
  90. ---
  91. --- M.check = function()
  92. --- vim.health.start("foo report")
  93. --- -- make sure setup function parameters are ok
  94. --- if check_setup() then
  95. --- vim.health.ok("Setup is correct")
  96. --- else
  97. --- vim.health.error("Setup is incorrect")
  98. --- end
  99. --- -- do some more checking
  100. --- -- ...
  101. --- end
  102. ---
  103. --- return M
  104. ---</pre>
  105. local M = {}
  106. local s_output = {} ---@type string[]
  107. local check_summary = { warn = 0, error = 0 }
  108. -- From a path return a list [{name}, {func}, {type}] representing a healthcheck
  109. local function filepath_to_healthcheck(path)
  110. path = vim.fs.abspath(vim.fs.normalize(path))
  111. local name --- @type string
  112. local func --- @type string
  113. local filetype --- @type string
  114. if path:find('vim$') then
  115. name = vim.fs.basename(path):gsub('%.vim$', '')
  116. func = 'health#' .. name .. '#check'
  117. filetype = 'v'
  118. else
  119. local rtp_lua = vim
  120. .iter(vim.api.nvim_get_runtime_file('lua/', true))
  121. :map(function(rtp_lua)
  122. return vim.fs.abspath(vim.fs.normalize(rtp_lua))
  123. end)
  124. :find(function(rtp_lua)
  125. return vim.fs.relpath(rtp_lua, path)
  126. end)
  127. -- "/path/to/rtp/lua/foo/bar/health.lua" => "foo/bar/health.lua"
  128. -- "/another/rtp/lua/baz/health/init.lua" => "baz/health/init.lua"
  129. local subpath = path:gsub('^' .. vim.pesc(rtp_lua), ''):gsub('^/+', '')
  130. if vim.fs.basename(subpath) == 'health.lua' then
  131. -- */health.lua
  132. name = vim.fs.dirname(subpath)
  133. else
  134. -- */health/init.lua
  135. name = vim.fs.dirname(vim.fs.dirname(subpath))
  136. end
  137. name = assert(name:gsub('/', '.')) --- @type string
  138. func = 'require("' .. name .. '.health").check()'
  139. filetype = 'l'
  140. end
  141. return { name, func, filetype }
  142. end
  143. --- @param plugin_names string
  144. --- @return table<any,string[]> { {name, func, type}, ... } representing healthchecks
  145. local function get_healthcheck_list(plugin_names)
  146. local healthchecks = {} --- @type table<any,string[]>
  147. local plugin_names_list = vim.split(plugin_names, ' ')
  148. for _, p in pairs(plugin_names_list) do
  149. -- support vim/lsp/health{/init/}.lua as :checkhealth vim.lsp
  150. p = p:gsub('%.', '/')
  151. p = p:gsub('*', '**')
  152. local paths = vim.api.nvim_get_runtime_file('autoload/health/' .. p .. '.vim', true)
  153. vim.list_extend(
  154. paths,
  155. vim.api.nvim_get_runtime_file('lua/**/' .. p .. '/health/init.lua', true)
  156. )
  157. vim.list_extend(paths, vim.api.nvim_get_runtime_file('lua/**/' .. p .. '/health.lua', true))
  158. if vim.tbl_count(paths) == 0 then
  159. healthchecks[#healthchecks + 1] = { p, '', '' } -- healthcheck not found
  160. else
  161. local unique_paths = {} --- @type table<string, boolean>
  162. for _, v in pairs(paths) do
  163. unique_paths[v] = true
  164. end
  165. paths = {}
  166. for k, _ in pairs(unique_paths) do
  167. paths[#paths + 1] = k
  168. end
  169. for _, v in ipairs(paths) do
  170. healthchecks[#healthchecks + 1] = filepath_to_healthcheck(v)
  171. end
  172. end
  173. end
  174. return healthchecks
  175. end
  176. --- @param plugin_names string
  177. --- @return table<string, string[]> {name: [func, type], ..} representing healthchecks
  178. local function get_healthcheck(plugin_names)
  179. local health_list = get_healthcheck_list(plugin_names)
  180. local healthchecks = {} --- @type table<string, string[]>
  181. for _, c in pairs(health_list) do
  182. if c[1] ~= 'vim' then
  183. healthchecks[c[1]] = { c[2], c[3] }
  184. end
  185. end
  186. return healthchecks
  187. end
  188. --- Indents lines *except* line 1 of a multiline string.
  189. ---
  190. --- @param s string
  191. --- @param columns integer
  192. --- @return string
  193. local function indent_after_line1(s, columns)
  194. return (vim.text.indent(columns, s):gsub('^%s+', ''))
  195. end
  196. --- Changes ':h clipboard' to ':help |clipboard|'.
  197. ---
  198. --- @param s string
  199. --- @return string
  200. local function help_to_link(s)
  201. return vim.fn.substitute(s, [[\v:h%[elp] ([^|][^"\r\n ]+)]], [[:help |\1|]], [[g]])
  202. end
  203. --- Format a message for a specific report item.
  204. ---
  205. --- @param status string
  206. --- @param msg string
  207. --- @param ... string|string[] Optional advice
  208. --- @return string
  209. local function format_report_message(status, msg, ...)
  210. local output = '- ' .. status
  211. if status ~= '' then
  212. output = output .. ' '
  213. end
  214. output = output .. indent_after_line1(msg, 2)
  215. local varargs = ...
  216. -- Optional parameters
  217. if varargs then
  218. if type(varargs) == 'string' then
  219. varargs = { varargs }
  220. end
  221. output = output .. '\n - ADVICE:'
  222. -- Report each suggestion
  223. for _, v in ipairs(varargs) do
  224. if v then
  225. output = output .. '\n - ' .. indent_after_line1(v, 6) --- @type string
  226. end
  227. end
  228. end
  229. return help_to_link(output)
  230. end
  231. --- @param output string
  232. local function collect_output(output)
  233. vim.list_extend(s_output, vim.split(output, '\n'))
  234. end
  235. --- Starts a new report. Most plugins should call this only once, but if
  236. --- you want different sections to appear in your report, call this once
  237. --- per section.
  238. ---
  239. --- @param name string
  240. function M.start(name)
  241. local input = string.format('\n%s ~', name)
  242. collect_output(input)
  243. end
  244. --- Reports an informational message.
  245. ---
  246. --- @param msg string
  247. function M.info(msg)
  248. local input = format_report_message('', msg)
  249. collect_output(input)
  250. end
  251. --- Reports a "success" message.
  252. ---
  253. --- @param msg string
  254. function M.ok(msg)
  255. local input = format_report_message('✅ OK', msg)
  256. collect_output(input)
  257. end
  258. --- Reports a warning.
  259. ---
  260. --- @param msg string
  261. --- @param ... string|string[] Optional advice
  262. function M.warn(msg, ...)
  263. local input = format_report_message('⚠️ WARNING', msg, ...)
  264. collect_output(input)
  265. check_summary['warn'] = check_summary['warn'] + 1
  266. end
  267. --- Reports an error.
  268. ---
  269. --- @param msg string
  270. --- @param ... string|string[] Optional advice
  271. function M.error(msg, ...)
  272. local input = format_report_message('❌ ERROR', msg, ...)
  273. collect_output(input)
  274. check_summary['error'] = check_summary['error'] + 1
  275. end
  276. local path2name = function(path)
  277. if path:match('%.lua$') then
  278. -- Lua: transform "../lua/vim/lsp/health.lua" into "vim.lsp"
  279. -- Get full path, make sure all slashes are '/'
  280. path = vim.fs.normalize(path)
  281. -- Remove everything up to the last /lua/ folder
  282. path = path:gsub('^.*/lua/', '')
  283. -- Remove the filename (health.lua) or (health/init.lua)
  284. path = vim.fs.dirname(path:gsub('/init%.lua$', ''))
  285. -- Change slashes to dots
  286. path = path:gsub('/', '.')
  287. return path
  288. else
  289. -- Vim: transform "../autoload/health/provider.vim" into "provider"
  290. return vim.fn.fnamemodify(path, ':t:r')
  291. end
  292. end
  293. local PATTERNS = { '/autoload/health/*.vim', '/lua/**/**/health.lua', '/lua/**/**/health/init.lua' }
  294. --- :checkhealth completion function used by cmdexpand.c get_healthcheck_names()
  295. M._complete = function()
  296. local unique = vim ---@type table<string,boolean>
  297. ---@param pattern string
  298. .iter(vim.tbl_map(function(pattern)
  299. return vim.tbl_map(path2name, vim.api.nvim_get_runtime_file(pattern, true))
  300. end, PATTERNS))
  301. :flatten()
  302. ---@param t table<string,boolean>
  303. :fold({}, function(t, name)
  304. t[name] = true -- Remove duplicates
  305. return t
  306. end)
  307. -- vim.health is this file, which is not a healthcheck
  308. unique['vim'] = nil
  309. local rv = vim.tbl_keys(unique)
  310. table.sort(rv)
  311. return rv
  312. end
  313. --- Gets the results heading for the current report section.
  314. ---
  315. ---@return string
  316. local function get_summary()
  317. local s = ''
  318. local errors = check_summary['error']
  319. local warns = check_summary['warn']
  320. s = s .. (warns > 0 and (' %2d ⚠️'):format(warns) or '')
  321. s = s .. (errors > 0 and (' %2d ❌'):format(errors) or '')
  322. if errors == 0 and warns == 0 then
  323. s = s .. '✅'
  324. end
  325. return s
  326. end
  327. --- Runs the specified healthchecks.
  328. --- Runs all discovered healthchecks if plugin_names is empty.
  329. ---
  330. --- @param mods string command modifiers that affect splitting a window.
  331. --- @param plugin_names string glob of plugin names, split on whitespace. For example, using
  332. --- `:checkhealth vim.* nvim` will healthcheck `vim.lsp`, `vim.treesitter`
  333. --- and `nvim` modules.
  334. function M._check(mods, plugin_names)
  335. local healthchecks = plugin_names == '' and get_healthcheck('*') or get_healthcheck(plugin_names)
  336. local emptybuf = vim.fn.bufnr('$') == 1 and vim.fn.getline(1) == '' and 1 == vim.fn.line('$')
  337. local bufnr = vim.api.nvim_create_buf(true, true)
  338. if
  339. vim.g.health
  340. and type(vim.g.health) == 'table'
  341. and vim.tbl_get(vim.g.health, 'style') == 'float'
  342. then
  343. local max_height = math.floor(vim.o.lines * 0.8)
  344. local max_width = 80
  345. local float_bufnr, float_winid = vim.lsp.util.open_floating_preview({}, '', {
  346. height = max_height,
  347. width = max_width,
  348. offset_x = math.floor((vim.o.columns - max_width) / 2),
  349. offset_y = math.floor((vim.o.lines - max_height) / 2) - 1,
  350. relative = 'editor',
  351. })
  352. vim.api.nvim_set_current_win(float_winid)
  353. vim.bo[float_bufnr].modifiable = true
  354. vim.wo[float_winid].list = false
  355. else
  356. -- When no command modifiers are used:
  357. -- - If the current buffer is empty, open healthcheck directly.
  358. -- - If not specified otherwise open healthcheck in a tab.
  359. local buf_cmd = #mods > 0 and (mods .. ' sbuffer') or emptybuf and 'buffer' or 'tab sbuffer'
  360. vim.cmd(buf_cmd .. ' ' .. bufnr)
  361. end
  362. if vim.fn.bufexists('health://') == 1 then
  363. vim.cmd.bwipe('health://')
  364. end
  365. vim.cmd.file('health://')
  366. vim.cmd.setfiletype('checkhealth')
  367. -- This should only happen when doing `:checkhealth vim`
  368. if next(healthchecks) == nil then
  369. vim.fn.setline(1, 'ERROR: No healthchecks found.')
  370. return
  371. end
  372. vim.cmd.redraw()
  373. vim.print('Running healthchecks...')
  374. for name, value in vim.spairs(healthchecks) do
  375. local func = value[1]
  376. local type = value[2]
  377. s_output = {}
  378. check_summary = { warn = 0, error = 0 }
  379. if func == '' then
  380. M.error('No healthcheck found for "' .. name .. '" plugin.')
  381. end
  382. if type == 'v' then
  383. vim.fn.call(func, {})
  384. else
  385. local f = assert(loadstring(func))
  386. local ok, output = pcall(f) ---@type boolean, string
  387. if not ok then
  388. M.error(
  389. string.format('Failed to run healthcheck for "%s" plugin. Exception:\n%s\n', name, output)
  390. )
  391. end
  392. end
  393. -- in the event the healthcheck doesn't return anything
  394. -- (the plugin author should avoid this possibility)
  395. if next(s_output) == nil then
  396. s_output = {}
  397. M.error('The healthcheck report for "' .. name .. '" plugin is empty.')
  398. end
  399. local report = get_summary()
  400. local replen = vim.fn.strwidth(report)
  401. local header = {
  402. string.rep('=', 78),
  403. -- Example: `foo.health: [ …] 1 ⚠️ 5 ❌`
  404. ('%s: %s%s'):format(name, (' '):rep(76 - name:len() - replen), report),
  405. '',
  406. }
  407. -- remove empty line after header from report_start
  408. if s_output[1] == '' then
  409. local tmp = {} ---@type string[]
  410. for i = 2, #s_output do
  411. tmp[#tmp + 1] = s_output[i]
  412. end
  413. s_output = {}
  414. for _, v in ipairs(tmp) do
  415. s_output[#s_output + 1] = v
  416. end
  417. end
  418. s_output[#s_output + 1] = ''
  419. s_output = vim.list_extend(header, s_output)
  420. vim.fn.append(vim.fn.line('$'), s_output)
  421. vim.cmd.redraw()
  422. end
  423. -- Clear the 'Running healthchecks...' message.
  424. vim.cmd.redraw()
  425. vim.print('')
  426. -- Quit with 'q' inside healthcheck buffers.
  427. vim._with({ buf = bufnr }, function()
  428. if vim.fn.maparg('q', 'n', false, false) == '' then
  429. vim.keymap.set('n', 'q', function()
  430. if not pcall(vim.cmd.close) then
  431. vim.cmd.bdelete()
  432. end
  433. end, { buffer = bufnr, silent = true, noremap = true, nowait = true })
  434. end
  435. end)
  436. -- Once we're done writing checks, set nomodifiable.
  437. vim.bo[bufnr].modifiable = false
  438. end
  439. return M