local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local clear, curbuf, curbuf_contents, curwin, eq, neq, ok, feed, insert, eval =
helpers.clear,
helpers.api.nvim_get_current_buf,
helpers.curbuf_contents,
helpers.api.nvim_get_current_win,
helpers.eq,
helpers.neq,
helpers.ok,
helpers.feed,
helpers.insert,
helpers.eval
local poke_eventloop = helpers.poke_eventloop
local exec = helpers.exec
local fn = helpers.fn
local request = helpers.request
local NIL = vim.NIL
local api = helpers.api
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(), api.nvim_win_get_buf(api.nvim_list_wins()[1]))
command('new')
api.nvim_set_current_win(api.nvim_list_wins()[2])
eq(curbuf(), api.nvim_win_get_buf(api.nvim_list_wins()[2]))
neq(
api.nvim_win_get_buf(api.nvim_list_wins()[1]),
api.nvim_win_get_buf(api.nvim_list_wins()[2])
)
end)
end)
describe('set_buf', function()
it('works', function()
command('new')
local windows = api.nvim_list_wins()
neq(api.nvim_win_get_buf(windows[2]), api.nvim_win_get_buf(windows[1]))
api.nvim_win_set_buf(windows[2], api.nvim_win_get_buf(windows[1]))
eq(api.nvim_win_get_buf(windows[2]), api.nvim_win_get_buf(windows[1]))
end)
it('validates args', function()
eq('Invalid buffer id: 23', pcall_err(api.nvim_win_set_buf, api.nvim_get_current_win(), 23))
eq('Invalid window id: 23', pcall_err(api.nvim_win_set_buf, 23, api.nvim_get_current_buf()))
end)
it('disallowed in cmdwin if win={old_}curwin or buf=curbuf', function()
local new_buf = api.nvim_create_buf(true, true)
local old_win = api.nvim_get_current_win()
local new_win = api.nvim_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(api.nvim_win_set_buf, 0, new_buf)
)
eq(
'E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(api.nvim_win_set_buf, old_win, new_buf)
)
eq(
'E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(api.nvim_win_set_buf, new_win, 0)
)
local next_buf = api.nvim_create_buf(true, true)
api.nvim_win_set_buf(new_win, next_buf)
eq(next_buf, api.nvim_win_get_buf(new_win))
end)
end)
describe('{get,set}_cursor', function()
it('works', function()
eq({ 1, 0 }, api.nvim_win_get_cursor(0))
command('normal ityping\027o some text')
eq('typing\n some text', curbuf_contents())
eq({ 2, 10 }, api.nvim_win_get_cursor(0))
api.nvim_win_set_cursor(0, { 2, 6 })
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(api.nvim_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 }, api.nvim_win_get_cursor(win))
-- move cursor to end
api.nvim_win_set_cursor(win, { 101
|