mirror of
https://github.com/neovim/neovim.git
synced 2024-12-19 10:45:16 -07:00
fix(api): make width/height for split by nvim_open_win work (#28341)
This commit is contained in:
parent
5371ed36b7
commit
4ec8fd43bf
@ -224,7 +224,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(win_config) *config, Err
|
|||||||
}
|
}
|
||||||
|
|
||||||
WinConfig fconfig = WIN_CONFIG_INIT;
|
WinConfig fconfig = WIN_CONFIG_INIT;
|
||||||
if (!parse_float_config(NULL, config, &fconfig, false, err)) {
|
if (!parse_win_config(NULL, config, &fconfig, false, err)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,8 +263,9 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(win_config) *config, Err
|
|||||||
int flags = win_split_flags(fconfig.split, parent == NULL) | WSP_NOENTER;
|
int flags = win_split_flags(fconfig.split, parent == NULL) | WSP_NOENTER;
|
||||||
|
|
||||||
TRY_WRAP(err, {
|
TRY_WRAP(err, {
|
||||||
|
int size = (flags & WSP_VERT) ? fconfig.width : fconfig.height;
|
||||||
if (parent == NULL || parent == curwin) {
|
if (parent == NULL || parent == curwin) {
|
||||||
wp = win_split_ins(0, flags, NULL, 0, NULL);
|
wp = win_split_ins(size, flags, NULL, 0, NULL);
|
||||||
} else {
|
} else {
|
||||||
tp = win_find_tabpage(parent);
|
tp = win_find_tabpage(parent);
|
||||||
switchwin_T switchwin;
|
switchwin_T switchwin;
|
||||||
@ -272,7 +273,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(win_config) *config, Err
|
|||||||
const int result = switch_win(&switchwin, parent, tp, true);
|
const int result = switch_win(&switchwin, parent, tp, true);
|
||||||
assert(result == OK);
|
assert(result == OK);
|
||||||
(void)result;
|
(void)result;
|
||||||
wp = win_split_ins(0, flags, NULL, 0, NULL);
|
wp = win_split_ins(size, flags, NULL, 0, NULL);
|
||||||
restore_win(&switchwin, true);
|
restore_win(&switchwin, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -405,7 +406,7 @@ void nvim_win_set_config(Window window, Dict(win_config) *config, Error *err)
|
|||||||
&& !(HAS_KEY_X(config, external) ? config->external : fconfig.external)
|
&& !(HAS_KEY_X(config, external) ? config->external : fconfig.external)
|
||||||
&& (has_split || has_vertical || was_split);
|
&& (has_split || has_vertical || was_split);
|
||||||
|
|
||||||
if (!parse_float_config(win, config, &fconfig, !was_split || to_split, err)) {
|
if (!parse_win_config(win, config, &fconfig, !was_split || to_split, err)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (was_split && !to_split) {
|
if (was_split && !to_split) {
|
||||||
@ -1043,8 +1044,8 @@ static void generate_api_error(win_T *wp, const char *attribute, Error *err)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool parse_float_config(win_T *wp, Dict(win_config) *config, WinConfig *fconfig, bool reconf,
|
static bool parse_win_config(win_T *wp, Dict(win_config) *config, WinConfig *fconfig, bool reconf,
|
||||||
Error *err)
|
Error *err)
|
||||||
{
|
{
|
||||||
#define HAS_KEY_X(d, key) HAS_KEY(d, win_config, key)
|
#define HAS_KEY_X(d, key) HAS_KEY(d, win_config, key)
|
||||||
bool has_relative = false, relative_is_win = false, is_split = false;
|
bool has_relative = false, relative_is_win = false, is_split = false;
|
||||||
|
@ -1227,81 +1227,151 @@ describe('API/win', function()
|
|||||||
eq(wins_before, api.nvim_list_wins())
|
eq(wins_before, api.nvim_list_wins())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('creates a split window', function()
|
describe('creates a split window above', function()
|
||||||
local win = api.nvim_open_win(0, true, {
|
local function test_open_win_split_above(key, val)
|
||||||
vertical = false,
|
local initial_win = api.nvim_get_current_win()
|
||||||
})
|
local win = api.nvim_open_win(0, true, {
|
||||||
eq('', api.nvim_win_get_config(win).relative)
|
[key] = val,
|
||||||
end)
|
height = 10,
|
||||||
|
|
||||||
it('creates split windows in the correct direction', function()
|
|
||||||
local initial_win = api.nvim_get_current_win()
|
|
||||||
local win = api.nvim_open_win(0, true, {
|
|
||||||
vertical = true,
|
|
||||||
})
|
|
||||||
eq('', api.nvim_win_get_config(win).relative)
|
|
||||||
|
|
||||||
local layout = fn.winlayout()
|
|
||||||
|
|
||||||
eq({
|
|
||||||
'row',
|
|
||||||
{
|
|
||||||
{ 'leaf', win },
|
|
||||||
{ 'leaf', initial_win },
|
|
||||||
},
|
|
||||||
}, layout)
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("respects the 'split' option", function()
|
|
||||||
local initial_win = api.nvim_get_current_win()
|
|
||||||
local win = api.nvim_open_win(0, true, {
|
|
||||||
split = 'below',
|
|
||||||
})
|
|
||||||
eq('', api.nvim_win_get_config(win).relative)
|
|
||||||
|
|
||||||
local layout = fn.winlayout()
|
|
||||||
|
|
||||||
eq({
|
|
||||||
'col',
|
|
||||||
{
|
|
||||||
{ 'leaf', initial_win },
|
|
||||||
{ 'leaf', win },
|
|
||||||
},
|
|
||||||
}, layout)
|
|
||||||
end)
|
|
||||||
|
|
||||||
it(
|
|
||||||
"doesn't change tp_curwin when splitting window in non-current tab with enter=false",
|
|
||||||
function()
|
|
||||||
local tab1 = api.nvim_get_current_tabpage()
|
|
||||||
local tab1_win = api.nvim_get_current_win()
|
|
||||||
|
|
||||||
t.command('tabnew')
|
|
||||||
local tab2 = api.nvim_get_current_tabpage()
|
|
||||||
local tab2_win = api.nvim_get_current_win()
|
|
||||||
|
|
||||||
eq({ tab1_win, tab2_win }, api.nvim_list_wins())
|
|
||||||
eq({ tab1, tab2 }, api.nvim_list_tabpages())
|
|
||||||
|
|
||||||
api.nvim_set_current_tabpage(tab1)
|
|
||||||
eq(tab1_win, api.nvim_get_current_win())
|
|
||||||
|
|
||||||
local tab2_prevwin = fn.tabpagewinnr(tab2, '#')
|
|
||||||
|
|
||||||
-- split in tab2 whine in tab2, with enter = false
|
|
||||||
local tab2_win2 = api.nvim_open_win(api.nvim_create_buf(false, true), false, {
|
|
||||||
win = tab2_win,
|
|
||||||
split = 'right',
|
|
||||||
})
|
})
|
||||||
eq(tab1_win, api.nvim_get_current_win()) -- we should still be in the first tp
|
eq('', api.nvim_win_get_config(win).relative)
|
||||||
eq(tab1_win, api.nvim_tabpage_get_win(tab1))
|
eq(10, api.nvim_win_get_height(win))
|
||||||
|
local layout = fn.winlayout()
|
||||||
eq(tab2_win, api.nvim_tabpage_get_win(tab2)) -- tab2's tp_curwin should not have changed
|
eq({
|
||||||
eq(tab2_prevwin, fn.tabpagewinnr(tab2, '#')) -- tab2's tp_prevwin should not have changed
|
'col',
|
||||||
eq({ tab1_win, tab2_win, tab2_win2 }, api.nvim_list_wins())
|
{
|
||||||
eq({ tab2_win, tab2_win2 }, api.nvim_tabpage_list_wins(tab2))
|
{ 'leaf', win },
|
||||||
|
{ 'leaf', initial_win },
|
||||||
|
},
|
||||||
|
}, layout)
|
||||||
end
|
end
|
||||||
)
|
|
||||||
|
it("with split = 'above'", function()
|
||||||
|
test_open_win_split_above('split', 'above')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("with vertical = false and 'nosplitbelow'", function()
|
||||||
|
api.nvim_set_option_value('splitbelow', false, {})
|
||||||
|
test_open_win_split_above('vertical', false)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('creates a split window below', function()
|
||||||
|
local function test_open_win_split_below(key, val)
|
||||||
|
local initial_win = api.nvim_get_current_win()
|
||||||
|
local win = api.nvim_open_win(0, true, {
|
||||||
|
[key] = val,
|
||||||
|
height = 15,
|
||||||
|
})
|
||||||
|
eq('', api.nvim_win_get_config(win).relative)
|
||||||
|
eq(15, api.nvim_win_get_height(win))
|
||||||
|
local layout = fn.winlayout()
|
||||||
|
eq({
|
||||||
|
'col',
|
||||||
|
{
|
||||||
|
{ 'leaf', initial_win },
|
||||||
|
{ 'leaf', win },
|
||||||
|
},
|
||||||
|
}, layout)
|
||||||
|
end
|
||||||
|
|
||||||
|
it("with split = 'below'", function()
|
||||||
|
test_open_win_split_below('split', 'below')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("with vertical = false and 'splitbelow'", function()
|
||||||
|
api.nvim_set_option_value('splitbelow', true, {})
|
||||||
|
test_open_win_split_below('vertical', false)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('creates a split window to the left', function()
|
||||||
|
local function test_open_win_split_left(key, val)
|
||||||
|
local initial_win = api.nvim_get_current_win()
|
||||||
|
local win = api.nvim_open_win(0, true, {
|
||||||
|
[key] = val,
|
||||||
|
width = 25,
|
||||||
|
})
|
||||||
|
eq('', api.nvim_win_get_config(win).relative)
|
||||||
|
eq(25, api.nvim_win_get_width(win))
|
||||||
|
local layout = fn.winlayout()
|
||||||
|
eq({
|
||||||
|
'row',
|
||||||
|
{
|
||||||
|
{ 'leaf', win },
|
||||||
|
{ 'leaf', initial_win },
|
||||||
|
},
|
||||||
|
}, layout)
|
||||||
|
end
|
||||||
|
|
||||||
|
it("with split = 'left'", function()
|
||||||
|
test_open_win_split_left('split', 'left')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("with vertical = true and 'nosplitright'", function()
|
||||||
|
api.nvim_set_option_value('splitright', false, {})
|
||||||
|
test_open_win_split_left('vertical', true)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('creates a split window to the right', function()
|
||||||
|
local function test_open_win_split_right(key, val)
|
||||||
|
local initial_win = api.nvim_get_current_win()
|
||||||
|
local win = api.nvim_open_win(0, true, {
|
||||||
|
[key] = val,
|
||||||
|
width = 30,
|
||||||
|
})
|
||||||
|
eq('', api.nvim_win_get_config(win).relative)
|
||||||
|
eq(30, api.nvim_win_get_width(win))
|
||||||
|
local layout = fn.winlayout()
|
||||||
|
eq({
|
||||||
|
'row',
|
||||||
|
{
|
||||||
|
{ 'leaf', initial_win },
|
||||||
|
{ 'leaf', win },
|
||||||
|
},
|
||||||
|
}, layout)
|
||||||
|
end
|
||||||
|
|
||||||
|
it("with split = 'right'", function()
|
||||||
|
test_open_win_split_right('split', 'right')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("with vertical = true and 'splitright'", function()
|
||||||
|
api.nvim_set_option_value('splitright', true, {})
|
||||||
|
test_open_win_split_right('vertical', true)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("doesn't change tp_curwin when splitting window in another tab with enter=false", function()
|
||||||
|
local tab1 = api.nvim_get_current_tabpage()
|
||||||
|
local tab1_win = api.nvim_get_current_win()
|
||||||
|
|
||||||
|
t.command('tabnew')
|
||||||
|
local tab2 = api.nvim_get_current_tabpage()
|
||||||
|
local tab2_win = api.nvim_get_current_win()
|
||||||
|
|
||||||
|
eq({ tab1_win, tab2_win }, api.nvim_list_wins())
|
||||||
|
eq({ tab1, tab2 }, api.nvim_list_tabpages())
|
||||||
|
|
||||||
|
api.nvim_set_current_tabpage(tab1)
|
||||||
|
eq(tab1_win, api.nvim_get_current_win())
|
||||||
|
|
||||||
|
local tab2_prevwin = fn.tabpagewinnr(tab2, '#')
|
||||||
|
|
||||||
|
-- split in tab2 whine in tab2, with enter = false
|
||||||
|
local tab2_win2 = api.nvim_open_win(api.nvim_create_buf(false, true), false, {
|
||||||
|
win = tab2_win,
|
||||||
|
split = 'right',
|
||||||
|
})
|
||||||
|
eq(tab1_win, api.nvim_get_current_win()) -- we should still be in the first tp
|
||||||
|
eq(tab1_win, api.nvim_tabpage_get_win(tab1))
|
||||||
|
|
||||||
|
eq(tab2_win, api.nvim_tabpage_get_win(tab2)) -- tab2's tp_curwin should not have changed
|
||||||
|
eq(tab2_prevwin, fn.tabpagewinnr(tab2, '#')) -- tab2's tp_prevwin should not have changed
|
||||||
|
eq({ tab1_win, tab2_win, tab2_win2 }, api.nvim_list_wins())
|
||||||
|
eq({ tab2_win, tab2_win2 }, api.nvim_tabpage_list_wins(tab2))
|
||||||
|
end)
|
||||||
|
|
||||||
it('creates splits in the correct location', function()
|
it('creates splits in the correct location', function()
|
||||||
local first_win = api.nvim_get_current_win()
|
local first_win = api.nvim_get_current_win()
|
||||||
|
Loading…
Reference in New Issue
Block a user