Skip to content

Commit

Permalink
feat: added config.lsp.hover. Undocumented for now and disabled by de…
Browse files Browse the repository at this point in the history
…fault.
  • Loading branch information
folke committed Oct 25, 2022
1 parent 42d771a commit c2f37ed
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 22 deletions.
40 changes: 27 additions & 13 deletions lua/noice/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ M.defaults = {
-- view: (default is cmdline view)
-- opts: any options passed to the view
-- icon_hl_group: optional hl_group for the icon
-- title: set to anything or empty string to hide
cmdline = { pattern = "^:", icon = "", lang = "vim" },
search_down = { kind = "search", pattern = "^/", icon = " ", lang = "regex" },
search_up = { kind = "search", pattern = "^%?", icon = " ", lang = "regex" },
Expand Down Expand Up @@ -62,16 +63,22 @@ M.defaults = {
enabled = true,
view = "notify",
},
lsp_progress = {
enabled = true,
-- Lsp Progress is formatted using the builtins for lsp_progress. See config.format.builtin
-- See the section on formatting for more details on how to customize.
--- @type NoiceFormat|string
format = "lsp_progress",
--- @type NoiceFormat|string
format_done = "lsp_progress_done",
throttle = 1000 / 30, -- frequency to update lsp progress message
view = "mini",
lsp = {
progress = {
enabled = true,
-- Lsp Progress is formatted using the builtins for lsp_progress. See config.format.builtin
-- See the section on formatting for more details on how to customize.
--- @type NoiceFormat|string
format = "lsp_progress",
--- @type NoiceFormat|string
format_done = "lsp_progress_done",
throttle = 1000 / 30, -- frequency to update lsp progress message
view = "mini",
},
hover = {
enabled = false,
view = "notify",
},
},
throttle = 1000 / 30, -- how frequently does Noice need to check for ui updates? This has no effect when in blocking mode.
---@type NoiceConfigViews
Expand All @@ -97,6 +104,8 @@ end
function M.setup(options)
options = options or {}

M.fix_legacy(options)

if options.popupmenu and options.popupmenu.kind_icons == true then
options.popupmenu.kind_icons = nil
end
Expand Down Expand Up @@ -125,10 +134,15 @@ function M.setup(options)
end,
})

if M.options.lsp_progress.enabled then
require("noice.source.lsp.progress").setup()
end
require("noice.source.lsp").setup()
M._running = true
end

function M.fix_legacy(opts)
if opts.lsp_progress then
opts.lsp = opts.lsp or {}
opts.lsp.progress = opts.lsp_progress
end
end

return M
9 changes: 7 additions & 2 deletions lua/noice/config/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ function M.defaults()
opts = { lang = "lua", replace = true, title = "Noice" },
},
{
view = Config.options.lsp_progress.view,
filter = { event = "lsp" },
view = Config.options.lsp.hover.view,
filter = { event = "lsp", kind = "hover" },
opts = { lang = "markdown", replace = true },
},
{
view = Config.options.lsp.progress.view,
filter = { event = "lsp", kind = "progress" },
},
})
end
Expand Down
111 changes: 111 additions & 0 deletions lua/noice/source/lsp/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
local require = require("noice.util.lazy")

local Message = require("noice.message")
local Manager = require("noice.message.manager")
local NoiceText = require("noice.text")
local Config = require("noice.config")

local M = {}

---@alias LspEvent "lsp"
M.event = "lsp"

---@enum LspKind
M.kinds = {
progress = "progress",
hover = "hover",
}

---@alias MarkedString string | { language: string; value: string }
---@alias MarkupContent { kind: ('plaintext' | 'markdown'), value: string}
---@alias MarkupContents MarkedString | MarkedString[] | MarkupContent

---@param contents MarkupContents
---@param kind LspKind
function M.format(contents, kind)
if type(contents) ~= "table" or not vim.tbl_islist(contents) then
contents = { contents }
end

local parts = {}

