Skip to content

Commit

Permalink
fix(health): better health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 25, 2024
1 parent 9ccd029 commit 0f5f8c9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 52 deletions.
62 changes: 45 additions & 17 deletions lua/noice/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ function M.check(opts)
return
end
else
log.ok("**Neovim** >= 0.9.0")
if opts.checkhealth and vim.fn.has("nvim-0.10.0") ~= 1 then
log.warn("**Neovim** >= 0.10 is highly recommended, since it fixes some issues related to `vim.ui_attach`")
log.ok("*Neovim* >= 0.9.0")
if opts.checkhealth and vim.fn.has("nvim-0.11.0") ~= 1 then
log.warn("*Neovim* >= 0.11 is highly recommended, since it fixes some issues related to `vim.ui_attach`")
end
end

Expand Down Expand Up @@ -98,17 +98,24 @@ function M.check(opts)
)
end
else
log.ok("**vim.go.lazyredraw** is not enabled")
log.ok("*vim.go.lazyredraw* is not enabled")
end

if opts.checkhealth then
if not Util.module_exists("notify") then
log.warn("Noice needs nvim-notify for routes using the `notify` view")
if not opts.checkhealth then
return
local notify = {
{ plugin = "snacks.nvim", modname = "snacks.notifier" },
{ plugin = "nvim-notify", modname = "notify" },
}
local have = false
for _, n in ipairs(notify) do
if Util.module_exists(n.modname) then
log.ok("`" .. n.plugin .. "` is installed")
have = true
end
else
log.ok("**nvim-notify** is installed")
end

if not have then
log.warn("Noice needs `snacks.nvim` or `nvim-notify` for routes using the `notify` view")
end

if vim.o.shortmess:find("S") then
Expand All @@ -119,57 +126,75 @@ function M.check(opts)

for _, lang in ipairs({ "vim", "regex", "lua", "bash", "markdown", "markdown_inline" }) do
if Treesitter.has_lang(lang) then
log.ok("**TreeSitter " .. lang .. "** parser is installed")
log.ok("{TreeSitter} `" .. lang .. "` parser is installed")
else
log.warn(
"**TreeSitter "
"{TreeSitter} `"
.. lang
.. "** parser is not installed. Highlighting of the cmdline for "
.. "` parser is not installed. Highlighting of the cmdline for `"
.. lang
.. " might be broken"
.. "` might be broken"
)
end
end
end

if Config.is_running() then
---@type {opt:string[], opt_str?:string, handler:fun(), handler_str:string}
---@type {opt:string[], opt_str?:string, handler:fun(), handler_str:string, want?:fun():unknown}
local checks = {
{
opt = "notify",
enabled = Config.options.notify.enabled,
handler = vim.notify,
handler_str = "vim.notify",
want = function()
return require("noice.source.notify").notify
end,
},
{
opt = "lsp.hover",
enabled = Config.options.lsp.hover.enabled,
handler = vim.lsp.buf.hover,
handler_str = "vim.lsp.buf.hover",
want = function()
return require("noice.lsp").hover
end,
},
{
opt = "lsp.signature",
enabled = Config.options.lsp.signature.enabled,
handler = vim.lsp.buf.signature_help,
handler_str = "vim.lsp.buf.signature_help",
want = function()
return require("noice.lsp").signature
end,
},
{
opt = "lsp.message",
enabled = Config.options.lsp.message.enabled,
handler = vim.lsp.handlers["window/showMessage"],
handler_str = 'vim.lsp.handlers["window/showMessage"]',
want = function()
return require("noice.lsp.message").on_message
end,
},
{
opt = 'lsp.override["vim.lsp.util.convert_input_to_markdown_lines"]',
enabled = Config.options.lsp.override["vim.lsp.util.convert_input_to_markdown_lines"],
handler = vim.lsp.util.convert_input_to_markdown_lines,
handler_str = "vim.lsp.util.convert_input_to_markdown_lines",
want = function()
return require("noice.lsp.override").convert_input_to_markdown_lines
end,
},
{
opt = 'lsp.override["vim.lsp.util.stylize_markdown"]',
enabled = Config.options.lsp.override["vim.lsp.util.stylize_markdown"],
handler = vim.lsp.util.stylize_markdown,
handler_str = "vim.lsp.util.stylize_markdown",
want = function()
return require("noice.lsp.override").stylize_markdown
end,
},
}

