mirror of
https://github.com/hrsh7th/nvim-cmp
synced 2024-09-16 20:54:03 +02:00
up
This commit is contained in:
parent
9f270f7144
commit
630f3633b8
7 changed files with 118 additions and 81 deletions
|
@ -21,10 +21,34 @@ return function()
|
|||
|
||||
window = {
|
||||
completion = {
|
||||
border = { '', '', '', '', '', '' , '', '' },
|
||||
border = { '', '', '', '', '', '', '', '' },
|
||||
max_width = -1,
|
||||
max_height = -1,
|
||||
win_mode = {
|
||||
default = {
|
||||
winhighlight = 'NormalFloat:Pmenu,FloatBorder:Pmenu,CursorLine:PmenuSel,Search:None',
|
||||
scrollbar = '',
|
||||
},
|
||||
bordered = {
|
||||
winhighlight = 'NormalFloat:Normal,FloatBorder:Normal,FloatBorder:Normal,CursorLine:Visual,Search:None',
|
||||
scrollbar = '║',
|
||||
},
|
||||
}
|
||||
},
|
||||
documentation = {
|
||||
border = { '', '', '', ' ', '', '' , '', ' ' },
|
||||
border = { '', '', '', ' ', '', '', '', ' ' },
|
||||
max_width = math.floor((WIDE_HEIGHT * 2) * (vim.o.columns / (WIDE_HEIGHT * 2 * 16 / 9))),
|
||||
max_height = math.floor(WIDE_HEIGHT * (WIDE_HEIGHT / vim.o.lines)),
|
||||
win_mode = {
|
||||
default = {
|
||||
winhighlight = 'FloatBorder:NormalFloat,CursorLine:Visual,Search:None',
|
||||
scrollbar = '',
|
||||
},
|
||||
bordered = {
|
||||
winhighlight = 'NormalFloat:Normal,FloatBorder:Normal,FloatBorder:Normal,CursorLine:Visual,Search:None',
|
||||
scrollbar = '║',
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
|
@ -36,13 +60,6 @@ return function()
|
|||
|
||||
preselect = types.cmp.PreselectMode.Item,
|
||||
|
||||
documentation = {
|
||||
border = { '', '', '', ' ', '', '', '', ' ' },
|
||||
winhighlight = 'NormalFloat:NormalFloat,FloatBorder:NormalFloat',
|
||||
maxwidth = math.floor((WIDE_HEIGHT * 2) * (vim.o.columns / (WIDE_HEIGHT * 2 * 16 / 9))),
|
||||
maxheight = math.floor(WIDE_HEIGHT * (WIDE_HEIGHT / vim.o.lines)),
|
||||
},
|
||||
|
||||
confirmation = {
|
||||
default_behavior = types.cmp.ConfirmBehavior.Insert,
|
||||
get_commit_characters = function(commit_characters)
|
||||
|
|
|
@ -74,7 +74,7 @@ cmp.ItemField.Menu = 'menu'
|
|||
---@field public enabled fun():boolean|boolean
|
||||
---@field public preselect cmp.PreselectMode
|
||||
---@field public completion cmp.CompletionConfig
|
||||
---@field public documentation cmp.DocumentationConfig|"false"
|
||||
---@field public window cmp.WindowConfig
|
||||
---@field public confirmation cmp.ConfirmationConfig
|
||||
---@field public sorting cmp.SortingConfig
|
||||
---@field public formatting cmp.FormattingConfig
|
||||
|
@ -90,12 +90,9 @@ cmp.ItemField.Menu = 'menu'
|
|||
---@field public keyword_length number
|
||||
---@field public get_trigger_characters fun(trigger_characters: string[]): string[]
|
||||
|
||||
---@class cmp.DocumentationConfig
|
||||
---@field public border string[]
|
||||
---@field public winhighlight string
|
||||
---@field public maxwidth number|nil
|
||||
---@field public maxheight number|nil
|
||||
---@field public zindex number|nil
|
||||
---@class cmp.WindowConfig
|
||||
---@field public completion cmp.WindowOption
|
||||
---@field public documentation cmp.WindowOption
|
||||
|
||||
---@class cmp.ConfirmationConfig
|
||||
---@field public default_behavior cmp.ConfirmBehavior
|
||||
|
@ -129,4 +126,16 @@ cmp.ItemField.Menu = 'menu'
|
|||
---@field public max_item_count number|nil
|
||||
---@field public group_index number|nil
|
||||
|
||||
---@class cmp.WindowOption
|
||||
---@field public winhighlight { bordered: string, padded: string }
|
||||
---@field public border string|string[]
|
||||
---@field public win_mode { default: cmp.WindowModeOption, bordered: cmp.WindowModeOption }
|
||||
---@field public max_width number
|
||||
---@field public max_height number
|
||||
---@field public zindex number
|
||||
|
||||
---@class cmp.WindowModeOption
|
||||
---@field public winhighlight string
|
||||
---@field public scrollbar string
|
||||
|
||||
return cmp
|
||||
|
|
|
@ -168,22 +168,6 @@ misc.to_vimindex = function(text, utfindex)
|
|||
return utfindex + 1
|
||||
end
|
||||
|
||||
---Redraw utiilty.
|
||||
misc.redraw = setmetatable({
|
||||
doing = false
|
||||
}, {
|
||||
__call = function(self)
|
||||
if self.doing then
|
||||
return
|
||||
end
|
||||
self.doing = true
|
||||
vim.schedule(function()
|
||||
self.doing = false
|
||||
vim.cmd([[redraw]])
|
||||
end)
|
||||
end
|
||||
})
|
||||
|
||||
---Mark the function as deprecated
|
||||
misc.deprecated = function(fn, msg)
|
||||
local printed = false
|
||||
|
@ -202,21 +186,36 @@ misc.redraw = setmetatable({
|
|||
force = false,
|
||||
}, {
|
||||
__call = function(self, force)
|
||||
if self.doing then
|
||||
return
|
||||
end
|
||||
self.doing = true
|
||||
self.force = not not force
|
||||
vim.schedule(function()
|
||||
if self.force then
|
||||
vim.cmd([[redraw!]])
|
||||
else
|
||||
vim.cmd([[redraw]])
|
||||
end
|
||||
self.doing = false
|
||||
self.force = false
|
||||
end)
|
||||
end
|
||||
if not self.doing then
|
||||
self.doing = true
|
||||
vim.schedule(function()
|
||||
if self.force then
|
||||
vim.cmd([[redraw!]])
|
||||
else
|
||||
vim.cmd([[redraw]])
|
||||
end
|
||||
self.doing = false
|
||||
self.force = false
|
||||
end)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
misc.rep = function(string_or_array, count)
|
||||
if type(string_or_array) == 'string' then
|
||||
return string.rep(string_or_array, count)
|
||||
end
|
||||
if count == 0 then
|
||||
return {}
|
||||
end
|
||||
local new_array = {}
|
||||
for _ = 1, count do
|
||||
for _, v in ipairs(string_or_array) do
|
||||
table.insert(new_array, v)
|
||||
end
|
||||
end
|
||||
return new_array
|
||||
end
|
||||
|
||||
return misc
|
||||
|
|
|
@ -20,6 +20,7 @@ local api = require('cmp.utils.api')
|
|||
---@field public style cmp.WindowStyle
|
||||
---@field public window_opt table<string, any>
|
||||
---@field public buffer_opt table<string, any>
|
||||
---@field public scrollbar string
|
||||
local window = {}
|
||||
|
||||
---new
|
||||
|
@ -33,9 +34,16 @@ window.new = function()
|
|||
self.style = {}
|
||||
self.window_opt = {}
|
||||
self.buffer_opt = {}
|
||||
self.scrollbar = ''
|
||||
return self
|
||||
end
|
||||
|
||||
---Set scrollbar character.
|
||||
---@param char string
|
||||
window.set_scrollbar = function(self, char)
|
||||
self.scrollbar = char
|
||||
end
|
||||
|
||||
---Set window option.
|
||||
---NOTE: If the window already visible, immediately applied to it.
|
||||
---@param key string
|
||||
|
@ -152,6 +160,7 @@ window.update = function(self)
|
|||
vim.api.nvim_win_set_option(self.sbar_win, 'winhighlight', 'EndOfBuffer:PmenuSbar,Normal:PmenuSbar,NormalNC:PmenuSbar,NormalFloat:PmenuSbar')
|
||||
end
|
||||
end
|
||||
|
||||
local style2 = {}
|
||||
style2.relative = 'editor'
|
||||
style2.style = 'minimal'
|
||||
|
@ -160,12 +169,17 @@ window.update = function(self)
|
|||
style2.row = analyzed.row + area_offset + bar_offset
|
||||
style2.col = analyzed.col + analyzed.width - 1
|
||||
style2.zindex = (self.style.zindex and (self.style.zindex + 2) or 2)
|
||||
vim.api.nvim_buf_set_lines(buffer.ensure(self.name .. 'thumb_buf'), 0, -1, false, misc.rep({ self.scrollbar }, style2.height))
|
||||
if self.thumb_win and vim.api.nvim_win_is_valid(self.thumb_win) then
|
||||
vim.api.nvim_win_set_config(self.thumb_win, style2)
|
||||
else
|
||||
style2.noautocmd = true
|
||||
self.thumb_win = vim.api.nvim_open_win(buffer.ensure(self.name .. 'thumb_win'), false, style2)
|
||||
vim.api.nvim_win_set_option(self.thumb_win, 'winhighlight', 'EndOfBuffer:PmenuThumb,Normal:PmenuThumb,NormalNC:PmenuThumb,NormalFloat:PmenuThumb')
|
||||
self.thumb_win = vim.api.nvim_open_win(buffer.ensure(self.name .. 'thumb_buf'), false, style2)
|
||||
if self.scrollbar == '' then
|
||||
vim.api.nvim_win_set_option(self.thumb_win, 'winhighlight', 'EndOfBuffer:PmenuThumb,Normal:PmenuThumb,NormalNC:PmenuThumb,NormalFloat:PmenuThumb')
|
||||
else
|
||||
vim.api.nvim_win_set_option(self.thumb_win, 'winhighlight', 'EndOfBuffer:Normal,Normal:Normal,NormalNC:Normal,NormalFloat:Normal')
|
||||
end
|
||||
end
|
||||
else
|
||||
if self.sbar_win and vim.api.nvim_win_is_valid(self.sbar_win) then
|
||||
|
|
|
@ -35,18 +35,16 @@ window_analysis.cache = cache.new()
|
|||
window_analysis.analyze = function(style, bufnr)
|
||||
local scroll_height = window_analysis.get_content_height(style.width, bufnr)
|
||||
local border_info = window_analysis.get_border_info(style.border)
|
||||
local scroll_info = style.height >= scroll_height and ({
|
||||
local scroll_info = style.height >= scroll_height and {
|
||||
scrollable = false,
|
||||
extra_width = 0,
|
||||
}) or (
|
||||
border_info.is_visible and ({
|
||||
scrollable = true,
|
||||
extra_width = 0,
|
||||
}) or ({
|
||||
scrollable = true,
|
||||
extra_width = 1,
|
||||
})
|
||||
)
|
||||
} or (border_info.is_visible and {
|
||||
scrollable = true,
|
||||
extra_width = 0,
|
||||
} or {
|
||||
scrollable = true,
|
||||
extra_width = 1,
|
||||
})
|
||||
return {
|
||||
row = style.row,
|
||||
col = style.col,
|
||||
|
@ -73,7 +71,7 @@ window_analysis.get_border_info = function(border)
|
|||
bottom = 0,
|
||||
horizontal = 0,
|
||||
vertical = 0,
|
||||
is_visible = false
|
||||
is_visible = false,
|
||||
}
|
||||
if border then
|
||||
if vim.tbl_contains({ 'single', 'solid', 'double', 'rounded' }, border) then
|
||||
|
@ -113,8 +111,9 @@ end
|
|||
---@return number
|
||||
window_analysis.get_content_width = function(bufnr)
|
||||
return window_analysis.cache:ensure({
|
||||
'get_content_width',
|
||||
bufnr,
|
||||
vim.api.nvim_buf_get_changedtick(bufnr)
|
||||
vim.api.nvim_buf_get_changedtick(bufnr),
|
||||
}, function()
|
||||
local content_width = 0
|
||||
vim.api.nvim_buf_call(bufnr, function()
|
||||
|
@ -132,9 +131,10 @@ end
|
|||
---@return number
|
||||
window_analysis.get_content_height = function(width, bufnr)
|
||||
return window_analysis.cache:ensure({
|
||||
'get_content_height',
|
||||
width,
|
||||
bufnr,
|
||||
vim.api.nvim_buf_get_changedtick(bufnr)
|
||||
vim.api.nvim_buf_get_changedtick(bufnr),
|
||||
}, function()
|
||||
local content_height = 0
|
||||
vim.api.nvim_buf_call(bufnr, function()
|
||||
|
@ -147,4 +147,3 @@ window_analysis.get_content_height = function(width, bufnr)
|
|||
end
|
||||
|
||||
return window_analysis
|
||||
|
||||
|
|
|
@ -106,6 +106,8 @@ custom_entries_view.on_change = function(self)
|
|||
end
|
||||
|
||||
custom_entries_view.open = function(self, offset, entries)
|
||||
local completion = config.get().window.completion
|
||||
|
||||
self.offset = offset
|
||||
self.entries = {}
|
||||
self.column_width = { abbr = 0, kind = 0, menu = 0 }
|
||||
|
@ -183,11 +185,9 @@ custom_entries_view.open = function(self, offset, entries)
|
|||
col = vim.o.columns - analyzed.width - 1
|
||||
end
|
||||
|
||||
if not analyzed.border_info.is_visible then
|
||||
self.entries_win:option('winhighlight', 'Normal:Pmenu,FloatBorder:Pmenu,CursorLine:PmenuSel,Search:None')
|
||||
else
|
||||
self.entries_win:option('winhighlight', 'FloatBorder:Normal,CursorLine:Visual,Search:None,NormalFloat:Normal,FloatBorder:Normal')
|
||||
end
|
||||
local win_mode_option = analyzed.border_info.is_visible and completion.win_mode.bordered or completion.win_mode.default
|
||||
self.entries_win:set_scrollbar(win_mode_option.scrollbar)
|
||||
self.entries_win:option('winhighlight', win_mode_option.winhighlight)
|
||||
self.entries_win:open({
|
||||
relative = 'editor',
|
||||
style = 'minimal',
|
||||
|
@ -195,8 +195,8 @@ custom_entries_view.open = function(self, offset, entries)
|
|||
col = math.max(0, col),
|
||||
width = analyzed.inner_width,
|
||||
height = analyzed.inner_height,
|
||||
border = config.get().window.completion.border,
|
||||
zindex = 1001,
|
||||
border = completion.border,
|
||||
zindex = completion.zindex or 1001,
|
||||
})
|
||||
|
||||
if not self.entries_win:visible() then
|
||||
|
|
|
@ -24,7 +24,7 @@ end
|
|||
---@param e cmp.Entry
|
||||
---@param entries_analyzed cmp.WindowAnalyzed
|
||||
docs_view.open = function(self, e, entries_analyzed)
|
||||
local documentation = config.get().documentation
|
||||
local documentation = config.get().window.documentation
|
||||
if not documentation then
|
||||
return
|
||||
end
|
||||
|
@ -42,8 +42,8 @@ docs_view.open = function(self, e, entries_analyzed)
|
|||
local right_space = vim.o.columns - (entries_analyzed.col + entries_analyzed.width)
|
||||
local left_space = entries_analyzed.col
|
||||
local bottom_space = vim.o.lines - entries_analyzed.row
|
||||
local max_content_width = math.min(documentation.maxwidth, math.max(left_space, right_space)) - border_info.horizontal - 1
|
||||
local max_content_height = math.min(documentation.maxheight, bottom_space) - border_info.vertical
|
||||
local max_content_width = math.min(documentation.max_width, math.max(left_space, right_space)) - border_info.horizontal - 1
|
||||
local max_content_height = math.min(documentation.max_height, bottom_space) - border_info.vertical
|
||||
|
||||
-- update buffer content if needed.
|
||||
if not self.entry or e.id ~= self.entry.id then
|
||||
|
@ -69,7 +69,7 @@ docs_view.open = function(self, e, entries_analyzed)
|
|||
col = 0,
|
||||
width = content_width,
|
||||
height = content_height,
|
||||
border = config.get().window.documentation.border,
|
||||
border = documentation.border,
|
||||
}, self.window:get_buffer())
|
||||
|
||||
local col
|
||||
|
@ -88,19 +88,18 @@ docs_view.open = function(self, e, entries_analyzed)
|
|||
else
|
||||
return self:close()
|
||||
end
|
||||
if not docs_analyzed.border_info.is_visible then
|
||||
self.window:option('winhighlight', 'Normal:NormalFloat,FloatBorder:NormalFloat,CursorLine:PmenuSel,Search:None')
|
||||
else
|
||||
self.window:option('winhighlight', 'FloatBorder:Normal,CursorLine:NormalFloat,Search:None,NormalFloat:Normal,FloatBorder:Normal')
|
||||
end
|
||||
local win_mode_option = docs_analyzed.border_info.is_visible and documentation.win_mode.bordered or documentation.win_mode.default
|
||||
self.window:set_scrollbar(win_mode_option.scrollbar)
|
||||
self.window:option('winhighlight', win_mode_option.winhighlight)
|
||||
self.window:open({
|
||||
relative = 'editor',
|
||||
style = 'minimal',
|
||||
width = docs_analyzed.inner_width,
|
||||
height = docs_analyzed.inner_height,
|
||||
border = config.get().window.documentation.border,
|
||||
row = entries_analyzed.row,
|
||||
col = col,
|
||||
width = docs_analyzed.inner_width,
|
||||
height = docs_analyzed.inner_height,
|
||||
border = documentation.border,
|
||||
zindex = documentation.zindex or 1001,
|
||||
})
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue