Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: padded window #315

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ MiniDeps.add({
| `BlinkCmpKind` | Special | Kind icon/text of the completion item |
| `BlinkCmpKind<kind>` | Special | Kind icon/text of the completion item |
| `BlinkCmpDoc` | NormalFloat | The documentation window |
| `BlinkCmpDocBorder` | FloatBorder | The documentation window border |
| `BlinkCmpDocBorder` | NormalFloat | The documentation window border |
| `BlinkCmpDocCursorLine` | Visual | The documentation window cursor line |
| `BlinkCmpSignatureHelp` | NormalFloat | The signature help window |
| `BlinkCmpSignatureHelpBorder` | FloatBorder | The signature help window border |
| `BlinkCmpSignatureHelpBorder` | NormalFloat | The signature help window border |
| `BlinkCmpSignatureHelpActiveParameter` | LspSignatureActiveParameter | Active parameter of the signature help |

</details>
Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ cmp.add_default_highlights = function()
set_hl('BlinkCmpMenuSelection', { link = 'PmenuSel' })

set_hl('BlinkCmpDoc', { link = 'NormalFloat' })
set_hl('BlinkCmpDocBorder', { link = 'FloatBorder' })
set_hl('BlinkCmpDocBorder', { link = 'NormalFloat' })
set_hl('BlinkCmpDocCursorLine', { link = 'Visual' })

set_hl('BlinkCmpSignatureHelp', { link = 'NormalFloat' })
set_hl('BlinkCmpSignatureHelpBorder', { link = 'FloatBorder' })
set_hl('BlinkCmpSignatureHelpBorder', { link = 'NormalFloat' })
set_hl('BlinkCmpSignatureHelpActiveParameter', { link = 'LspSignatureActiveParameter' })
end

Expand Down
46 changes: 27 additions & 19 deletions lua/blink/cmp/windows/lib/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function win.new(config)
}

if self.config.scrollbar then
self.scrollbar = require('blink.cmp.windows.lib.scrollbar').new({ enable_gutter = self.config.border == 'none' })
self.scrollbar = require('blink.cmp.windows.lib.scrollbar').new({
enable_gutter = self.config.border == 'none' or self.config.border == 'padded',
})
end

return self
Expand Down Expand Up @@ -149,19 +151,24 @@ end
function win:get_border_size()
if not self:is_open() then return { vertical = 0, horizontal = 0, left = 0, right = 0, top = 0, bottom = 0 } end

local left = 0
local right = 0
local top = 0
local bottom = 0

local border = self.config.border
if border == 'none' then
if self.scrollbar and self.scrollbar:is_visible() then
return { vertical = 0, horizontal = 1, left = 0, right = 1, top = 0, bottom = 0 }
end
return { vertical = 0, horizontal = 0, left = 0, right = 0, top = 0, bottom = 0 }
elseif border == 'padded' then
return { vertical = 0, horizontal = 2, left = 1, right = 1, top = 0, bottom = 0 }
if border == 'padded' then
left = 1
right = 1
elseif border == 'shadow' then
return { vertical = 1, horizontal = 1, left = 0, right = 1, top = 0, bottom = 1 }
elseif type(border) == 'string' then
return { vertical = 2, horizontal = 2, left = 1, right = 1, top = 1, bottom = 1 }
elseif type(border) == 'table' and border ~= nil then
right = 1
bottom = 1
elseif type(border) == 'string' and border ~= 'none' then
left = 1
right = 1
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
Expand All @@ -173,17 +180,18 @@ function win:get_border_size()
end
end

local top = resolved_border[2] == '' and 0 or 1
local bottom = resolved_border[6] == '' and 0 or 1
local left = resolved_border[8] == '' and 0 or 1
local right = resolved_border[4] == '' and 0 or 1
return { vertical = top + bottom, horizontal = left + right, left = left, right = right, top = top, bottom = bottom }
top = resolved_border[2] == '' and 0 or 1
bottom = resolved_border[6] == '' and 0 or 1
left = resolved_border[8] == '' and 0 or 1
right = resolved_border[4] == '' and 0 or 1
end

if self.scrollbar and self.scrollbar:is_visible() then
return { vertical = 0, horizontal = 1, left = 0, right = 1, top = 0, bottom = 0 }
local offset = (border == 'none' or border == 'padded') and 1 or 0
right = right + offset
end
return { vertical = 0, horizontal = 0, left = 0, right = 0, top = 0, bottom = 0 }

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

--- Gets the height of the window, taking into account the border
Expand Down
25 changes: 16 additions & 9 deletions lua/blink/cmp/windows/lib/scrollbar/geometry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,34 @@ local function get_win_buf_height(target_win)
return height
end

--- @param border string|string[]
--- @return number
local function get_col_offset(border)
-- we only need an extra offset when working with a padded window
if type(border) == 'table' and border[1] == ' ' and border[4] == ' ' and border[7] == ' ' and border[8] == ' ' then
return 1
end
return 0
end

--- @param target_win number
--- @return { should_hide: boolean, thumb: blink.cmp.ScrollbarGeometry, gutter: blink.cmp.ScrollbarGeometry }
function M.get_geometry(target_win)
local width = vim.api.nvim_win_get_width(target_win)
local height = vim.api.nvim_win_get_height(target_win)
local zindex = vim.api.nvim_win_get_config(target_win).zindex or 1
local config = vim.api.nvim_win_get_config(target_win)
local width = config.width
local height = config.height
local zindex = config.zindex

local buf_height = get_win_buf_height(target_win)

local thumb_height = math.max(1, math.floor(height * height / buf_height + 0.5) - 1)

local start_line = math.max(1, vim.fn.line('w0', target_win))

local pct = (start_line - 1) / (buf_height - height)

local thumb_height = math.max(1, math.floor(height * height / buf_height + 0.5) - 1)
local thumb_offset = math.floor((pct * (height - thumb_height)) + 0.5)

local common_geometry = {
width = 1,
row = thumb_offset,
col = width,
col = width + get_col_offset(config.border),
relative = 'win',
win = target_win,
}
Expand Down
6 changes: 4 additions & 2 deletions lua/blink/cmp/windows/lib/scrollbar/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
--- @field enable_gutter boolean

--- @class blink.cmp.Scrollbar
--- @field target_win? number
--- @field win? blink.cmp.ScrollbarWin
--- @field win blink.cmp.ScrollbarWin
--- @field autocmd? number
--- @field target_win? number
---
--- @field new fun(opts: blink.cmp.ScrollbarConfig): blink.cmp.Scrollbar
--- @field is_visible fun(self: blink.cmp.Scrollbar): boolean
Expand Down Expand Up @@ -56,13 +56,15 @@ function scrollbar:mount(target_win)
{ 'WinScrolled', 'WinClosed', 'WinResized', 'CursorMoved', 'CursorMovedI' },
{ callback = update }
)
self.target_win = target_win
end

