Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 68 additions & 29 deletions plugins/nobbz/lua/nobbz/health.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

local programs = {}
local lsp_configs = {}
local done = false

local CRITICALITY = { OK = 1, ERROR = 2, WARN = 3, NOTICE = 4, }
Expand All @@ -12,6 +13,50 @@ local function in_path(program)
return vim.fn.executable(program) == 1
end

local function health_cmp(a, b)
-- Sort by program name, if same criticality
if a[4] == b[4] then return a[1] < b[1] end

-- Sort by criticality otherwise
return a[4] < b[4]
end

local function health_sort(tbl)
table.sort(tbl, health_cmp)
end

local function report_table(tbl)
for _, info in ipairs(tbl) do
local report_func = info[2]
local label = info[1]
local msg = info[3]

local message = string.format("`%s` %s", label, msg)

report_func(message)
end
end


---Performs health checks for all registered LSP configs
---Displays results sorted by criticality and then alphabetically
local function check_lspconfigs()
local configs = {}

vim.health.start("LSP Configurations:")
for lsp, available in pairs(lsp_configs) do
if available then
table.insert(configs, { lsp, vim.health.ok, "is configured", CRITICALITY.OK, })
else
table.insert(configs, { lsp, vim.health.error, "is not configured", CRITICALITY.ERROR, })
end
end

health_sort(configs)
report_table(configs)
end


---Performs health checks for all registered programs
---Displays results sorted by criticality and then alphabetically
local function check_programs()
Expand All @@ -28,23 +73,8 @@ local function check_programs()
end
end

table.sort(binaries, function(a, b)
-- Sort by program name, if same criticality
if a[4] == b[4] then return a[1] < b[1] end

-- Sort by criticality otherwise
return a[4] < b[4]
end)

for _, info in ipairs(binaries) do
local report_func = info[2]
local program = vim.fs.basename(info[1])
local msg = info[3]

local message = string.format("`%s` %s", program, msg)

report_func(message)
end
health_sort(binaries)
report_table(binaries)
end

---Registers a program for the healthcheck, the program will be searched in the
Expand All @@ -53,10 +83,21 @@ end
---If a program is registered for a second time, the `required` will be overwritten.
---
---@param program string the name of the program
---@param required boolean whether the program is required (missing programs
---@param required_or_filetypes (boolean | string[]) whether the program is required (missing programs
--- will issue an error when required, a warning otherwise)
M.register_program = function(program, required)
programs[program] = required
M.register_program = function(program, required_or_filetypes)
if type(required_or_filetypes) == "table" then
M.register_program(program, false)
vim.api.nvim_create_autocmd("FileType", {
pattern = required_or_filetypes,
callback = function()
M.register_program(program, true)
end,
})
return
end

programs[program] = required_or_filetypes
end

---Unregisters a program from the healthcheck
Expand All @@ -72,17 +113,14 @@ end
---@param lsp string the name of the LSP server as defined in lspconfig
M.register_lsp = function(lsp)
local config = vim.lsp.config[lsp]
local program = config.cmd[1]
local pattern = config.filetypes[1]

M.register_program(program, false)
lsp_configs[lsp] = not not config
if not config then return end

local program = config.cmd[1]
local patterns = config.filetypes

vim.api.nvim_create_autocmd("FileType", {
pattern = pattern,
callback = function()
M.register_program(program, true)
end,
})
M.register_program(program, patterns)
end

---Marks the configuration as completely loaded
Expand All @@ -101,6 +139,7 @@ M.check = function()
vim.health.warn("Config *not* loaded completely")
end

check_lspconfigs()
check_programs()
end

Expand Down
3 changes: 3 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/nil.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
local helpers = require("nobbz.lsp.helpers")
local health = require("nobbz.health")

health.register_program("alejandra", { "nix", })

return {
name = "nil_ls",
Expand Down