Skip to content

Commit

Permalink
feat: vertical_positioning = 'above'|'below' #1701 integrated with #1955
Browse files Browse the repository at this point in the history
  • Loading branch information
user committed Nov 5, 2024
1 parent 93b9074 commit 99d8bd4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ return function()
entries = {
name = 'custom',
selection_order = 'top_down',
vertical_positioning = 'below',
follow_cursor = false,
},
docs = {
Expand Down
1 change: 1 addition & 0 deletions lua/cmp/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ return function()
entries = {
name = 'custom',
selection_order = 'top_down',
vertical_positioning = 'below',
follow_cursor = false,
},
docs = {
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 @@ -188,6 +188,7 @@ cmp.ItemField = {
---@class cmp.CustomEntriesViewConfig
---@field name 'custom'
---@field selection_order 'top_down'|'near_cursor'
---@field vertical_positioning 'below'|'above'
---@field follow_cursor boolean

---@class cmp.NativeEntriesViewConfig
Expand Down
6 changes: 4 additions & 2 deletions lua/cmp/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ view.open_docs = function(self)
if not self:visible() then
return
end
self.docs_view:open(e, self:_get_entries_view():info())
local bottom_up = self.custom_entries_view.bottom_up
self.docs_view:open(e, self:_get_entries_view():info(), bottom_up)
end)))
end
end
Expand Down Expand Up @@ -297,7 +298,8 @@ view.on_entry_change = async.throttle(function(self)
return
end
if self.is_docs_view_pinned or config.get().view.docs.auto_open then
self.docs_view:open(e, self:_get_entries_view():info())
local bottom_up = self.custom_entries_view.bottom_up
self.docs_view:open(e, self:_get_entries_view():info(), bottom_up)
end
end)))
else
Expand Down
27 changes: 16 additions & 11 deletions lua/cmp/view/custom_entries_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local DEFAULT_HEIGHT = 10 -- @see https://github.com/vim/vim/blob/master/src/pop
---@field private active boolean
---@field private entries cmp.Entry[]
---@field private column_width any
---@field private bottom_up boolean
---@field public event cmp.Event
local custom_entries_view = {}

Expand Down Expand Up @@ -134,7 +135,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
assert(completion, 'config.get() must resolve window.completion with defaults')

self.offset = offset
Expand Down Expand Up @@ -193,12 +195,21 @@ 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 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 should_position_above = cant_fit_at_bottom or (prefers_above and not cant_fit_at_top)

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 @@ -208,12 +219,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 Down Expand Up @@ -247,9 +252,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 Expand Up @@ -457,7 +462,7 @@ custom_entries_view._select = function(self, cursor, option)
0,
})

if not self.bottom_up then
if not self.bottom_up and config.get().view.entries.vertical_positioning == 'below' then
local info = self.entries_win:info()
local border_info = info.border_info
local border_offset_row = border_info.top + border_info.bottom
Expand Down
6 changes: 4 additions & 2 deletions lua/cmp/view/docs_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end
---Open documentation window
---@param e cmp.Entry
---@param view cmp.WindowStyle
docs_view.open = function(self, e, view)
docs_view.open = function(self, e, view, bottom_up)
local documentation = config.get().window.documentation
if not documentation then
return
Expand Down Expand Up @@ -106,6 +106,8 @@ docs_view.open = function(self, e, view)
return self:close()
end

local row = bottom_up and math.max(view.row - (height + border_info.vert - view.height), 1) or view.row

-- Render window.
self.window:option('winblend', documentation.winblend)
self.window:option('winhighlight', documentation.winhighlight)
Expand All @@ -114,7 +116,7 @@ docs_view.open = function(self, e, view)
style = 'minimal',
width = width,
height = height,
row = view.row,
row = row,
col = col + documentation.col_offset,
border = documentation.border,
zindex = documentation.zindex or 50,
Expand Down
3 changes: 2 additions & 1 deletion merged.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

### Fix

- fix: some symbols can't be mapped correctly #2081
- fix: some symbols can't be mapped correctly [#2081](https://github.com/hrsh7th/nvim-cmp/pull/2081)
- support cpying self-referenced table [#2076](https://github.com/hrsh7th/nvim-cmp/pull/2076)
- fix: make <c-;> work again [#2073](https://github.com/hrsh7th/nvim-cmp/pull/2073)
- fix: use \<\* notation for keymap.normalize ([#2069](https://github.com/hrsh7th/nvim-cmp/pull/2069))
Expand Down Expand Up @@ -42,6 +42,7 @@
- @ Multiline snippet ghost text with dynamic window position flip [#1955](https://github.com/hrsh7th/nvim-cmp/pull/1955)
- @ Allow comparators access to the complete list of entries [#1894](https://github.com/hrsh7th/nvim-cmp/pull/1894)
- @ Add option to not reverse select_next_item on bottom_up list ([#1346](https://github.com/hrsh7th/nvim-cmp/pull/1346)) [#1711](https://github.com/hrsh7th/nvim-cmp/pull/1711)
- @ feat: config.view.entries.vertical_positioning = 'above'|'below' [#1701](https://github.com/hrsh7th/nvim-cmp/pull/1701) (integrated with [#1955](https://github.com/hrsh7th/nvim-cmp/pull/1955) from above)
- @ feat: add col_offset option for doc view [#1528](https://github.com/hrsh7th/nvim-cmp/pull/1528)
- @ feat: enabled for sources [#1314](https://github.com/hrsh7th/nvim-cmp/pull/1314)

Expand Down

0 comments on commit 99d8bd4

Please sign in to comment.