fix(checkhealth): mitigate issues with duplicate healthchecks #15919

* fix(runtime/health): mitigate issues with duplicate healthchecks
  Previously if a healthcheck was found as Lua and Vim it was executed
  both times.
  This new implementations prefers Lua, therefore if two are found It only
  runs the Lua one, this way a plugin can mantain both implementations the
  Lua one with the method `check()` and the autoload function `#check()`
  (for none HEAD nvim versions).
  **Note: This will require plugins to use `check()` as the function name,
  since the autoload function that wraps the lua implementation won't be
  called**
* docs(health): use spaces and don't overuse backtics

followup to #15259
This commit is contained in:
Javier Lopez 2021-10-05 17:37:39 -05:00 committed by GitHub
parent 6a930a9dc4
commit acd5e831b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 125 additions and 98 deletions

View File

@ -41,8 +41,8 @@ function! health#check(plugin_names) abort
call setline(1, 'ERROR: No healthchecks found.')
else
redraw|echo 'Running healthchecks...'
for c in healthchecks
let [name, func, type] = c
for name in sort(keys(healthchecks))
let [func, type] = healthchecks[name]
let s:output = []
try
if func == ''
@ -176,8 +176,30 @@ function! s:discover_healthchecks() abort
return s:get_healthcheck('*')
endfunction
" Returns list of lists [ [{name}, {func}, {type}] ] representing healthchecks
" Returns Dictionary {name: [func, type], ..} representing healthchecks
function! s:get_healthcheck(plugin_names) abort
let health_list = s:get_healthcheck_list(a:plugin_names)
let healthchecks = {}
for c in health_list
let name = c[0]
let existent = get(healthchecks, name, [])
" If an entry with the same name exists and is from vim, prefer Lua so
" overwrite it.
if existent != []
if existent[1] == "v"
let healthchecks[name] = c[1:]
else
continue
endif
else
let healthchecks[name] = c[1:]
endif
endfor
return healthchecks
endfunction
" Returns list of lists [ [{name}, {func}, {type}] ] representing healthchecks
function! s:get_healthcheck_list(plugin_names) abort
let healthchecks = []
let plugin_names = type('') == type(a:plugin_names)
\ ? split(a:plugin_names, ' ', v:false)

View File

@ -83,14 +83,14 @@ will automatically find and invoke this function.
If your plugin is named "foo", then its healthcheck module should be a file in
one of these locations on 'runtimepath' or 'packpath':
- `lua/foo/health/init.lua`
- `lua/foo/health.lua`
- lua/foo/health/init.lua
- lua/foo/health.lua
If your plugin provides a submodule named "bar" for which you want a separate
healthcheck, define the healthcheck at one of these locations on 'runtimepath'
or 'packpath':
- `lua/foo/bar/health/init.lua`
- `lua/foo/bar/health.lua`
- lua/foo/bar/health/init.lua
- lua/foo/bar/health.lua
All submodules should return a Lua table containing the method `check()`.
@ -159,8 +159,8 @@ health#{plugin}#check() function in autoload/health/{plugin}.vim.
If your plugin is named "foo", then its healthcheck function must be >
health#foo#check()
defined in this file on 'runtimepath' or 'packpath': >
autoload/health/foo.vim
defined in this file on 'runtimepath' or 'packpath':
- autoload/health/foo.vim
Copy this sample code into autoload/health/foo.vim and replace "foo" with your
plugin name: >
@ -177,4 +177,4 @@ plugin name: >
endif
endfunction
vim:noet tw=78:ts=8:ft=help:fdm=marker
vim:et:tw=78:ts=8:ft=help:fdm=marker

View File

@ -0,0 +1,3 @@
function! health#success1#check()
call health#report_start("If you see this I'm broken")
endfunction

View File

@ -100,8 +100,10 @@ describe('health.vim', function()
]])
end)
it("lua plugins", function()
it("lua plugins, skips vimscript healthchecks with the same name", function()
command("checkhealth test_plug")
-- Existing file in test/functional/fixtures/lua/test_plug/autoload/health/test_plug.vim
-- and the Lua healthcheck is used instead.
helpers.expect([[
test_plug: require("test_plug.health").check()