Skip to content

Commit

Permalink
feat: config.view.entries.vertical_positioning = 'auto'|'above'|'below'
Browse files Browse the repository at this point in the history
'above' works best with vim.opt.scrolloff = 999.

Fixes: hrsh7th#495
Helped-by: Thanatermesis <[email protected]>
  • Loading branch information
llllvvuu authored and jimzk committed Feb 12, 2024
1 parent 18f84c5 commit 21713a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions lua/cmp/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ return function()
entries = {
name = 'custom',
selection_order = 'top_down',
vertical_positioning = 'below',
},
docs = {
auto_open = true,
Expand Down
1 change: 1 addition & 0 deletions lua/cmp/types/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ cmp.ItemField = {
---@class cmp.CustomEntriesViewConfig
---@field name 'custom'
---@field selection_order 'top_down'|'near_cursor'
---@field vertical_positioning 'auto'|'above'|'below'

---@class cmp.NativeEntriesViewConfig
---@field name 'native'
Expand Down
28 changes: 18 additions & 10 deletions lua/cmp/view/custom_entries_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ custom_entries_view.is_direction_top_down = function(self)
end

custom_entries_view.open = function(self, offset, entries)
local completion = config.get().window.completion
local c = config.get()
local completion = c.window.completion
self.offset = offset
self.entries = {}
self.column_width = { abbr = 0, kind = 0, menu = 0 }
Expand Down Expand Up @@ -168,12 +169,25 @@ custom_entries_view.open = function(self, offset, entries)
local border_info = window.get_border_info({ style = completion })
local border_offset_row = border_info.top + border_info.bottom
local border_offset_col = border_info.left + border_info.right
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

local prefers_above = c.view.entries.vertical_positioning == 'above'
local prefers_auto = c.view.entries.vertical_positioning == 'auto'
local cant_fit_at_bottom = vim.o.lines - row - border_offset_row <= math.min(DEFAULT_HEIGHT, height)
local cant_fit_at_top = row - border_offset_row <= math.min(DEFAULT_HEIGHT, height)
local is_in_top_half = math.floor(vim.o.lines * 0.5) > row + border_offset_row
local should_position_above =
cant_fit_at_bottom or
(prefers_above and not cant_fit_at_top) or
(prefers_auto and is_in_top_half)
if should_position_above then
self.bottom_up = true
height = math.min(height, row - 1)
row = row - height - border_offset_row - 1
if row < 0 then
height = height + row
end
else
self.bottom_up = false
end
if math.floor(vim.o.columns * 0.5) <= col + border_offset_col and vim.o.columns - col - border_offset_col <= width then
width = math.min(width, vim.o.columns - 1)
Expand All @@ -183,12 +197,6 @@ custom_entries_view.open = function(self, offset, entries)
end
end

if pos[1] > row then
self.bottom_up = true
else
self.bottom_up = false
end

if not self:is_direction_top_down() then
local n = #self.entries
for i = 1, math.floor(n / 2) do
Expand All @@ -215,9 +223,9 @@ custom_entries_view.open = function(self, offset, entries)
})
-- always set cursor when starting. It will be adjusted on the call to _select
vim.api.nvim_win_set_cursor(self.entries_win.win, { 1, 0 })
if preselect_index > 0 and config.get().preselect == types.cmp.PreselectMode.Item then
if preselect_index > 0 and c.preselect == types.cmp.PreselectMode.Item then
self:_select(preselect_index, { behavior = types.cmp.SelectBehavior.Select, active = false })
elseif not string.match(config.get().completion.completeopt, 'noselect') then
elseif not string.match(c.completion.completeopt, 'noselect') then
if self:is_direction_top_down() then
self:_select(1, { behavior = types.cmp.SelectBehavior.Select, active = false })
else
Expand Down

0 comments on commit 21713a2

Please sign in to comment.