From 29ce932edc63b1342906f65c56a9d3a0d4535f38 Mon Sep 17 00:00:00 2001 From: el Date: Fri, 24 Jul 2020 17:32:33 +0800 Subject: [PATCH] Ensure that diagnostic character-positions are > 0 Some language clients will set the diagnostic.range.start.character = -1 for particular sorts of issues, which causes an "index-out-of-bounds" error when setting virtual text. This fix makes sure all characters start / end positions are are (at least) 0. --- lua/diagnostic.lua | 1 + lua/diagnostic/util.lua | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/lua/diagnostic.lua b/lua/diagnostic.lua index 3bf0f0e..2ff67af 100644 --- a/lua/diagnostic.lua +++ b/lua/diagnostic.lua @@ -74,6 +74,7 @@ function M.publish_diagnostics(bufnr) if #vim.lsp.buf_get_clients() == 0 then return end local diagnostics = vim.lsp.util.diagnostics_by_buf[bufnr] if diagnostics == nil then return end + util.align_diagnostic_indices(diagnostics) vim.fn.setloclist(0, {}, 'r') if vim.api.nvim_get_var('diagnostic_enable_underline') == 1 then vim.lsp.util.buf_diagnostics_underline(bufnr, diagnostics) diff --git a/lua/diagnostic/util.lua b/lua/diagnostic/util.lua index 334194f..cd6a7fb 100644 --- a/lua/diagnostic/util.lua +++ b/lua/diagnostic/util.lua @@ -179,4 +179,11 @@ function M.buf_diagnostics_signs(bufnr, diagnostics) end end +function M.align_diagnostic_indices(diagnostics) + for idx, diagnostic in ipairs(diagnostics) do + if diagnostic.range.start.character < 0 then diagnostic.range.start.character = 0 end + if diagnostic.range['end'].character < 0 then diagnostic.range['end'].character = 0 end + end +end + return M