Skip to content

Commit 77f037c

Browse files
committed
feat: avoid taking up space when scrollbar is hidden
1 parent c8cf209 commit 77f037c

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

lua/blink/cmp/windows/lib/init.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ function win:get_border_size()
155155

156156
local border = self.config.border
157157
if border == 'none' then
158-
if self.config.scrollbar then return { vertical = 0, horizontal = 1, left = 0, right = 1, top = 0, bottom = 0 } end
158+
if self.scrollbar and self.scrollbar:is_visible() then
159+
return { vertical = 0, horizontal = 1, left = 0, right = 1, top = 0, bottom = 0 }
160+
end
159161
return { vertical = 0, horizontal = 0, left = 0, right = 0, top = 0, bottom = 0 }
160162
elseif border == 'padded' then
161163
return { vertical = 0, horizontal = 2, left = 1, right = 1, top = 0, bottom = 0 }
@@ -182,7 +184,9 @@ function win:get_border_size()
182184
return { vertical = top + bottom, horizontal = left + right, left = left, right = right, top = top, bottom = bottom }
183185
end
184186

185-
if self.config.scrollbar then return { vertical = 0, horizontal = 1, left = 0, right = 1, top = 0, bottom = 0 } end
187+
if self.scrollbar and self.scrollbar:is_visible() then
188+
return { vertical = 0, horizontal = 1, left = 0, right = 1, top = 0, bottom = 0 }
189+
end
186190
return { vertical = 0, horizontal = 0, left = 0, right = 0, top = 0, bottom = 0 }
187191
end
188192

lua/blink/cmp/windows/lib/scrollbar/geometry.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ local function get_win_buf_height(target_win)
2929
end
3030

3131
--- @param target_win number
32-
--- @return { should_hide_thumb: boolean, thumb: blink.cmp.ScrollbarGeometry, gutter: blink.cmp.ScrollbarGeometry }
32+
--- @return { should_hide: boolean, thumb: blink.cmp.ScrollbarGeometry, gutter: blink.cmp.ScrollbarGeometry }
3333
function M.get_geometry(target_win)
3434
local width = vim.api.nvim_win_get_width(target_win)
3535
local height = vim.api.nvim_win_get_height(target_win)
@@ -52,7 +52,7 @@ function M.get_geometry(target_win)
5252
}
5353

5454
return {
55-
should_hide_thumb = height >= buf_height,
55+
should_hide = height >= buf_height,
5656
thumb = vim.tbl_deep_extend('force', common_geometry, { height = thumb_height, zindex = zindex + 2 }),
5757
gutter = vim.tbl_deep_extend('force', common_geometry, { row = 0, height = height, zindex = zindex + 1 }),
5858
}

lua/blink/cmp/windows/lib/scrollbar/init.lua

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
--- @field autocmd? number
88
---
99
--- @field new fun(opts: blink.cmp.ScrollbarConfig): blink.cmp.Scrollbar
10+
--- @field is_visible fun(self: blink.cmp.Scrollbar): boolean
1011
--- @field is_mounted fun(self: blink.cmp.Scrollbar): boolean
1112
--- @field mount fun(self: blink.cmp.Scrollbar, target_win: number)
1213
--- @field unmount fun(self: blink.cmp.Scrollbar)
@@ -21,6 +22,8 @@ function scrollbar.new(opts)
2122
return self
2223
end
2324

25+
function scrollbar:is_visible() return self.win:is_visible() end
26+
2427
function scrollbar:is_mounted() return self.autocmd ~= nil end
2528

2629
function scrollbar:mount(target_win)
@@ -40,12 +43,10 @@ function scrollbar:mount(target_win)
4043
if not vim.api.nvim_win_is_valid(target_win) then return self:unmount() end
4144

4245
local updated_geometry = require('blink.cmp.windows.lib.scrollbar.geometry').get_geometry(target_win)
46+
if updated_geometry.should_hide then return self.win:hide() end
47+
48+
self.win:show_thumb(updated_geometry.thumb)
4349
self.win:show_gutter(updated_geometry.gutter)
44-
if updated_geometry.should_hide_thumb then
45-
self.win:hide_thumb()
46-
else
47-
self.win:show_thumb(updated_geometry.thumb)
48-
end
4950
end
5051
-- HACK: for some reason, the autocmds don't fire on the initial mount
5152
-- so we apply after on the next event loop iteration after the windows are definitely setup

lua/blink/cmp/windows/lib/scrollbar/win.lua

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
--- @field buf? number
88
---
99
--- @field new fun(opts: blink.cmp.ScrollbarConfig): blink.cmp.ScrollbarWin
10+
--- @field is_visible fun(self: blink.cmp.ScrollbarWin): boolean
1011
--- @field show_thumb fun(self: blink.cmp.ScrollbarWin, geometry: blink.cmp.ScrollbarGeometry)
1112
--- @field show_gutter fun(self: blink.cmp.ScrollbarWin, geometry: blink.cmp.ScrollbarGeometry)
1213
--- @field hide_thumb fun(self: blink.cmp.ScrollbarWin)
@@ -18,6 +19,8 @@ local scrollbar_win = {}
1819

1920
function scrollbar_win.new(opts) return setmetatable(opts, { __index = scrollbar_win }) end
2021

22+
function scrollbar_win:is_visible() return self.thumb_win ~= nil and vim.api.nvim_win_is_valid(self.thumb_win) end
23+
2124
function scrollbar_win:show_thumb(geometry)
2225
-- create window if it doesn't exist
2326
if self.thumb_win == nil or not vim.api.nvim_win_is_valid(self.thumb_win) then

0 commit comments

Comments
 (0)