Skip to content

Commit

Permalink
refactor(checkhealth)!: rename to vim.health, move logic to Lua #18720
Browse files Browse the repository at this point in the history
- Complete function:
  There was lots of unnecessary C code for the complete function, therefore
  moving it to Lua and use all the plumbing we have in place to retrieve the
  results.
- Moving the module:
  It's important we keep nvim lua modules name spaced, avoids conflict with
  plugins, luarocks, etc.
  • Loading branch information
muniter committed May 31, 2022
1 parent 7380ebf commit e665282
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 117 deletions.
24 changes: 11 additions & 13 deletions runtime/doc/pi_health.txt
Expand Up @@ -48,27 +48,26 @@ Commands *health-commands*
:checkhealth vim*
<
==============================================================================
Lua Functions *health-functions-lua* *health-lua*
Lua Functions *health-functions-lua* *health-lua* *vim.health*

The Lua "health" module can be used to create new healthchecks (see also
|health-functions-vim|). To get started, simply use: >
local health = require('health')
<
health.report_start({name}) *health.report_start()*
|health-functions-vim|). To get started, simply use:

vim.health.report_start({name}) *vim.health.report_start()*
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.

health.report_info({msg}) *health.report_info()*
vim.health.report_info({msg}) *vim.health.report_info()*
Reports an informational message.

health.report_ok({msg}) *health.report_ok()*
vim.health.report_ok({msg}) *vim.health.report_ok()*
Reports a "success" message.

health.report_warn({msg} [, {advice}]) *health.report_warn()*
vim.health.report_warn({msg} [, {advice}]) *vim.health.report_warn()*
Reports a warning. {advice} is an optional List of suggestions.

health.report_error({msg} [, {advice}]) *health.report_error()*
vim.health.report_error({msg} [, {advice}]) *vim.health.report_error()*
Reports an error. {advice} is an optional List of suggestions.

==============================================================================
Expand Down Expand Up @@ -98,15 +97,14 @@ Copy this sample code into `lua/foo/health/init.lua` or `lua/foo/health.lua`,
replacing "foo" in the path with your plugin name: >
local M = {}
local health = require("health")
M.check = function()
health.report_start("my_plugin report")
vim.health.report_start("my_plugin report")
-- make sure setup function parameters are ok
if check_setup() then
health.report_ok("Setup function is correct")
vim.health.report_ok("Setup function is correct")
else
health.report_error("Setup function is incorrect")
vim.health.report_error("Setup function is incorrect")
end
-- do some more checking
-- ...
Expand Down
29 changes: 6 additions & 23 deletions runtime/lua/health.lua
@@ -1,23 +1,6 @@
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
return setmetatable({}, {
__index = function(_, k)
vim.deprecate("require('health')", 'vim.health', '0.9', false)
return vim.health[k]
end,
})
1 change: 1 addition & 0 deletions runtime/lua/vim/_editor.lua
Expand Up @@ -49,6 +49,7 @@ for k, v in pairs({
diagnostic = true,
keymap = true,
ui = true,
health = true,
}) do
vim._submodules[k] = v
end
Expand Down
47 changes: 47 additions & 0 deletions runtime/lua/vim/health.lua
@@ -0,0 +1,47 @@
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

local path2name = function(path)
if path:match('%.lua$') then
-- Lua: transform "../lua/vim/lsp/health.lua" into "vim.lsp"
return path:gsub('.-lua[%\\%/]', '', 1):gsub('[%\\%/]', '.'):gsub('%.health.-$', '')
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' }
-- :checkhealth completion function used by ex_getln.c get_healthcheck_names()
M._complete = function()
local names = vim.tbl_flatten(vim.tbl_map(function(pattern)
return vim.tbl_map(path2name, vim.api.nvim_get_runtime_file(pattern, true))
end, PATTERNS))
-- Remove duplicates
local unique = {}
vim.tbl_map(function(f)
unique[f] = true
end, names)
return vim.tbl_keys(unique)
end

return M
81 changes: 14 additions & 67 deletions src/nvim/ex_getln.c
Expand Up @@ -233,23 +233,6 @@ static int compl_match_arraysize;
static int compl_startcol;
static int compl_selected;

/// |:checkhealth| completion items
///
/// Regenerates on every new command line prompt, to accommodate changes on the
/// runtime files.
typedef struct {
garray_T names; // healthcheck names
unsigned last_gen; // last_prompt_id where names were generated
} CheckhealthComp;

/// Cookie used when converting filepath to name
struct healthchecks_cookie {
garray_T *names; // global healthchecks
bool is_lua; // true if the current entry is a Lua healthcheck
};

