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
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ WK.add({
})

gitsigns.setup()

require("nobbz.health").register_program("git", true)
107 changes: 107 additions & 0 deletions plugins/nobbz/lua/nobbz/health.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
local M = {}

local programs = {}
local done = false

local CRITICALITY = { OK = 1, ERROR = 2, WARN = 3, NOTICE = 4, }

---Checks if a program is available in the system `PATH`
---@param program string the program to check
---@return boolean whether the program is in the `PATH`
local function in_path(program)
return vim.fn.executable(program) == 1
end

---Performs health checks for all registered programs
---Displays results sorted by criticality and then alphabetically
local function check_programs()
local binaries = {}

vim.health.start("Programs in `PATH`:")
for program, required in pairs(programs) do
if in_path(program) then
table.insert(binaries, { program, vim.health.ok, "is installed", CRITICALITY.OK, })
elseif required then
table.insert(binaries, { program, vim.health.error, "is not installed", CRITICALITY.ERROR, })
else
table.insert(binaries, { program, vim.health.info, "is not installed", CRITICALITY.NOTICE, })
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
end

---Registers a program for the healthcheck, the program will be searched in the
---`PATH`.
---
---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
--- will issue an error when required, a warning otherwise)
M.register_program = function(program, required)
programs[program] = required
end

---Unregisters a program from the healthcheck
---@param program string the program to unregister, it will not be checked by
--- the healthcheck anymore
M.unregister_program = function(program)
programs[program] = nil
end

---Registers an LSP server for healthcheck
---Automatically registers the LSP binary as optional by default,
---but makes it required when a relevant filetype is opened
---@param lsp string the name of the LSP server as defined in lspconfig
M.register_lsp = function(lsp)
local config = require("lspconfig")[lsp]
local program = config.cmd[1]
local pattern = config.filetypes[1]

M.register_program(program, false)

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

---Marks the configuration as completely loaded
---This affects the health check status message
M.done = function()
done = true
end

---Runs all registered health checks, usually called by `:checkhealth`.
---Displays overall configuration status and checks all registered programs
M.check = function()
vim.health.start("nobbz")
if done then
vim.health.ok("Config loaded completely")
else
vim.health.warn("Config *not* loaded completely")
end

check_programs()
end

return M
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ lazy.add_specs({
})

lazy.finish()

require("nobbz.health").done()
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/astro.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require("lspconfig").astro.setup({
on_attach = helpers.keymap,
capabilities = LSP_CAPAS,
})

require("nobbz.health").register_lsp("astro")
4 changes: 3 additions & 1 deletion plugins/nobbz/lua/nobbz/lsp/beancount.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local root_dir = vim.fs.dirname(vim.fs.find({ ".git", }, { upward = true, })[1])
-- the main journal always is "main.beancount" at the repo root
local journal_file = nil
if root_dir ~= nil then
vim.fs.joinpath(root_dir, "main.beancount")
journal_file = vim.fs.joinpath(root_dir, "main.beancount")
end

if root_dir ~= nil and journal_file ~= nil then
Expand All @@ -15,4 +15,6 @@ if root_dir ~= nil and journal_file ~= nil then
root_dir = root_dir,
init_options = { journal_file = journal_file, },
})

require("nobbz.health").register_lsp("beancount")
end
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/digestif.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require("lspconfig").digestif.setup({
on_attach = helpers.keymap,
capabilities = LSP_CAPAS,
})

require("nobbz.health").register_lsp("digestif")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/elixir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ require("lspconfig").elixirls.setup({
capabilities = LSP_CAPAS,
cmd = { "elixir-ls", },
})

require("nobbz.health").register_lsp("elixirls")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/gleam.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require("lspconfig").gleam.setup({
on_attach = helpers.keymap,
capabilities = LSP_CAPAS,
})

require("nobbz.health").register_lsp("gleam")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/html.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require("lspconfig").html.setup({
on_attach = helpers.keymap,
capabilities = LSP_CAPAS,
})

require("nobbz.health").register_lsp("html")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ require("lspconfig").lua_ls.setup({
},
capabilities = LSP_CAPAS,
})

require("nobbz.health").register_lsp("lua_ls")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/mdx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ require("lspconfig").mdx_analyzer.setup({
on_attach = helpers.keymap,
capabilities = LSP_CAPAS,
})

require("nobbz.health").register_lsp("mdx_analyzer")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/nil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ require("lspconfig").nil_ls.setup({
},
},
})

require("nobbz.health").register_lsp("nil_ls")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/nushell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ require("lspconfig").nushell.setup({
filetypes = { "nu", },
cmd = { "nu", "--lsp", },
})

require("nobbz.health").register_lsp("nushell")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/oxide.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ require("lspconfig").markdown_oxide.setup({
on_attach = helpers.default,
capabilities = capabilities,
})

require("nobbz.health").register_lsp("markdown_oxide")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/python.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ require("lspconfig").basedpyright.setup({
},
},
})

require("nobbz.health").register_lsp("basedpyright")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/rust.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ require("nobbz.lazy").add_specs({ {
end,
ft = { "toml", },
}, })

require("nobbz.health").register_lsp("rust_analyzer")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/tailwind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require("lspconfig").tailwindcss.setup({
on_attach = helpers.keymap,
capabilities = LSP_CAPAS,
})

require("nobbz.health").register_lsp("tailwindcss")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/typescript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require("lspconfig").ts_ls.setup({
on_attach = helpers.keymap,
capabilities = LSP_CAPAS,
})

require("nobbz.health").register_lsp("ts_ls")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/lsp/zig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require("lspconfig").zls.setup({
on_attach = helpers.default,
capabilities = LSP_CAPAS,
})

require("nobbz.health").register_lsp("zls")
2 changes: 2 additions & 0 deletions plugins/nobbz/lua/nobbz/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ WK.add({
{ "<leader>fh", builtin.help_tags, desc = "open help", },
{ "<leader>fx", builtin.commands, desc = "run command (M-x)", },
})

require("nobbz.health").register_program("rg", true)