Skip to content

Commit 39af5ca

Browse files
committed
fix(messages): Fixed an issue with message window having incorrect height in specific cases
1 parent 4e13fcb commit 39af5ca

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

lua/ui/message.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,15 +862,16 @@ message.__render = function ()
862862
end
863863

864864
local W = math.min(math.floor(vim.o.columns * 0.5), utils.max_len(lines));
865+
local H = utils.wrapped_height(lines, W);
865866

866867
local window_config = vim.tbl_extend("keep", spec.config.message.message_winconfig or {}, {
867868
relative = "editor",
868869

869-
row = vim.o.lines - (vim.o.cmdheight + message.__get_cmdline_offset() + 1) - utils.wrapped_height(lines, W),
870+
row = vim.o.lines - (vim.o.cmdheight + message.__get_cmdline_offset() + 1) - H,
870871
col = vim.o.columns,
871872

872873
width = W + decor_size,
873-
height = utils.wrapped_height(lines, W),
874+
height = H,
874875

875876
border = "none",
876877

@@ -890,6 +891,7 @@ message.__render = function ()
890891

891892
utils.set("w", message.msg_window, "wrap", true);
892893
utils.set("w", message.msg_window, "linebreak", true);
894+
utils.set("w", message.msg_window, "breakindent", true);
893895

894896
vim.api.nvim__redraw({
895897
flush = true,

lua/ui/utils.lua

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -342,31 +342,49 @@ utils.confirm_keys = function (prompt, content)
342342
---|fE
343343
end
344344

345+
---@type integer, integer Buffer & window used for checking wrapped line height.
346+
utils.__wrapped_buf, utils.__wrapped_win = nil, nil;
347+
345348
--- Gets line number for wrapped text.
346349
---@param lines string[]
347350
---@param width? integer
348351
---@return integer
349-
utils.wrapped_height = function (lines, width)
352+
utils.wrapped_height = function(lines, width)
350353
---|fS
351354

352355
width = width or vim.o.columns;
353-
local height = 0;
354356

355-
for _, line in ipairs(lines) do
356-
local len = vim.fn.strdisplaywidth(line);
357+
if type(utils.__wrapped_buf) ~= "number" or vim.api.nvim_buf_is_valid(utils.__wrapped_buf) then
358+
utils.__wrapped_buf = vim.api.nvim_create_buf(false, true);
359+
end
357360

358-
if len <= width then
359-
height = height + 1;
360-
else
361-
height = height + math.floor(vim.fn.strchars(line) / width);
361+
local win_config = {
362+
hide = true,
363+
relative = "editor",
362364

363-
if vim.fn.strchars(line) % width ~= 0 then
364-
height = height + 1;
365-
end
366-
end
365+
row = 5,
366+
col = 5,
367+
368+
width = width,
369+
height = 1,
370+
371+
style = "minimal",
372+
};
373+
374+
if type(utils.__wrapped_win) ~= "number" or vim.api.nvim_win_is_valid(utils.__wrapped_win) then
375+
utils.__wrapped_win = vim.api.nvim_open_win(utils.__wrapped_buf, false, win_config);
376+
else
377+
vim.api.nvim_win_set_config(utils.__wrapped_win, win_config);
367378
end
368379

369-
return height;
380+
vim.wo[utils.__wrapped_win].wrap = true;
381+
vim.wo[utils.__wrapped_win].linebreak = true;
382+
vim.wo[utils.__wrapped_win].breakindent = true;
383+
384+
vim.api.nvim_buf_set_lines(utils.__wrapped_buf, 0, -1, false, lines);
385+
386+
local text_height = vim.api.nvim_win_text_height(utils.__wrapped_win, { start_row = 0, end_row = -1 });
387+
return text_height.all;
370388

371389
---|fE
372390
end

0 commit comments

Comments
 (0)