feat(runtime/health): support lua healthchecks

- Refactor health.vim to discover lua healthcheck in the runtime
  directories lua/**/health{/init}.lua
- Support healthchecks for lua submodules e.g :checkhealth vim.lsp and
  also support wildcard "*" at the end for all submodules
  :checkhealth vim*
- Refactor health.vim to use variable scope instead of output capturing
- Create health.lua module to wrap report functions and future
  extensibility.
- Move away from searching just in the runtimepath, use
  `nvim_get_runtime_file` due to #15632

Example:
Plugin linter in rtp can declare it's checkhealts in lua module
`lua/linter/health{/init}.lua` that returns a table with a method
"check" that when executed calls the report functions provided by the
builtin lua module require("health").

The plugin also has a submodule `/lua/linter/providers` in which it
defines `/lua/linter/providers/health{/init}.lua`

This plugin healthcheck can now be run by the ex command:
  `:checkhealth linter linter.providers`

Also calling all submodules can be done by:
  `:checkhealth linter*

And "linter" and "linter.provider" would be discovered when:
  `:checkhealth`
This commit is contained in:
Javier López 2021-08-03 15:53:09 -05:00
parent c4d581deae
commit 9249dcdda1
2 changed files with 86 additions and 40 deletions

View File

@ -26,8 +26,8 @@ endfunction
" Runs all discovered healthchecks if a:plugin_names is empty. " Runs all discovered healthchecks if a:plugin_names is empty.
function! health#check(plugin_names) abort function! health#check(plugin_names) abort
let healthchecks = empty(a:plugin_names) let healthchecks = empty(a:plugin_names)
\ ? s:discover_health_checks() \ ? s:discover_healthchecks()
\ : s:to_fn_names(a:plugin_names) \ : s:get_healthcheck(a:plugin_names)
tabnew tabnew
setlocal wrap breakindent linebreak setlocal wrap breakindent linebreak
@ -42,24 +42,28 @@ function! health#check(plugin_names) abort
else else
redraw|echo 'Running healthchecks...' redraw|echo 'Running healthchecks...'
for c in healthchecks for c in healthchecks
let output = '' let [name, func, type] = c
call append('$', split(printf("\n%s\n%s", c, repeat('=',72)), "\n")) let s:output = []
try try
let output = "\n\n".execute('call '.c.'()') if func == ''
throw 'healthcheck_not_found'
endif
eval type == 'v' ? call(func, []) : luaeval(func)
catch catch
if v:exception =~# '^Vim\%((\a\+)\)\=:E117.*\V'.c let s:output = [] " Clear the output
let output = execute( if v:exception =~# 'healthcheck_not_found'
\ 'call health#report_error(''No healthcheck found for "' call health#report_error('No healthcheck found for "'.name.'" plugin.')
\ .s:to_plugin_name(c)
\ .'" plugin.'')')
else else
let output = execute( call health#report_error(printf(
\ 'call health#report_error(''Failed to run healthcheck for "' \ "Failed to run healthcheck for \"%s\" plugin. Exception:\n%s\n%s",
\ .s:to_plugin_name(c) \ name, v:throwpoint, v:exception))
\ .'" plugin. Exception:''."\n".v:throwpoint."\n".v:exception)')
endif endif
endtry endtry
call append('$', split(output, "\n") + ['']) let header = [name. ': ' . func, repeat('=', 72)]
" remove empty line after header from report_start
let s:output = s:output[0] == '' ? s:output[1:] : s:output
let s:output = header + s:output + ['']
call append('$', s:output)
redraw redraw
endfor endfor
endif endif
@ -71,9 +75,13 @@ function! health#check(plugin_names) abort
redraw|echo '' redraw|echo ''
endfunction endfunction
function! s:collect_output(output)
let s:output += split(a:output, "\n", 1)
endfunction
" Starts a new report. " Starts a new report.
function! health#report_start(name) abort function! health#report_start(name) abort
echo "\n## " . a:name call s:collect_output("\n## " . a:name)
endfunction endfunction
" Indents lines *except* line 1 of a string if it contains newlines. " Indents lines *except* line 1 of a string if it contains newlines.
@ -119,21 +127,21 @@ endfunction " }}}
" Use {msg} to report information in the current section " Use {msg} to report information in the current section
function! health#report_info(msg) abort " {{{ function! health#report_info(msg) abort " {{{
echo s:format_report_message('INFO', a:msg) call s:collect_output(s:format_report_message('INFO', a:msg))
endfunction " }}} endfunction " }}}
" Reports a successful healthcheck. " Reports a successful healthcheck.
function! health#report_ok(msg) abort " {{{ function! health#report_ok(msg) abort " {{{
echo s:format_report_message('OK', a:msg) call s:collect_output(s:format_report_message('OK', a:msg))
endfunction " }}} endfunction " }}}
" Reports a health warning. " Reports a health warning.
" a:1: Optional advice (string or list) " a:1: Optional advice (string or list)
function! health#report_warn(msg, ...) abort " {{{ function! health#report_warn(msg, ...) abort " {{{
if a:0 > 0 if a:0 > 0
echo s:format_report_message('WARNING', a:msg, a:1) call s:collect_output(s:format_report_message('WARNING', a:msg, a:1))
else else
echo s:format_report_message('WARNING', a:msg) call s:collect_output(s:format_report_message('WARNING', a:msg))
endif endif
endfunction " }}} endfunction " }}}
@ -141,37 +149,52 @@ endfunction " }}}
" a:1: Optional advice (string or list) " a:1: Optional advice (string or list)
function! health#report_error(msg, ...) abort " {{{ function! health#report_error(msg, ...) abort " {{{
if a:0 > 0 if a:0 > 0
echo s:format_report_message('ERROR', a:msg, a:1) call s:collect_output(s:format_report_message('ERROR', a:msg, a:1))
else else
echo s:format_report_message('ERROR', a:msg) call s:collect_output(s:format_report_message('ERROR', a:msg))
endif endif
endfunction " }}} endfunction " }}}
function! s:filepath_to_function(name) abort " From a path return a list [{name}, {func}, {type}] representing a healthcheck
return substitute(substitute(substitute(a:name, '.*autoload[\/]', '', ''), function! s:filepath_to_healthcheck(path) abort
\ '\.vim', '#check', ''), '[\/]', '#', 'g') if a:path =~# 'vim$'
let name = matchstr(a:path, '\zs[^\/]*\ze\.vim$')
let func = 'health#'.name.'#check'
let type = 'v'
else
let base_path = substitute(a:path,
\ '.*lua[\/]\(.\{-}\)[\/]health\([\/]init\)\?\.lua$',
\ '\1', '')
let name = substitute(base_path, '[\/]', '.', 'g')
let func = 'require("'.name.'.health").check()'
let type = 'l'
endif
return [name, func, type]
endfunction endfunction
function! s:discover_health_checks() abort function! s:discover_healthchecks() abort
let healthchecks = globpath(&runtimepath, 'autoload/health/*.vim', 1, 1) return s:get_healthcheck('*')
let healthchecks = map(healthchecks, '<SID>filepath_to_function(v:val)')
return healthchecks
endfunction endfunction
" Translates a list of plugin names to healthcheck function names. " Returns list of lists [ [{name}, {func}, {type}] ] representing healthchecks
function! s:to_fn_names(plugin_names) abort function! s:get_healthcheck(plugin_names) abort
let healthchecks = [] let healthchecks = []
let plugin_names = type('') ==# type(a:plugin_names) let plugin_names = type('') == type(a:plugin_names)
\ ? split(a:plugin_names, '', v:false) \ ? split(a:plugin_names, ' ', v:false)
\ : a:plugin_names \ : a:plugin_names
for p in plugin_names for p in plugin_names
call add(healthchecks, 'health#'.p.'#check') " support vim/lsp/health{/init/}.lua as :checkhealth vim.lsp
let p = substitute(p, '\.', '/', 'g')
let p = substitute(p, '*$', '**', 'g') " find all submodule e.g vim*
let paths = nvim_get_runtime_file('autoload/health/'.p.'.vim', v:true)
\ + nvim_get_runtime_file('lua/**/'.p.'/health/init.lua', v:true)
\ + nvim_get_runtime_file('lua/**/'.p.'/health.lua', v:true)
if len(paths) == 0
let healthchecks += [[p, '', '']] " healthchek not found
else
let healthchecks += map(uniq(sort(paths)),
\'<SID>filepath_to_healthcheck(v:val)')
end
endfor endfor
return healthchecks return healthchecks
endfunction endfunction
" Extracts 'foo' from 'health#foo#check'.
function! s:to_plugin_name(fn_name) abort
return substitute(a:fn_name,
\ '\v.*health\#(.+)\#check.*', '\1', '')
endfunction

23
runtime/lua/health.lua Normal file
View File

@ -0,0 +1,23 @@
local M = {}
function M.report_start(msg)
vim.fn['health#report_start'](msg)
end
function M.report_info(msg)
vim.fn['health#report_info'](msg)
end
function M.report_ok(msg)
vim.fn['health#report_ok'](msg)
end
function M.report_warn(msg, ...)
vim.fn['health#report_warn'](msg, ...)
end
function M.report_error(msg, ...)
vim.fn['health#report_error'](msg, ...)
end
return M