for _, content in ipairs(contents) do
if type(content) == "string" then
table.insert(parts, content)
elseif content.language then
table.insert(parts, ("```%s\n%s\n```"):format(content.language, content.value))
elseif content.kind == "markdown" then
table.insert(parts, content.value)
elseif content.kind == "plaintext" then
table.insert(parts, ("```\n%s\n```"):format(content.value))
end
end

local text = table.concat(parts, "\n")
text = text:gsub("\n\n\n", "\n\n")
text = text:gsub("\n%s*\n```", "\n```")
text = text:gsub("```\n%s*\n", "```\n")

local lines = vim.split(text, "\n")

local width = 50
for _, line in pairs(lines) do
width = math.max(width, vim.api.nvim_strwidth(line))
end

local message = Message(M.event, kind)
message.once = true
message.opts.title = kind

for _, line in ipairs(lines) do
message:newline()
-- Make the horizontal ruler extend the whole window width
if line:find("^[%*%-_][%*%-_][%*%-_]+$") then
message:append(NoiceText("", {
virt_text_win_col = 0,
virt_text = { { (""):rep(width), "@punctuation.special.markdown" } },
priority = 100,
}))
else
message:append(line)
end
end
return message
end

function M.setup()
if Config.options.lsp.hover.enabled then
vim.lsp.handlers["textDocument/hover"] = M.hover
end
if Config.options.lsp.progress.enabled then
require("noice.source.lsp.progress").setup()
end
end

---@param message NoiceMessage
function M.close_on_move(message)
local open = true
message.opts.timeout = 100
message.opts.keep = function()
return open
end
vim.api.nvim_create_autocmd("CursorMoved", {
callback = function()
open = false
end,
once = true,
})
end

function M.hover(_, result)
if not (result and result.contents) then
vim.notify("No information available")
return
end

local message = M.format(result.contents, "hover")
M.close_on_move(message)
Manager.add(message)
end

return M
6 changes: 3 additions & 3 deletions lua/noice/source/lsp/progress.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ function M._update()
if not vim.tbl_isempty(M._progress) then
for _, message in pairs(M._progress) do
if message.opts.progress.kind == "end" then
Manager.add(Format.format(message, Config.options.lsp_progress.format_done))
Manager.add(Format.format(message, Config.options.lsp.progress.format_done))
else
Manager.add(Format.format(message, Config.options.lsp_progress.format))
Manager.add(Format.format(message, Config.options.lsp.progress.format))
end
end
return
Expand All @@ -81,7 +81,7 @@ function M.update()
end

function M.setup()
M.update = Util.interval(Config.options.lsp_progress.throttle, M._update, {
M.update = Util.interval(Config.options.lsp.progress.throttle, M._update, {
enabled = function()
return not vim.tbl_isempty(M._progress)
end,
Expand Down
4 changes: 2 additions & 2 deletions lua/noice/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ local Util = require("noice.util")
local Router = require("noice.message.router")
local Manager = require("noice.message.manager")

---@alias NoiceEvent MsgEvent|CmdlineEvent|NotifyEvent
---@alias NoiceKind MsgKind|NotifyLevel
---@alias NoiceEvent MsgEvent|CmdlineEvent|NotifyEvent|LspEvent
---@alias NoiceKind MsgKind|NotifyLevel|LspKind

local M = {}
M._attached = false
Expand Down
11 changes: 9 additions & 2 deletions lua/noice/view/backend/mini.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ function MiniView:update_options()
self._opts = vim.tbl_deep_extend("force", defaults, self._opts)
end

function MiniView:can_hide()
---@param message NoiceMessage
function MiniView:can_hide(message)
if message.opts.keep and message.opts.keep() then
return false
end
return not Util.is_blocking()
end

Expand All @@ -42,7 +46,10 @@ function MiniView:autohide(id)
self.timers[id] = vim.loop.new_timer()
end
self.timers[id]:start(self._opts.timeout, 0, function()
if not self:can_hide() then
if not self.active[id] then
return
end
if not self:can_hide(self.active[id]) then
return self:autohide(id)
end
self.active[id] = nil
Expand Down

0 comments on commit c2f37ed

Please sign in to comment.