Skip to content

Commit

Permalink
fix: correctly calculate popupmenu width based on border and padding. F…
Browse files Browse the repository at this point in the history
…ixes #122
  • Loading branch information
folke committed Oct 26, 2022
1 parent a2f6e4c commit b152bf5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 24 deletions.
5 changes: 4 additions & 1 deletion lua/noice/ui/popupmenu/nui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ function M.create(state)
-- otherwise the selected CursorLine does not extend to the edges
if opts.border and opts.border.padding then
opts.border.padding = vim.tbl_deep_extend("force", {}, padding, { left = 0, right = 0 })
if opts.size and type(opts.size.width) == "number" then
opts.size.width = opts.size.width + padding.left + padding.right
end
end

for _, item in ipairs(state.items) do
Expand Down Expand Up @@ -178,7 +181,7 @@ function M.create(state)

M.scroll = Scrollbar({
winnr = M.menu.winid,
border_size = Util.nui.get_border_size(opts.border),
border_size = Util.nui.normalize_padding(opts.border),
})
M.scroll:mount()

Expand Down
17 changes: 0 additions & 17 deletions lua/noice/util/nui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,6 @@ function M.normalize_padding(opts)
}, opts.padding or {})
end

---@param opts? _.NuiBorder
---@return _.NuiBorderPadding
function M.get_border_size(opts)
opts = opts or {}

local border_size = opts.style and opts.style ~= "none" and 1 or 0
local padding = M.normalize_padding(opts)

return {
top = border_size + padding.top,
bottom = border_size + padding.bottom,
right = border_size + padding.right,
left = border_size + padding.left,
}
end

function M.win_buf_height(win)
local buf = vim.api.nvim_win_get_buf(win)

Expand All @@ -168,7 +152,6 @@ function M.win_buf_height(win)
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

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 @@ -114,7 +114,7 @@ function NuiView:create()
self:update_layout()
self._scroll = Scrollbar({
winnr = self._nui.winid,
border_size = Util.nui.get_border_size(self._opts.border),
border_size = Util.nui.normalize_padding(self._opts.border),
})
self._scroll:mount()
-- NOTE: this is needed, to make sure the border is rendered properly during blocking events
Expand Down
10 changes: 5 additions & 5 deletions lua/noice/view/scrollbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local defaults = {
thumb = "NoiceScrollbarThumb",
},
---@type _.NuiBorderPadding
border_size = {
padding = {
top = 0,
right = 0,
bottom = 0,
Expand Down Expand Up @@ -89,10 +89,10 @@ function Scrollbar:update()
local pos = vim.api.nvim_win_get_position(self.winnr)

local dim = {
row = pos[1] - self.opts.border_size.top,
col = pos[2] - self.opts.border_size.left,
width = vim.api.nvim_win_get_width(self.winnr) + self.opts.border_size.left + self.opts.border_size.right,
height = vim.api.nvim_win_get_height(self.winnr) + self.opts.border_size.top + self.opts.border_size.bottom,
row = pos[1] - self.opts.padding.top,
col = pos[2] - self.opts.padding.left,
width = vim.api.nvim_win_get_width(self.winnr) + self.opts.padding.left + self.opts.padding.right,
height = vim.api.nvim_win_get_height(self.winnr) + self.opts.padding.top + self.opts.padding.bottom,
}

local buf_height = Util.nui.win_buf_height(self.winnr)
Expand Down

0 comments on commit b152bf5

Please sign in to comment.