diff options
author | Gregory Anders <greg@gpanders.com> | 2023-04-10 16:48:58 -0600 |
---|---|---|
committer | dundargoc <33953936+dundargoc@users.noreply.github.com> | 2024-01-01 20:12:37 +0100 |
commit | 164f1ea06d17e935f41e178e46bb05bbb676af20 (patch) | |
tree | 6e04a060877aef5f979326ae025329828dcb4ac9 /runtime/lua/vim/health.lua | |
parent | b49d4e18a67d16548fa72013237a87564656170e (diff) | |
download | rneovim-164f1ea06d17e935f41e178e46bb05bbb676af20.tar.gz rneovim-164f1ea06d17e935f41e178e46bb05bbb676af20.tar.bz2 rneovim-164f1ea06d17e935f41e178e46bb05bbb676af20.zip |
refactor(health): refactor provider healthchecks
* Prefer pure Lua functions over vim.fn
* Split up provider healthchecks into separate modules to help manage
complexity
Diffstat (limited to 'runtime/lua/vim/health.lua')
-rw-r--r-- | runtime/lua/vim/health.lua | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/runtime/lua/vim/health.lua b/runtime/lua/vim/health.lua index 55a451bf56..d9265f6bd6 100644 --- a/runtime/lua/vim/health.lua +++ b/runtime/lua/vim/health.lua @@ -267,6 +267,114 @@ function M.report_error(msg, ...) M.error(msg, ...) end +function M.provider_disabled(provider) + local loaded_var = 'loaded_' .. provider .. '_provider' + local v = vim.g[loaded_var] + if v == 0 then + M.info('Disabled (' .. loaded_var .. '=' .. v .. ').') + return true + end + return false +end + +-- Handler for s:system() function. +local function system_handler(self, _, data, event) + if event == 'stderr' then + if self.add_stderr_to_output then + self.output = self.output .. table.concat(data, '') + else + self.stderr = self.stderr .. table.concat(data, '') + end + elseif event == 'stdout' then + self.output = self.output .. table.concat(data, '') + end +end + +-- Attempts to construct a shell command from an args list. +-- Only for display, to help users debug a failed command. +local function shellify(cmd) + if type(cmd) ~= 'table' then + return cmd + end + local escaped = {} + for i, v in ipairs(cmd) do + if v:match('[^A-Za-z_/.-]') then + escaped[i] = vim.fn.shellescape(v) + else + escaped[i] = v + end + end + return table.concat(escaped, ' ') +end + +function M.cmd_ok(cmd) + local out = vim.fn.system(cmd) + return vim.v.shell_error == 0, out +end + +--- Run a system command and timeout after 30 seconds. +--- +--- @param cmd table List of command arguments to execute +--- @param args ?table Optional arguments: +--- - stdin (string): Data to write to the job's stdin +--- - stderr (boolean): Append stderr to stdout +--- - ignore_error (boolean): If true, ignore error output +--- - timeout (number): Number of seconds to wait before timing out (default 30) +function M.system(cmd, args) + args = args or {} + local stdin = args.stdin or '' + local stderr = vim.F.if_nil(args.stderr, false) + local ignore_error = vim.F.if_nil(args.ignore_error, false) + + local shell_error_code = 0 + local opts = { + add_stderr_to_output = stderr, + output = '', + stderr = '', + on_stdout = system_handler, + on_stderr = system_handler, + on_exit = function(_, data) + shell_error_code = data + end, + } + local jobid = vim.fn.jobstart(cmd, opts) + + if jobid < 1 then + local message = + string.format('Command error (job=%d): %s (in %s)', jobid, shellify(cmd), vim.loop.cwd()) + error(message) + return opts.output, 1 + end + + if stdin:find('^%s$') then + vim.fn.chansend(jobid, stdin) + end + + local res = vim.fn.jobwait({ jobid }, vim.F.if_nil(args.timeout, 30) * 1000) + if res[1] == -1 then + error('Command timed out: ' .. shellify(cmd)) + vim.fn.jobstop(jobid) + elseif shell_error_code ~= 0 and not ignore_error then + local emsg = string.format( + 'Command error (job=%d, exit code %d): %s (in %s)', + jobid, + shell_error_code, + shellify(cmd), + vim.loop.cwd() + ) + if opts.output:find('%S') then + emsg = string.format('%s\noutput: %s', emsg, opts.output) + end + if opts.stderr:find('%S') then + emsg = string.format('%s\nstderr: %s', emsg, opts.stderr) + end + error(emsg) + end + + -- return opts.output + return vim.trim(vim.fn.system(cmd)), shell_error_code +end + local path2name = function(path) if path:match('%.lua$') then -- Lua: transform "../lua/vim/lsp/health.lua" into "vim.lsp" @@ -389,4 +497,11 @@ function M._check(mods, plugin_names) vim.print('') end +local fn_bool = function(key) + return function(...) + return vim.fn[key](...) == 1 + end +end +M.executable = fn_bool('executable') + return M |