diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c index 43d8706255..ec68a55389 100644 --- a/src/nvim/api/win_config.c +++ b/src/nvim/api/win_config.c @@ -260,9 +260,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 size = (flags & WSP_VERT) ? fconfig.width : fconfig.height; TRY_WRAP(err, { - int size = (flags & WSP_VERT) ? fconfig.width : fconfig.height; if (parent == NULL || parent == curwin) { wp = win_split_ins(size, flags, NULL, 0, NULL); } else { @@ -277,12 +277,14 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(win_config) *config, Err }); if (wp) { wp->w_config = fconfig; - // Without room for the requested size, window sizes may have been equalized instead. - // If the size differs from what was requested, try to set it again now. - if ((flags & WSP_VERT) && wp->w_width != fconfig.width) { - win_setwidth_win(fconfig.width, wp); - } else if (!(flags & WSP_VERT) && wp->w_height != fconfig.height) { - win_setheight_win(fconfig.height, wp); + if (size > 0) { + // Without room for the requested size, window sizes may have been equalized instead. + // If the size differs from what was requested, try to set it again now. + if ((flags & WSP_VERT) && wp->w_width != size) { + win_setwidth_win(size, wp); + } else if (!(flags & WSP_VERT) && wp->w_height != size) { + win_setheight_win(size, wp); + } } } } else { diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua index e6d50b47f3..876b4a284c 100644 --- a/test/functional/api/window_spec.lua +++ b/test/functional/api/window_spec.lua @@ -2273,6 +2273,16 @@ describe('API/win', function() command('split') win = api.nvim_open_win(0, false, { win = 0, split = 'below', height = 10 }) eq(10, api.nvim_win_get_height(win)) + + -- Still defaults to half-sized when no size was specified. + command('only') + eq(80, api.nvim_win_get_width(0)) + api.nvim_open_win(0, true, { split = 'right' }) + eq(40, api.nvim_win_get_width(0)) + + eq(22, api.nvim_win_get_height(0)) + api.nvim_open_win(0, true, { split = 'below' }) + eq(11, api.nvim_win_get_height(0)) end) end)