diff options
| author | Josh Rahm <joshuarahm@gmail.com> | 2024-03-09 14:57:57 -0700 |
|---|---|---|
| committer | Josh Rahm <joshuarahm@gmail.com> | 2024-03-09 14:57:57 -0700 |
| commit | c324271b99eee4c621463f368914d57cd729bd9c (patch) | |
| tree | 5d979d333a2d5f9c080991d5482fd5916f8579c6 /test/functional/api/window_spec.lua | |
| parent | 931bffbda3668ddc609fc1da8f9eb576b170aa52 (diff) | |
| parent | ade1b12f49c3b3914c74847d791eb90ea90b56b7 (diff) | |
| download | rneovim-c324271b99eee4c621463f368914d57cd729bd9c.tar.gz rneovim-c324271b99eee4c621463f368914d57cd729bd9c.tar.bz2 rneovim-c324271b99eee4c621463f368914d57cd729bd9c.zip | |
Merge remote-tracking branch 'upstream/master' into userreg
Diffstat (limited to 'test/functional/api/window_spec.lua')
| -rw-r--r-- | test/functional/api/window_spec.lua | 1908 |
1 files changed, 1441 insertions, 467 deletions
diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua index 6737c2d15b..097a546ef2 100644 --- a/test/functional/api/window_spec.lua +++ b/test/functional/api/window_spec.lua @@ -1,17 +1,24 @@ 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 clear, curbuf, curbuf_contents, curwin, eq, neq, matches, 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.matches, + helpers.ok, + helpers.feed, + helpers.insert, + helpers.eval local poke_eventloop = helpers.poke_eventloop -local curwinmeths = helpers.curwinmeths local exec = helpers.exec -local funcs = helpers.funcs +local exec_lua = helpers.exec_lua +local fn = helpers.fn local request = helpers.request -local NIL = helpers.NIL -local meths = helpers.meths +local NIL = vim.NIL +local api = helpers.api local command = helpers.command local pcall_err = helpers.pcall_err local assert_alive = helpers.assert_alive @@ -21,201 +28,229 @@ describe('API/win', function() 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])) + 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() - 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])) + 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(window, 'set_buf', nvim('get_current_win'), 23)) - eq('Invalid window id: 23', pcall_err(window, 'set_buf', 23, nvim('get_current_buf'))) + 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 = 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, + it('disallowed in cmdwin if win=cmdwin_{old_cur}win or buf=cmdwin_buf', 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(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)) + 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) + ) + matches( + 'E11: Invalid in command%-line window; <CR> executes, CTRL%-C quits$', + pcall_err( + exec_lua, + [[ + local cmdwin_buf = vim.api.nvim_get_current_buf() + local new_win, new_buf = ... + vim.api.nvim_buf_call(new_buf, function() + vim.api.nvim_win_set_buf(new_win, cmdwin_buf) + end) + ]], + new_win, + new_buf + ) + ) + matches( + 'E11: Invalid in command%-line window; <CR> executes, CTRL%-C quits$', + pcall_err( + exec_lua, + [[ + local cmdwin_win = vim.api.nvim_get_current_win() + local new_win, new_buf = ... + vim.api.nvim_win_call(new_win, function() + vim.api.nvim_win_set_buf(cmdwin_win, new_buf) + end) + ]], + new_win, + new_buf + ) + ) + + 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}, curwin('get_cursor')) - nvim('command', 'normal ityping\027o some text') + 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}, curwin('get_cursor')) - curwin('set_cursor', {2, 6}) - nvim('command', 'normal i dumb') + 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(meths.win_set_cursor, 1, {"b\na"})) + 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}; + [1] = { bold = true, foreground = Screen.colors.Blue }, + [2] = { bold = true, reverse = true }, + [3] = { reverse = true }, }) screen:attach() - insert("prologue") + insert('prologue') feed('100o<esc>') - insert("epilogue") + insert('epilogue') local win = curwin() feed('gg') - screen:expect{grid=[[ + screen:expect { + grid = [[ ^prologue | - | - | - | - | - | - | - | - | - ]]} + |*8 + ]], + } -- cursor position is at beginning - eq({1, 0}, window('get_cursor', win)) + eq({ 1, 0 }, api.nvim_win_get_cursor(win)) -- move cursor to end - window('set_cursor', win, {101, 0}) - screen:expect{grid=[[ - | - | - | - | - | - | - | + api.nvim_win_set_cursor(win, { 101, 0 }) + screen:expect { + grid = [[ + |*7 ^epilogue | | - ]]} + ]], + } -- move cursor to the beginning again - window('set_cursor', win, {1, 0}) - screen:expect{grid=[[ + api.nvim_win_set_cursor(win, { 1, 0 }) + screen:expect { + grid = [[ ^prologue | - | - | - | - | - | - | - | - | - ]]} + |*8 + ]], + } -- move focus to new window - nvim('command',"new") + command('new') neq(win, curwin()) -- sanity check, cursor position is kept - eq({1, 0}, window('get_cursor', win)) - screen:expect{grid=[[ + eq({ 1, 0 }, api.nvim_win_get_cursor(win)) + screen:expect { + grid = [[ ^ | - {1:~ }| - {1:~ }| + {1:~ }|*2 {2:[No Name] }| prologue | - | - | + |*2 {3:[No Name] [+] }| | - ]]} + ]], + } -- move cursor to end - window('set_cursor', win, {101, 0}) - screen:expect{grid=[[ + api.nvim_win_set_cursor(win, { 101, 0 }) + screen:expect { + grid = [[ ^ | - {1:~ }| - {1:~ }| + {1:~ }|*2 {2:[No Name] }| - | - | + |*2 epilogue | {3:[No Name] [+] }| | - ]]} + ]], + } -- move cursor to the beginning again - window('set_cursor', win, {1, 0}) - screen:expect{grid=[[ + api.nvim_win_set_cursor(win, { 1, 0 }) + screen:expect { + grid = [[ ^ | - {1:~ }| - {1:~ }| + {1:~ }|*2 {2:[No Name] }| prologue | - | - | + |*2 {3:[No Name] [+] }| | - ]]} + ]], + } -- curwin didn't change back neq(win, curwin()) end) it('remembers what column it wants to be in', function() - insert("first line") + insert('first line') feed('o<esc>') - insert("second line") + insert('second line') feed('gg') poke_eventloop() -- let nvim process the 'gg' command -- cursor position is at beginning local win = curwin() - eq({1, 0}, window('get_cursor', win)) + eq({ 1, 0 }, api.nvim_win_get_cursor(win)) -- move cursor to column 5 - window('set_cursor', win, {1, 5}) + api.nvim_win_set_cursor(win, { 1, 5 }) -- move down a line feed('j') poke_eventloop() -- let nvim process the 'j' command -- cursor is still in column 5 - eq({2, 5}, window('get_cursor', win)) + eq({ 2, 5 }, api.nvim_win_get_cursor(win)) end) it('updates cursorline and statusline ruler in non-current window', function() local screen = Screen.new(60, 8) screen:set_default_attr_ids({ - [1] = {bold = true, foreground = Screen.colors.Blue}, -- NonText - [2] = {background = Screen.colors.Grey90}, -- CursorLine - [3] = {bold = true, reverse = true}, -- StatusLine - [4] = {reverse = true}, -- StatusLineNC + [1] = { bold = true, foreground = Screen.colors.Blue }, -- NonText + [2] = { background = Screen.colors.Grey90 }, -- CursorLine + [3] = { bold = true, reverse = true }, -- StatusLine + [4] = { reverse = true }, -- StatusLineNC }) screen:attach() command('set ruler') @@ -232,19 +267,17 @@ describe('API/win', function() bbb │bbb | ccc │ccc | {2:dd^d }│{2:ddd }| - {1:~ }│{1:~ }| - {1:~ }│{1:~ }| + {1:~ }│{1:~ }|*2 {3:[No Name] [+] 4,3 All }{4:[No Name] [+] 4,3 All}| | ]]) - window('set_cursor', oldwin, {1, 0}) + api.nvim_win_set_cursor(oldwin, { 1, 0 }) screen:expect([[ aaa │{2:aaa }| bbb │bbb | ccc │ccc | {2:dd^d }│ddd | - {1:~ }│{1:~ }| - {1:~ }│{1:~ }| + {1:~ }│{1:~ }|*2 {3:[No Name] [+] 4,3 All }{4:[No Name] [+] 1,1 All}| | ]]) @@ -253,10 +286,10 @@ describe('API/win', function() it('updates cursorcolumn in non-current window', function() local screen = Screen.new(60, 8) screen:set_default_attr_ids({ - [1] = {bold = true, foreground = Screen.colors.Blue}, -- NonText - [2] = {background = Screen.colors.Grey90}, -- CursorColumn - [3] = {bold = true, reverse = true}, -- StatusLine - [4] = {reverse = true}, -- StatusLineNC + [1] = { bold = true, foreground = Screen.colors.Blue }, -- NonText + [2] = { background = Screen.colors.Grey90 }, -- CursorColumn + [3] = { bold = true, reverse = true }, -- StatusLine + [4] = { reverse = true }, -- StatusLineNC }) screen:attach() command('set cursorcolumn') @@ -272,19 +305,17 @@ describe('API/win', function() bb{2:b} │bb{2:b} | cc{2:c} │cc{2:c} | dd^d │ddd | - {1:~ }│{1:~ }| - {1:~ }│{1:~ }| + {1:~ }│{1:~ }|*2 {3:[No Name] [+] }{4:[No Name] [+] }| | ]]) - window('set_cursor', oldwin, {2, 0}) + api.nvim_win_set_cursor(oldwin, { 2, 0 }) screen:expect([[ aa{2:a} │{2:a}aa | bb{2:b} │bbb | cc{2:c} │{2:c}cc | dd^d │{2:d}dd | - {1:~ }│{1:~ }| - {1:~ }│{1:~ }| + {1:~ }│{1:~ }|*2 {3:[No Name] [+] }{4:[No Name] [+] }| | ]]) @@ -293,31 +324,35 @@ describe('API/win', function() describe('{get,set}_height', function() it('works', function() - nvim('command', 'vsplit') - eq(window('get_height', nvim('list_wins')[2]), - window('get_height', nvim('list_wins')[1])) - nvim('set_current_win', nvim('list_wins')[2]) - nvim('command', 'split') - eq(window('get_height', nvim('list_wins')[2]), - math.floor(window('get_height', nvim('list_wins')[1]) / 2)) - window('set_height', nvim('list_wins')[2], 2) - eq(2, window('get_height', nvim('list_wins')[2])) + command('vsplit') + eq( + api.nvim_win_get_height(api.nvim_list_wins()[2]), + api.nvim_win_get_height(api.nvim_list_wins()[1]) + ) + api.nvim_set_current_win(api.nvim_list_wins()[2]) + command('split') + eq( + api.nvim_win_get_height(api.nvim_list_wins()[2]), + math.floor(api.nvim_win_get_height(api.nvim_list_wins()[1]) / 2) + ) + api.nvim_win_set_height(api.nvim_list_wins()[2], 2) + eq(2, api.nvim_win_get_height(api.nvim_list_wins()[2])) end) it('correctly handles height=1', function() - nvim('command', 'split') - nvim('set_current_win', nvim('list_wins')[1]) - window('set_height', nvim('list_wins')[2], 1) - eq(1, window('get_height', nvim('list_wins')[2])) + command('split') + api.nvim_set_current_win(api.nvim_list_wins()[1]) + api.nvim_win_set_height(api.nvim_list_wins()[2], 1) + eq(1, api.nvim_win_get_height(api.nvim_list_wins()[2])) end) it('correctly handles height=1 with a winbar', function() - nvim('command', 'set winbar=foobar') - nvim('command', 'set winminheight=0') - nvim('command', 'split') - nvim('set_current_win', nvim('list_wins')[1]) - window('set_height', nvim('list_wins')[2], 1) - eq(1, window('get_height', nvim('list_wins')[2])) + command('set winbar=foobar') + command('set winminheight=0') + command('split') + api.nvim_set_current_win(api.nvim_list_wins()[1]) + api.nvim_win_set_height(api.nvim_list_wins()[2], 1) + eq(1, api.nvim_win_get_height(api.nvim_list_wins()[2])) end) it('do not cause ml_get errors with foldmethod=expr #19989', function() @@ -333,21 +368,25 @@ describe('API/win', function() call nvim_win_set_height(w, 5) ]]) feed('l') - eq('', meths.get_vvar('errmsg')) + eq('', api.nvim_get_vvar('errmsg')) end) end) describe('{get,set}_width', function() it('works', function() - nvim('command', 'split') - eq(window('get_width', nvim('list_wins')[2]), - window('get_width', nvim('list_wins')[1])) - nvim('set_current_win', nvim('list_wins')[2]) - nvim('command', 'vsplit') - eq(window('get_width', nvim('list_wins')[2]), - math.floor(window('get_width', nvim('list_wins')[1]) / 2)) - window('set_width', nvim('list_wins')[2], 2) - eq(2, window('get_width', nvim('list_wins')[2])) + command('split') + eq( + api.nvim_win_get_width(api.nvim_list_wins()[2]), + api.nvim_win_get_width(api.nvim_list_wins()[1]) + ) + api.nvim_set_current_win(api.nvim_list_wins()[2]) + command('vsplit') + eq( + api.nvim_win_get_width(api.nvim_list_wins()[2]), + math.floor(api.nvim_win_get_width(api.nvim_list_wins()[1]) / 2) + ) + api.nvim_win_set_width(api.nvim_list_wins()[2], 2) + eq(2, api.nvim_win_get_width(api.nvim_list_wins()[2])) end) it('do not cause ml_get errors with foldmethod=expr #19989', function() @@ -363,36 +402,36 @@ describe('API/win', function() call nvim_win_set_width(w, 5) ]]) feed('l') - eq('', meths.get_vvar('errmsg')) + eq('', api.nvim_get_vvar('errmsg')) end) end) describe('{get,set,del}_var', function() it('works', function() - curwin('set_var', 'lua', {1, 2, {['3'] = 1}}) - eq({1, 2, {['3'] = 1}}, curwin('get_var', 'lua')) - eq({1, 2, {['3'] = 1}}, nvim('eval', 'w:lua')) - eq(1, funcs.exists('w:lua')) - curwinmeths.del_var('lua') - eq(0, funcs.exists('w:lua')) - eq('Key not found: lua', pcall_err(curwinmeths.del_var, 'lua')) - curwinmeths.set_var('lua', 1) + api.nvim_win_set_var(0, 'lua', { 1, 2, { ['3'] = 1 } }) + eq({ 1, 2, { ['3'] = 1 } }, api.nvim_win_get_var(0, 'lua')) + eq({ 1, 2, { ['3'] = 1 } }, api.nvim_eval('w:lua')) + eq(1, fn.exists('w:lua')) + api.nvim_win_del_var(0, 'lua') + eq(0, fn.exists('w:lua')) + eq('Key not found: lua', pcall_err(api.nvim_win_del_var, 0, 'lua')) + api.nvim_win_set_var(0, 'lua', 1) command('lockvar w:lua') - eq('Key is locked: lua', pcall_err(curwinmeths.del_var, 'lua')) - eq('Key is locked: lua', pcall_err(curwinmeths.set_var, 'lua', 1)) + eq('Key is locked: lua', pcall_err(api.nvim_win_del_var, 0, 'lua')) + eq('Key is locked: lua', pcall_err(api.nvim_win_set_var, 0, 'lua', 1)) end) it('window_set_var returns the old value', function() - local val1 = {1, 2, {['3'] = 1}} - local val2 = {4, 7} + local val1 = { 1, 2, { ['3'] = 1 } } + local val2 = { 4, 7 } eq(NIL, request('window_set_var', 0, 'lua', val1)) eq(val1, request('window_set_var', 0, 'lua', val2)) end) it('window_del_var returns the old value', function() - local val1 = {1, 2, {['3'] = 1}} - local val2 = {4, 7} - eq(NIL, request('window_set_var', 0, 'lua', val1)) + local val1 = { 1, 2, { ['3'] = 1 } } + local val2 = { 4, 7 } + eq(NIL, request('window_set_var', 0, 'lua', val1)) eq(val1, request('window_set_var', 0, 'lua', val2)) eq(val2, request('window_del_var', 0, 'lua')) end) @@ -400,53 +439,51 @@ describe('API/win', function() describe('nvim_get_option_value, nvim_set_option_value', function() it('works', function() - nvim('set_option_value', 'colorcolumn', '4,3', {}) - eq('4,3', nvim('get_option_value', 'colorcolumn', {})) - command("set modified hidden") - command("enew") -- edit new buffer, window option is preserved - eq('4,3', nvim('get_option_value', 'colorcolumn', {})) + api.nvim_set_option_value('colorcolumn', '4,3', {}) + eq('4,3', api.nvim_get_option_value('colorcolumn', {})) + command('set modified hidden') + command('enew') -- edit new buffer, window option is preserved + eq('4,3', api.nvim_get_option_value('colorcolumn', {})) -- global-local option - nvim('set_option_value', 'statusline', 'window-status', {win=0}) - eq('window-status', nvim('get_option_value', 'statusline', {win=0})) - eq('', nvim('get_option_value', 'statusline', {scope='global'})) - command("set modified") - command("enew") -- global-local: not preserved in new buffer + api.nvim_set_option_value('statusline', 'window-status', { win = 0 }) + eq('window-status', api.nvim_get_option_value('statusline', { win = 0 })) + eq('', api.nvim_get_option_value('statusline', { scope = 'global' })) + command('set modified') + command('enew') -- global-local: not preserved in new buffer -- confirm local value was not copied - eq('', nvim('get_option_value', 'statusline', {win = 0})) + eq('', api.nvim_get_option_value('statusline', { win = 0 })) eq('', eval('&l:statusline')) end) it('after switching windows #15390', function() - nvim('command', 'tabnew') - local tab1 = unpack(nvim('list_tabpages')) - local win1 = unpack(tabpage('list_wins', tab1)) - nvim('set_option_value', 'statusline', 'window-status', {win=win1.id}) - nvim('command', 'split') - nvim('command', 'wincmd J') - nvim('command', 'wincmd j') - eq('window-status', nvim('get_option_value', 'statusline', {win = win1.id})) + command('tabnew') + local tab1 = unpack(api.nvim_list_tabpages()) + local win1 = unpack(api.nvim_tabpage_list_wins(tab1)) + api.nvim_set_option_value('statusline', 'window-status', { win = win1 }) + command('split') + command('wincmd J') + command('wincmd j') + eq('window-status', api.nvim_get_option_value('statusline', { win = win1 })) assert_alive() end) it('returns values for unset local options', function() - eq(-1, nvim('get_option_value', 'scrolloff', {win=0, scope='local'})) + eq(-1, api.nvim_get_option_value('scrolloff', { win = 0, scope = 'local' })) end) end) describe('get_position', function() it('works', function() - local height = window('get_height', nvim('list_wins')[1]) - local width = window('get_width', nvim('list_wins')[1]) - nvim('command', 'split') - nvim('command', 'vsplit') - eq({0, 0}, window('get_position', nvim('list_wins')[1])) + local height = api.nvim_win_get_height(api.nvim_list_wins()[1]) + local width = api.nvim_win_get_width(api.nvim_list_wins()[1]) + command('split') + command('vsplit') + eq({ 0, 0 }, api.nvim_win_get_position(api.nvim_list_wins()[1])) local vsplit_pos = math.floor(width / 2) local split_pos = math.floor(height / 2) - local win2row, win2col = - unpack(window('get_position', nvim('list_wins')[2])) - local win3row, win3col = - unpack(window('get_position', nvim('list_wins')[3])) + local win2row, win2col = unpack(api.nvim_win_get_position(api.nvim_list_wins()[2])) + local win3row, win3col = unpack(api.nvim_win_get_position(api.nvim_list_wins()[3])) eq(0, win2row) eq(0, win3col) ok(vsplit_pos - 1 <= win2col and win2col <= vsplit_pos + 1) @@ -456,239 +493,362 @@ describe('API/win', function() describe('get_position', function() it('works', function() - nvim('command', 'tabnew') - nvim('command', 'vsplit') - eq(window('get_tabpage', - nvim('list_wins')[1]), nvim('list_tabpages')[1]) - eq(window('get_tabpage', - nvim('list_wins')[2]), nvim('list_tabpages')[2]) - eq(window('get_tabpage', - nvim('list_wins')[3]), nvim('list_tabpages')[2]) + command('tabnew') + command('vsplit') + eq(api.nvim_win_get_tabpage(api.nvim_list_wins()[1]), api.nvim_list_tabpages()[1]) + eq(api.nvim_win_get_tabpage(api.nvim_list_wins()[2]), api.nvim_list_tabpages()[2]) + eq(api.nvim_win_get_tabpage(api.nvim_list_wins()[3]), api.nvim_list_tabpages()[2]) end) end) describe('get_number', function() it('works', function() - local wins = nvim('list_wins') - eq(1, window('get_number', wins[1])) + local wins = api.nvim_list_wins() + eq(1, api.nvim_win_get_number(wins[1])) - nvim('command', 'split') - local win1, win2 = unpack(nvim('list_wins')) - eq(1, window('get_number', win1)) - eq(2, window('get_number', win2)) + command('split') + local win1, win2 = unpack(api.nvim_list_wins()) + eq(1, api.nvim_win_get_number(win1)) + eq(2, api.nvim_win_get_number(win2)) - nvim('command', 'wincmd J') - eq(2, window('get_number', win1)) - eq(1, window('get_number', win2)) + command('wincmd J') + eq(2, api.nvim_win_get_number(win1)) + eq(1, api.nvim_win_get_number(win2)) - nvim('command', 'tabnew') - local win3 = nvim('list_wins')[3] + command('tabnew') + local win3 = api.nvim_list_wins()[3] -- First tab page - eq(2, window('get_number', win1)) - eq(1, window('get_number', win2)) + eq(2, api.nvim_win_get_number(win1)) + eq(1, api.nvim_win_get_number(win2)) -- Second tab page - eq(1, window('get_number', win3)) + eq(1, api.nvim_win_get_number(win3)) end) end) describe('is_valid', function() it('works', function() - nvim('command', 'split') - local win = nvim('list_wins')[2] - nvim('set_current_win', win) - ok(window('is_valid', win)) - nvim('command', 'close') - ok(not window('is_valid', win)) + command('split') + local win = api.nvim_list_wins()[2] + api.nvim_set_current_win(win) + ok(api.nvim_win_is_valid(win)) + command('close') + ok(not api.nvim_win_is_valid(win)) end) end) describe('close', function() it('can close current window', function() - local oldwin = meths.get_current_win() + local oldwin = api.nvim_get_current_win() command('split') - local newwin = meths.get_current_win() - meths.win_close(newwin,false) - eq({oldwin}, meths.list_wins()) + local newwin = api.nvim_get_current_win() + api.nvim_win_close(newwin, false) + eq({ oldwin }, api.nvim_list_wins()) end) it('can close noncurrent window', function() - local oldwin = meths.get_current_win() + local oldwin = api.nvim_get_current_win() command('split') - local newwin = meths.get_current_win() - meths.win_close(oldwin,false) - eq({newwin}, meths.list_wins()) + local newwin = api.nvim_get_current_win() + api.nvim_win_close(oldwin, false) + eq({ newwin }, api.nvim_list_wins()) end) it("handles changed buffer when 'hidden' is unset", function() command('set nohidden') - local oldwin = meths.get_current_win() + local oldwin = api.nvim_get_current_win() insert('text') command('new') - local newwin = meths.get_current_win() - eq("Vim:E37: No write since last change (add ! to override)", - pcall_err(meths.win_close, oldwin,false)) - eq({newwin,oldwin}, meths.list_wins()) + local newwin = api.nvim_get_current_win() + eq( + 'Vim:E37: No write since last change (add ! to override)', + pcall_err(api.nvim_win_close, oldwin, false) + ) + eq({ newwin, oldwin }, api.nvim_list_wins()) end) it('handles changed buffer with force', function() - local oldwin = meths.get_current_win() + local oldwin = api.nvim_get_current_win() insert('text') command('new') - local newwin = meths.get_current_win() - meths.win_close(oldwin,true) - eq({newwin}, meths.list_wins()) + local newwin = api.nvim_get_current_win() + api.nvim_win_close(oldwin, true) + eq({ newwin }, api.nvim_list_wins()) end) it('in cmdline-window #9767', function() command('split') - eq(2, #meths.list_wins()) - local oldwin = meths.get_current_win() - local otherwin = meths.open_win(0, false, { - relative='editor', row=10, col=10, width=10, height=10, + eq(2, #api.nvim_list_wins()) + local oldbuf = api.nvim_get_current_buf() + local oldwin = api.nvim_get_current_win() + local otherwin = api.nvim_open_win(0, false, { + relative = 'editor', + row = 10, + col = 10, + width = 10, + height = 10, }) -- Open cmdline-window. feed('q:') - eq(4, #meths.list_wins()) - eq(':', funcs.getcmdwintype()) + eq(4, #api.nvim_list_wins()) + eq(':', fn.getcmdwintype()) -- Not allowed to close previous window from cmdline-window. - eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits', - pcall_err(meths.win_close, oldwin, true)) + eq( + 'E11: Invalid in command-line window; <CR> executes, CTRL-C quits', + pcall_err(api.nvim_win_close, oldwin, true) + ) -- Closing other windows is fine. - meths.win_close(otherwin, true) - eq(false, meths.win_is_valid(otherwin)) + api.nvim_win_close(otherwin, true) + eq(false, api.nvim_win_is_valid(otherwin)) -- Close cmdline-window. - meths.win_close(0, true) - eq(2, #meths.list_wins()) - eq('', funcs.getcmdwintype()) + api.nvim_win_close(0, true) + eq(2, #api.nvim_list_wins()) + eq('', fn.getcmdwintype()) + + -- Closing curwin in context of a different window shouldn't close cmdwin. + otherwin = api.nvim_open_win(0, false, { + relative = 'editor', + row = 10, + col = 10, + width = 10, + height = 10, + }) + feed('q:') + exec_lua( + [[ + vim.api.nvim_win_call(..., function() + vim.api.nvim_win_close(0, true) + end) + ]], + otherwin + ) + eq(false, api.nvim_win_is_valid(otherwin)) + eq(':', fn.getcmdwintype()) + -- Closing cmdwin in context of a non-previous window is still OK. + otherwin = api.nvim_open_win(oldbuf, false, { + relative = 'editor', + row = 10, + col = 10, + width = 10, + height = 10, + }) + exec_lua( + [[ + local otherwin, cmdwin = ... + vim.api.nvim_win_call(otherwin, function() + vim.api.nvim_win_close(cmdwin, true) + end) + ]], + otherwin, + api.nvim_get_current_win() + ) + eq('', fn.getcmdwintype()) + eq(true, api.nvim_win_is_valid(otherwin)) end) it('closing current (float) window of another tabpage #15313', function() command('tabedit') command('botright split') - local prevwin = curwin().id + local prevwin = curwin() eq(2, eval('tabpagenr()')) - local win = meths.open_win(0, true, { - relative='editor', row=10, col=10, width=50, height=10 + local win = api.nvim_open_win(0, true, { + relative = 'editor', + row = 10, + col = 10, + width = 50, + height = 10, }) local tab = eval('tabpagenr()') command('tabprevious') eq(1, eval('tabpagenr()')) - meths.win_close(win, false) + api.nvim_win_close(win, false) - eq(prevwin, meths.tabpage_get_win(tab).id) + eq(prevwin, api.nvim_tabpage_get_win(tab)) assert_alive() end) end) describe('hide', function() it('can hide current window', function() - local oldwin = meths.get_current_win() + local oldwin = api.nvim_get_current_win() command('split') - local newwin = meths.get_current_win() - meths.win_hide(newwin) - eq({oldwin}, meths.list_wins()) + local newwin = api.nvim_get_current_win() + api.nvim_win_hide(newwin) + eq({ oldwin }, api.nvim_list_wins()) end) it('can hide noncurrent window', function() - local oldwin = meths.get_current_win() + local oldwin = api.nvim_get_current_win() command('split') - local newwin = meths.get_current_win() - meths.win_hide(oldwin) - eq({newwin}, meths.list_wins()) + local newwin = api.nvim_get_current_win() + api.nvim_win_hide(oldwin) + eq({ newwin }, api.nvim_list_wins()) end) it('does not close the buffer', function() - local oldwin = meths.get_current_win() - local oldbuf = meths.get_current_buf() - local buf = meths.create_buf(true, false) |