Skip to content

Commit

Permalink
fix(diagnostic): safer place_sign (#405)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Boris Churzin <[email protected]>
  • Loading branch information
devenv and Boris Churzin authored Jun 9, 2024
1 parent 2149a76 commit f30bab1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
40 changes: 25 additions & 15 deletions lua/neotest/consumers/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,45 @@ local function init(client)
if not status then
return
end
if config.status.signs then
nio.fn.sign_place(0, sign_group, "neotest_" .. status, pos.path, {
lnum = pos.range[1] + 1,
priority = 1000,
})
if config.status.signs and nio.api.nvim_buf_is_valid(buf) and nio.fn.buflisted(buf) ~= 0 then
local line_count = vim.api.nvim_buf_line_count(buf)
local line_number = pos.range[1] + 1
if line_number <= line_count then
nio.fn.sign_place(0, sign_group, "neotest_" .. status, buf, {
lnum = line_number,
priority = 1000,
})
end
end
if config.status.virtual_text then
nio.api.nvim_buf_set_extmark(buf, namespace, pos.range[1], 0, {
virt_text = {
{ statuses[status].text .. " ", statuses[status].texthl },
},
})
if config.status.virtual_text and nio.api.nvim_buf_is_valid(buf) then
local line_count = vim.api.nvim_buf_line_count(buf)
local line_number = pos.range[1]
if line_number < line_count then
nio.api.nvim_buf_set_extmark(buf, namespace, line_number, 0, {
virt_text_pos = "eol",
virt_text = {
{ statuses[status].text .. " ", statuses[status].texthl },
},
})
end
end
end

local function render_files(adapter_id, files)
for _, file_path in pairs(files) do
if nio.fn.buflisted(nio.fn.bufnr(file_path)) ~= 0 then
local bufnr = nio.fn.bufnr(file_path)
if nio.fn.buflisted(bufnr) ~= 0 and nio.api.nvim_buf_is_valid(bufnr) then
local results = client:get_results(adapter_id)
nio.fn.sign_unplace(sign_group, { buffer = file_path })
nio.api.nvim_buf_clear_namespace(nio.fn.bufnr(file_path), namespace, 0, -1)
nio.fn.sign_unplace(sign_group, { buffer = bufnr })
nio.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)
local tree = client:get_position(file_path, { adapter = adapter_id })
if not tree then
return
end
for _, node in tree:iter_nodes() do
local pos = node:data()
if pos.range ~= nil and pos.type ~= "file" then
place_sign(nio.fn.bufnr(file_path), pos, adapter_id, results)
place_sign(bufnr, pos, adapter_id, results)
end
end
end
Expand Down
16 changes: 12 additions & 4 deletions lua/neotest/lib/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ neotest.lib.ui.open_buf = function(bufnr, line, column)
if line then
api.nvim_win_set_cursor(win, { line + 1, column })
end
api.nvim_set_current_win(win)
if api.nvim_win_is_valid(win) then
api.nvim_set_current_win(win)
else
print("Attempted to set an invalid window as current.")
end
end

for _, win in pairs(api.nvim_tabpage_list_wins(0)) do
Expand All @@ -106,10 +110,14 @@ neotest.lib.ui.open_buf = function(bufnr, line, column)

local success, win = pcall(select_win)
if not success or not win then
print("Failed to select a window for buffer: " .. bufnr)
return
end
api.nvim_win_set_buf(win, bufnr)
set_win_pos(win)
if api.nvim_buf_is_valid(bufnr) and api.nvim_win_is_valid(win) then
api.nvim_win_set_buf(win, bufnr)
set_win_pos(win)
else
print("Buffer or window is not valid.")
end
end

return neotest.lib.ui

0 comments on commit f30bab1

Please sign in to comment.