From 6ea06c926fc3bc495e07ab5a1b51bcb19628d771 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 26 Oct 2022 13:46:46 +0200 Subject: [PATCH] feat: markdown formatter --- lua/noice/config/init.lua | 11 ++++++ lua/noice/source/lsp/format.lua | 68 ++++----------------------------- lua/noice/text/markdown.lua | 43 +++++++++++++++++++++ 3 files changed, 62 insertions(+), 60 deletions(-) create mode 100644 lua/noice/text/markdown.lua diff --git a/lua/noice/config/init.lua b/lua/noice/config/init.lua index 3efde14..c5443c7 100644 --- a/lua/noice/config/init.lua +++ b/lua/noice/config/init.lua @@ -77,6 +77,10 @@ M.defaults = { }, hover = { enabled = false, + view = nil, -- when nil, use defaults from documentation + ---@type NoiceViewOptions + opts = {}, -- merged with defaults from documentation + }, view = "hover", ---@type NoiceViewOptions opts = { @@ -89,6 +93,13 @@ M.defaults = { }, }, hl_patterns = { + }, + markdown = { + hover = { + ["|(%S-)|"] = vim.cmd.help, -- vim help links + ["%[.-%]%((%S-)%)"] = require("noice.util").open, -- markdown links + }, + highlights = { ["|%S-|"] = "@text.reference", ["@%S+"] = "@parameter", ["^%s*(Parameters:)"] = "@text.title", diff --git a/lua/noice/source/lsp/format.lua b/lua/noice/source/lsp/format.lua index 4c9fe9c..fabefec 100644 --- a/lua/noice/source/lsp/format.lua +++ b/lua/noice/source/lsp/format.lua @@ -1,29 +1,17 @@ local require = require("noice.util.lazy") -local Message = require("noice.message") -local NoiceText = require("noice.text") -local Config = require("noice.config") - -local M = {} - ----@type NoiceMessage[] -M._messages = {} - -function M.get(kind) - if not M._messages[kind] then - M._messages[kind] = Message("lsp", kind) - end - M._messages[kind]:clear() - return M._messages[kind] -end +local Markdown = require("noice.text.markdown") ---@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) +local M = {} + +-- Formats the content and adds it to the message +---@param contents MarkupContents Markup content +---@param message NoiceMessage Noice message +function M.format(message, contents) if type(contents) ~= "table" or not vim.tbl_islist(contents) then contents = { contents } end @@ -43,47 +31,7 @@ function M.format(contents, kind) 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 = M.get(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) - for pattern, hl_group in pairs(Config.options.lsp.hl_patterns) do - local from, to, match = line:find(pattern) - if match then - from, to = line:find(match, from) - end - if from then - message:append(NoiceText(" ", { - hl_group = hl_group, - col = from - 1, - length = to - from + 1, - })) - end - end - end - end + Markdown.format(message, text) return message end diff --git a/lua/noice/text/markdown.lua b/lua/noice/text/markdown.lua new file mode 100644 index 0000000..c8afec4 --- /dev/null +++ b/lua/noice/text/markdown.lua @@ -0,0 +1,43 @@ +local require = require("noice.util.lazy") + +local NoiceText = require("noice.text") +local Config = require("noice.config") + +local M = {} + +---@param message NoiceMessage +---@param text string +function M.format(message, text) + 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(vim.trim(text), "\n") + + for l, line in ipairs(lines) do + if l ~= 1 then + message:newline() + end + -- Make the horizontal ruler extend the whole window width + if line:find("^[%*%-_][%*%-_][%*%-_]+$") then + M.horizontal_line(message) + else + message:append(line) + for pattern, hl_group in pairs(Config.options.markdown.highlights) do + local from, to, match = line:find(pattern) + if match then + from, to = line:find(match, from) + end + if from then + message:append(NoiceText("", { + hl_group = hl_group, + col = from - 1, + length = to - from + 1, + })) + end + end + end + end +end + +return M