function scrollbar:unmount()
self.win:hide()

if self.autocmd then vim.api.nvim_del_autocmd(self.autocmd) end
self.autocmd = nil
self.target_win = nil
end

return scrollbar
20 changes: 10 additions & 10 deletions lua/blink/cmp/windows/lib/scrollbar/win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ function scrollbar_win:show_thumb(geometry)
-- create window if it doesn't exist
if self.thumb_win == nil or not vim.api.nvim_win_is_valid(self.thumb_win) then
self.thumb_win = self:_make_win(geometry, 'BlinkCmpScrollBarThumb')
else
-- update with the geometry
local thumb_existing_config = vim.api.nvim_win_get_config(self.thumb_win)
local thumb_config = vim.tbl_deep_extend('force', thumb_existing_config, geometry)
vim.api.nvim_win_set_config(self.thumb_win, thumb_config)
end

-- update with the geometry
local thumb_existing_config = vim.api.nvim_win_get_config(self.thumb_win)
local thumb_config = vim.tbl_deep_extend('force', thumb_existing_config, geometry)
vim.api.nvim_win_set_config(self.thumb_win, thumb_config)
end

function scrollbar_win:show_gutter(geometry)
Expand All @@ -39,12 +39,12 @@ function scrollbar_win:show_gutter(geometry)
-- create window if it doesn't exist
if self.gutter_win == nil or not vim.api.nvim_win_is_valid(self.gutter_win) then
self.gutter_win = self:_make_win(geometry, 'BlinkCmpScrollBarGutter')
else
-- update with the geometry
local gutter_existing_config = vim.api.nvim_win_get_config(self.gutter_win)
local gutter_config = vim.tbl_deep_extend('force', gutter_existing_config, geometry)
vim.api.nvim_win_set_config(self.gutter_win, gutter_config)
end

-- update with the geometry
local gutter_existing_config = vim.api.nvim_win_get_config(self.gutter_win)
local gutter_config = vim.tbl_deep_extend('force', gutter_existing_config, geometry)
vim.api.nvim_win_set_config(self.gutter_win, gutter_config)
end

function scrollbar_win:hide_thumb()
Expand Down