local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
local tt = require('test.functional.testterm')
local describe, it, before_each, setup, teardown =
t.describe, t.it, t.before_each, t.setup, t.teardown
local clear, eq, neq = n.clear, t.eq, t.neq
local feed, testprg = n.feed, n.testprg
local fn = n.fn
local eval = n.eval
local command = n.command
local poke_eventloop = n.poke_eventloop
local retry = t.retry
local api = n.api
local feed_data = tt.feed_data
local pcall_err = t.pcall_err
local exec_lua = n.exec_lua
local assert_alive = n.assert_alive
local skip = t.skip
local is_os = t.is_os
local function test_terminal_scrollback(hide_curbuf)
local screen --- @type test.functional.ui.screen
local buf --- @type integer
local chan --- @type integer
local otherbuf --- @type integer
local restore_terminal_mode --- @type boolean?
local save_feed_data = feed_data
local function may_hide_curbuf()
if hide_curbuf then
eq(nil, restore_terminal_mode)
restore_terminal_mode = vim.startswith(api.nvim_get_mode().mode, 't')
api.nvim_set_current_buf(otherbuf)
end
end
local function may_restore_curbuf()
if hide_curbuf then
neq(nil, restore_terminal_mode)
eq(buf, fn.bufnr('#'))
feed('<C-^>') -- "view" in 'jumpoptions' applies to this
if restore_terminal_mode then
feed('i')
else
-- Cursor position was restored from wi_mark, not b_last_cursor.
-- Check that b_last_cursor and wi_mark are the same.
--- @type integer[], integer[]
local last_cursor, restored_cursor = unpack(exec_lua(function()
-- Get these two positions on the same RPC call.
return { vim.fn.getpos([['"]]), vim.fn.getpos('.') }
end))
if last_cursor[2] > 0 then
eq(restored_cursor, last_cursor)
else
eq({ 0, 0, 0, 0 }, last_cursor)
eq({ 0, 1, 1, 0 }, restored_cursor)
end
end
restore_terminal_mode = nil
end
end
setup(function()
feed_data = function(data)
may_hide_curbuf()
api.nvim_chan_send(chan, data)
may_restore_curbuf()
end
end)
teardown(function()
feed_data = save_feed_data
end)
--- @param prefix string
--- @param start integer
--- @param stop integer
local function feed_lines(prefix, start, stop)
may_hide_curbuf()
local data = ''
for i = start, stop do
data = data .. prefix .. tostring(i) .. '\n'
end
api.nvim_chan_send(chan, data)
retry(nil, 1000, function()
eq({ prefix .. tostring(stop), '' }, api.nvim_buf_get_lines(buf, -3, -1, true))
end)
may_restore_curbuf()
end
local function try_resize(width, height)
may_hide_curbuf()
screen:try_resize(width, height)
may_restore_curbuf()
end
before_each(function()
clear()
command('set nostartofline jumpoptions+=view')
screen = tt.setup_screen(nil, nil, 30)
buf = api.nvim_get_current_buf()
chan =
|