From 479bb9cbab4fff68b778a72016a3b5dcd29a24b5 Mon Sep 17 00:00:00 2001 From: glepnir Date: Sat, 4 Oct 2025 16:07:27 +0800 Subject: [PATCH] fix(api): nvim_open_win respects requested split window size (#35601) Problem: requested window size passed to nvim_open_win for splits may be ignored by win_split_ins if it decides to forcefully equalize window sizes instead (e.g: in an attempt to make room for the new window). Solution: try to set the size again if it differs from what was requested. Co-authored-by: Sean Dewar <6256228+seandewar@users.noreply.github.com> --- src/nvim/api/win_config.c | 7 +++++++ test/functional/api/window_spec.lua | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c index 81bc3934b1..bcad760caf 100644 --- a/src/nvim/api/win_config.c +++ b/src/nvim/api/win_config.c @@ -279,6 +279,13 @@ 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); + } } } else { wp = win_new_float(NULL, false, fconfig, err); diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua index 6836c76b26..c38ff75269 100644 --- a/test/functional/api/window_spec.lua +++ b/test/functional/api/window_spec.lua @@ -1930,6 +1930,21 @@ describe('API/win', function() }) ) end) + + it('respects requested size for large splits', function() + command('vsplit') + local win = api.nvim_open_win(0, false, { win = -1, split = 'right', width = 38 }) + eq(38, api.nvim_win_get_width(win)) + + -- No zero-sized windows (e.g: from skipping forced equalization in win_split_ins) if + -- requesting a chonky window; that could lead to crashes! + api.nvim_open_win(0, false, { win = -1, split = 'right', width = 9999 }) + eq({ 1, 1, 1, 74 }, eval("range(1, winnr('$'))->map({_, nr -> winwidth(nr)})")) + + command('split') + win = api.nvim_open_win(0, false, { win = 0, split = 'below', height = 10 }) + eq(10, api.nvim_win_get_height(win)) + end) end) describe('set_config', function()