Skip to content

Commit 92aa8c0

Browse files
authored
fix(proselint): compatibility with proselint 0.16 (#308)
1 parent 5501975 commit 92aa8c0

File tree

2 files changed

+88
-22
lines changed

2 files changed

+88
-22
lines changed

lua/null-ls/builtins/code_actions/proselint.lua

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,66 @@ return h.make_builtin({
1313
filetypes = { "markdown", "tex" },
1414
generator_opts = {
1515
command = "proselint",
16-
args = { "--json" },
16+
args = { "check", "--output-format=json" },
1717
format = "json",
1818
to_stdin = true,
1919
check_exit_code = function(c)
2020
return c <= 1
2121
end,
2222
on_output = function(params)
2323
local actions = {}
24-
for _, d in ipairs(params.output.data.errors) do
25-
if d.replacements ~= vim.NIL and params.row == d.line then
26-
local row = d.line - 1
27-
local col_beg = d.column - 1
28-
local col_end = d.column + d.extent - 2
29-
table.insert(actions, {
30-
title = d.message,
31-
action = function()
32-
vim.api.nvim_buf_set_text(params.bufnr, row, col_beg, row, col_end, { d.replacements })
33-
end,
34-
})
24+
25+
local output = params.output
26+
if not output or output.error then
27+
return actions
28+
end
29+
30+
local result = output.result
31+
if not result then
32+
return actions
33+
end
34+
35+
local buf = params.bufnr
36+
local text = table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), "\n")
37+
38+
for _, file_output in pairs(result) do
39+
if file_output.diagnostics then
40+
for _, d in ipairs(file_output.diagnostics) do
41+
if d.replacements and d.replacements ~= vim.NIL then
42+
-- byte offsets (1-based)
43+
local byte_start = d.span[1]
44+
local byte_end = d.span[2]
45+
46+
-- turn into 0-based Lua string indices
47+
local sub = text:sub(byte_start, byte_end)
48+
49+
-- find the (line, col) from byte indices
50+
local before = text:sub(1, byte_start - 1)
51+
local line = select(2, before:gsub("\n", "")) + 1
52+
local col = #before:match("[^\n]*$") + 1
53+
54+
-- end col
55+
local length = #sub
56+
local end_col = col + length - 1
57+
58+
table.insert(actions, {
59+
title = d.message,
60+
action = function()
61+
vim.api.nvim_buf_set_text(
62+
buf,
63+
line - 1,
64+
col - 1,
65+
line - 1,
66+
end_col,
67+
{ d.replacements }
68+
)
69+
end,
70+
})
71+
end
72+
end
3573
end
3674
end
75+
3776
return actions
3877
end,
3978
},

lua/null-ls/builtins/diagnostics/proselint.lua

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ return h.make_builtin({
1313
filetypes = { "markdown", "tex" },
1414
generator_opts = {
1515
command = "proselint",
16-
args = { "--json" },
16+
args = { "check", "--output-format=json" },
1717
format = "json",
1818
to_stdin = true,
1919
check_exit_code = function(c)
@@ -26,16 +26,43 @@ return h.make_builtin({
2626
warning = 2,
2727
suggestion = 4,
2828
}
29-
for _, d in ipairs(params.output.data.errors) do
30-
table.insert(diags, {
31-
row = d.line,
32-
col = d.column,
33-
end_col = d.column + d.extent - 1,
34-
code = d.check,
35-
message = d.message,
36-
severity = sev[d.severity],
37-
})
29+
30+
local output = params.output
31+
if not output then
32+
return diags
33+
end
34+
35+
if output.error then
36+
return diags
37+
end
38+
39+
local result = output.result
40+
if not result then
41+
return diags
3842
end
43+
44+
for _, file_output in pairs(result) do
45+
if file_output.diagnostics then
46+
for _, d in ipairs(file_output.diagnostics) do
47+
local line = d.pos[1]
48+
local col = d.pos[2]
49+
50+
-- span = {start_col, end_col}
51+
local end_col = d.span[2]
52+
53+
table.insert(diags, {
54+
row = line,
55+
col = col,
56+
end_col = end_col,
57+
code = d.check_path,
58+
message = d.message,
59+
-- Proselint no longer includes a severity -> choose warning
60+
severity = sev.warning,
61+
})
62+
end
63+
end
64+
end
65+
3966
return diags
4067
end,
4168
},

0 commit comments

Comments
 (0)