Skip to content

Commit d6a81d3

Browse files
committed
fix: window placement with border
1 parent 7c05431 commit d6a81d3

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

lua/blink/cmp/windows/autocomplete.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ function autocomplete.setup()
1717
max_width = autocmp_config.max_width,
1818
max_height = autocmp_config.max_height,
1919
border = autocmp_config.border,
20+
winhighlight = autocmp_config.winhighlight,
2021
cursorline = true,
21-
winhighlight = autocmp_config.winhighlight or 'Normal:Pmenu,FloatBorder:Pmenu,CursorLine:PmenuSel,Search:None',
22-
scrolloff = 2,
22+
scrolloff = autocmp_config.scrolloff,
2323
})
2424

2525
-- Setting highlights is slow and we update on every keystroke so we instead use a decoration provider
@@ -107,7 +107,7 @@ function autocomplete.update_position(context)
107107

108108
win:update_size()
109109

110-
local height = vim.api.nvim_win_get_height(winnr)
110+
local height = win:get_height()
111111
local screen_height = vim.api.nvim_win_get_height(0)
112112
local screen_scroll_range = win.get_screen_scroll_range()
113113

lua/blink/cmp/windows/documentation.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ function docs.update_position()
110110
local direction_priority = autocomplete_win_is_up and config.direction_priority.autocomplete_north
111111
or config.direction_priority.autocomplete_south
112112

113-
local height = vim.api.nvim_win_get_height(winnr)
114-
local width = vim.api.nvim_win_get_width(winnr)
113+
local height = docs.win:get_height()
114+
local width = docs.win:get_width()
115115

116116
local space_above = autocomplete_win_config.row - 1 > height
117117
local space_below = screen_height - autocomplete_win_config.height - autocomplete_win_config.row > height

lua/blink/cmp/windows/lib.lua

+49
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,53 @@ function win:update_size()
105105
end
106106

107107
-- todo: fix nvim_win_text_height
108+
-- @return number
108109
function win:get_content_height()
109110
if not self:is_open() then return 0 end
110111
return vim.api.nvim_win_text_height(self:get_win(), {}).all
111112
end
112113

114+
--- @return { vertical: number, horizontal: number }
115+
function win:get_border_size()
116+
if not self:is_open() then return { vertical = 0, horizontal = 0 } end
117+
118+
local border = self.config.border
119+
if border == 'none' then
120+
return { vertical = 0, horizontal = 0 }
121+
elseif border == 'padded' then
122+
return { vertical = 0, horizontal = 1 }
123+
elseif border == 'shadow' then
124+
return { vertical = 1, horizontal = 1 }
125+
elseif type(border) == 'string' then
126+
return { vertical = 2, horizontal = 2 }
127+
elseif type(border) == 'table' and border ~= nil then
128+
-- borders can be a table of strings and act differently with different # of chars
129+
-- so we normalize it: https://neovim.io/doc/user/api.html#nvim_open_win()
130+
-- based on nvim-cmp
131+
local resolved_border = {}
132+
while #resolved_border <= 8 do
133+
for _, b in ipairs(border) do
134+
table.insert(resolved_border, type(b) == 'string' and b or b[1])
135+
end
136+
end
137+
138+
local top = resolved_border[2] == '' and 0 or 1
139+
local bottom = resolved_border[6] == '' and 0 or 1
140+
local left = resolved_border[8] == '' and 0 or 1
141+
local right = resolved_border[4] == '' and 0 or 1
142+
return { vertical = top + bottom, horizontal = left + right }
143+
end
144+
145+
return { vertical = 0, horizontal = 0 }
146+
end
147+
148+
--- @return number
149+
function win:get_height()
150+
if not self:is_open() then return 0 end
151+
return vim.api.nvim_win_get_height(self:get_win()) + self:get_border_size().vertical
152+
end
153+
154+
--- @return number
113155
function win:get_content_width()
114156
if not self:is_open() then return 0 end
115157
local max_width = 0
@@ -119,6 +161,13 @@ function win:get_content_width()
119161
return max_width
120162
end
121163

164+
--- @return number
165+
function win:get_width()
166+
if not self:is_open() then return 0 end
167+
return vim.api.nvim_win_get_width(self:get_win()) + self:get_border_size().horizontal
168+
end
169+
170+
--- @return { bufnr: number, start_line: number, end_line: number, horizontal_offset: number }
122171
function win.get_screen_scroll_range()
123172
local bufnr = vim.api.nvim_win_get_buf(0)
124173
local line_count = vim.api.nvim_buf_line_count(bufnr)

lua/blink/cmp/windows/signature.lua

+5-13
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,18 @@ function signature.update_position(context)
101101
local autocomplete_win_is_up = autocomplete_win_config and autocomplete_win_config.row - cursor_screen_row < 0
102102
local direction = autocomplete_win_is_up and 's' or 'n'
103103

104-
local height = vim.api.nvim_win_get_height(winnr)
104+
local height = signature.win:get_height()
105105
local screen_height = vim.api.nvim_win_get_height(0)
106106
local screen_scroll_range = signature.win.get_screen_scroll_range()
107107

108-
local cursor = vim.api.nvim_win_get_cursor(0)
109-
local cursor_row = cursor[1]
110-
local cursor_col = cursor[2]
111-
112-
-- place the window at the start col of the current text we're fuzzy matching against
113-
-- so the window doesnt move around as we type
114-
local col = context.start_col - cursor_col - 1
115-
116108
-- detect if there's space above/below the cursor
117-
local is_space_below = autocomplete_win_is_up ~= false
118-
and screen_height - cursor_row - screen_scroll_range.start_line > height
119-
local is_space_above = not autocomplete_win_is_up and cursor_row - screen_scroll_range.start_line > height
109+
local cursor_row = vim.api.nvim_win_get_cursor(0)[1]
110+
local is_space_below = screen_height - cursor_row - screen_scroll_range.start_line > height
111+
local is_space_above = cursor_row - screen_scroll_range.start_line > height
120112

121113
-- default to the user's preference but attempt to use the other options
122114
local row = direction == 's' and 1 or -height
123-
vim.api.nvim_win_set_config(winnr, { relative = 'cursor', row = row, col = col })
115+
vim.api.nvim_win_set_config(winnr, { relative = 'cursor', row = row, col = -1 })
124116
end
125117

126118
return signature

0 commit comments

Comments
 (0)