Skip to content

Commit

Permalink
feat: added routing of LSP window/showMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 28, 2022
1 parent de1d5dc commit 75730f4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lua/noice/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ function M.defaults()
---@type NoiceViewOptions
opts = {}, -- merged with defaults from documentation
},
message = {
-- Messages shown by lsp servers
enabled = false,
view = "notify",
opts = {},
},
-- defaults for hover and signature help
documentation = {
view = "hover",
Expand Down
5 changes: 5 additions & 0 deletions lua/noice/config/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ function M.defaults()
view = Config.options.lsp.progress.view,
filter = { event = "lsp", kind = "progress" },
},
{
view = Config.options.lsp.message.view,
opts = Config.options.lsp.message.opts,
filter = { event = "lsp", kind = "message" },
},
})
end

Expand Down
12 changes: 12 additions & 0 deletions lua/noice/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ function M.check(opts)
log.warn([[`vim.lsp.handlers["textDocument/signatureHelp"]` is not handled by **Noice**]])
end
end

if Config.options.lsp.message.enabled then
if vim.lsp.handlers["window/showMessage"] ~= Lsp.message then
log.error([[`vim.lsp.handlers["window/showMessage"]` has been overwritten by another plugin?]])
else
log.ok([[`vim.lsp.handlers["window/showMessage"]` is handled by **Noice**]])
end
else
if opts.checkhealth then
log.warn([[`vim.lsp.handlers["window/showMessage"]` is not handled by **Noice**]])
end
end
end

return true
Expand Down
36 changes: 34 additions & 2 deletions lua/noice/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ M.event = "lsp"
M.kinds = {
progress = "progress",
hover = "hover",
message = "message",
signature = "signature",
}

Expand All @@ -32,12 +33,11 @@ function M.get(kind)
end

function M.setup()
M.hover = Util.protect(M.hover)

local group = vim.api.nvim_create_augroup("noice_lsp", {
clear = true,
})

M.hover = Util.protect(M.hover)
if Config.options.lsp.hover.enabled then
vim.lsp.handlers["textDocument/hover"] = M.hover
end
Expand All @@ -47,6 +47,11 @@ function M.setup()
vim.lsp.handlers["textDocument/signatureHelp"] = M.signature
end

M.message = Util.protect(M.message)
if Config.options.lsp.message.enabled then
vim.lsp.handlers["window/showMessage"] = M.message
end

if Config.options.lsp.signature.auto_open.enabled then
require("noice.lsp.signature").setup(group)
end
Expand All @@ -63,6 +68,33 @@ function M.setup()
})
end

---@enum MessageType
M.message_type = {
error = 1,
warn = 2,
info = 3,
debug = 4,
}

---@alias ShowMessageParams {type:MessageType, message:string}

---@param result ShowMessageParams
function M.message(_, result, ctx)
local client_id = ctx.client_id
local client = vim.lsp.get_client_by_id(client_id)
local client_name = client and client.name or string.format("lsp id=%d", client_id)

local message = Message(M.event, M.kinds.message, result.message)
message.opts.title = "LSP Message (" .. client_name .. ")"
for level, type in pairs(M.message_type) do
if type == result.type then
message.level = level
end
end
message.once = true
Manager.add(message)
end

function M.on_close()
for _, message in pairs(M._messages) do
-- close the message if we're not in it's buffer (focus)
Expand Down

0 comments on commit 75730f4

Please sign in to comment.