save and restore current cursor position

This commit is contained in:
2026-01-17 15:01:33 +01:00
parent fd3952bedf
commit 24bd89a7be
4 changed files with 165 additions and 7 deletions

View File

@@ -23,6 +23,9 @@ local state = {
detail_win = nil,
detail_opening = false,
detail_full = false,
trigger_win = nil,
trigger_buf = nil,
trigger_cursor = nil,
listing_unfiltered_lines = nil,
listing_filtered_kind = nil,
hardtime_refcount = 0,
@@ -71,7 +74,7 @@ local function help_lines()
"",
"Testing-Float (Listing):",
" <cr> Open Detail-Float for selected test",
" <esc><esc> Close Testing-Float",
" <esc><esc> Close Testing-Float and restore cursor",
" <C-l> Focus Detail-Float (press l again for full)",
" <C-h> Focus Test-Listing-Float",
" <leader>z Toggle Detail-Float full width",
@@ -81,7 +84,7 @@ local function help_lines()
" ? Show this help",
"",
"Testing-Float (Detail):",
" <esc><esc> Close Testing-Float",
" <esc><esc> Close Testing-Float and restore cursor",
" <C-h> Focus Test-Listing-Float",
" <C-w>h Focus Test-Listing-Float",
" <C-l> Focus Detail-Float",
@@ -337,6 +340,54 @@ local function find_normal_window()
return nil
end
local function capture_trigger_location()
local win = vim.api.nvim_get_current_win()
local cfg = vim.api.nvim_win_get_config(win)
if cfg.relative ~= "" then
win = find_normal_window()
end
if not (win and vim.api.nvim_win_is_valid(win)) then
return
end
local buf = vim.api.nvim_win_get_buf(win)
if not (buf and vim.api.nvim_buf_is_valid(buf)) then
return
end
local cursor = vim.api.nvim_win_get_cursor(win)
state.trigger_win = win
state.trigger_buf = buf
state.trigger_cursor = { cursor[1], cursor[2] }
end
local function restore_trigger_location()
local buf = state.trigger_buf
local cursor = state.trigger_cursor
if not (buf and vim.api.nvim_buf_is_valid(buf)) then
return
end
local target_win = nil
if state.trigger_win and vim.api.nvim_win_is_valid(state.trigger_win) then
local cfg = vim.api.nvim_win_get_config(state.trigger_win)
if cfg.relative == "" then
target_win = state.trigger_win
end
end
if not target_win then
target_win = find_normal_window()
end
if not (target_win and vim.api.nvim_win_is_valid(target_win)) then
return
end
vim.api.nvim_set_current_win(target_win)
vim.api.nvim_win_set_buf(target_win, buf)
if cursor then
pcall(vim.api.nvim_win_set_cursor, target_win, cursor)
end
state.trigger_win = nil
state.trigger_buf = nil
state.trigger_cursor = nil
end
local function jump_to_listing_test()
local cursor = vim.api.nvim_win_get_cursor(0)
local line = cursor[1]
@@ -643,6 +694,11 @@ close_container = function()
end
end
local function close_container_and_restore()
close_container()
restore_trigger_location()
end
jump_to_first_quickfix = function()
close_container()
local info = vim.fn.getqflist({ size = 0 })
@@ -728,6 +784,7 @@ local function apply_split_layout(left_ratio)
end
local function create_output_win(initial_lines)
capture_trigger_location()
if state.detail_win and vim.api.nvim_win_is_valid(state.detail_win) then
pcall(vim.api.nvim_win_close, state.detail_win, true)
state.detail_win = nil
@@ -762,7 +819,7 @@ local function create_output_win(initial_lines)
hardtime_disable()
vim.keymap.set("n", "<esc><esc>", function()
close_container()
close_container_and_restore()
end, { buffer = buf, nowait = true, silent = true })
vim.keymap.set("n", "<cr>", function()
M.open_test_output_at_cursor()
@@ -832,6 +889,7 @@ local function reopen_output_win()
state.detail_win = nil
end
capture_trigger_location()
local width, height, row, col = float_geometry()
state.last_float = { width = width, height = height, row = row, col = col }
@@ -847,7 +905,7 @@ local function reopen_output_win()
hardtime_disable()
vim.keymap.set("n", "<esc><esc>", function()
close_container()
close_container_and_restore()
end, { buffer = state.last_buf, nowait = true, silent = true })
vim.keymap.set("n", "<cr>", function()
M.open_test_output_at_cursor()
@@ -1189,7 +1247,7 @@ local function ensure_detail_buf(lines)
vim.api.nvim_buf_set_option(buf, "filetype", "test-samurai-output")
state.detail_buf = buf
vim.keymap.set("n", "<esc><esc>", function()
close_container()
close_container_and_restore()
end, { buffer = buf, nowait = true, silent = true })
vim.keymap.set("n", "<C-w>h", function()
M.focus_listing()
@@ -2393,6 +2451,10 @@ function M.run_failed_only()
})
end
function M.close_output_and_restore()
close_container_and_restore()
end
function M.show_output()
if not (state.last_buf and vim.api.nvim_buf_is_valid(state.last_buf)) then
vim.notify("[test-samurai] No previous output", vim.log.levels.WARN)