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)

View File

@@ -2492,6 +2492,26 @@ function vim.api.nvim_win_hide(win) end
--- @return boolean # true if the window is valid, false otherwise
function vim.api.nvim_win_is_valid(win) end
--- Resize a window, choosing which edge stays anchored.
---
--- The window first takes space from the non-anchored side (the window below or
--- to the right by default), and only then from the anchored side. The "anchor"
--- selects the fixed edge, so a window can also grow upwards or leftwards.
---
--- Can set the height, the width, or both in a single call. "anchor" applies to the
--- matching axis ("top"/"bottom" for height, "left"/"right" for width); the other
--- axis, if also resized, uses its default anchor.
---
--- @param win integer `window-ID`, or 0 for current window
--- @param width integer New width as a count of columns, set -1 as "no change"
--- @param height integer New height as a count of rows, set -1 as "no change"
--- @param opts vim.api.keyset.win_resize Optional parameters.
--- - anchor: Edge that stays fixed while the opposite edge moves; the
--- neighbor on the moving side is resized first. One of:
--- - "top" (default for height) or "bottom"
--- - "left" (default for width) or "right"
function vim.api.nvim_win_resize(win, width, height, opts) end
--- Sets the current buffer in a window.
---
--- Note: As a side-effect, this executes `BufEnter` and `BufLeave` autocommands.
@@ -2526,10 +2546,9 @@ function vim.api.nvim_win_set_config(win, config) end
--- @param pos [integer, integer] (row, col) tuple representing the new position
function vim.api.nvim_win_set_cursor(win, pos) end
--- Sets the window height.
---
--- @param win integer `window-ID`, or 0 for current window
--- @param height integer Height as a count of rows
--- @deprecated
--- @param win integer
--- @param height integer
function vim.api.nvim_win_set_height(win, height) end
--- Set highlight namespace for a window. This will use highlights defined with
@@ -2555,11 +2574,9 @@ function vim.api.nvim_win_set_option(window, name, value) end
--- @param value any Variable value
function vim.api.nvim_win_set_var(win, name, value) end
--- Sets the window width. This will only succeed if the screen is split
--- vertically.
---
--- @param win integer `window-ID`, or 0 for current window
--- @param width integer Width as a count of columns
--- @deprecated
--- @param win integer
--- @param width integer
function vim.api.nvim_win_set_width(win, width) end
--- Computes the number of screen lines occupied by a range of text in a given window.

View File

@@ -493,6 +493,9 @@ error('Cannot require a meta file')
--- @field zindex? integer
--- @field _cmdline_offset? integer
--- @class vim.api.keyset.win_resize
--- @field anchor? string
--- @class vim.api.keyset.win_text_height
--- @field end_row? integer
--- @field end_vcol? integer

View File

@@ -677,7 +677,7 @@ local function update_popup_window(winid, bufnr, kind)
vim.treesitter.start(bufnr, kind)
end
local all = api.nvim_win_text_height(winid, {}).all
api.nvim_win_set_height(winid, all)
api.nvim_win_resize(winid, -1, all, {})
end
end

View File

@@ -1625,7 +1625,7 @@ function M.open_floating_preview(contents, syntax, opts)
local win_height = api.nvim_win_get_height(floating_winnr)
local text_height = api.nvim_win_text_height(floating_winnr, { max_height = win_height }).all
if text_height < win_height then
api.nvim_win_set_height(floating_winnr, text_height)
api.nvim_win_resize(floating_winnr, -1, text_height, {})
end
end
end