2024-05-22 07:07:45 -07:00
|
|
|
--- @brief
|
|
|
|
---<pre>help
|
2024-09-01 13:01:24 -07:00
|
|
|
--- vim.health is a minimal framework to help users troubleshoot configuration and
|
2024-05-22 07:07:45 -07:00
|
|
|
--- any other environment conditions that a plugin might care about. Nvim ships
|
|
|
|
--- with healthchecks for configuration, performance, python support, ruby
|
|
|
|
--- support, clipboard support, and more.
|
|
|
|
---
|
|
|
|
--- To run all healthchecks, use: >vim
|
|
|
|
---
|
|
|
|
--- :checkhealth
|
|
|
|
--- <
|
|
|
|
--- Plugin authors are encouraged to write new healthchecks. |health-dev|
|
|
|
|
---
|
|
|
|
--- Commands *health-commands*
|
|
|
|
---
|
|
|
|
--- *:che* *:checkhealth*
|
|
|
|
--- :che[ckhealth] Run all healthchecks.
|
|
|
|
--- *E5009*
|
|
|
|
--- Nvim depends on |$VIMRUNTIME|, 'runtimepath' and 'packpath' to
|
|
|
|
--- find the standard "runtime files" for syntax highlighting,
|
|
|
|
--- filetype-specific behavior, and standard plugins (including
|
|
|
|
--- :checkhealth). If the runtime files cannot be found then
|
|
|
|
--- those features will not work.
|
|
|
|
---
|
|
|
|
--- :che[ckhealth] {plugins}
|
|
|
|
--- Run healthcheck(s) for one or more plugins. E.g. to run only
|
|
|
|
--- the standard Nvim healthcheck: >vim
|
|
|
|
--- :checkhealth vim.health
|
|
|
|
--- <
|
|
|
|
--- To run the healthchecks for the "foo" and "bar" plugins
|
|
|
|
--- (assuming they are on 'runtimepath' and they have implemented
|
|
|
|
--- the Lua `require("foo.health").check()` interface): >vim
|
|
|
|
--- :checkhealth foo bar
|
|
|
|
--- <
|
|
|
|
--- To run healthchecks for Lua submodules, use dot notation or
|
|
|
|
--- "*" to refer to all submodules. For example Nvim provides
|
|
|
|
--- `vim.lsp` and `vim.treesitter`: >vim
|
|
|
|
--- :checkhealth vim.lsp vim.treesitter
|
|
|
|
--- :checkhealth vim*
|
|
|
|
--- <
|
|
|
|
---
|
2024-09-01 13:01:24 -07:00
|
|
|
--- Create a healthcheck *health-dev*
|
2024-05-22 07:07:45 -07:00
|
|
|
---
|
|
|
|
--- Healthchecks are functions that check the user environment, configuration, or
|
|
|
|
--- any other prerequisites that a plugin cares about. Nvim ships with
|
|
|
|
--- healthchecks in:
|
|
|
|
--- - $VIMRUNTIME/autoload/health/
|
|
|
|
--- - $VIMRUNTIME/lua/vim/lsp/health.lua
|
|
|
|
--- - $VIMRUNTIME/lua/vim/treesitter/health.lua
|
|
|
|
--- - and more...
|
|
|
|
---
|
|
|
|
--- To add a new healthcheck for your own plugin, simply create a "health.lua"
|
|
|
|
--- module on 'runtimepath' that returns a table with a "check()" function. Then
|
|
|
|
--- |:checkhealth| will automatically find and invoke the function.
|
|
|
|
---
|
|
|
|
--- For example if your plugin is named "foo", define your healthcheck module at
|
|
|
|
--- one of these locations (on 'runtimepath'):
|
|
|
|
--- - lua/foo/health/init.lua
|
|
|
|
--- - lua/foo/health.lua
|
|
|
|
---
|
|
|
|
--- If your plugin also provides a submodule named "bar" for which you want
|
|
|
|
--- a separate healthcheck, define the healthcheck at one of these locations:
|
|
|
|
--- - lua/foo/bar/health/init.lua
|
|
|
|
--- - lua/foo/bar/health.lua
|
|
|
|
---
|
|
|
|
--- All such health modules must return a Lua table containing a `check()`
|
|
|
|
--- function.
|
|
|
|
---
|
|
|
|
--- Copy this sample code into `lua/foo/health.lua`, replacing "foo" in the path
|
|
|
|
--- with your plugin name: >lua
|
|
|
|
---
|
|
|
|
--- local M = {}
|
|
|
|
---
|
|
|
|
--- M.check = function()
|
|
|
|
--- vim.health.start("foo report")
|
|
|
|
--- -- make sure setup function parameters are ok
|
|
|
|
--- if check_setup() then
|
|
|
|
--- vim.health.ok("Setup is correct")
|
|
|
|
--- else
|
|
|
|
--- vim.health.error("Setup is incorrect")
|
|
|
|
--- end
|
|
|
|
--- -- do some more checking
|
|
|
|
--- -- ...
|
|
|
|
--- end
|
|
|
|
---
|
|
|
|
--- return M
|
|
|
|
---</pre>
|
|
|
|
|
2022-05-31 11:10:18 -07:00
|
|
|
local M = {}
|
|
|
|
|
2023-12-13 06:04:24 -07:00
|
|
|
local s_output = {} ---@type string[]
|
2023-04-15 14:40:48 -07:00
|
|
|
|
2024-04-08 01:57:37 -07:00
|
|
|
-- From a path return a list [{name}, {func}, {type}] representing a healthcheck
|
2023-04-15 14:40:48 -07:00
|
|
|
local function filepath_to_healthcheck(path)
|
|
|
|
path = vim.fs.normalize(path)
|
2023-12-13 06:04:24 -07:00
|
|
|
local name --- @type string
|
|
|
|
local func --- @type string
|
|
|
|
local filetype --- @type string
|
2023-04-15 14:40:48 -07:00
|
|
|
if path:find('vim$') then
|
|
|
|
name = vim.fs.basename(path):gsub('%.vim$', '')
|
|
|
|
func = 'health#' .. name .. '#check'
|
|
|
|
filetype = 'v'
|
|
|
|
else
|
|
|
|
local subpath = path:gsub('.*lua/', '')
|
|
|
|
if vim.fs.basename(subpath) == 'health.lua' then
|
|
|
|
-- */health.lua
|
2024-05-25 11:35:37 -07:00
|
|
|
name = vim.fs.dirname(subpath)
|
2023-04-15 14:40:48 -07:00
|
|
|
else
|
|
|
|
-- */health/init.lua
|
2024-05-25 11:35:37 -07:00
|
|
|
name = vim.fs.dirname(vim.fs.dirname(subpath))
|
2023-04-15 14:40:48 -07:00
|
|
|
end
|
|
|
|
name = name:gsub('/', '.')
|
|
|
|
|
|
|
|
func = 'require("' .. name .. '.health").check()'
|
|
|
|
filetype = 'l'
|
|
|
|
end
|
|
|
|
return { name, func, filetype }
|
2022-05-31 11:10:18 -07:00
|
|
|
end
|
|
|
|
|
2023-12-13 06:04:24 -07:00
|
|
|
--- @param plugin_names string
|
|
|
|
--- @return table<any,string[]> { {name, func, type}, ... } representing healthchecks
|
2023-04-15 14:40:48 -07:00
|
|
|
local function get_healthcheck_list(plugin_names)
|
2023-12-13 06:04:24 -07:00
|
|
|
local healthchecks = {} --- @type table<any,string[]>
|
|
|
|
local plugin_names_list = vim.split(plugin_names, ' ')
|
|
|
|
for _, p in pairs(plugin_names_list) do
|
2023-04-15 14:40:48 -07:00
|
|
|
-- support vim/lsp/health{/init/}.lua as :checkhealth vim.lsp
|
|
|
|
|
|
|
|
p = p:gsub('%.', '/')
|
|
|
|
p = p:gsub('*', '**')
|
|
|
|
|
|
|
|
local paths = vim.api.nvim_get_runtime_file('autoload/health/' .. p .. '.vim', true)
|
|
|
|
vim.list_extend(
|
|
|
|
paths,
|
|
|
|
vim.api.nvim_get_runtime_file('lua/**/' .. p .. '/health/init.lua', true)
|
|
|
|
)
|
|
|
|
vim.list_extend(paths, vim.api.nvim_get_runtime_file('lua/**/' .. p .. '/health.lua', true))
|
|
|
|
|
|
|
|
if vim.tbl_count(paths) == 0 then
|
|
|
|
healthchecks[#healthchecks + 1] = { p, '', '' } -- healthcheck not found
|
|
|
|
else
|
2023-12-13 06:04:24 -07:00
|
|
|
local unique_paths = {} --- @type table<string, boolean>
|
2023-04-15 14:40:48 -07:00
|
|
|
for _, v in pairs(paths) do
|
|
|
|
unique_paths[v] = true
|
|
|
|
end
|
|
|
|
paths = {}
|
|
|
|
for k, _ in pairs(unique_paths) do
|
|
|
|
paths[#paths + 1] = k
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, v in ipairs(paths) do
|
|
|
|
healthchecks[#healthchecks + 1] = filepath_to_healthcheck(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return healthchecks
|
2022-05-31 11:10:18 -07:00
|
|
|
end
|
|
|
|
|
2023-12-13 06:04:24 -07:00
|
|
|
--- @param plugin_names string
|
|
|
|
--- @return table<string, string[]> {name: [func, type], ..} representing healthchecks
|
2023-04-15 14:40:48 -07:00
|
|
|
local function get_healthcheck(plugin_names)
|
|
|
|
local health_list = get_healthcheck_list(plugin_names)
|
2023-12-13 06:04:24 -07:00
|
|
|
local healthchecks = {} --- @type table<string, string[]>
|
2023-04-15 14:40:48 -07:00
|
|
|
for _, c in pairs(health_list) do
|
|
|
|
if c[1] ~= 'vim' then
|
|
|
|
healthchecks[c[1]] = { c[2], c[3] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return healthchecks
|
2022-05-31 11:10:18 -07:00
|
|
|
end
|
|
|
|
|
2023-12-13 06:04:24 -07:00
|
|
|
--- Indents lines *except* line 1 of a string if it contains newlines.
|
|
|
|
---
|
|
|
|
--- @param s string
|
|
|
|
--- @param columns integer
|
|
|
|
--- @return string
|
2023-04-15 14:40:48 -07:00
|
|
|
local function indent_after_line1(s, columns)
|
|
|
|
local lines = vim.split(s, '\n')
|
|
|
|
local indent = string.rep(' ', columns)
|
|
|
|
for i = 2, #lines do
|
|
|
|
lines[i] = indent .. lines[i]
|
|
|
|
end
|
|
|
|
return table.concat(lines, '\n')
|
|
|
|
end
|
|
|
|
|
2023-12-13 06:04:24 -07:00
|
|
|
--- Changes ':h clipboard' to ':help |clipboard|'.
|
|
|
|
---
|
|
|
|
--- @param s string
|
|
|
|
--- @return string
|
2023-04-15 14:40:48 -07:00
|
|
|
local function help_to_link(s)
|
|
|
|
return vim.fn.substitute(s, [[\v:h%[elp] ([^|][^"\r\n ]+)]], [[:help |\1|]], [[g]])
|
|
|
|
end
|
|
|
|
|
2023-12-13 06:04:24 -07:00
|
|
|
--- Format a message for a specific report item.
|
|
|
|
---
|
|
|
|
--- @param status string
|
|
|
|
--- @param msg string
|
|
|
|
--- @param ... string|string[] Optional advice
|
|
|
|
--- @return string
|
2023-04-15 14:40:48 -07:00
|
|
|
local function format_report_message(status, msg, ...)
|
|
|
|
local output = '- ' .. status
|
|
|
|
if status ~= '' then
|
|
|
|
output = output .. ' '
|
|
|
|
end
|
|
|
|
|
|
|
|
output = output .. indent_after_line1(msg, 2)
|
|
|
|
|
|
|
|
local varargs = ...
|
|
|
|
|
|
|
|
-- Optional parameters
|
|
|
|
if varargs then
|
|
|
|
if type(varargs) == 'string' then
|
|
|
|
varargs = { varargs }
|
|
|
|
end
|
|
|
|
|
|
|
|
output = output .. '\n - ADVICE:'
|
|
|
|
|
|
|
|
-- Report each suggestion
|
|
|
|
for _, v in ipairs(varargs) do
|
|
|
|
if v then
|
|
|
|
output = output .. '\n - ' .. indent_after_line1(v, 6)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return help_to_link(output)
|
|
|
|
end
|
|
|
|
|
2023-12-13 06:04:24 -07:00
|
|
|
--- @param output string
|
2023-04-15 14:40:48 -07:00
|
|
|
local function collect_output(output)
|
|
|
|
vim.list_extend(s_output, vim.split(output, '\n'))
|
|
|
|
end
|
|
|
|
|
2024-05-22 07:07:45 -07:00
|
|
|
--- Starts a new report. Most plugins should call this only once, but if
|
|
|
|
--- you want different sections to appear in your report, call this once
|
|
|
|
--- per section.
|
2023-12-13 06:04:24 -07:00
|
|
|
---
|
|
|
|
--- @param name string
|
2023-04-15 14:40:48 -07:00
|
|
|
function M.start(name)
|
|
|
|
local input = string.format('\n%s ~', name)
|
|
|
|
collect_output(input)
|
|
|
|
end
|
|
|
|
|
2024-05-22 07:07:45 -07:00
|
|
|
--- Reports an informational message.
|
2023-12-13 06:04:24 -07:00
|
|
|
---
|
|
|
|
--- @param msg string
|
2023-04-15 14:40:48 -07:00
|
|
|
function M.info(msg)
|
|
|
|
local input = format_report_message('', msg)
|
|
|
|
collect_output(input)
|
|
|
|
end
|
|
|
|
|
2024-05-22 07:07:45 -07:00
|
|
|
--- Reports a "success" message.
|
2023-12-13 06:04:24 -07:00
|
|
|
---
|
|
|
|
--- @param msg string
|
2023-04-15 14:40:48 -07:00
|
|
|
function M.ok(msg)
|
|
|
|
local input = format_report_message('OK', msg)
|
|
|
|
collect_output(input)
|
|
|
|
end
|
|
|
|
|
2024-05-22 07:07:45 -07:00
|
|
|
--- Reports a warning.
|
2023-12-13 06:04:24 -07:00
|
|
|
---
|
|
|
|
--- @param msg string
|
|
|
|
--- @param ... string|string[] Optional advice
|
2023-04-15 14:40:48 -07:00
|
|
|
function M.warn(msg, ...)
|
|
|
|
local input = format_report_message('WARNING', msg, ...)
|
|
|
|
collect_output(input)
|
2022-05-31 11:10:18 -07:00
|
|
|
end
|
|
|
|
|
2024-05-22 07:07:45 -07:00
|
|
|
--- Reports an error.
|
2023-12-13 06:04:24 -07:00
|
|
|
---
|
|
|
|
--- @param msg string
|
|
|
|
--- @param ... string|string[] Optional advice
|
2023-04-15 14:40:48 -07:00
|
|
|
function M.error(msg, ...)
|
|
|
|
local input = format_report_message('ERROR', msg, ...)
|
|
|
|
collect_output(input)
|
|
|
|
end
|
|
|
|
|
2022-05-31 11:10:18 -07:00
|
|
|
local path2name = function(path)
|
|
|
|
if path:match('%.lua$') then
|
|
|
|
-- Lua: transform "../lua/vim/lsp/health.lua" into "vim.lsp"
|
2023-01-16 02:55:24 -07:00
|
|
|
|
|
|
|
-- Get full path, make sure all slashes are '/'
|
|
|
|
path = vim.fs.normalize(path)
|
|
|
|
|
|
|
|
-- Remove everything up to the last /lua/ folder
|
|
|
|
path = path:gsub('^.*/lua/', '')
|
|
|
|
|
2024-09-10 21:55:37 -07:00
|
|
|
-- Remove the filename (health.lua) or (health/init.lua)
|
|
|
|
path = vim.fs.dirname(path:gsub('/init%.lua$', ''))
|
2023-01-16 02:55:24 -07:00
|
|
|
|
|
|
|
-- Change slashes to dots
|
|
|
|
path = path:gsub('/', '.')
|
|
|
|
|
|
|
|
return path
|
2022-05-31 11:10:18 -07:00
|
|
|
else
|
|
|
|
-- Vim: transform "../autoload/health/provider.vim" into "provider"
|
|
|
|
return vim.fn.fnamemodify(path, ':t:r')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local PATTERNS = { '/autoload/health/*.vim', '/lua/**/**/health.lua', '/lua/**/**/health/init.lua' }
|
2023-12-13 06:04:24 -07:00
|
|
|
--- :checkhealth completion function used by cmdexpand.c get_healthcheck_names()
|
2022-05-31 11:10:18 -07:00
|
|
|
M._complete = function()
|
2024-05-25 11:35:37 -07:00
|
|
|
local unique = vim ---@type table<string,boolean>
|
|
|
|
---@param pattern string
|
2024-04-21 15:58:48 -07:00
|
|
|
.iter(vim.tbl_map(function(pattern)
|
|
|
|
return vim.tbl_map(path2name, vim.api.nvim_get_runtime_file(pattern, true))
|
|
|
|
end, PATTERNS))
|
|
|
|
:flatten()
|
2024-05-25 11:35:37 -07:00
|
|
|
---@param t table<string,boolean>
|
2024-04-21 15:58:48 -07:00
|
|
|
:fold({}, function(t, name)
|
|
|
|
t[name] = true -- Remove duplicates
|
|
|
|
return t
|
|
|
|
end)
|
2022-06-01 07:10:10 -07:00
|
|
|
-- vim.health is this file, which is not a healthcheck
|
|
|
|
unique['vim'] = nil
|
2024-04-21 17:43:24 -07:00
|
|
|
local rv = vim.tbl_keys(unique)
|
|
|
|
table.sort(rv)
|
|
|
|
return rv
|
2022-05-31 11:10:18 -07:00
|
|
|
end
|
|
|
|
|
2023-12-24 19:21:13 -07:00
|
|
|
--- Runs the specified healthchecks.
|
|
|
|
--- Runs all discovered healthchecks if plugin_names is empty.
|
|
|
|
---
|
|
|
|
--- @param mods string command modifiers that affect splitting a window.
|
2023-12-13 06:04:24 -07:00
|
|
|
--- @param plugin_names string glob of plugin names, split on whitespace. For example, using
|
|
|
|
--- `:checkhealth vim.* nvim` will healthcheck `vim.lsp`, `vim.treesitter`
|
|
|
|
--- and `nvim` modules.
|
2023-12-24 19:21:13 -07:00
|
|
|
function M._check(mods, plugin_names)
|
2023-04-15 14:40:48 -07:00
|
|
|
local healthchecks = plugin_names == '' and get_healthcheck('*') or get_healthcheck(plugin_names)
|
|
|
|
|
|
|
|
local emptybuf = vim.fn.bufnr('$') == 1 and vim.fn.getline(1) == '' and 1 == vim.fn.line('$')
|
2023-12-24 19:21:13 -07:00
|
|
|
|
|
|
|
-- When no command modifiers are used:
|
|
|
|
-- - If the current buffer is empty, open healthcheck directly.
|
|
|
|
-- - If not specified otherwise open healthcheck in a tab.
|
|
|
|
local buf_cmd = #mods > 0 and (mods .. ' sbuffer') or emptybuf and 'buffer' or 'tab sbuffer'
|
2023-12-24 17:30:56 -07:00
|
|
|
|
2023-04-15 14:40:48 -07:00
|
|
|
local bufnr = vim.api.nvim_create_buf(true, true)
|
2023-12-24 19:21:13 -07:00
|
|
|
vim.cmd(buf_cmd .. ' ' .. bufnr)
|
2023-04-15 14:40:48 -07:00
|
|
|
|
|
|
|
if vim.fn.bufexists('health://') == 1 then
|
|
|
|
vim.cmd.bwipe('health://')
|
|
|
|
end
|
|
|
|
vim.cmd.file('health://')
|
|
|
|
vim.cmd.setfiletype('checkhealth')
|
|
|
|
|
2023-12-13 06:04:24 -07:00
|
|
|
-- This should only happen when doing `:checkhealth vim`
|
|
|
|
if next(healthchecks) == nil then
|
2023-04-15 14:40:48 -07:00
|
|
|
vim.fn.setline(1, 'ERROR: No healthchecks found.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
vim.cmd.redraw()
|
|
|
|
vim.print('Running healthchecks...')
|
|
|
|
|
|
|
|
for name, value in vim.spairs(healthchecks) do
|
|
|
|
local func = value[1]
|
|
|
|
local type = value[2]
|
|
|
|
s_output = {}
|
|
|
|
|
|
|
|
if func == '' then
|
|
|
|
s_output = {}
|
|
|
|
M.error('No healthcheck found for "' .. name .. '" plugin.')
|
|
|
|
end
|
|
|
|
if type == 'v' then
|
2023-04-16 03:26:13 -07:00
|
|
|
vim.fn.call(func, {})
|
2023-04-15 14:40:48 -07:00
|
|
|
else
|
|
|
|
local f = assert(loadstring(func))
|
2024-05-25 11:35:37 -07:00
|
|
|
local ok, output = pcall(f) ---@type boolean, string
|
2023-04-15 14:40:48 -07:00
|
|
|
if not ok then
|
|
|
|
M.error(
|
|
|
|
string.format('Failed to run healthcheck for "%s" plugin. Exception:\n%s\n', name, output)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- in the event the healthcheck doesn't return anything
|
|
|
|
-- (the plugin author should avoid this possibility)
|
|
|
|
if next(s_output) == nil then
|
|
|
|
s_output = {}
|
|
|
|
M.error('The healthcheck report for "' .. name .. '" plugin is empty.')
|
|
|
|
end
|
2024-09-26 07:45:03 -07:00
|
|
|
|
|
|
|
local header = {
|
|
|
|
string.rep('=', 78),
|
|
|
|
-- Example: `foo.health: [ …] require("foo.health").check()`
|
|
|
|
('%s: %s%s'):format(name, (' '):rep(76 - name:len() - func:len()), func),
|
|
|
|
'',
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:40:48 -07:00
|
|
|
-- remove empty line after header from report_start
|
|
|
|
if s_output[1] == '' then
|
2023-12-13 06:04:24 -07:00
|
|
|
local tmp = {} ---@type string[]
|
2023-04-15 14:40:48 -07:00
|
|
|
for i = 2, #s_output do
|
|
|
|
tmp[#tmp + 1] = s_output[i]
|
|
|
|
end
|
|
|
|
s_output = {}
|
|
|
|
for _, v in ipairs(tmp) do
|
|
|
|
s_output[#s_output + 1] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
s_output[#s_output + 1] = ''
|
|
|
|
s_output = vim.list_extend(header, s_output)
|
2024-05-25 11:35:37 -07:00
|
|
|
vim.fn.append(vim.fn.line('$'), s_output)
|
2023-04-15 14:40:48 -07:00
|
|
|
vim.cmd.redraw()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Clear the 'Running healthchecks...' message.
|
|
|
|
vim.cmd.redraw()
|
|
|
|
vim.print('')
|
|
|
|
end
|
|
|
|
|
2022-05-31 11:10:18 -07:00
|
|
|
return M
|