From efd5262f2c45d3289e482b1091d3c1d44cf67a42 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sat, 15 Apr 2023 12:11:57 +0200 Subject: support highlighting the line number of available diagnostics --- README.md | 2 +- init.lua | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 749673f..1e881aa 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ Available options are: * `menu_cmd = 'fzf' or 'vis-menu'` - program to prompt for user choices * `confirm_cmd = 'vis-menu'` - program to prompt for user confirmation * `ls_map` - a table mapping vis syntax names to language server configurations -* `highlight_diagnostics = false` - highlight available diagnostics +* `highlight_diagnostics = 'line'` - highlight the `range` or `line`number of available diagnostics * `diagnostic_style = 'back:#e3514f'` - style used to highlight diagnostics #### Configure your own Language Server diff --git a/init.lua b/init.lua index 7d82bcb..1d848d6 100644 --- a/init.lua +++ b/init.lua @@ -567,16 +567,39 @@ local function vis_apply_workspaceEdit(_, _, workspaceEdit) end end -local function lspc_highlight_diagnostics(win, diagnostics) +local function lspc_highlight_diagnostics(win, diagnostics, style) + if not style then + style = lspc.diagnostic_style_id + end + for _, diagnostic in ipairs(diagnostics) do local range = diagnostic.vis_range - -- make sure to highlight only ranges which actually contain the diagnostic - if diagnostic.content == win.file:content(range) then - win:style(lspc.diagnostic_style_id, range.start, range.finish - 1) + + if lspc.highlight_diagnostics == 'range' then + -- 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) + end + + elseif lspc.highlight_diagnostics == 'line' then + if not win.style_lineno then + lspc_err('Vis build does not support style_lineno') + return + end + + local start_line = range.start.line + 1 + local end_line = range['end'].line + 1 + for line = start_line, end_line, 1 do + win:style_lineno(style, line) + end end end end +local function lspc_clear_highlight_diagnostics(win, diagnostics) + lspc_highlight_diagnostics(win, diagnostics, win.STYLE_DEFAULT) +end + -- send a rpc message to a language server local function ls_rpc(ls, req) req.jsonrpc = '2.0' @@ -964,6 +987,9 @@ local function lspc_handle_publish_diagnostics(ls, uri, diagnostics) diagnostic.content = vis.win.file:content(diagnostic.vis_range) end + if file.diagnostics then + lspc_clear_highlight_diagnostics(vis.win, file.diagnostics) + end file.diagnostics = diagnostics lspc.log('remember diagnostics for ' .. file_path) @@ -1499,10 +1525,10 @@ vis.events.subscribe(vis.events.FILE_OPEN, function(file) lspc_open(ls, win, file) end) -vis:option_register('lspc-highlight-diagnostics', 'bool', function(value) +vis:option_register('lspc-highlight-diagnostics', 'string', function(value) lspc.highlight_diagnostics = value return true -end, 'Should lspc highlight diagnostics if available') +end, 'How should lspc highlight available diagnostics') vis:option_register('lspc-menu-cmd', 'string', function(value) lspc.menu_cmd = value -- cgit v1.2.3