Skip to content

Commit

Permalink
feat: properly calculate buf height taking wrap into account
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 26, 2022
1 parent 13097dc commit add20ee
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lua/noice/util/nui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/noice/view/nui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lua/noice/view/scrollbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit add20ee

Please sign in to comment.