From add20ee9ffabc80b34e1b80a53ef68df9969d02e Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 26 Oct 2022 12:07:32 +0200 Subject: [PATCH] feat: properly calculate buf height taking wrap into account --- lua/noice/util/nui.lua | 18 ++++++++++++++++++ lua/noice/view/nui.lua | 2 +- lua/noice/view/scrollbar.lua | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lua/noice/util/nui.lua b/lua/noice/util/nui.lua index d2e60cb..2931cc7 100644 --- a/lua/noice/util/nui.lua +++ b/lua/noice/util/nui.lua @@ -154,6 +154,24 @@ function M.get_border_size(opts) } end +function M.win_buf_height(win) + local buf = vim.api.nvim_win_get_buf(win) + + if not vim.wo[win].wrap then + return vim.api.nvim_buf_line_count(buf) + end + + local width = vim.api.nvim_win_get_width(win) + + local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) + local height = 0 + for _, l in ipairs(lines) do + height = height + math.max(1, (math.ceil(vim.fn.strwidth(l) / width))) + end + assert(height >= vim.api.nvim_buf_line_count(buf)) + return height +end + ---@param dim {width: number, height:number} ---@param _opts NoiceNuiOptions ---@return _.NoiceNuiOptions diff --git a/lua/noice/view/nui.lua b/lua/noice/view/nui.lua index 93a42a1..02fda7f 100644 --- a/lua/noice/view/nui.lua +++ b/lua/noice/view/nui.lua @@ -156,7 +156,7 @@ function NuiView:get_layout() local height = 0 for _, m in ipairs(self._messages) do for _, l in ipairs(m._lines) do - height = height + (math.ceil(l:width() / layout.size.width)) + height = height + math.max(1, (math.ceil(l:width() / layout.size.width))) end end return Util.nui.get_layout({ width = self:width(), height = height }, self._opts) diff --git a/lua/noice/view/scrollbar.lua b/lua/noice/view/scrollbar.lua index e67b55e..77cb63c 100644 --- a/lua/noice/view/scrollbar.lua +++ b/lua/noice/view/scrollbar.lua @@ -95,7 +95,7 @@ function Scrollbar:update() height = vim.api.nvim_win_get_height(self.winnr) + self.opts.border_size.top + self.opts.border_size.bottom, } - local buf_height = vim.api.nvim_buf_line_count(vim.api.nvim_win_get_buf(self.winnr)) + local buf_height = Util.nui.win_buf_height(self.winnr) if self.opts.autohide and dim.height >= buf_height then self:hide()