fix(diagnostic): handle diagnostics placed past the end of line (#16095)

This commit is contained in:
Gregory Anders 2021-10-19 16:27:49 -06:00 committed by GitHub
parent 208d259e83
commit a2994c82e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -1146,8 +1146,12 @@ function M.open_float(bufnr, opts)
return d.lnum == lnum return d.lnum == lnum
end, diagnostics) end, diagnostics)
elseif scope == "cursor" then elseif scope == "cursor" then
-- LSP servers can send diagnostics with `end_col` past the length of the line
local line_length = #vim.api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1]
diagnostics = vim.tbl_filter(function(d) diagnostics = vim.tbl_filter(function(d)
return d.lnum == lnum and d.col <= col and (d.end_col >= col or d.end_lnum > lnum) return d.lnum == lnum
and math.min(d.col, line_length - 1) <= col
and (d.end_col >= col or d.end_lnum > lnum)
end, diagnostics) end, diagnostics)
end end

View File

@ -1046,6 +1046,21 @@ describe('vim.diagnostic', function()
vim.api.nvim_win_close(winnr, true) vim.api.nvim_win_close(winnr, true)
return lines return lines
]]) ]])
-- With column position past the end of the line. #16062
eq({'1. Syntax error'}, exec_lua [[
local first_line_len = #vim.api.nvim_buf_get_lines(diagnostic_bufnr, 0, 1, true)[1]
local diagnostics = {
make_error("Syntax error", 0, first_line_len + 1, 1, 0),
}
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
vim.api.nvim_win_set_cursor(0, {1, 1})
local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor", pos={0,first_line_len}})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
]])
end) end)
it('creates floating window and returns float bufnr and winnr if current line contains diagnostics', function() it('creates floating window and returns float bufnr and winnr if current line contains diagnostics', function()