static CheckhealthComp healthchecks = { GA_INIT(sizeof(char_u *), 10), 0 };

#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ex_getln.c.generated.h"
#endif
Expand Down Expand Up @@ -301,59 +284,23 @@ static void init_incsearch_state(incsearch_state_T *s)
/// @param[in] xp Not used.
static char *get_healthcheck_names(expand_T *xp, int idx)
{
// Generate the first time or on new prompt.
if (healthchecks.last_gen == 0 || healthchecks.last_gen != last_prompt_id) {
ga_clear_strings(&healthchecks.names);
char *patterns[3] = { "autoload/health/**.vim", "lua/**/**/health/init.lua", // NOLINT
"lua/**/**/health.lua" }; // NOLINT
for (int i = 0; i < 3; i++) {
struct healthchecks_cookie hcookie = { .names = &healthchecks.names, .is_lua = i != 0 };
do_in_runtimepath(patterns[i], DIP_ALL, get_healthcheck_cb, &hcookie);

if (healthchecks.names.ga_len > 0) {
ga_remove_duplicate_strings(&healthchecks.names);
}
}
// Tracked to regenerate items on next prompt.
healthchecks.last_gen = last_prompt_id;
}
return idx < healthchecks.names.ga_len
? ((char **)(healthchecks.names.ga_data))[idx] : NULL;
}

/// Transform healthcheck file path into it's name.
///
/// Used as a callback for do_in_runtimepath
/// @param[in] path Expanded path to a possible healthcheck.
/// @param[out] cookie Array where names will be inserted.
static void get_healthcheck_cb(char *path, void *cookie)
{
if (path != NULL) {
struct healthchecks_cookie *hcookie = (struct healthchecks_cookie *)cookie;
char *pattern;
char *sub = "\\1";
char *res;

if (hcookie->is_lua) {
// Lua: transform "../lua/vim/lsp/health.lua" into "vim.lsp"
pattern = ".*lua[\\/]\\(.\\{-}\\)[\\/]health\\([\\/]init\\)\\?\\.lua$";
} else {
// Vim: transform "../autoload/health/provider.vim" into "provider"
pattern = ".*[\\/]\\([^\\/]*\\)\\.vim$";
}
static Object names = OBJECT_INIT;
static unsigned last_gen = 0;

res = do_string_sub(path, pattern, sub, NULL, "g");
if (hcookie->is_lua && res != NULL) {
// Replace slashes with dots as represented by the healthcheck plugin.
char *ares = do_string_sub(res, "[\\/]", ".", NULL, "g");
xfree(res);
res = ares;
}
if (last_gen != last_prompt_id || last_gen == 0) {
Array a = ARRAY_DICT_INIT;
Error err = ERROR_INIT;
Object res = nlua_exec(STATIC_CSTR_AS_STRING("return vim.health._complete()"), a, &err);
api_clear_error(&err);
api_free_object(names);
names = res;
last_gen = last_prompt_id;
}

if (res != NULL) {
GA_APPEND(char *, hcookie->names, res);
}
if (names.type == kObjectTypeArray && idx < (int)names.data.array.size) {
return names.data.array.items[idx].data.string.data;
}
return NULL;
}

// Return true when 'incsearch' highlighting is to be done.
Expand Down
9 changes: 4 additions & 5 deletions test/functional/fixtures/lua/test_plug/health/init.lua
@@ -1,11 +1,10 @@
local M = {}
local health = require("health")

M.check = function()
health.report_start("report 1")
health.report_ok("everything is fine")
health.report_start("report 2")
health.report_ok("nothing to see here")
vim.health.report_start("report 1")
vim.health.report_ok("everything is fine")
vim.health.report_start("report 2")
vim.health.report_ok("nothing to see here")
end

return M
9 changes: 4 additions & 5 deletions test/functional/fixtures/lua/test_plug/submodule/health.lua
@@ -1,11 +1,10 @@
local M = {}
local health = require("health")

M.check = function()
health.report_start("report 1")
health.report_ok("everything is fine")
health.report_start("report 2")
health.report_ok("nothing to see here")
vim.health.report_start("report 1")
vim.health.report_ok("everything is fine")
vim.health.report_start("report 2")
vim.health.report_ok("nothing to see here")
end

return M
@@ -1,10 +1,9 @@
local M = {}
local health = require("health")

M.check = function()
health.report_start("report 1")
health.report_ok("everything is fine")
health.report_warn("About to add a number to nil")
vim.health.report_start("report 1")
vim.health.report_ok("everything is fine")
vim.health.report_warn("About to add a number to nil")
local a = nil + 2
return a
end
Expand Down

0 comments on commit e665282

Please sign in to comment.