From e57251351b14ff61690f89faaba4a1358050a27c Mon Sep 17 00:00:00 2001 From: Michael McDonagh Date: Sat, 22 Feb 2025 15:07:26 -0500 Subject: [PATCH 1/3] Pull WikiLink text from diagnostics --- lua/render-markdown/core/context.lua | 22 ++++++++++++++++++++++ lua/render-markdown/init.lua | 12 +++++++++++- lua/render-markdown/manager.lua | 2 +- lua/render-markdown/render/shortcut.lua | 21 ++++++++++++++++----- lua/render-markdown/types.lua | 2 ++ 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/lua/render-markdown/core/context.lua b/lua/render-markdown/core/context.lua index 63133a70..20729d07 100644 --- a/lua/render-markdown/core/context.lua +++ b/lua/render-markdown/core/context.lua @@ -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? string +---@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 local cache = {} diff --git a/lua/render-markdown/init.lua b/lua/render-markdown/init.lua index e3bd325c..c6a03131 100644 --- a/lua/render-markdown/init.lua +++ b/lua/render-markdown/init.lua @@ -75,6 +75,7 @@ local M = {} ---@class (exact) render.md.UserWikiLink ---@field public icon? string ---@field public highlight? string +---@field public fetch_from_diagnostics? boolean ---@class (exact) render.md.UserFootnote ---@field public superscript? boolean @@ -738,7 +739,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. diff --git a/lua/render-markdown/manager.lua b/lua/render-markdown/manager.lua index 18c613a0..4505051c 100644 --- a/lua/render-markdown/manager.lua +++ b/lua/render-markdown/manager.lua @@ -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' }) diff --git a/lua/render-markdown/render/shortcut.lua b/lua/render-markdown/render/shortcut.lua index e52c77d5..f3c9225b 100644 --- a/lua/render-markdown/render/shortcut.lua +++ b/lua/render-markdown/render/shortcut.lua @@ -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 diff --git a/lua/render-markdown/types.lua b/lua/render-markdown/types.lua index 9b79e1a3..ed696d1f 100644 --- a/lua/render-markdown/types.lua +++ b/lua/render-markdown/types.lua @@ -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 From 104f9daeeabd4d0bf54dfdfc538aa37cf5af3938 Mon Sep 17 00:00:00 2001 From: Michael McDonagh Date: Sat, 22 Feb 2025 15:20:49 -0500 Subject: [PATCH 2/3] Update type --- lua/render-markdown/init.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/render-markdown/init.lua b/lua/render-markdown/init.lua index c6a03131..e9311af8 100644 --- a/lua/render-markdown/init.lua +++ b/lua/render-markdown/init.lua @@ -75,7 +75,8 @@ local M = {} ---@class (exact) render.md.UserWikiLink ---@field public icon? string ---@field public highlight? string ----@field public fetch_from_diagnostics? boolean +---@field public diagnostic_severity? vim.diagnostic.Severity +---@field public diagnostic_source? string ---@class (exact) render.md.UserFootnote ---@field public superscript? boolean From 84bf83c4380f486ae74303c8540d2763750d5de8 Mon Sep 17 00:00:00 2001 From: Michael McDonagh Date: Sat, 22 Feb 2025 15:22:45 -0500 Subject: [PATCH 3/3] Fix type --- lua/render-markdown/core/context.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/render-markdown/core/context.lua b/lua/render-markdown/core/context.lua index 20729d07..70a8cd69 100644 --- a/lua/render-markdown/core/context.lua +++ b/lua/render-markdown/core/context.lua @@ -229,7 +229,7 @@ end --- Gets the first diagnostic of the node of the given source and severity, if any ---@param node render.md.Node ----@param severity? string +---@param severity? vim.diagnostic.Severity ---@param source? string ---@return string? message function Context:get_diagnostic(node, severity, source)