Skip to content

Commit

Permalink
fix(diagnostic): handle diagnostics placed past the end of line (#16095)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpanders committed Oct 19, 2021
1 parent 208d259 commit a2994c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion runtime/lua/vim/diagnostic.lua
Expand Up @@ -1146,8 +1146,12 @@ function M.open_float(bufnr, opts)
return d.lnum == lnum
end, diagnostics)
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)
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

Expand Down
15 changes: 15 additions & 0 deletions test/functional/lua/diagnostic_spec.lua
Expand Up @@ -1046,6 +1046,21 @@ describe('vim.diagnostic', function()
vim.api.nvim_win_close(winnr, true)
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)

it('creates floating window and returns float bufnr and winnr if current line contains diagnostics', function()
Expand Down

0 comments on commit a2994c8

Please sign in to comment.