From 7099e56062861fc8dbb143e78447cce6bf886a97 Mon Sep 17 00:00:00 2001 From: Thim Cederlund Date: Wed, 11 Oct 2023 17:28:59 +0200 Subject: fix diagnostic highlighting and message Some language servers report diagnostics with empty ranges, start and end are at the same line and character, which currently results in no highlighting at all. Since the end of a LSP range is exclusive we always subtract 1 from it which results in invalid ranges when the server has sent an empty one. Introduce a check to fix this special case. `textDocument/publishDiagnostics` doesn't always have a `code` field, resulting in `diagnostic.code` being nil and so it is replaced with `diagnostic` in those cases. --- init.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'init.lua') diff --git a/init.lua b/init.lua index 222bd49..c7be0f3 100644 --- a/init.lua +++ b/init.lua @@ -575,9 +575,19 @@ local function lspc_highlight_diagnostics(win, diagnostics, style) for _, diagnostic in ipairs(diagnostics) do if lspc.highlight_diagnostics == 'range' then local range = diagnostic.vis_range + + -- LSP ranges use an exclusive finish + local finish = range.finish - 1 + + -- In some instances the range defined by the diagnostic starts + -- and ends at the same position. Highlight the exact position. + if range.finish == range.start then + finish = range.finish + end + -- make sure to highlight only ranges which actually contain the diagnostic if diagnostic.content == win.file:content(range) then - win:style(style, range.start, range.finish - 1) + win:style(style, range.start, finish) end elseif lspc.highlight_diagnostics == 'line' then @@ -1339,7 +1349,8 @@ local function lspc_show_diagnostic(ls, win, line) for _, diagnostic in ipairs(diagnostics_to_show) do diagnostics_msg = diagnostics_msg .. string.format(diagnostics_fmt, diagnostic.start.line, - diagnostic.start.col, diagnostic.code, diagnostic.message) + diagnostic.start.col, diagnostic.code or 'diagnostic', + diagnostic.message) end if diagnostics_msg ~= '' then -- cgit v1.2.3