fix(api): make width/height for split by nvim_open_win work (#28341)

This commit is contained in:
zeertzjq 2024-04-15 17:55:57 +08:00 committed by GitHub
parent 5371ed36b7
commit 4ec8fd43bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 150 additions and 79 deletions

View File

@ -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,7 +1044,7 @@ 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)

View File

@ -1227,40 +1227,45 @@ 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,
})
eq('', api.nvim_win_get_config(win).relative)
end)
it('creates split windows in the correct direction', function()
local initial_win = api.nvim_get_current_win() local initial_win = api.nvim_get_current_win()
local win = api.nvim_open_win(0, true, { local win = api.nvim_open_win(0, true, {
vertical = true, [key] = val,
height = 10,
}) })
eq('', api.nvim_win_get_config(win).relative) eq('', api.nvim_win_get_config(win).relative)
eq(10, api.nvim_win_get_height(win))
local layout = fn.winlayout() local layout = fn.winlayout()
eq({ eq({
'row', 'col',
{ {
{ 'leaf', win }, { 'leaf', win },
{ 'leaf', initial_win }, { 'leaf', initial_win },
}, },
}, layout) }, layout)
end
it("with split = 'above'", function()
test_open_win_split_above('split', 'above')
end) end)
it("respects the 'split' option", function() 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 initial_win = api.nvim_get_current_win()
local win = api.nvim_open_win(0, true, { local win = api.nvim_open_win(0, true, {
split = 'below', [key] = val,
height = 15,
}) })
eq('', api.nvim_win_get_config(win).relative) eq('', api.nvim_win_get_config(win).relative)
eq(15, api.nvim_win_get_height(win))
local layout = fn.winlayout() local layout = fn.winlayout()
eq({ eq({
'col', 'col',
{ {
@ -1268,11 +1273,77 @@ describe('API/win', function()
{ 'leaf', win }, { 'leaf', win },
}, },
}, layout) }, layout)
end
it("with split = 'below'", function()
test_open_win_split_below('split', 'below')
end) end)
it( it("with vertical = false and 'splitbelow'", function()
"doesn't change tp_curwin when splitting window in non-current tab with enter=false", api.nvim_set_option_value('splitbelow', true, {})
function() 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 = api.nvim_get_current_tabpage()
local tab1_win = api.nvim_get_current_win() local tab1_win = api.nvim_get_current_win()
@ -1300,8 +1371,7 @@ describe('API/win', function()
eq(tab2_prevwin, fn.tabpagewinnr(tab2, '#')) -- tab2's tp_prevwin 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({ tab1_win, tab2_win, tab2_win2 }, api.nvim_list_wins())
eq({ tab2_win, tab2_win2 }, api.nvim_tabpage_list_wins(tab2)) eq({ tab2_win, tab2_win2 }, api.nvim_tabpage_list_wins(tab2))
end 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()