aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Toerien <49614525+AThePeanut4@users.noreply.github.com>2024-06-04 23:35:44 +0200
committerGitHub <noreply@github.com>2024-06-04 16:35:44 -0500
commitf69937fdbd162630c35e119e67bbbf052558c0e0 (patch)
tree7a61a1a9fe77f73e094c661afe8576bce6ad60ab
parent946a839aa9621e11f53e7044e594132d0497b99f (diff)
downloadrneovim-f69937fdbd162630c35e119e67bbbf052558c0e0.tar.gz
rneovim-f69937fdbd162630c35e119e67bbbf052558c0e0.tar.bz2
rneovim-f69937fdbd162630c35e119e67bbbf052558c0e0.zip
fix(diagnostic): fix float scope filtering (#29134)
-rw-r--r--runtime/lua/vim/diagnostic.lua13
-rw-r--r--test/functional/lua/diagnostic_spec.lua114
2 files changed, 115 insertions, 12 deletions
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
index c6649ae09b..9342727b2e 100644
--- a/runtime/lua/vim/diagnostic.lua
+++ b/runtime/lua/vim/diagnostic.lua
@@ -1857,16 +1857,19 @@ function M.open_float(opts, ...)
if scope == 'line' then
--- @param d vim.Diagnostic
diagnostics = vim.tbl_filter(function(d)
- return lnum >= d.lnum and lnum <= d.end_lnum
+ return lnum >= d.lnum
+ and lnum <= d.end_lnum
+ and (d.lnum == d.end_lnum or lnum ~= d.end_lnum or d.end_col ~= 0)
end, diagnostics)
elseif scope == 'cursor' then
- -- LSP servers can send diagnostics with `end_col` past the length of the line
+ -- If `col` is past the end of the line, show if the cursor is on the last char in the line
local line_length = #api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1]
--- @param d vim.Diagnostic
diagnostics = vim.tbl_filter(function(d)
- return d.lnum == lnum
- and math.min(d.col, line_length - 1) <= col
- and (d.end_col >= col or d.end_lnum > lnum)
+ return lnum >= d.lnum
+ and lnum <= d.end_lnum
+ and (lnum ~= d.lnum or col >= math.min(d.col, line_length - 1))
+ and ((d.lnum == d.end_lnum and d.col == d.end_col) or lnum ~= d.end_lnum or col < d.end_col)
end, diagnostics)
end
diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua
index a4f882e363..decb58dc4d 100644
--- a/test/functional/lua/diagnostic_spec.lua
+++ b/test/functional/lua/diagnostic_spec.lua
@@ -2291,6 +2291,38 @@ describe('vim.diagnostic', function()
return lines
]]
)
+
+ -- End position is exclusive
+ eq(
+ vim.NIL,
+ exec_lua [[
+ local diagnostics = {
+ make_error("Syntax error", 1, 1, 2, 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 _, winnr = vim.diagnostic.open_float(0, {header=false, pos={2,0}})
+ return winnr
+ ]]
+ )
+
+ -- Works when width == 0
+ eq(
+ { '1. Syntax error' },
+ exec_lua [[
+ local diagnostics = {
+ make_error("Syntax error", 2, 0, 2, 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, {header=false, pos={2,1}})
+ local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
+ vim.api.nvim_win_close(winnr, true)
+ return lines
+ ]]
+ )
end)
it('can show diagnostics from a specific position', function()
@@ -2299,7 +2331,7 @@ describe('vim.diagnostic', function()
{ 'Syntax error' },
exec_lua [[
local diagnostics = {
- make_error("Syntax error", 1, 1, 1, 2),
+ make_error("Syntax error", 1, 1, 1, 3),
make_warning("Some warning", 1, 3, 1, 4),
}
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
@@ -2317,7 +2349,7 @@ describe('vim.diagnostic', function()
{ 'Some warning' },
exec_lua [[
local diagnostics = {
- make_error("Syntax error", 1, 1, 1, 2),
+ make_error("Syntax error", 1, 1, 1, 3),
make_warning("Some warning", 1, 3, 1, 4),
}
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
@@ -2347,6 +2379,38 @@ describe('vim.diagnostic', function()
return lines
]]
)
+
+ -- End position is exclusive
+ eq(
+ vim.NIL,
+ exec_lua [[
+ local diagnostics = {
+ make_error("Syntax error", 1, 1, 1, 3),
+ }
+ 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 _, winnr = vim.diagnostic.open_float(0, {header=false, scope="cursor", pos={1,3}})
+ return winnr
+ ]]
+ )
+
+ -- Works when width == 0
+ eq(
+ { 'Syntax error' },
+ exec_lua [[
+ local diagnostics = {
+ make_error("Syntax error", 2, 0, 2, 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({header=false, scope="cursor", pos={2,1}})
+ local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
+ vim.api.nvim_win_close(winnr, true)
+ return lines
+ ]]
+ )
end)
it(
@@ -2755,20 +2819,32 @@ describe('vim.diagnostic', function()
end)
it('works for multi-line diagnostics #21949', function()
- -- open float failed non diagnostic lnum
- eq(
- vim.NIL,
- exec_lua [[
+ -- create diagnostic
+ exec_lua [[
local diagnostics = {
make_error("Error in two lines lnum is 1 and end_lnum is 2", 1, 1, 2, 3),
}
- local winids = {}
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
+ ]]
+
+ -- open float failed non diagnostic lnum
+ eq(
+ vim.NIL,
+ exec_lua [[
+ vim.api.nvim_win_set_cursor(0, {1, 0})
local _, winnr = vim.diagnostic.open_float(0, { header = false })
return winnr
]]
)
+ eq(
+ vim.NIL,
+ exec_lua [[
+ vim.api.nvim_win_set_cursor(0, {1, 0})
+ local _, winnr = vim.diagnostic.open_float(0, { header = false, scope = "cursor" })
+ return winnr
+ ]]
+ )
-- can open a float window on lnum 1
eq(
@@ -2782,6 +2858,18 @@ describe('vim.diagnostic', function()
]]
)
+ -- can open a cursor-scoped float window on lnum 1
+ eq(
+ { 'Error in two lines lnum is 1 and end_lnum is 2' },
+ exec_lua [[
+ vim.api.nvim_win_set_cursor(0, {2, 1})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false, scope = "cursor" })
+ local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
+ vim.api.nvim_win_close(winnr, true)
+ return lines
+ ]]
+ )
+
-- can open a float window on end_lnum 2
eq(
{ '1. Error in two lines lnum is 1 and end_lnum is 2' },
@@ -2793,6 +2881,18 @@ describe('vim.diagnostic', function()
return lines
]]
)
+
+ -- can open a cursor-scoped float window on end_lnum 2
+ eq(
+ { 'Error in two lines lnum is 1 and end_lnum is 2' },
+ exec_lua [[
+ vim.api.nvim_win_set_cursor(0, {3, 2})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false, scope = "cursor" })
+ local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
+ vim.api.nvim_win_close(winnr, true)
+ return lines
+ ]]
+ )
end)
end)