Skip to content

Commit bbed640

Browse files
committed
feat: config.view.entries.vertical_positioning = 'above'|'below'
Works best with vim.opt.scrolloff = 999 Fixes: #495 Suggested-by: Thanatermesis <thanatermesis@gmail.com>
1 parent 5dce1b7 commit bbed640

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

lua/cmp/config/default.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ return function()
9494
entries = {
9595
name = 'custom',
9696
selection_order = 'top_down',
97+
vertical_positioning = 'below',
9798
},
9899
docs = {
99100
auto_open = true,

lua/cmp/types/cmp.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ cmp.ItemField = {
176176
---@class cmp.CustomEntriesViewConfig
177177
---@field name 'custom'
178178
---@field selection_order 'top_down'|'near_cursor'
179+
---@field vertical_positioning 'above'|'below'
179180

180181
---@class cmp.NativeEntriesViewConfig
181182
---@field name 'native'

lua/cmp/view/custom_entries_view.lua

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ custom_entries_view.is_direction_top_down = function(self)
117117
end
118118

119119
custom_entries_view.open = function(self, offset, entries)
120-
local completion = config.get().window.completion
120+
local c = config.get()
121+
local completion = c.window.completion
121122
self.offset = offset
122123
self.entries = {}
123124
self.column_width = { abbr = 0, kind = 0, menu = 0 }
@@ -161,12 +162,20 @@ custom_entries_view.open = function(self, offset, entries)
161162
local border_info = window.get_border_info({ style = completion })
162163
local border_offset_row = border_info.top + border_info.bottom
163164
local border_offset_col = border_info.left + border_info.right
164-
if math.floor(vim.o.lines * 0.5) <= row + border_offset_row and vim.o.lines - row - border_offset_row <= math.min(DEFAULT_HEIGHT, height) then
165+
166+
local prefers_above = c.view.entries.vertical_positioning == 'above'
167+
local cant_fit_at_bottom = vim.o.lines - row - border_offset_row <= math.min(DEFAULT_HEIGHT, height)
168+
local cant_fit_at_top = row - border_offset_row <= math.min(DEFAULT_HEIGHT, height)
169+
local should_position_above = cant_fit_at_bottom or (prefers_above and not cant_fit_at_top)
170+
if should_position_above then
171+
self.bottom_up = true
165172
height = math.min(height, row - 1)
166173
row = row - height - border_offset_row - 1
167174
if row < 0 then
168175
height = height + row
169176
end
177+
else
178+
self.bottom_up = false
170179
end
171180
if math.floor(vim.o.columns * 0.5) <= col + border_offset_col and vim.o.columns - col - border_offset_col <= width then
172181
width = math.min(width, vim.o.columns - 1)
@@ -176,12 +185,6 @@ custom_entries_view.open = function(self, offset, entries)
176185
end
177186
end
178187

179-
if pos[1] > row then
180-
self.bottom_up = true
181-
else
182-
self.bottom_up = false
183-
end
184-
185188
if not self:is_direction_top_down() then
186189
local n = #self.entries
187190
for i = 1, math.floor(n / 2) do
@@ -208,9 +211,9 @@ custom_entries_view.open = function(self, offset, entries)
208211
})
209212
-- always set cursor when starting. It will be adjusted on the call to _select
210213
vim.api.nvim_win_set_cursor(self.entries_win.win, { 1, 0 })
211-
if preselect_index > 0 and config.get().preselect == types.cmp.PreselectMode.Item then
214+
if preselect_index > 0 and c.preselect == types.cmp.PreselectMode.Item then
212215
self:_select(preselect_index, { behavior = types.cmp.SelectBehavior.Select, active = false })
213-
elseif not string.match(config.get().completion.completeopt, 'noselect') then
216+
elseif not string.match(c.completion.completeopt, 'noselect') then
214217
if self:is_direction_top_down() then
215218
self:_select(1, { behavior = types.cmp.SelectBehavior.Select, active = false })
216219
else

0 commit comments

Comments
 (0)