refactor(presets): simplify mermaid error parser
Problem: the inline `mermaid` error_parser looped over every line and used the `Parse error on line N:` header as the message, losing the useful `Expecting ..., got ...` token detail. Solution: extract `parse_mermaid` alongside the other parse functions, use a single `output:match` (mermaid's JISON parser stops at the first error), and surface the `Expecting ..., got ...` line as the message.
This commit is contained in:
parent
28dda6515e
commit
59419e6003
1 changed files with 15 additions and 13 deletions
|
|
@ -115,6 +115,20 @@ local function parse_asciidoctor(output)
|
||||||
return diagnostics
|
return diagnostics
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param output string
|
||||||
|
---@return preview.Diagnostic[]
|
||||||
|
local function parse_mermaid(output)
|
||||||
|
local lnum = output:match('Parse error on line (%d+)')
|
||||||
|
if not lnum then return {} end
|
||||||
|
local msg = output:match('(Expecting .+)') or 'parse error'
|
||||||
|
return { {
|
||||||
|
lnum = tonumber(lnum) - 1,
|
||||||
|
col = 0,
|
||||||
|
message = msg,
|
||||||
|
severity = vim.diagnostic.severity.ERROR,
|
||||||
|
} }
|
||||||
|
end
|
||||||
|
|
||||||
---@type preview.ProviderConfig
|
---@type preview.ProviderConfig
|
||||||
M.typst = {
|
M.typst = {
|
||||||
ft = 'typst',
|
ft = 'typst',
|
||||||
|
|
@ -313,19 +327,7 @@ M.mermaid = {
|
||||||
return (ctx.file:gsub('%.mmd$', '.svg'))
|
return (ctx.file:gsub('%.mmd$', '.svg'))
|
||||||
end,
|
end,
|
||||||
error_parser = function(output)
|
error_parser = function(output)
|
||||||
local diagnostics = {}
|
return parse_mermaid(output)
|
||||||
for line in output:gmatch('[^\r\n]+') do
|
|
||||||
local lnum = line:match('^%s*Parse error on line (%d+)')
|
|
||||||
if lnum then
|
|
||||||
table.insert(diagnostics, {
|
|
||||||
lnum = tonumber(lnum) - 1,
|
|
||||||
col = 0,
|
|
||||||
message = line,
|
|
||||||
severity = vim.diagnostic.severity.ERROR,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return diagnostics
|
|
||||||
end,
|
end,
|
||||||
clean = function(ctx)
|
clean = function(ctx)
|
||||||
return { 'rm', '-f', (ctx.file:gsub('%.mmd$', '.svg')) }
|
return { 'rm', '-f', (ctx.file:gsub('%.mmd$', '.svg')) }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue