Skip to content

Commit

Permalink
feat: markdown formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 26, 2022
1 parent add20ee commit 6ea06c9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 60 deletions.
11 changes: 11 additions & 0 deletions lua/noice/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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",
Expand Down
68 changes: 8 additions & 60 deletions lua/noice/source/lsp/format.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
43 changes: 43 additions & 0 deletions lua/noice/text/markdown.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6ea06c9

Please sign in to comment.