Skip to content

Commit ba62bda

Browse files
committed
fix: autocomplete window positioning with borders
Closes #29
1 parent d20e34d commit ba62bda

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

lua/blink/cmp/windows/autocomplete.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function autocomplete.update_position(context)
105105

106106
-- detect if there's space above/below the cursor
107107
-- todo: should pick the largest space if both are false and limit height of the window
108-
local is_space_below = screen_height - (cursor_row - screen_scroll_range.start_line) >= height
108+
local is_space_below = screen_height - (cursor_row - screen_scroll_range.start_line) > height
109109
local is_space_above = cursor_row - screen_scroll_range.start_line > height
110110

111111
-- default to the user's preference but attempt to use the other options

lua/blink/cmp/windows/documentation.lua

+20-8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ function docs.update_position()
100100
local autocomplete_winnr = autocomplete.win:get_win()
101101
if not autocomplete_winnr then return end
102102
local autocomplete_win_config = vim.api.nvim_win_get_config(autocomplete_winnr)
103+
local autocomplete_win_width = autocomplete.win:get_width()
104+
local autocomplete_win_height = autocomplete.win:get_height()
105+
local autocomplete_border_size = autocomplete.win:get_border_size()
103106

104107
local screen_width = vim.api.nvim_win_get_width(0)
105108
local screen_height = vim.api.nvim_win_get_height(0)
@@ -113,33 +116,42 @@ function docs.update_position()
113116
local width = docs.win:get_width()
114117

115118
local space_above = autocomplete_win_config.row - 1 > height
116-
local space_below = screen_height - autocomplete_win_config.height - autocomplete_win_config.row > height
119+
local space_below = screen_height - autocomplete_win_height - autocomplete_win_config.row > height
117120
local space_left = autocomplete_win_config.col > width
118-
local space_right = screen_width - autocomplete_win_config.width - autocomplete_win_config.col > width
121+
local space_right = screen_width - autocomplete_win_width - autocomplete_win_config.col > width
119122

120123
local function set_config(opts)
121124
vim.api.nvim_win_set_config(winnr, { relative = 'win', win = autocomplete_winnr, row = opts.row, col = opts.col })
122125
end
123126
for _, direction in ipairs(direction_priority) do
124127
if direction == 'n' and space_above then
125128
if autocomplete_win_is_up then
126-
set_config({ row = -height, col = 0 })
129+
set_config({ row = -height - autocomplete_border_size.top, col = -autocomplete_border_size.left })
127130
else
128-
set_config({ row = -1 - height, col = 0 })
131+
set_config({ row = -1 - height - autocomplete_border_size.top, col = -autocomplete_border_size.left })
129132
end
130133
return
131134
elseif direction == 's' and space_below then
132135
if autocomplete_win_is_up then
133-
set_config({ row = 1 + autocomplete_win_config.height, col = 0 })
136+
set_config({
137+
row = 1 + autocomplete_win_height - autocomplete_border_size.top,
138+
col = -autocomplete_border_size.left,
139+
})
134140
else
135-
set_config({ row = autocomplete_win_config.height, col = 0 })
141+
set_config({
142+
row = autocomplete_win_height - autocomplete_border_size.top,
143+
col = -autocomplete_border_size.left,
144+
})
136145
end
137146
return
138147
elseif direction == 'e' and space_right then
139-
set_config({ row = 0, col = autocomplete_win_config.width })
148+
set_config({
149+
row = -autocomplete_border_size.top,
150+
col = autocomplete_win_config.width + autocomplete_border_size.left,
151+
})
140152
return
141153
elseif direction == 'w' and space_left then
142-
set_config({ row = 0, col = -1 - width })
154+
set_config({ row = -autocomplete_border_size.top, col = -width - autocomplete_border_size.left })
143155
return
144156
end
145157
end

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,19 @@ function win:get_content_height()
111111
return vim.api.nvim_win_text_height(self:get_win(), {}).all
112112
end
113113

114-
--- @return { vertical: number, horizontal: number }
114+
--- @return { vertical: number, horizontal: number, left: number, right: number, top: number, bottom: number }
115115
function win:get_border_size()
116-
if not self:is_open() then return { vertical = 0, horizontal = 0 } end
116+
if not self:is_open() then return { vertical = 0, horizontal = 0, left = 0, right = 0, top = 0, bottom = 0 } end
117117

118118
local border = self.config.border
119119
if border == 'none' then
120-
return { vertical = 0, horizontal = 0 }
120+
return { vertical = 0, horizontal = 0, left = 0, right = 0, top = 0, bottom = 0 }
121121
elseif border == 'padded' then
122-
return { vertical = 0, horizontal = 1 }
122+
return { vertical = 0, horizontal = 1, left = 1, right = 0, top = 0, bottom = 0 }
123123
elseif border == 'shadow' then
124-
return { vertical = 1, horizontal = 1 }
124+
return { vertical = 1, horizontal = 1, left = 0, right = 1, top = 0, bottom = 1 }
125125
elseif type(border) == 'string' then
126-
return { vertical = 2, horizontal = 2 }
126+
return { vertical = 2, horizontal = 2, left = 1, right = 1, top = 1, bottom = 1 }
127127
elseif type(border) == 'table' and border ~= nil then
128128
-- borders can be a table of strings and act differently with different # of chars
129129
-- so we normalize it: https://neovim.io/doc/user/api.html#nvim_open_win()
@@ -139,10 +139,10 @@ function win:get_border_size()
139139
local bottom = resolved_border[6] == '' and 0 or 1
140140
local left = resolved_border[8] == '' and 0 or 1
141141
local right = resolved_border[4] == '' and 0 or 1
142-
return { vertical = top + bottom, horizontal = left + right }
142+
return { vertical = top + bottom, horizontal = left + right, left = left, right = right, top = top, bottom = bottom }
143143
end
144144

145-
return { vertical = 0, horizontal = 0 }
145+
return { vertical = 0, horizontal = 0, left = 0, right = 0, top = 0, bottom = 0 }
146146
end
147147

148148
--- @return number

0 commit comments

Comments
 (0)