Skip to content

Commit

Permalink
fix: scrollbar gutter detection with border table
Browse files Browse the repository at this point in the history
Closes #828
  • Loading branch information
Saghen committed Jan 19, 2025
1 parent f74f249 commit ecbac4b
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions lua/blink/cmp/lib/window/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
--- @field update_size fun(self: blink.cmp.Window)
--- @field get_content_height fun(self: blink.cmp.Window): number
--- @field get_border_size fun(self: blink.cmp.Window, border?: 'none' | 'single' | 'double' | 'rounded' | 'solid' | 'shadow' | 'padded' | string[]): { vertical: number, horizontal: number, left: number, right: number, top: number, bottom: number }
--- @field expand_border_chars fun(border: string[]): string[]
--- @field get_height fun(self: blink.cmp.Window): number
--- @field get_content_width fun(self: blink.cmp.Window): number
--- @field get_width fun(self: blink.cmp.Window): number
Expand Down Expand Up @@ -69,9 +70,15 @@ function win.new(config)
self.redraw_queued = false

if self.config.scrollbar then
self.scrollbar = require('blink.cmp.lib.window.scrollbar').new({
enable_gutter = self.config.border == 'none' or self.config.border == 'padded',
})
-- Enable the gutter if there's no border, or the border is a space
local enable_gutter = self.config.border == 'padded' or self.config.border == 'none'
local border = self.config.border
if type(border) == 'table' then
local resolved_border = self.expand_border_chars(border)
enable_gutter = resolved_border[4] == '' or resolved_border[4] == ' '
end

self.scrollbar = require('blink.cmp.lib.window.scrollbar').new({ enable_gutter = enable_gutter })
end

return self
Expand Down Expand Up @@ -186,31 +193,37 @@ function win:get_border_size()
top = 1
bottom = 1
elseif type(border) == 'table' then
-- borders can be a table of strings and act differently with different # of chars
-- so we normalize it: https://neovim.io/doc/user/api.html#nvim_open_win()
-- based on nvim-cmp
-- TODO: doesn't handle scrollbar
local resolved_border = {}
while #resolved_border <= 8 do
for _, b in ipairs(border) do
table.insert(resolved_border, type(b) == 'string' and b or b[1])
end
end
local resolved_border = self.expand_border_chars(border)

top = resolved_border[2] == '' and 0 or 1
bottom = resolved_border[6] == '' and 0 or 1
-- TODO: shouldn't this be the length of the string?
left = resolved_border[8] == '' and 0 or 1
right = resolved_border[4] == '' and 0 or 1
top = resolved_border[2] == '' and 0 or 1
bottom = resolved_border[6] == '' and 0 or 1
end

if self.scrollbar and self.scrollbar:is_visible() then
local offset = (border == 'none' or border == 'padded') and 1 or 0
right = right + offset
end
-- If there's a scrollbar, the border on the right must be atleast 1
if self.scrollbar and self.scrollbar:is_visible() then right = math.max(1, right) end

return { vertical = top + bottom, horizontal = left + right, left = left, right = right, top = top, bottom = bottom }
end

--- Gets the characters used for the border, if defined
function win.expand_border_chars(border)
assert(type(border) == 'table', 'Border must be a table')

-- borders can be a table of strings and act differently with different # of chars
-- so we normalize it: https://neovim.io/doc/user/api.html#nvim_open_win()
-- based on nvim-cmp
local resolved_border = {}
while #resolved_border <= 8 do
for _, b in ipairs(border) do
table.insert(resolved_border, type(b) == 'string' and b or b[1])
end
end
return resolved_border
end

--- Gets the height of the window, taking into account the border
function win:get_height()
if not self:is_open() then return 0 end
Expand Down

0 comments on commit ecbac4b

Please sign in to comment.