feat(api): add nvim_win_resize()

Ref #6645

Problem:
When a window is resized it takes space from the window right/below first,
and only falls back to the window left/above when there is no more room.
Sometimes a user wants the space to come from a specific direction.

Solution:
Add nvim_win_resize(win, width, height, {anchor}) which resizes a window
with a choosable anchor edge, letting a window grow leftwards or upwards
by taking space from the window to the left or above first. The default
anchor reproduces nvim_win_set_width()/nvim_win_set_height().
This commit is contained in:
XiaowenHu96
2026-06-27 10:41:12 +08:00
parent 08a1228f82
commit ce718e31f4
17 changed files with 527 additions and 177 deletions

View File

@@ -22,7 +22,7 @@ local function win_config(win, hide, height)
if ui.cmdheight == 0 and api.nvim_win_get_config(win).hide ~= hide then
api.nvim_win_set_config(win, { hide = hide, height = not hide and height or nil })
elseif api.nvim_win_get_height(win) ~= height then
api.nvim_win_set_height(win, height)
api.nvim_win_resize(win, -1, height, {})
end
if vim.o.cmdheight ~= height then
-- Avoid moving the cursor with 'splitkeep' = "screen", and altering the user

View File

@@ -139,7 +139,7 @@ local function set_virttext(type, tgt)
-- Check if adding the virt_text on this line will exceed the current window width.
local maxwidth = math.max(M.msg.width, math.min(o.columns, scol - offset + width))
if tgt == 'msg' and api.nvim_win_get_width(win) < maxwidth then
api.nvim_win_set_width(win, maxwidth)
api.nvim_win_resize(win, maxwidth, -1, {})
M.msg.width = maxwidth
end
@@ -293,7 +293,7 @@ function M.show_msg(tgt, kind, content, replace_last, append, id)
if tgt == 'msg' then
local width_cmd = [[echo max(map(range(1, line('$')), 'virtcol([v:val, "$"])'))]]
local width = tonumber(fn.win_execute(ui.wins.msg, width_cmd)) - 1
api.nvim_win_set_width(ui.wins.msg, width)
api.nvim_win_resize(ui.wins.msg, width, -1, {})
local texth = api.nvim_win_text_height(ui.wins.msg, { start_row = start_row, end_row = row })
if texth.all > math.ceil(lines * 0.5) then
tgt, buf = M.expand_msg(tgt)