From e9a40aca40b73d9f278757e1180152191ca9bcab Mon Sep 17 00:00:00 2001 From: ray-x Date: Mon, 29 Aug 2022 14:28:01 +1000 Subject: [PATCH] diagnostic: show line diagnostic: offset the floating windows to first error pos --- lua/navigator/diagnostics.lua | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/lua/navigator/diagnostics.lua b/lua/navigator/diagnostics.lua index 7ea659b..019ea89 100644 --- a/lua/navigator/diagnostics.lua +++ b/lua/navigator/diagnostics.lua @@ -464,24 +464,38 @@ end function M.get_line_diagnostic() local lnum = api.nvim_win_get_cursor(0)[1] - 1 - return diagnostic.get(api.nvim_get_current_buf(), { lnum = lnum }) + local diags = diagnostic.get(api.nvim_get_current_buf(), { lnum = lnum }) + + table.sort(diags, function(diag1, diag2) + return diag1.severity < diag2.severity + end) + return diags end function M.show_diagnostics(pos) local bufnr = api.nvim_get_current_buf() - local lnum = api.nvim_win_get_cursor(0)[1] - 1 - local opt = { border = 'single' } - if diagnostic.open_float and type(diagnostic.open_float) == 'function' then + + local lnum, col = unpack(api.nvim_win_get_cursor(0)) + lnum = lnum - 1 + local opt = { border = 'single', severity_sort = true } + + if pos ~= nil and type(pos) == 'number' then + opt.scope = 'buffer' + else if pos == true then opt.scope = 'cursor' else opt.scope = 'line' end - diagnostic.open_float(bufnr, opt) - else - -- deprecated - diagnostic.show_line_diagnostics(opt, bufnr, lnum) end + + local diags = M.get_line_diagnostic() + if diags == nil or next(diags) == nil then + return + end + local diag1 = diags[1] + opt.offset_x = -1 * (col - diag1.col) + diagnostic.open_float(bufnr, opt) end function M.treesitter_and_diag_panel()