Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lua/render-markdown/core/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,28 @@ function Context:for_each(callback)
return false
end

--- Gets the first diagnostic of the node of the given source and severity, if any
---@param node render.md.Node
---@param severity? vim.diagnostic.Severity
---@param source? string
---@return string? message
function Context:get_diagnostic(node, severity, source)
local buf = self.buf
local col = node.start_col - 1
local row = node.start_row
local diagnostic = vim.diagnostic.get(buf)
for _, value in pairs(diagnostic) do
if value['col'] == col and value['lnum'] == row then
if severity == nil or value['severity'] == severity then
if source == nil or value['source'] == source then
return value['message']
end
end
end
end
return nil
end

---@type table<integer, render.md.Context>
local cache = {}

Expand Down
13 changes: 12 additions & 1 deletion lua/render-markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ local M = {}
---@class (exact) render.md.UserWikiLink
---@field public icon? string
---@field public highlight? string
---@field public diagnostic_severity? vim.diagnostic.Severity
---@field public diagnostic_source? string

---@class (exact) render.md.UserFootnote
---@field public superscript? boolean
Expand Down Expand Up @@ -738,7 +740,16 @@ M.default_config = {
-- Applies to the inlined icon as a fallback
highlight = 'RenderMarkdownLink',
-- Applies to WikiLink elements
wiki = { icon = '󱗖 ', highlight = 'RenderMarkdownWikiLink' },
wiki = {
icon = '󱗖 ',
highlight = 'RenderMarkdownWikiLink',
-- render any diagnostics on the WikiLink of this severity. `nil` to ignore diagnostics, Example:
-- diagnostic_severity = vim.diagnostic.severity.HINT,
diagnostic_severity = nil,
-- only render diagnostics from this source. Example:
-- diagnostic_source = "zk",
diagnostic_source = nil,
},
-- Define custom destination patterns so icons can quickly inform you of what a link
-- contains. Applies to 'inline_link', 'uri_autolink', and wikilink nodes. When multiple
-- patterns match a link the one with the longer pattern is used.
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function M.attach(buf)
state.on.attach({ buf = buf })

local events = { 'BufWinEnter', 'BufLeave', 'CmdlineChanged', 'CursorHold', 'CursorMoved', 'WinScrolled' }
local change_events = { 'DiffUpdated', 'ModeChanged', 'TextChanged' }
local change_events = { 'DiffUpdated', 'ModeChanged', 'TextChanged', 'DiagnosticChanged' }
if config:render('i') then
vim.list_extend(events, { 'CursorHoldI', 'CursorMovedI' })
vim.list_extend(change_events, { 'TextChangedI' })
Expand Down
21 changes: 16 additions & 5 deletions lua/render-markdown/render/shortcut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,28 @@ function Render:wiki_link()
self:hide(start_col - 1, start_col)

-- Add icon
local icon, highlight = self:from_destination(wiki.icon, wiki.highlight, values[1])
self.marks:add_over('link', self.node, {
virt_text = { { icon, highlight } },
virt_text_pos = 'inline',
})
local text, highlight = self:from_destination(wiki.icon, wiki.highlight, values[1])

-- Hide destination if there is an alias
if #values > 1 then
self:hide(start_col + 1, start_col + 1 + #values[1] + 1)
elseif self.config.link.wiki.diagnostic_severity then
local diagnostic = self.context:get_diagnostic(
self.node,
self.config.link.wiki.diagnostic_severity,
self.config.link.wiki.diagnostic_source
)
if diagnostic then
self:hide(start_col + 1, end_col)
text = text .. diagnostic
end
end

self.marks:add_over('link', self.node, {
virt_text = { { text, highlight } },
virt_text_pos = 'inline',
})

-- Hide closing outer bracket
self:hide(end_col, end_col + 1)
end
Expand Down
2 changes: 2 additions & 0 deletions lua/render-markdown/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
---@class (exact) render.md.WikiLink
---@field public icon string
---@field public highlight string
---@field public diagnostic_severity? vim.diagnostic.Severity
---@field public diagnostic_source? string

---@class (exact) render.md.Footnote
---@field public superscript boolean
Expand Down