From 3213bc36c5d0cddf8f81498a1ac590bc6d81d743 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 30 Oct 2022 04:47:23 +0100 Subject: refactor(checkhealth): convert "nvim" check to Lua --- runtime/lua/nvim/health.lua | 321 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 321 insertions(+) create mode 100644 runtime/lua/nvim/health.lua (limited to 'runtime/lua/nvim') diff --git a/runtime/lua/nvim/health.lua b/runtime/lua/nvim/health.lua new file mode 100644 index 0000000000..a8464b491a --- /dev/null +++ b/runtime/lua/nvim/health.lua @@ -0,0 +1,321 @@ +local M = {} +local health = require('vim.health') + +local suggest_faq = 'https://github.com/neovim/neovim/wiki/FAQ' + +local function check_config() + health.report_start('Configuration') + local ok = true + local empty = function(o) return 0 ~= vim.fn.empty(o) end + local filereadable = function(o) return 0 ~= vim.fn.filereadable(o) end + local filewritable = function(o) return 0 ~= vim.fn.filewritable(o) end + + local vimrc = empty(vim.env.MYVIMRC) and vim.fn.stdpath('config')..'/init.vim' or vim.env.MYVIMRC + if not filereadable(vimrc) then + ok = false + local has_vim = filereadable(vim.fn.expand('~/.vimrc')) + health.report_warn((-1 == vim.fn.getfsize(vimrc) and 'Missing' or 'Unreadable')..' user config file: '..vimrc, + { has_vim and ':help nvim-from-vim' or ':help init.vim' }) + end + + -- If $VIM is empty we don't care. Else make sure it is valid. + if not empty(vim.env.VIM) and not filereadable(vim.env.VIM..'/runtime/doc/nvim.txt') then + ok = false + health.report_error('$VIM is invalid: '..vim.env.VIM) + end + + if 1 == vim.fn.exists('$NVIM_TUI_ENABLE_CURSOR_SHAPE') then + ok = false + health.report_warn('$NVIM_TUI_ENABLE_CURSOR_SHAPE is ignored in Nvim 0.2+', + { "Use the 'guicursor' option to configure cursor shape. :help 'guicursor'", + 'https://github.com/neovim/neovim/wiki/Following-HEAD#20170402' }) + end + + if vim.v.ctype == 'C' then + ok = false + health.report_error('Locale does not support UTF-8. Unicode characters may not display correctly.' + ..("\n$LANG=%s $LC_ALL=%s $LC_CTYPE=%s"):format(vim.env.LANG, vim.env.LC_ALL, vim.env.LC_CTYPE), + { 'If using tmux, try the -u option.', + 'Ensure that your terminal/shell/tmux/etc inherits the environment, or set $LANG explicitly.' , + 'Configure your system locale.' }) + end + + if vim.o.paste == 1 then + ok = false + health.report_error("'paste' is enabled. This option is only for pasting text.\nIt should not be set in your config.", + { 'Remove `set paste` from your init.vim, if applicable.', + 'Check `:verbose set paste?` to see if a plugin or script set the option.', }) + end + + local writeable = true + local shadaopt = vim.fn.split(vim.o.shada, ',') + local shadafile = (empty(vim.o.shada) + and vim.o.shada + or vim.fn.substitute(vim.fn.matchstr(shadaopt[#shadaopt], '^n.\\+'), '^n', '', '')) + shadafile = (empty(vim.o.shadafile) + and (empty(shadafile) and vim.fn.stdpath('state')..'/shada/main.shada' or vim.fn.expand(shadafile)) + or (vim.o.shadafile == 'NONE' and '' or vim.o.shadafile)) + if not empty(shadafile) and empty(vim.fn.glob(shadafile)) then + -- Since this may be the first time Nvim has been run, try to create a shada file. + if not pcall(vim.cmd.wshada) then + writeable = false + end + end + if not writeable or (not empty(shadafile) and (not filereadable(shadafile) or not filewritable(shadafile))) then + ok = false + health.report_error('shada file is not ' + ..((not writeable or filereadable(shadafile)) and 'writeable' or 'readable')..':\n'..shadafile) + end + + if ok then + health.report_ok('no issues found') + end +end + +local function check_performance() + vim.api.nvim_exec([=[ + func! s:check_performance() abort + let s:suggest_faq = ']=]..suggest_faq..[=[' + + call health#report_start('Performance') + + " check buildtype + let s:buildtype = matchstr(execute('version'), '\v\cbuild type:?\s*[^\n\r\t ]+') + if empty(s:buildtype) + call health#report_error('failed to get build type from :version') + elseif s:buildtype =~# '\v(MinSizeRel|Release|RelWithDebInfo)' + call health#report_ok(s:buildtype) + else + call health#report_info(s:buildtype) + call health#report_warn( + \ 'Non-optimized '.(has('debug')?'(DEBUG) ':'').'build. Nvim will be slower.', + \ ['Install a different Nvim package, or rebuild with `CMAKE_BUILD_TYPE=RelWithDebInfo`.', + \ s:suggest_faq]) + endif + + " check for slow shell invocation + let s:slow_cmd_time = 1.5 + let s:start_time = reltime() + call system('echo') + let s:elapsed_time = reltimefloat(reltime(s:start_time)) + if s:elapsed_time > s:slow_cmd_time + call health#report_warn( + \ 'Slow shell invocation (took '.printf('%.2f', s:elapsed_time).' seconds).') + endif + endf + + call s:check_performance() + ]=], + false) +end + +-- Load the remote plugin manifest file and check for unregistered plugins +local function check_rplugin_manifest() + vim.api.nvim_exec([=[ + func! s:check_rplugin_manifest() abort + call health#report_start('Remote Plugins') + let existing_rplugins = {} + + for item in remote#host#PluginsForHost('python') + let existing_rplugins[item.path] = 'python' + endfor + + for item in remote#host#PluginsForHost('python3') + let existing_rplugins[item.path] = 'python3' + endfor + + let require_update = 0 + + for path in map(split(&runtimepath, ','), 'resolve(v:val)') + let python_glob = glob(path.'/rplugin/python*', 1, 1) + if empty(python_glob) + continue + endif + + let python_dir = python_glob[0] + let python_version = fnamemodify(python_dir, ':t') + + for script in glob(python_dir.'/*.py', 1, 1) + \ + glob(python_dir.'/*/__init__.py', 1, 1) + let contents = join(readfile(script)) + if contents =~# '\<\%(from\|import\)\s\+neovim\>' + if script =~# '[\/]__init__\.py$' + let script = tr(fnamemodify(script, ':h'), '\', '/') + endif + + if !has_key(existing_rplugins, script) + let msg = printf('"%s" is not registered.', fnamemodify(path, ':t')) + if python_version ==# 'pythonx' + if !has('python3') + let msg .= ' (python3 not available)' + endif + elseif !has(python_version) + let msg .= printf(' (%s not available)', python_version) + else + let require_update = 1 + endif + + call health#report_warn(msg) + endif + + break + endif + endfor + endfor + + if require_update + call health#report_warn('Out of date', ['Run `:UpdateRemotePlugins`']) + else + call health#report_ok('Up to date') + endif + endf + + call s:check_rplugin_manifest() + ]=], + false) +end + + +local function check_tmux() + vim.api.nvim_exec([=[ + let s:suggest_faq = ']=]..suggest_faq..[=[' + + func! s:get_tmux_option(option) abort + let cmd = 'tmux show-option -qvg '.a:option " try global scope + let out = system(split(cmd)) + let val = substitute(out, '\v(\s|\r|\n)', '', 'g') + if v:shell_error + call health#report_error('command failed: '.cmd."\n".out) + return 'error' + elseif empty(val) + let cmd = 'tmux show-option -qvgs '.a:option " try session scope + let out = system(split(cmd)) + let val = substitute(out, '\v(\s|\r|\n)', '', 'g') + if v:shell_error + call health#report_error('command failed: '.cmd."\n".out) + return 'error' + endif + endif + return val + endf + + func! s:check_tmux() abort + if empty($TMUX) || !executable('tmux') + return + endif + call health#report_start('tmux') + + " check escape-time + let suggestions = ["set escape-time in ~/.tmux.conf:\nset-option -sg escape-time 10", + \ s:suggest_faq] + let tmux_esc_time = s:get_tmux_option('escape-time') + if tmux_esc_time !=# 'error' + if empty(tmux_esc_time) + call health#report_error('`escape-time` is not set', suggestions) + elseif tmux_esc_time > 300 + call health#report_error( + \ '`escape-time` ('.tmux_esc_time.') is higher than 300ms', suggestions) + else + call health#report_ok('escape-time: '.tmux_esc_time) + endif + endif + + " check focus-events + let suggestions = ["(tmux 1.9+ only) Set `focus-events` in ~/.tmux.conf:\nset-option -g focus-events on"] + let tmux_focus_events = s:get_tmux_option('focus-events') + if tmux_focus_events !=# 'error' + if empty(tmux_focus_events) || tmux_focus_events !=# 'on' + call health#report_warn( + \ "`focus-events` is not enabled. |'autoread'| may not work.", suggestions) + else + call health#report_ok('focus-events: '.tmux_focus_events) + endif + endif + + " check default-terminal and $TERM + call health#report_info('$TERM: '.$TERM) + let cmd = 'tmux show-option -qvg default-terminal' + let out = system(split(cmd)) + let tmux_default_term = substitute(out, '\v(\s|\r|\n)', '', 'g') + if empty(tmux_default_term) + let cmd = 'tmux show-option -qvgs default-terminal' + let out = system(split(cmd)) + let tmux_default_term = substitute(out, '\v(\s|\r|\n)', '', 'g') + endif + + if v:shell_error + call health#report_error('command failed: '.cmd."\n".out) + elseif tmux_default_term !=# $TERM + call health#report_info('default-terminal: '.tmux_default_term) + call health#report_error( + \ '$TERM differs from the tmux `default-terminal` setting. Colors might look wrong.', + \ ['$TERM may have been set by some rc (.bashrc, .zshrc, ...).']) + elseif $TERM !~# '\v(tmux-256color|screen-256color)' + call health#report_error( + \ '$TERM should be "screen-256color" or "tmux-256color" in tmux. Colors might look wrong.', + \ ["Set default-terminal in ~/.tmux.conf:\nset-option -g default-terminal \"screen-256color\"", + \ s:suggest_faq]) + endif + + " check for RGB capabilities + let info = system(['tmux', 'server-info']) + let has_tc = stridx(info, " Tc: (flag) true") != -1 + let has_rgb = stridx(info, " RGB: (flag) true") != -1 + if !has_tc && !has_rgb + call health#report_warn( + \ "Neither Tc nor RGB capability set. True colors are disabled. |'termguicolors'| won't work properly.", + \ ["Put this in your ~/.tmux.conf and replace XXX by your $TERM outside of tmux:\nset-option -sa terminal-overrides ',XXX:RGB'", + \ "For older tmux versions use this instead:\nset-option -ga terminal-overrides ',XXX:Tc'"]) + endif + endf + + call s:check_tmux() + ]=], + false) +end + +local function check_terminal() + vim.api.nvim_exec([=[ + func! s:check_terminal() abort + if !executable('infocmp') + return + endif + call health#report_start('terminal') + let cmd = 'infocmp -L' + let out = system(split(cmd)) + let kbs_entry = matchstr(out, 'key_backspace=[^,[:space:]]*') + let kdch1_entry = matchstr(out, 'key_dc=[^,[:space:]]*') + + if v:shell_error + \ && (!has('win32') + \ || empty(matchstr(out, + \ 'infocmp: couldn''t open terminfo file .\+' + \ ..'\%(conemu\|vtpcon\|win32con\)'))) + call health#report_error('command failed: '.cmd."\n".out) + else + call health#report_info('key_backspace (kbs) terminfo entry: ' + \ .(empty(kbs_entry) ? '? (not found)' : kbs_entry)) + call health#report_info('key_dc (kdch1) terminfo entry: ' + \ .(empty(kbs_entry) ? '? (not found)' : kdch1_entry)) + endif + for env_var in ['XTERM_VERSION', 'VTE_VERSION', 'TERM_PROGRAM', 'COLORTERM', 'SSH_TTY'] + if exists('$'.env_var) + call health#report_info(printf("$%s='%s'", env_var, eval('$'.env_var))) + endif + endfor + endf + + call s:check_terminal() + ]=], + false) +end + +function M.check() + check_config() + check_performance() + check_rplugin_manifest() + check_terminal() + check_tmux() +end + +return M -- cgit From cc7c378bf319d62491ca121ab598397428e4ced4 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 30 Oct 2022 06:41:28 +0100 Subject: feat(checkhealth): check runtime ($VIMRUNTIME) Move man/health.lua into the "runtime" check. fix #20696 --- runtime/lua/nvim/health.lua | 148 ++++++++++++++++++++++++++++++++------------ 1 file changed, 108 insertions(+), 40 deletions(-) (limited to 'runtime/lua/nvim') diff --git a/runtime/lua/nvim/health.lua b/runtime/lua/nvim/health.lua index a8464b491a..413eae0bf9 100644 --- a/runtime/lua/nvim/health.lua +++ b/runtime/lua/nvim/health.lua @@ -3,68 +3,134 @@ local health = require('vim.health') local suggest_faq = 'https://github.com/neovim/neovim/wiki/FAQ' +local function check_runtime() + health.report_start('Runtime') + -- Files from an old installation. + local bad_files = { + ['plugin/man.vim'] = false, + ['scripts.vim'] = false, + ['autoload/man.vim'] = false, + } + local bad_files_msg = '' + for k, _ in pairs(bad_files) do + local path = ('%s/%s'):format(vim.env.VIMRUNTIME, k) + if vim.loop.fs_stat(path) then + bad_files[k] = true + bad_files_msg = ('%s%s\n'):format(bad_files_msg, path) + end + end + + local ok = (bad_files_msg == '') + local info = ok and health.report_ok or health.report_info + info(string.format('$VIMRUNTIME: %s', vim.env.VIMRUNTIME)) + if not ok then + health.report_error( + string.format( + '$VIMRUNTIME has files from an old installation (this can cause weird behavior):\n%s', + bad_files_msg + ), + { 'Delete $VIMRUNTIME (or uninstall Nvim), then reinstall Nvim.' } + ) + end +end + local function check_config() health.report_start('Configuration') local ok = true - local empty = function(o) return 0 ~= vim.fn.empty(o) end - local filereadable = function(o) return 0 ~= vim.fn.filereadable(o) end - local filewritable = function(o) return 0 ~= vim.fn.filewritable(o) end + local empty = function(o) + return 0 ~= vim.fn.empty(o) + end + local filereadable = function(o) + return 0 ~= vim.fn.filereadable(o) + end + local filewritable = function(o) + return 0 ~= vim.fn.filewritable(o) + end - local vimrc = empty(vim.env.MYVIMRC) and vim.fn.stdpath('config')..'/init.vim' or vim.env.MYVIMRC + local vimrc = ( + empty(vim.env.MYVIMRC) and vim.fn.stdpath('config') .. '/init.vim' or vim.env.MYVIMRC + ) if not filereadable(vimrc) then ok = false local has_vim = filereadable(vim.fn.expand('~/.vimrc')) - health.report_warn((-1 == vim.fn.getfsize(vimrc) and 'Missing' or 'Unreadable')..' user config file: '..vimrc, - { has_vim and ':help nvim-from-vim' or ':help init.vim' }) + health.report_warn( + (-1 == vim.fn.getfsize(vimrc) and 'Missing' or 'Unreadable') .. ' user config file: ' .. vimrc, + { has_vim and ':help nvim-from-vim' or ':help init.vim' } + ) end -- If $VIM is empty we don't care. Else make sure it is valid. - if not empty(vim.env.VIM) and not filereadable(vim.env.VIM..'/runtime/doc/nvim.txt') then + if not empty(vim.env.VIM) and not filereadable(vim.env.VIM .. '/runtime/doc/nvim.txt') then ok = false - health.report_error('$VIM is invalid: '..vim.env.VIM) + health.report_error('$VIM is invalid: ' .. vim.env.VIM) end if 1 == vim.fn.exists('$NVIM_TUI_ENABLE_CURSOR_SHAPE') then ok = false - health.report_warn('$NVIM_TUI_ENABLE_CURSOR_SHAPE is ignored in Nvim 0.2+', - { "Use the 'guicursor' option to configure cursor shape. :help 'guicursor'", - 'https://github.com/neovim/neovim/wiki/Following-HEAD#20170402' }) + health.report_warn('$NVIM_TUI_ENABLE_CURSOR_SHAPE is ignored in Nvim 0.2+', { + "Use the 'guicursor' option to configure cursor shape. :help 'guicursor'", + 'https://github.com/neovim/neovim/wiki/Following-HEAD#20170402', + }) end if vim.v.ctype == 'C' then ok = false - health.report_error('Locale does not support UTF-8. Unicode characters may not display correctly.' - ..("\n$LANG=%s $LC_ALL=%s $LC_CTYPE=%s"):format(vim.env.LANG, vim.env.LC_ALL, vim.env.LC_CTYPE), - { 'If using tmux, try the -u option.', - 'Ensure that your terminal/shell/tmux/etc inherits the environment, or set $LANG explicitly.' , - 'Configure your system locale.' }) + health.report_error( + 'Locale does not support UTF-8. Unicode characters may not display correctly.' + .. ('\n$LANG=%s $LC_ALL=%s $LC_CTYPE=%s'):format( + vim.env.LANG, + vim.env.LC_ALL, + vim.env.LC_CTYPE + ), + { + 'If using tmux, try the -u option.', + 'Ensure that your terminal/shell/tmux/etc inherits the environment, or set $LANG explicitly.', + 'Configure your system locale.', + } + ) end if vim.o.paste == 1 then ok = false - health.report_error("'paste' is enabled. This option is only for pasting text.\nIt should not be set in your config.", - { 'Remove `set paste` from your init.vim, if applicable.', - 'Check `:verbose set paste?` to see if a plugin or script set the option.', }) + health.report_error( + "'paste' is enabled. This option is only for pasting text.\nIt should not be set in your config.", + { + 'Remove `set paste` from your init.vim, if applicable.', + 'Check `:verbose set paste?` to see if a plugin or script set the option.', + } + ) end local writeable = true local shadaopt = vim.fn.split(vim.o.shada, ',') - local shadafile = (empty(vim.o.shada) - and vim.o.shada - or vim.fn.substitute(vim.fn.matchstr(shadaopt[#shadaopt], '^n.\\+'), '^n', '', '')) - shadafile = (empty(vim.o.shadafile) - and (empty(shadafile) and vim.fn.stdpath('state')..'/shada/main.shada' or vim.fn.expand(shadafile)) - or (vim.o.shadafile == 'NONE' and '' or vim.o.shadafile)) + local shadafile = ( + empty(vim.o.shada) and vim.o.shada + or vim.fn.substitute(vim.fn.matchstr(shadaopt[#shadaopt], '^n.\\+'), '^n', '', '') + ) + shadafile = ( + empty(vim.o.shadafile) + and (empty(shadafile) and vim.fn.stdpath('state') .. '/shada/main.shada' or vim.fn.expand( + shadafile + )) + or (vim.o.shadafile == 'NONE' and '' or vim.o.shadafile) + ) if not empty(shadafile) and empty(vim.fn.glob(shadafile)) then -- Since this may be the first time Nvim has been run, try to create a shada file. if not pcall(vim.cmd.wshada) then writeable = false end end - if not writeable or (not empty(shadafile) and (not filereadable(shadafile) or not filewritable(shadafile))) then + if + not writeable + or (not empty(shadafile) and (not filereadable(shadafile) or not filewritable(shadafile))) + then ok = false - health.report_error('shada file is not ' - ..((not writeable or filereadable(shadafile)) and 'writeable' or 'readable')..':\n'..shadafile) + health.report_error( + 'shada file is not ' + .. ((not writeable or filereadable(shadafile)) and 'writeable' or 'readable') + .. ':\n' + .. shadafile + ) end if ok then @@ -75,7 +141,7 @@ end local function check_performance() vim.api.nvim_exec([=[ func! s:check_performance() abort - let s:suggest_faq = ']=]..suggest_faq..[=[' + let s:suggest_faq = ']=] .. suggest_faq .. [=[' call health#report_start('Performance') @@ -105,13 +171,13 @@ local function check_performance() endf call s:check_performance() - ]=], - false) + ]=], false) end -- Load the remote plugin manifest file and check for unregistered plugins local function check_rplugin_manifest() - vim.api.nvim_exec([=[ + vim.api.nvim_exec( + [=[ func! s:check_rplugin_manifest() abort call health#report_start('Remote Plugins') let existing_rplugins = {} @@ -172,13 +238,13 @@ local function check_rplugin_manifest() call s:check_rplugin_manifest() ]=], - false) + false + ) end - local function check_tmux() vim.api.nvim_exec([=[ - let s:suggest_faq = ']=]..suggest_faq..[=[' + let s:suggest_faq = ']=] .. suggest_faq .. [=[' func! s:get_tmux_option(option) abort let cmd = 'tmux show-option -qvg '.a:option " try global scope @@ -258,7 +324,7 @@ local function check_tmux() endif " check for RGB capabilities - let info = system(['tmux', 'server-info']) + let info = system(['tmux', 'show-messages', '-JT']) let has_tc = stridx(info, " Tc: (flag) true") != -1 let has_rgb = stridx(info, " RGB: (flag) true") != -1 if !has_tc && !has_rgb @@ -270,12 +336,12 @@ local function check_tmux() endf call s:check_tmux() - ]=], - false) + ]=], false) end local function check_terminal() - vim.api.nvim_exec([=[ + vim.api.nvim_exec( + [=[ func! s:check_terminal() abort if !executable('infocmp') return @@ -307,11 +373,13 @@ local function check_terminal() call s:check_terminal() ]=], - false) + false + ) end function M.check() check_config() + check_runtime() check_performance() check_rplugin_manifest() check_terminal() -- cgit From 4d2373f5f6570fcc851b818198f45fbda391fd6a Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Fri, 11 Nov 2022 21:33:31 -0500 Subject: feat(checkhealth): use "help" syntax, avoid tabpage #20879 - If Nvim was just started, don't create a new tab. - Name the buffer "health://". - Use "help" syntax instead of "markdown". It fits better, and eliminates various workarounds. - Simplfy formatting, avoid visual noise. - Don't print a "INFO" status, it is noisy. - Drop the ":" after statuses, they are already UPPERCASE and highlighted. --- runtime/lua/nvim/health.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'runtime/lua/nvim') diff --git a/runtime/lua/nvim/health.lua b/runtime/lua/nvim/health.lua index 413eae0bf9..0dd32ddc0b 100644 --- a/runtime/lua/nvim/health.lua +++ b/runtime/lua/nvim/health.lua @@ -359,14 +359,12 @@ local function check_terminal() \ ..'\%(conemu\|vtpcon\|win32con\)'))) call health#report_error('command failed: '.cmd."\n".out) else - call health#report_info('key_backspace (kbs) terminfo entry: ' - \ .(empty(kbs_entry) ? '? (not found)' : kbs_entry)) - call health#report_info('key_dc (kdch1) terminfo entry: ' - \ .(empty(kbs_entry) ? '? (not found)' : kdch1_entry)) + call health#report_info(printf('key_backspace (kbs) terminfo entry: `%s`', (empty(kbs_entry) ? '? (not found)' : kbs_entry))) + call health#report_info(printf('key_dc (kdch1) terminfo entry: `%s`', (empty(kbs_entry) ? '? (not found)' : kdch1_entry))) endif for env_var in ['XTERM_VERSION', 'VTE_VERSION', 'TERM_PROGRAM', 'COLORTERM', 'SSH_TTY'] if exists('$'.env_var) - call health#report_info(printf("$%s='%s'", env_var, eval('$'.env_var))) + call health#report_info(printf('$%s="%s"', env_var, eval('$'.env_var))) endif endfor endf -- cgit From 936e191fef9865600af211c29ea4959ffbce81dd Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Wed, 4 Jan 2023 00:38:48 +0100 Subject: docs: fix typos (#21427) Co-authored-by: Gustavo Sampaio Co-authored-by: C.D. MacEachern Co-authored-by: Sean Dewar Co-authored-by: Tomas Nemec --- runtime/lua/nvim/health.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/nvim') diff --git a/runtime/lua/nvim/health.lua b/runtime/lua/nvim/health.lua index 0dd32ddc0b..c00b921d5c 100644 --- a/runtime/lua/nvim/health.lua +++ b/runtime/lua/nvim/health.lua @@ -1,7 +1,7 @@ local M = {} local health = require('vim.health') -local suggest_faq = 'https://github.com/neovim/neovim/wiki/FAQ' +local suggest_faq = 'https://github.com/neovim/neovim/wiki/Building-Neovim#optimized-builds' local function check_runtime() health.report_start('Runtime') -- cgit From 307efe4906de9f0b7d8611ea36bddb85493c1447 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Mon, 16 Jan 2023 04:55:24 -0500 Subject: health: migrate to Lua #21661 * refactor: remove all vimscript from nvim/health * fixup: previous method broke if you had a folder named 'x-lua' --- runtime/lua/nvim/health.lua | 470 +++++++++++++++++++++++--------------------- 1 file changed, 241 insertions(+), 229 deletions(-) (limited to 'runtime/lua/nvim') diff --git a/runtime/lua/nvim/health.lua b/runtime/lua/nvim/health.lua index c00b921d5c..e11574ee97 100644 --- a/runtime/lua/nvim/health.lua +++ b/runtime/lua/nvim/health.lua @@ -1,6 +1,22 @@ local M = {} local health = require('vim.health') +local fn_bool = function(key) + return function(...) + return vim.fn[key](...) == 1 + end +end + +local has = fn_bool('has') +local executable = fn_bool('executable') +local empty = fn_bool('empty') +local filereadable = fn_bool('filereadable') +local filewritable = fn_bool('filewritable') + +local shell_error = function() + return vim.v.shell_error ~= 0 +end + local suggest_faq = 'https://github.com/neovim/neovim/wiki/Building-Neovim#optimized-builds' local function check_runtime() @@ -37,15 +53,6 @@ end local function check_config() health.report_start('Configuration') local ok = true - local empty = function(o) - return 0 ~= vim.fn.empty(o) - end - local filereadable = function(o) - return 0 ~= vim.fn.filereadable(o) - end - local filewritable = function(o) - return 0 ~= vim.fn.filewritable(o) - end local vimrc = ( empty(vim.env.MYVIMRC) and vim.fn.stdpath('config') .. '/init.vim' or vim.env.MYVIMRC @@ -65,7 +72,7 @@ local function check_config() health.report_error('$VIM is invalid: ' .. vim.env.VIM) end - if 1 == vim.fn.exists('$NVIM_TUI_ENABLE_CURSOR_SHAPE') then + if vim.env.NVIM_TUI_ENABLE_CURSOR_SHAPE then ok = false health.report_warn('$NVIM_TUI_ENABLE_CURSOR_SHAPE is ignored in Nvim 0.2+', { "Use the 'guicursor' option to configure cursor shape. :help 'guicursor'", @@ -139,240 +146,245 @@ local function check_config() end local function check_performance() - vim.api.nvim_exec([=[ - func! s:check_performance() abort - let s:suggest_faq = ']=] .. suggest_faq .. [=[' - - call health#report_start('Performance') - - " check buildtype - let s:buildtype = matchstr(execute('version'), '\v\cbuild type:?\s*[^\n\r\t ]+') - if empty(s:buildtype) - call health#report_error('failed to get build type from :version') - elseif s:buildtype =~# '\v(MinSizeRel|Release|RelWithDebInfo)' - call health#report_ok(s:buildtype) - else - call health#report_info(s:buildtype) - call health#report_warn( - \ 'Non-optimized '.(has('debug')?'(DEBUG) ':'').'build. Nvim will be slower.', - \ ['Install a different Nvim package, or rebuild with `CMAKE_BUILD_TYPE=RelWithDebInfo`.', - \ s:suggest_faq]) - endif - - " check for slow shell invocation - let s:slow_cmd_time = 1.5 - let s:start_time = reltime() - call system('echo') - let s:elapsed_time = reltimefloat(reltime(s:start_time)) - if s:elapsed_time > s:slow_cmd_time - call health#report_warn( - \ 'Slow shell invocation (took '.printf('%.2f', s:elapsed_time).' seconds).') - endif - endf - - call s:check_performance() - ]=], false) + health.report_start('Performance') + + -- Check buildtype + local buildtype = vim.fn.matchstr(vim.fn.execute('version'), [[\v\cbuild type:?\s*[^\n\r\t ]+]]) + if empty(buildtype) then + health.report_error('failed to get build type from :version') + elseif vim.regex([[\v(MinSizeRel|Release|RelWithDebInfo)]]):match_str(buildtype) then + health.report_ok(buildtype) + else + health.report_info(buildtype) + health.report_warn( + 'Non-optimized ' .. (has('debug') and '(DEBUG) ' or '') .. 'build. Nvim will be slower.', + { + 'Install a different Nvim package, or rebuild with `CMAKE_BUILD_TYPE=RelWithDebInfo`.', + suggest_faq, + } + ) + end + + -- check for slow shell invocation + local slow_cmd_time = 1.5 + local start_time = vim.fn.reltime() + vim.fn.system('echo') + local elapsed_time = vim.fn.reltimefloat(vim.fn.reltime(start_time)) + if elapsed_time > slow_cmd_time then + health.report_warn( + 'Slow shell invocation (took ' .. vim.fn.printf('%.2f', elapsed_time) .. ' seconds).' + ) + end end -- Load the remote plugin manifest file and check for unregistered plugins local function check_rplugin_manifest() - vim.api.nvim_exec( - [=[ - func! s:check_rplugin_manifest() abort - call health#report_start('Remote Plugins') - let existing_rplugins = {} - - for item in remote#host#PluginsForHost('python') - let existing_rplugins[item.path] = 'python' - endfor - - for item in remote#host#PluginsForHost('python3') - let existing_rplugins[item.path] = 'python3' - endfor - - let require_update = 0 - - for path in map(split(&runtimepath, ','), 'resolve(v:val)') - let python_glob = glob(path.'/rplugin/python*', 1, 1) - if empty(python_glob) - continue - endif - - let python_dir = python_glob[0] - let python_version = fnamemodify(python_dir, ':t') - - for script in glob(python_dir.'/*.py', 1, 1) - \ + glob(python_dir.'/*/__init__.py', 1, 1) - let contents = join(readfile(script)) - if contents =~# '\<\%(from\|import\)\s\+neovim\>' - if script =~# '[\/]__init__\.py$' - let script = tr(fnamemodify(script, ':h'), '\', '/') - endif - - if !has_key(existing_rplugins, script) - let msg = printf('"%s" is not registered.', fnamemodify(path, ':t')) - if python_version ==# 'pythonx' - if !has('python3') - let msg .= ' (python3 not available)' - endif - elseif !has(python_version) - let msg .= printf(' (%s not available)', python_version) - else - let require_update = 1 - endif - - call health#report_warn(msg) - endif - - break - endif - endfor - endfor - - if require_update - call health#report_warn('Out of date', ['Run `:UpdateRemotePlugins`']) - else - call health#report_ok('Up to date') - endif - endf + health.report_start('Remote Plugins') - call s:check_rplugin_manifest() - ]=], - false - ) + local existing_rplugins = {} + for _, item in ipairs(vim.fn['remote#host#PluginsForHost']('python')) do + existing_rplugins[item.path] = 'python' + end + + for item in ipairs(vim.fn['remote#host#PluginsForHost']('python3')) do + existing_rplugins[item.path] = 'python3' + end + + local require_update = false + local handle_path = function(path) + local python_glob = vim.fn.glob(path .. '/rplugin/python*', true, true) + if empty(python_glob) then + return + end + + local python_dir = python_glob[1] + local python_version = vim.fn.fnamemodify(python_dir, ':t') + + local scripts = vim.fn.glob(python_dir .. '/*.py', true, true) + vim.list_extend(scripts, vim.fn.glob(python_dir .. '/*/__init__.py', true, true)) + + for script in ipairs(scripts) do + local contents = vim.fn.join(vim.fn.readfile(script)) + if vim.regex([[\<\%(from\|import\)\s\+neovim\>]]):match_str(contents) then + if vim.regex([[[\/]__init__\.py$]]):match_str(script) then + script = vim.fn.tr(vim.fn.fnamemodify(script, ':h'), '\\', '/') + end + if not existing_rplugins[script] then + local msg = vim.fn.printf('"%s" is not registered.', vim.fn.fnamemodify(path, ':t')) + if python_version == 'pythonx' then + if not has('python3') then + msg = msg .. ' (python3 not available)' + end + elseif not has(python_version) then + msg = msg .. vim.fn.printf(' (%s not available)', python_version) + else + require_update = true + end + + health.report_warn(msg) + end + + break + end + end + end + + for _, path in ipairs(vim.fn.map(vim.fn.split(vim.o.runtimepath, ','), 'resolve(v:val)')) do + handle_path(path) + end + + if require_update then + health.report_warn('Out of date', { 'Run `:UpdateRemotePlugins`' }) + else + health.report_ok('Up to date') + end end local function check_tmux() - vim.api.nvim_exec([=[ - let s:suggest_faq = ']=] .. suggest_faq .. [=[' - - func! s:get_tmux_option(option) abort - let cmd = 'tmux show-option -qvg '.a:option " try global scope - let out = system(split(cmd)) - let val = substitute(out, '\v(\s|\r|\n)', '', 'g') - if v:shell_error - call health#report_error('command failed: '.cmd."\n".out) + if empty(vim.env.TMUX) or not executable('tmux') then + return + end + + local get_tmux_option = function(option) + local cmd = 'tmux show-option -qvg ' .. option -- try global scope + local out = vim.fn.system(vim.fn.split(cmd)) + local val = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g') + if shell_error() then + health.report_error('command failed: ' .. cmd .. '\n' .. out) return 'error' - elseif empty(val) - let cmd = 'tmux show-option -qvgs '.a:option " try session scope - let out = system(split(cmd)) - let val = substitute(out, '\v(\s|\r|\n)', '', 'g') - if v:shell_error - call health#report_error('command failed: '.cmd."\n".out) + elseif empty(val) then + cmd = 'tmux show-option -qvgs ' .. option -- try session scope + out = vim.fn.system(vim.fn.split(cmd)) + val = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g') + if shell_error() then + health.report_error('command failed: ' .. cmd .. '\n' .. out) return 'error' - endif - endif + end + end return val - endf + end - func! s:check_tmux() abort - if empty($TMUX) || !executable('tmux') - return - endif - call health#report_start('tmux') - - " check escape-time - let suggestions = ["set escape-time in ~/.tmux.conf:\nset-option -sg escape-time 10", - \ s:suggest_faq] - let tmux_esc_time = s:get_tmux_option('escape-time') - if tmux_esc_time !=# 'error' - if empty(tmux_esc_time) - call health#report_error('`escape-time` is not set', suggestions) - elseif tmux_esc_time > 300 - call health#report_error( - \ '`escape-time` ('.tmux_esc_time.') is higher than 300ms', suggestions) - else - call health#report_ok('escape-time: '.tmux_esc_time) - endif - endif - - " check focus-events - let suggestions = ["(tmux 1.9+ only) Set `focus-events` in ~/.tmux.conf:\nset-option -g focus-events on"] - let tmux_focus_events = s:get_tmux_option('focus-events') - if tmux_focus_events !=# 'error' - if empty(tmux_focus_events) || tmux_focus_events !=# 'on' - call health#report_warn( - \ "`focus-events` is not enabled. |'autoread'| may not work.", suggestions) - else - call health#report_ok('focus-events: '.tmux_focus_events) - endif - endif - - " check default-terminal and $TERM - call health#report_info('$TERM: '.$TERM) - let cmd = 'tmux show-option -qvg default-terminal' - let out = system(split(cmd)) - let tmux_default_term = substitute(out, '\v(\s|\r|\n)', '', 'g') - if empty(tmux_default_term) - let cmd = 'tmux show-option -qvgs default-terminal' - let out = system(split(cmd)) - let tmux_default_term = substitute(out, '\v(\s|\r|\n)', '', 'g') - endif - - if v:shell_error - call health#report_error('command failed: '.cmd."\n".out) - elseif tmux_default_term !=# $TERM - call health#report_info('default-terminal: '.tmux_default_term) - call health#report_error( - \ '$TERM differs from the tmux `default-terminal` setting. Colors might look wrong.', - \ ['$TERM may have been set by some rc (.bashrc, .zshrc, ...).']) - elseif $TERM !~# '\v(tmux-256color|screen-256color)' - call health#report_error( - \ '$TERM should be "screen-256color" or "tmux-256color" in tmux. Colors might look wrong.', - \ ["Set default-terminal in ~/.tmux.conf:\nset-option -g default-terminal \"screen-256color\"", - \ s:suggest_faq]) - endif - - " check for RGB capabilities - let info = system(['tmux', 'show-messages', '-JT']) - let has_tc = stridx(info, " Tc: (flag) true") != -1 - let has_rgb = stridx(info, " RGB: (flag) true") != -1 - if !has_tc && !has_rgb - call health#report_warn( - \ "Neither Tc nor RGB capability set. True colors are disabled. |'termguicolors'| won't work properly.", - \ ["Put this in your ~/.tmux.conf and replace XXX by your $TERM outside of tmux:\nset-option -sa terminal-overrides ',XXX:RGB'", - \ "For older tmux versions use this instead:\nset-option -ga terminal-overrides ',XXX:Tc'"]) - endif - endf - - call s:check_tmux() - ]=], false) + health.report_start('tmux') + + -- check escape-time + local suggestions = + { 'set escape-time in ~/.tmux.conf:\nset-option -sg escape-time 10', suggest_faq } + local tmux_esc_time = get_tmux_option('escape-time') + if tmux_esc_time ~= 'error' then + if empty(tmux_esc_time) then + health.report_error('`escape-time` is not set', suggestions) + elseif tmux_esc_time > 300 then + health.report_error( + '`escape-time` (' .. tmux_esc_time .. ') is higher than 300ms', + suggestions + ) + else + health.report_ok('escape-time: ' .. tmux_esc_time) + end + end + + -- check focus-events + local tmux_focus_events = get_tmux_option('focus-events') + if tmux_focus_events ~= 'error' then + if empty(tmux_focus_events) or tmux_focus_events ~= 'on' then + health.report_warn( + "`focus-events` is not enabled. |'autoread'| may not work.", + { '(tmux 1.9+ only) Set `focus-events` in ~/.tmux.conf:\nset-option -g focus-events on' } + ) + else + health.report_ok('focus-events: ' .. tmux_focus_events) + end + end + + -- check default-terminal and $TERM + health.report_info('$TERM: ' .. vim.env.TERM) + local cmd = 'tmux show-option -qvg default-terminal' + local out = vim.fn.system(vim.fn.split(cmd)) + local tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g') + if empty(tmux_default_term) then + cmd = 'tmux show-option -qvgs default-terminal' + out = vim.fn.system(vim.fn.split(cmd)) + tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g') + end + + if shell_error() then + health.report_error('command failed: ' .. cmd .. '\n' .. out) + elseif tmux_default_term ~= vim.env.TERM then + health.report_info('default-terminal: ' .. tmux_default_term) + health.report_error( + '$TERM differs from the tmux `default-terminal` setting. Colors might look wrong.', + { '$TERM may have been set by some rc (.bashrc, .zshrc, ...).' } + ) + elseif not vim.regex([[\v(tmux-256color|screen-256color)]]):match_str(vim.env.TERM) then + health.report_error( + '$TERM should be "screen-256color" or "tmux-256color" in tmux. Colors might look wrong.', + { + 'Set default-terminal in ~/.tmux.conf:\nset-option -g default-terminal "screen-256color"', + suggest_faq, + } + ) + end + + -- check for RGB capabilities + local info = vim.fn.system({ 'tmux', 'show-messages', '-JT' }) + local has_tc = vim.fn.stridx(info, ' Tc: (flag) true') ~= -1 + local has_rgb = vim.fn.stridx(info, ' RGB: (flag) true') ~= -1 + if not has_tc and not has_rgb then + health.report_warn( + "Neither Tc nor RGB capability set. True colors are disabled. |'termguicolors'| won't work properly.", + { + "Put this in your ~/.tmux.conf and replace XXX by your $TERM outside of tmux:\nset-option -sa terminal-overrides ',XXX:RGB'", + "For older tmux versions use this instead:\nset-option -ga terminal-overrides ',XXX:Tc'", + } + ) + end end local function check_terminal() - vim.api.nvim_exec( - [=[ - func! s:check_terminal() abort - if !executable('infocmp') - return - endif - call health#report_start('terminal') - let cmd = 'infocmp -L' - let out = system(split(cmd)) - let kbs_entry = matchstr(out, 'key_backspace=[^,[:space:]]*') - let kdch1_entry = matchstr(out, 'key_dc=[^,[:space:]]*') - - if v:shell_error - \ && (!has('win32') - \ || empty(matchstr(out, - \ 'infocmp: couldn''t open terminfo file .\+' - \ ..'\%(conemu\|vtpcon\|win32con\)'))) - call health#report_error('command failed: '.cmd."\n".out) - else - call health#report_info(printf('key_backspace (kbs) terminfo entry: `%s`', (empty(kbs_entry) ? '? (not found)' : kbs_entry))) - call health#report_info(printf('key_dc (kdch1) terminfo entry: `%s`', (empty(kbs_entry) ? '? (not found)' : kdch1_entry))) - endif - for env_var in ['XTERM_VERSION', 'VTE_VERSION', 'TERM_PROGRAM', 'COLORTERM', 'SSH_TTY'] - if exists('$'.env_var) - call health#report_info(printf('$%s="%s"', env_var, eval('$'.env_var))) - endif - endfor - endf - - call s:check_terminal() - ]=], - false - ) + if not executable('infocmp') then + return + end + + health.report_start('terminal') + local cmd = 'infocmp -L' + local out = vim.fn.system(vim.fn.split(cmd)) + local kbs_entry = vim.fn.matchstr(out, 'key_backspace=[^,[:space:]]*') + local kdch1_entry = vim.fn.matchstr(out, 'key_dc=[^,[:space:]]*') + + if + shell_error() + and ( + not has('win32') + or empty( + vim.fn.matchstr( + out, + [[infocmp: couldn't open terminfo file .\+\%(conemu\|vtpcon\|win32con\)]] + ) + ) + ) + then + health.report_error('command failed: ' .. cmd .. '\n' .. out) + else + health.report_info( + vim.fn.printf( + 'key_backspace (kbs) terminfo entry: `%s`', + (empty(kbs_entry) and '? (not found)' or kbs_entry) + ) + ) + + health.report_info( + vim.fn.printf( + 'key_dc (kdch1) terminfo entry: `%s`', + (empty(kbs_entry) and '? (not found)' or kdch1_entry) + ) + ) + end + + for env_var in ipairs({ 'XTERM_VERSION', 'VTE_VERSION', 'TERM_PROGRAM', 'COLORTERM', 'SSH_TTY' }) do + if vim.env[env_var] then + health.report_info(vim.fn.printf('$%s="%s"', env_var, vim.env[env_var])) + end + end end function M.check() -- cgit From 60df0c06510cc65d68a2693722577d437264f67d Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 16 Jan 2023 10:37:14 +0000 Subject: fix(health): fix `tmux_esc_time` comparison Regression from the health.vim to .lua changes. Unlike Vim script, Lua does not implicitly convert strings to numbers, so this comparison threw an error. --- runtime/lua/nvim/health.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/nvim') diff --git a/runtime/lua/nvim/health.lua b/runtime/lua/nvim/health.lua index e11574ee97..02e08bd381 100644 --- a/runtime/lua/nvim/health.lua +++ b/runtime/lua/nvim/health.lua @@ -273,7 +273,7 @@ local function check_tmux() if tmux_esc_time ~= 'error' then if empty(tmux_esc_time) then health.report_error('`escape-time` is not set', suggestions) - elseif tmux_esc_time > 300 then + elseif tonumber(tmux_esc_time) > 300 then health.report_error( '`escape-time` (' .. tmux_esc_time .. ') is higher than 300ms', suggestions -- cgit From 7e24c45221b330ccf8ed3808570dab38d212ba2e Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 16 Jan 2023 13:25:11 +0000 Subject: feat(health): detect tmux RGB support via `client_termfeatures` Problem: On tmux v3.2+, the `terminal-features` option may be used to enable RGB capabilities over `terminal-overrides`. However, `show-messages` cannot be used to detect if RGB capabilities are enabled using `terminal-features`. Solution: Try to use `display-message -p #{client_termfeatures}` instead. The returned features include "RGB" if either "RGB" is set in `terminal-features`, or if "Tc" or "RGB" is set in `terminal-overrides` (as before). Nothing is returned by tmux versions older than v3.2, so fallback to checking `show-messages` in that case. Also, un-Vimscriptify the previous logic a bit, and change the error message to point to using the `terminal-features` option instead for newer tmux versions. --- runtime/lua/nvim/health.lua | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'runtime/lua/nvim') diff --git a/runtime/lua/nvim/health.lua b/runtime/lua/nvim/health.lua index 02e08bd381..b76106f241 100644 --- a/runtime/lua/nvim/health.lua +++ b/runtime/lua/nvim/health.lua @@ -326,17 +326,24 @@ local function check_tmux() end -- check for RGB capabilities - local info = vim.fn.system({ 'tmux', 'show-messages', '-JT' }) - local has_tc = vim.fn.stridx(info, ' Tc: (flag) true') ~= -1 - local has_rgb = vim.fn.stridx(info, ' RGB: (flag) true') ~= -1 - if not has_tc and not has_rgb then - health.report_warn( - "Neither Tc nor RGB capability set. True colors are disabled. |'termguicolors'| won't work properly.", - { - "Put this in your ~/.tmux.conf and replace XXX by your $TERM outside of tmux:\nset-option -sa terminal-overrides ',XXX:RGB'", - "For older tmux versions use this instead:\nset-option -ga terminal-overrides ',XXX:Tc'", - } - ) + local info = vim.fn.system({ 'tmux', 'display-message', '-p', '#{client_termfeatures}' }) + info = vim.split(vim.trim(info), ',', { trimempty = true }) + if not vim.tbl_contains(info, 'RGB') then + local has_rgb = false + if #info == 0 then + -- client_termfeatures may not be supported; fallback to checking show-messages + info = vim.fn.system({ 'tmux', 'show-messages', '-JT' }) + has_rgb = info:find(' Tc: (flag) true', 1, true) or info:find(' RGB: (flag) true', 1, true) + end + if not has_rgb then + health.report_warn( + "Neither Tc nor RGB capability set. True colors are disabled. |'termguicolors'| won't work properly.", + { + "Put this in your ~/.tmux.conf and replace XXX by your $TERM outside of tmux:\nset-option -sa terminal-features ',XXX:RGB'", + "For older tmux versions use this instead:\nset-option -ga terminal-overrides ',XXX:Tc'", + } + ) + end end end -- cgit