local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local clear, nvim, curbuf, curbuf_contents, window, curwin, eq, neq, ok, feed, insert, eval, tabpage =
helpers.clear,
helpers.nvim,
helpers.curbuf,
helpers.curbuf_contents,
helpers.window,
helpers.curwin,
helpers.eq,
helpers.neq,
helpers.ok,
helpers.feed,
helpers.insert,
helpers.eval,
helpers.tabpage
local poke_eventloop = helpers.poke_eventloop
local curwinmeths = helpers.curwinmeths
local exec = helpers.exec
local funcs = helpers.funcs
local request = helpers.request
local NIL = helpers.NIL
local meths = helpers.meths
local command = helpers.command
local pcall_err = helpers.pcall_err
local assert_alive = helpers.assert_alive
describe('API/win', function()
before_each(clear)
describe('get_buf', function()
it('works', function()
eq(curbuf(), window('get_buf', nvim('list_wins')[1]))
nvim('command', 'new')
nvim('set_current_win', nvim('list_wins')[2])
eq(curbuf(), window('get_buf', nvim('list_wins')[2]))
neq(window('get_buf', nvim('list_wins')[1]), window('get_buf', nvim('list_wins')[2]))
end)
end)
describe('set_buf', function()
it('works', function()
nvim('command', 'new')
local windows = nvim('list_wins')
neq(window('get_buf', windows[2]), window('get_buf', windows[1]))
window('set_buf', windows[2], window('get_buf', windows[1]))
eq(window('get_buf', windows[2]), window('get_buf', windows[1]))
end)
it('validates args', function()
eq('Invalid buffer id: 23', pcall_err(window, 'set_buf', nvim('get_current_win'), 23))
eq('Invalid window id: 23', pcall_err(window, 'set_buf', 23, nvim('get_current_buf')))
end)
it('disallowed in cmdwin if win={old_}curwin or buf=curbuf', function()
local new_buf = meths.create_buf(true, true)
local old_win = meths.get_current_win()
local new_win = meths.open_win(new_buf, false, {
relative = 'editor',
row = 10,
col = 10,
width = 50,
height = 10,
})
feed('q:')
eq(
'E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_set_buf, 0, new_buf)
)
eq(
'E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_set_buf, old_win, new_buf)
)
eq(
'E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_set_buf, new_win, 0)
)
local next_buf = meths.create_buf(true, true)
meths.win_set_buf(new_win, next_buf)
eq(next_buf, meths.win_get_buf(new_win))
end)
end)
describe('{get,set}_cursor', function()
it('works', function()
eq({ 1, 0 }, curwin('get_cursor'))
nvim('command', 'normal ityping\027o some text')
eq('typing\n some text', curbuf_contents())
eq({ 2, 10 }, curwin('get_cursor'))
curwin('set_cursor', { 2, 6 })
nvim('command', 'normal i dumb')
eq('typing\n some dumb text', curbuf_contents())
end)
it('does not leak memory when using invalid window ID with invalid pos', function()
eq('Invalid window id: 1', pcall_err(meths.win_set_cursor, 1, { 'b\na' }))
end)
it('updates the screen, and also when the window is unfocused', function()
local screen = Screen.new(30, 9)
screen:set_default_attr_ids({
[1] = { bold = true, foreground = Screen.colors.Blue },
[2] = { bold = true, reverse = true },
[3] = { reverse = true },
})
screen:attach()
insert('prologue')
feed('100o<esc>')
insert('epilogue')
local win = curwin()
feed('gg')
screen:expect {
grid = [[
^prologue |
|*8
]],
}
-- cursor position is at beginning
eq({ 1, 0 }, window('
|