Expand All @@ -180,14 +205,17 @@ function M.check(opts)
enabled = Config.options.lsp.override["cmp.entry.get_documentation"],
handler = mod.get_documentation,
handler_str = "cmp.entry.get_documentation",
want = function()
return require("noice.lsp.override").cmp_get_documentation
end,
})
end

for _, check in ipairs(checks) do
if check.handler then
if check.enabled then
local source = M.get_source(check.handler)
if source.plugin ~= "noice.nvim" then
if check.want() ~= check.handler then
local source = M.get_source(check.handler)
log.error(([[`%s` has been overwritten by another plugin?
Either disable the other plugin or set `config.%s.enabled = false` in your **Noice** config.
Expand Down
2 changes: 1 addition & 1 deletion lua/noice/lsp/message.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ M.message_type = {
---@alias ShowMessageParams {type:MessageType, message:string}

function M.setup()
vim.lsp.handlers["window/showMessage"] = Util.protect(M.on_message)
vim.lsp.handlers["window/showMessage"] = M._on_message
end

---@param result ShowMessageParams
Expand Down
74 changes: 40 additions & 34 deletions lua/noice/lsp/override.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,55 @@ local M = {}
function M.setup()
if Config.options.lsp.override["cmp.entry.get_documentation"] then
Hacks.on_module("cmp.entry", function(mod)
mod.get_documentation = function(self)
local item = self:get_completion_item()

local lines = item.documentation and Format.format_markdown(item.documentation) or {}
local ret = table.concat(lines, "\n")
local detail = item.detail
if detail and type(detail) == "table" then
detail = table.concat(detail, "\n")
end

if detail and not ret:find(detail, 1, true) then
local ft = self.context.filetype
local dot_index = string.find(ft, "%.")
if dot_index ~= nil then
ft = string.sub(ft, 0, dot_index - 1)
end
ret = ("```%s\n%s\n```\n%s"):format(ft, vim.trim(detail), ret)
end
return vim.split(ret, "\n")
end
mod.get_documentation = M.cmp_get_documentation
end)
end

if Config.options.lsp.override["vim.lsp.util.convert_input_to_markdown_lines"] then
vim.lsp.util.convert_input_to_markdown_lines = function(input, contents)
contents = contents or {}
local ret = Format.format_markdown(input)
vim.list_extend(contents, ret)
return contents
end
vim.lsp.util.convert_input_to_markdown_lines = M.convert_input_to_markdown_lines
end

if Config.options.lsp.override["vim.lsp.util.stylize_markdown"] then
vim.lsp.util.stylize_markdown = function(buf, contents, _opts)
vim.api.nvim_buf_clear_namespace(buf, Config.ns, 0, -1)
local text = table.concat(contents, "\n")
local message = Message("lsp")
Markdown.format(message, text)
message:render(buf, Config.ns)
Markdown.keys(buf)
return vim.api.nvim_buf_get_lines(buf, 0, -1, false)
vim.lsp.util.stylize_markdown = M.stylize_markdown
end
end

function M.cmp_get_documentation(self)
local item = self:get_completion_item()

local lines = item.documentation and Format.format_markdown(item.documentation) or {}
local ret = table.concat(lines, "\n")
local detail = item.detail
if detail and type(detail) == "table" then
detail = table.concat(detail, "\n")
end

if detail and not ret:find(detail, 1, true) then
local ft = self.context.filetype
local dot_index = string.find(ft, "%.")
if dot_index ~= nil then
ft = string.sub(ft, 0, dot_index - 1)
end
ret = ("```%s\n%s\n```\n%s"):format(ft, vim.trim(detail), ret)
end
return vim.split(ret, "\n")
end

function M.convert_input_to_markdown_lines(input, contents)
contents = contents or {}
local ret = Format.format_markdown(input)
vim.list_extend(contents, ret)
return contents
end

function M.stylize_markdown(buf, contents, _opts)
vim.api.nvim_buf_clear_namespace(buf, Config.ns, 0, -1)
local text = table.concat(contents, "\n")
local message = Message("lsp")
Markdown.format(message, text)
message:render(buf, Config.ns)
Markdown.keys(buf)
return vim.api.nvim_buf_get_lines(buf, 0, -1, false)
end

return M

0 comments on commit 0f5f8c9

Please sign in to comment.