Compare commits
1 commit
build/nix-
...
feat/error
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b462be187 |
2 changed files with 232 additions and 5 deletions
|
|
@ -1,15 +1,108 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
---@param stderr string
|
||||||
|
---@return preview.Diagnostic[]
|
||||||
|
local function parse_typst(stderr)
|
||||||
|
local diagnostics = {}
|
||||||
|
for line in stderr:gmatch('[^\r\n]+') do
|
||||||
|
local file, lnum, col, severity, msg = line:match('^(.+):(%d+):(%d+): (%w+): (.+)$')
|
||||||
|
if lnum then
|
||||||
|
local sev = vim.diagnostic.severity.ERROR
|
||||||
|
if severity == 'warning' then
|
||||||
|
sev = vim.diagnostic.severity.WARN
|
||||||
|
end
|
||||||
|
table.insert(diagnostics, {
|
||||||
|
lnum = tonumber(lnum) - 1,
|
||||||
|
col = tonumber(col) - 1,
|
||||||
|
message = msg,
|
||||||
|
severity = sev,
|
||||||
|
source = file,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return diagnostics
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param stderr string
|
||||||
|
---@return preview.Diagnostic[]
|
||||||
|
local function parse_latexmk(stderr)
|
||||||
|
local diagnostics = {}
|
||||||
|
for line in stderr:gmatch('[^\r\n]+') do
|
||||||
|
local _, lnum, msg = line:match('^%.?/?(.+%.tex):(%d+): (.+)$')
|
||||||
|
if lnum then
|
||||||
|
table.insert(diagnostics, {
|
||||||
|
lnum = tonumber(lnum) - 1,
|
||||||
|
col = 0,
|
||||||
|
message = msg,
|
||||||
|
severity = vim.diagnostic.severity.ERROR,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
local rule_msg = line:match('^%s+(%S.+gave return code %d+)$')
|
||||||
|
if rule_msg then
|
||||||
|
table.insert(diagnostics, {
|
||||||
|
lnum = 0,
|
||||||
|
col = 0,
|
||||||
|
message = rule_msg,
|
||||||
|
severity = vim.diagnostic.severity.ERROR,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return diagnostics
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param stderr string
|
||||||
|
---@return preview.Diagnostic[]
|
||||||
|
local function parse_pandoc(stderr)
|
||||||
|
local diagnostics = {}
|
||||||
|
for line in stderr:gmatch('[^\r\n]+') do
|
||||||
|
local lnum, col, msg = line:match('Error at .+ %(line (%d+), column (%d+)%): (.+)$')
|
||||||
|
if lnum then
|
||||||
|
table.insert(diagnostics, {
|
||||||
|
lnum = tonumber(lnum) - 1,
|
||||||
|
col = tonumber(col) - 1,
|
||||||
|
message = msg,
|
||||||
|
severity = vim.diagnostic.severity.ERROR,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
local ylnum, ycol, ymsg =
|
||||||
|
line:match('YAML parse exception at line (%d+), column (%d+)[,:]%s*(.+)$')
|
||||||
|
if ylnum then
|
||||||
|
table.insert(diagnostics, {
|
||||||
|
lnum = tonumber(ylnum) - 1,
|
||||||
|
col = tonumber(ycol) - 1,
|
||||||
|
message = ymsg,
|
||||||
|
severity = vim.diagnostic.severity.ERROR,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
local errmsg = line:match('^pandoc: (.+)$')
|
||||||
|
if errmsg and not errmsg:match('^Error at') then
|
||||||
|
table.insert(diagnostics, {
|
||||||
|
lnum = 0,
|
||||||
|
col = 0,
|
||||||
|
message = errmsg,
|
||||||
|
severity = vim.diagnostic.severity.ERROR,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return diagnostics
|
||||||
|
end
|
||||||
|
|
||||||
---@type preview.ProviderConfig
|
---@type preview.ProviderConfig
|
||||||
M.typst = {
|
M.typst = {
|
||||||
ft = 'typst',
|
ft = 'typst',
|
||||||
cmd = { 'typst', 'compile' },
|
cmd = { 'typst', 'compile' },
|
||||||
args = function(ctx)
|
args = function(ctx)
|
||||||
return { ctx.file }
|
return { '--diagnostic-format', 'short', ctx.file }
|
||||||
end,
|
end,
|
||||||
output = function(ctx)
|
output = function(ctx)
|
||||||
return (ctx.file:gsub('%.typ$', '.pdf'))
|
return (ctx.file:gsub('%.typ$', '.pdf'))
|
||||||
end,
|
end,
|
||||||
|
error_parser = function(stderr)
|
||||||
|
return parse_typst(stderr)
|
||||||
|
end,
|
||||||
open = { 'xdg-open' },
|
open = { 'xdg-open' },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -18,11 +111,19 @@ M.latex = {
|
||||||
ft = 'tex',
|
ft = 'tex',
|
||||||
cmd = { 'latexmk' },
|
cmd = { 'latexmk' },
|
||||||
args = function(ctx)
|
args = function(ctx)
|
||||||
return { '-pdf', '-interaction=nonstopmode', ctx.file }
|
return {
|
||||||
|
'-pdf',
|
||||||
|
'-interaction=nonstopmode',
|
||||||
|
'-pdflatex=pdflatex -file-line-error -interaction=nonstopmode %O %S',
|
||||||
|
ctx.file,
|
||||||
|
}
|
||||||
end,
|
end,
|
||||||
output = function(ctx)
|
output = function(ctx)
|
||||||
return (ctx.file:gsub('%.tex$', '.pdf'))
|
return (ctx.file:gsub('%.tex$', '.pdf'))
|
||||||
end,
|
end,
|
||||||
|
error_parser = function(stderr)
|
||||||
|
return parse_latexmk(stderr)
|
||||||
|
end,
|
||||||
clean = function(ctx)
|
clean = function(ctx)
|
||||||
return { 'latexmk', '-c', ctx.file }
|
return { 'latexmk', '-c', ctx.file }
|
||||||
end,
|
end,
|
||||||
|
|
@ -40,6 +141,9 @@ M.markdown = {
|
||||||
output = function(ctx)
|
output = function(ctx)
|
||||||
return (ctx.file:gsub('%.md$', '.html'))
|
return (ctx.file:gsub('%.md$', '.html'))
|
||||||
end,
|
end,
|
||||||
|
error_parser = function(stderr)
|
||||||
|
return parse_pandoc(stderr)
|
||||||
|
end,
|
||||||
clean = function(ctx)
|
clean = function(ctx)
|
||||||
return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) }
|
return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) }
|
||||||
end,
|
end,
|
||||||
|
|
@ -67,6 +171,9 @@ M.github = {
|
||||||
output = function(ctx)
|
output = function(ctx)
|
||||||
return (ctx.file:gsub('%.md$', '.html'))
|
return (ctx.file:gsub('%.md$', '.html'))
|
||||||
end,
|
end,
|
||||||
|
error_parser = function(stderr)
|
||||||
|
return parse_pandoc(stderr)
|
||||||
|
end,
|
||||||
clean = function(ctx)
|
clean = function(ctx)
|
||||||
return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) }
|
return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) }
|
||||||
end,
|
end,
|
||||||
|
|
|
||||||
|
|
@ -21,10 +21,10 @@ describe('presets', function()
|
||||||
assert.are.same({ 'typst', 'compile' }, presets.typst.cmd)
|
assert.are.same({ 'typst', 'compile' }, presets.typst.cmd)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('returns args with file path', function()
|
it('returns args with diagnostic format and file path', function()
|
||||||
local args = presets.typst.args(ctx)
|
local args = presets.typst.args(ctx)
|
||||||
assert.is_table(args)
|
assert.is_table(args)
|
||||||
assert.are.same({ '/tmp/document.typ' }, args)
|
assert.are.same({ '--diagnostic-format', 'short', '/tmp/document.typ' }, args)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('returns pdf output path', function()
|
it('returns pdf output path', function()
|
||||||
|
|
@ -36,6 +36,30 @@ describe('presets', function()
|
||||||
it('has open enabled', function()
|
it('has open enabled', function()
|
||||||
assert.are.same({ 'xdg-open' }, presets.typst.open)
|
assert.are.same({ 'xdg-open' }, presets.typst.open)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('parses errors from stderr', function()
|
||||||
|
local stderr = table.concat({
|
||||||
|
'main.typ:5:23: error: unexpected token',
|
||||||
|
'main.typ:12:1: warning: unused variable',
|
||||||
|
}, '\n')
|
||||||
|
local diagnostics = presets.typst.error_parser(stderr, ctx)
|
||||||
|
assert.is_table(diagnostics)
|
||||||
|
assert.are.equal(2, #diagnostics)
|
||||||
|
assert.are.equal(4, diagnostics[1].lnum)
|
||||||
|
assert.are.equal(22, diagnostics[1].col)
|
||||||
|
assert.are.equal('unexpected token', diagnostics[1].message)
|
||||||
|
assert.are.equal(vim.diagnostic.severity.ERROR, diagnostics[1].severity)
|
||||||
|
assert.are.equal('main.typ', diagnostics[1].source)
|
||||||
|
assert.are.equal(11, diagnostics[2].lnum)
|
||||||
|
assert.are.equal(0, diagnostics[2].col)
|
||||||
|
assert.are.equal('unused variable', diagnostics[2].message)
|
||||||
|
assert.are.equal(vim.diagnostic.severity.WARN, diagnostics[2].severity)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns empty table for clean stderr', function()
|
||||||
|
local diagnostics = presets.typst.error_parser('', ctx)
|
||||||
|
assert.are.same({}, diagnostics)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('latex', function()
|
describe('latex', function()
|
||||||
|
|
@ -57,7 +81,12 @@ describe('presets', function()
|
||||||
it('returns args with pdf flag and file path', function()
|
it('returns args with pdf flag and file path', function()
|
||||||
local args = presets.latex.args(tex_ctx)
|
local args = presets.latex.args(tex_ctx)
|
||||||
assert.is_table(args)
|
assert.is_table(args)
|
||||||
assert.are.same({ '-pdf', '-interaction=nonstopmode', '/tmp/document.tex' }, args)
|
assert.are.same({
|
||||||
|
'-pdf',
|
||||||
|
'-interaction=nonstopmode',
|
||||||
|
'-pdflatex=pdflatex -file-line-error -interaction=nonstopmode %O %S',
|
||||||
|
'/tmp/document.tex',
|
||||||
|
}, args)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('returns pdf output path', function()
|
it('returns pdf output path', function()
|
||||||
|
|
@ -75,6 +104,44 @@ describe('presets', function()
|
||||||
it('has open enabled', function()
|
it('has open enabled', function()
|
||||||
assert.are.same({ 'xdg-open' }, presets.latex.open)
|
assert.are.same({ 'xdg-open' }, presets.latex.open)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('parses file-line-error format from stderr', function()
|
||||||
|
local stderr = table.concat({
|
||||||
|
'./document.tex:10: Undefined control sequence.',
|
||||||
|
'l.10 \\badcommand',
|
||||||
|
'Collected error summary (may duplicate other messages):',
|
||||||
|
" pdflatex: Command for 'pdflatex' gave return code 256",
|
||||||
|
}, '\n')
|
||||||
|
local diagnostics = presets.latex.error_parser(stderr, tex_ctx)
|
||||||
|
assert.is_table(diagnostics)
|
||||||
|
assert.is_true(#diagnostics > 0)
|
||||||
|
assert.are.equal(9, diagnostics[1].lnum)
|
||||||
|
assert.are.equal(0, diagnostics[1].col)
|
||||||
|
assert.are.equal('Undefined control sequence.', diagnostics[1].message)
|
||||||
|
assert.are.equal(vim.diagnostic.severity.ERROR, diagnostics[1].severity)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('parses collected error summary', function()
|
||||||
|
local stderr = table.concat({
|
||||||
|
'Latexmk: Errors, so I did not complete making targets',
|
||||||
|
'Collected error summary (may duplicate other messages):',
|
||||||
|
" pdflatex: Command for 'pdflatex' gave return code 256",
|
||||||
|
}, '\n')
|
||||||
|
local diagnostics = presets.latex.error_parser(stderr, tex_ctx)
|
||||||
|
assert.is_table(diagnostics)
|
||||||
|
assert.are.equal(1, #diagnostics)
|
||||||
|
assert.are.equal(0, diagnostics[1].lnum)
|
||||||
|
assert.are.equal(0, diagnostics[1].col)
|
||||||
|
assert.are.equal(
|
||||||
|
"pdflatex: Command for 'pdflatex' gave return code 256",
|
||||||
|
diagnostics[1].message
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns empty table for clean stderr', function()
|
||||||
|
local diagnostics = presets.latex.error_parser('', tex_ctx)
|
||||||
|
assert.are.same({}, diagnostics)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('markdown', function()
|
describe('markdown', function()
|
||||||
|
|
@ -117,6 +184,43 @@ describe('presets', function()
|
||||||
it('has open enabled', function()
|
it('has open enabled', function()
|
||||||
assert.are.same({ 'xdg-open' }, presets.markdown.open)
|
assert.are.same({ 'xdg-open' }, presets.markdown.open)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('parses pandoc parse errors from stderr', function()
|
||||||
|
local stderr = 'Error at "source" (line 75, column 1): unexpected end of input'
|
||||||
|
local diagnostics = presets.markdown.error_parser(stderr, md_ctx)
|
||||||
|
assert.is_table(diagnostics)
|
||||||
|
assert.are.equal(1, #diagnostics)
|
||||||
|
assert.are.equal(74, diagnostics[1].lnum)
|
||||||
|
assert.are.equal(0, diagnostics[1].col)
|
||||||
|
assert.are.equal('unexpected end of input', diagnostics[1].message)
|
||||||
|
assert.are.equal(vim.diagnostic.severity.ERROR, diagnostics[1].severity)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('parses YAML parse exceptions from stderr', function()
|
||||||
|
local stderr =
|
||||||
|
'YAML parse exception at line 3, column 2, while scanning a block scalar: did not find expected comment or line break'
|
||||||
|
local diagnostics = presets.markdown.error_parser(stderr, md_ctx)
|
||||||
|
assert.is_table(diagnostics)
|
||||||
|
assert.are.equal(1, #diagnostics)
|
||||||
|
assert.are.equal(2, diagnostics[1].lnum)
|
||||||
|
assert.are.equal(1, diagnostics[1].col)
|
||||||
|
assert.is_string(diagnostics[1].message)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('parses generic pandoc errors from stderr', function()
|
||||||
|
local stderr = 'pandoc: Could not find data file templates/default.html5'
|
||||||
|
local diagnostics = presets.markdown.error_parser(stderr, md_ctx)
|
||||||
|
assert.is_table(diagnostics)
|
||||||
|
assert.are.equal(1, #diagnostics)
|
||||||
|
assert.are.equal(0, diagnostics[1].lnum)
|
||||||
|
assert.are.equal(0, diagnostics[1].col)
|
||||||
|
assert.are.equal('Could not find data file templates/default.html5', diagnostics[1].message)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns empty table for clean stderr', function()
|
||||||
|
local diagnostics = presets.markdown.error_parser('', md_ctx)
|
||||||
|
assert.are.same({}, diagnostics)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('github', function()
|
describe('github', function()
|
||||||
|
|
@ -179,5 +283,21 @@ describe('presets', function()
|
||||||
it('has open enabled', function()
|
it('has open enabled', function()
|
||||||
assert.are.same({ 'xdg-open' }, presets.github.open)
|
assert.are.same({ 'xdg-open' }, presets.github.open)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('parses pandoc parse errors from stderr', function()
|
||||||
|
local stderr = 'Error at "document.md" (line 12, column 5): unexpected "}" expecting letter'
|
||||||
|
local diagnostics = presets.github.error_parser(stderr, md_ctx)
|
||||||
|
assert.is_table(diagnostics)
|
||||||
|
assert.are.equal(1, #diagnostics)
|
||||||
|
assert.are.equal(11, diagnostics[1].lnum)
|
||||||
|
assert.are.equal(4, diagnostics[1].col)
|
||||||
|
assert.are.equal('unexpected "}" expecting letter', diagnostics[1].message)
|
||||||
|
assert.are.equal(vim.diagnostic.severity.ERROR, diagnostics[1].severity)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns empty table for clean stderr', function()
|
||||||
|
local diagnostics = presets.github.error_parser('', md_ctx)
|
||||||
|
assert.are.same({}, diagnostics)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue