diff --git a/lua/noice/lsp/format.lua b/lua/noice/lsp/format.lua index 766de5e3..00a8b720 100644 --- a/lua/noice/lsp/format.lua +++ b/lua/noice/lsp/format.lua @@ -39,9 +39,10 @@ end -- Formats the content and adds it to the message ---@param contents MarkupContents Markup content ---@param message NoiceMessage Noice message -function M.format(message, contents) +---@param opts? MarkdownFormatOptions +function M.format(message, contents, opts) local text = table.concat(M.format_markdown(contents), "\n") - Markdown.format(message, text) + Markdown.format(message, text, opts) return message end diff --git a/lua/noice/lsp/hover.lua b/lua/noice/lsp/hover.lua index 6ff82253..cd50e2f2 100644 --- a/lua/noice/lsp/hover.lua +++ b/lua/noice/lsp/hover.lua @@ -10,7 +10,7 @@ function M.setup() vim.lsp.handlers["textDocument/hover"] = M.on_hover end -function M.on_hover(_, result) +function M.on_hover(_, result, ctx) if not (result and result.contents) then vim.notify("No information available") return @@ -19,7 +19,7 @@ function M.on_hover(_, result) local message = Docs.get("hover") if not message:focus() then - Format.format(message, result.contents) + Format.format(message, result.contents, { ft = vim.bo[ctx.bufnr].filetype }) if message:is_empty() then vim.notify("No information available") return diff --git a/lua/noice/lsp/signature.lua b/lua/noice/lsp/signature.lua index 17295622..408df241 100644 --- a/lua/noice/lsp/signature.lua +++ b/lua/noice/lsp/signature.lua @@ -205,7 +205,7 @@ function M:format_signature(sig_index, sig) if sig.documentation then Markdown.horizontal_line(self.message) - Format.format(self.message, sig.documentation) + Format.format(self.message, sig.documentation, { ft = self.ft }) end end diff --git a/lua/noice/text/markdown.lua b/lua/noice/text/markdown.lua index 2bc7a42d..2e51a2e6 100644 --- a/lua/noice/text/markdown.lua +++ b/lua/noice/text/markdown.lua @@ -56,7 +56,9 @@ function M.conceal_escape_characters(buf, ns, range) end ---@param text string -function M.parse(text) +---@param opts? MarkdownFormatOptions +function M.parse(text, opts) + opts = opts or {} ---@type string text = text:gsub("", "```") text = M.html_entities(text) @@ -85,7 +87,7 @@ function M.parse(text) end elseif M.is_code_block(line) then ---@type string - local lang = line:match("```(%S+)") or "text" + local lang = line:match("```(%S+)") or opts.ft or "text" local block = { lang = lang, code = {} } while lines[l + 1] and not M.is_code_block(lines[l + 1]) do table.insert(block.code, lines[l + 1]) @@ -147,15 +149,20 @@ function M.get_highlights(line) return ret end +---@alias MarkdownFormatOptions {ft?: string} + ---@param message NoiceMessage ---@param text string +---@param opts? MarkdownFormatOptions --```lua --local a = 1 --local b = true --``` --foo tex -function M.format(message, text) - local blocks = M.parse(text) +function M.format(message, text, opts) + opts = opts or {} + + local blocks = M.parse(text, opts) local md_lines = 0