* feat: completion menu borders (#472)
* feat(custom_entries_view): pass custom border option
* feat(window): calculate offset needed for borders
* fix(window): adjust window height w/ too many results
* fix(window): center scrollbar with borders
* ref(custom_entries_view): use `FloatBorder` for borders
* fix(window): offset at bottom of window
* ref(window): move height adjustment to more logical place
* fix(window): improve popup placement
* fix(window): `border_offset` always `0` first time
* feat(window): support compact scrollbar with border
* fix(window): completion popup on cursorline
* perf(window): simplify offset calculation
String indexing will result in the same thing as if I gated it behind
`type()` calls here.
* docs(window): add `border` to `cmp.WindowStyle`
* docs(window): correct `border_offset_scrollbar`
* perf(window): calulated row -> `screenrow`
This will also be more accurate since it accounts for wrapped lines, as
well as buffers.
* fix(window): edge case with multiple splits
* ref(winhighlight): don't specify defaults by default
`NormalFloat:NormalFloat` isn't needed, since `NormalFloat` defaults to
`NormalFloat`. As for `FloatBorder`, that should be set to `Floatborder`
rather than `NormalFloat` or else you get unintended artifacts on the
edges of borders.
* fix(window): popup covers cursor when scrollbar disappears
* ref(window): calc `border_offset_col` on `set_style`
* perf(window): remove unecessary `col` calculation
Taking it out didn't change anything about the popup behavior.
* feat: add `CmpItemMenuThumb` group
* feat(window): improve scrollbar appearance
* chore(window): remove references to unused property
* docs: document new option `thin_scrollbar`
* ref(plugin): remove background from `thin_scrollbar`
* feat(view): pass `thin_scrollbar` option to window
* feat(window): gate new `thin_scrollbar` behind option
* fix(window): cmdline bugging out
* fix(cmp): docs_view pops up overlapped when using borders
This is related to 1cfe2f7dfd. The
calculation for how the popup position is calculated was changed, and
so it needed to be reworked to include borders in order to be able to
work.
* ref: `thin_scrollbar` flag -> `scrollbar` option
This change allows users to define which character they will use for
their scrollbar.
* fix(window): use `scrollbar` setting for scrollbar character
Thanks @Astrantia for pointing this one out.
* docs(README): add completion appearance options to FAQ
* fix(): account for `border_offset_row` with `has_bottom_space`
* style(custom_entries_view): group offset with `row`/`col`
* fix(window): scrollbar at full view height
Because the `bar_height` variable must be whole number, and must be rounded up
from a percent, there is a change that we will end up with the maximum
height as a number.
For example, `info.height` = 24 and `total` = 25.
* feat(window): allow scrollbar to be disabled
* fix(window): scrollbar size < 1
* ref(cmp): move border logic to `window.info`
* ref!: window highlighting based on borders
BREAKING CHANGE: `documentation.winhighlight` does not determine the
highlighting of the `documentation` view— `CmpWindow`
or `CmpBorderedWindow` depending on whether it has a
border.
* ref!: float appearance opts -> `cmp.setup.window`
`cmp.setup.completion.border` and `.scrollbar` were both moved to
`cmp.setup.window.completion.border` and `.scrollbar`
BREAKING CHANGE: `cmp.setup.documentation` has been moved to
`cmp.window.documentation`, as all of the pertaining
options were cosmetic.
TODO: document the change
* fix(window): attempt to get scrollbar's border
* fix(cmp): restore `view.menu.hl_group`
* fix(window): wrong scrollbar position
* ref: get default `CmpItemMenu` from border existence
* chore(cmp): remove old PR comments
* fix(window): scrollbar sometimes too big
* fix(window): docs far away with complete menu scrollbar
* perf(docs_view): reuse `border_width` value
* rev(cmp): restore `CmpItemMenu`
* ref(cmp): distinguish between `ScrollBar` and `ScrollThumb`
* fix(plugin): consistently refer to `Thumb` as `Thumb`
* rev(window): `Pmenu`-style scrollbar when no border
* fix(window): docs_view size wrong when first shown
* fix(window): docs_view scrollbar not responding to size
* fix(window): scrollbar sometimes to small, take 2
* fix(window): scrollbar bg not hiding
* ref(docs_view): put docs closer to completion menu
* fix(window): scrollbar position wrong with right border
* ref(config): add default border to documentation
* fix(window): scrollbar too close without border
* ref(plugin): link `CmpWindow` to `Pmenu`
I set `CmpWindow` to `NormalFloat`, because that is what you would
expect a floating window to use for a highlight group. However at
request I changed it to `Pmenu`.
* ref(plugin): link `CmpWindowBorder` to `CmpWindow`
* fix(window): scrollbar following thumb while scrolling
* ref: add more highlight groups
There just weren't enough highlight groups to satisfy the demands of the
project. If you change `CmpWindow` to `Pmenu`, then the `docs_view`
becomes `Pmenu` as well when on `main` it is `NormalFloat`.
* fix(window): scrollbar overlapping `docs_view` by default
* ref: remove `Bordered` highlight variants
* ref(utils): extract whitespace check to func
* feat: `window.completion.zindex` setting
* ref: `maxwidth|height` -> `max_`
* ref: simplify highlight groups
* feat: `window.*.winhighlight` setting
* ref(utils): `is_whitespace_char` -> `is_visible`
As hrsh7th noted, `''` is not a whitespace character. Yet, it is
necessary to group `''` and `' '` together for certain border behaviors
that are based on visibility. Thus I have renamed the function
* feat: specify `window.*.winhighlight` for un/bordered
* fix(custom_entries_view): set `winhighlight` on `open`
* ref: remove `Cmp*Scroll*` variants
There's no way for `window` to know which kind of window it is drawing a
scrollbar on. Simpler to just have one kind of scrollbar
* feat: distinguish between bordered and unbordered
* ref(cmp): `is_visible` -> `is_invisible`
That's what the function was checking for.
* fix(default): mislabeling of `default` and `bordered`
* chore: rebase fixup
* Change default highlight
* Add misc.rep
* Fix left-side docs_view with scrollbar
* Fix scrollbar
* Fix sbar/thumb win
Improve highlights
* Remove scrollbar cutomization for now
* Remove scrollbar option
* Simplify implementation
* Fix doc width
* Fix outdated docs
* Add comments
* Fix configuration schema
* fmt
* Fix for lint
Co-authored-by: Iron-E <36409591+Iron-E@users.noreply.github.com>
Co-authored-by: hrsh7th <>
210 lines
5.7 KiB
Lua
210 lines
5.7 KiB
Lua
local mapping = require('cmp.config.mapping')
|
|
local cache = require('cmp.utils.cache')
|
|
local keymap = require('cmp.utils.keymap')
|
|
local misc = require('cmp.utils.misc')
|
|
local api = require('cmp.utils.api')
|
|
|
|
---@class cmp.Config
|
|
---@field public g cmp.ConfigSchema
|
|
local config = {}
|
|
|
|
---@type cmp.Cache
|
|
config.cache = cache.new()
|
|
|
|
---@type cmp.ConfigSchema
|
|
config.global = require('cmp.config.default')()
|
|
|
|
---@type table<number, cmp.ConfigSchema>
|
|
config.buffers = {}
|
|
|
|
---@type table<string, cmp.ConfigSchema>
|
|
config.filetypes = {}
|
|
|
|
---@type table<string, cmp.ConfigSchema>
|
|
config.cmdline = {}
|
|
|
|
---@type cmp.ConfigSchema
|
|
config.onetime = {}
|
|
|
|
---Set configuration for global.
|
|
---@param c cmp.ConfigSchema
|
|
config.set_global = function(c)
|
|
config.global = config.normalize(misc.merge(c, config.global))
|
|
config.global.revision = config.global.revision or 1
|
|
config.global.revision = config.global.revision + 1
|
|
end
|
|
|
|
---Set configuration for buffer
|
|
---@param c cmp.ConfigSchema
|
|
---@param bufnr number|nil
|
|
config.set_buffer = function(c, bufnr)
|
|
local revision = (config.buffers[bufnr] or {}).revision or 1
|
|
config.buffers[bufnr] = c or {}
|
|
config.buffers[bufnr].revision = revision + 1
|
|
end
|
|
|
|
---Set configuration for filetype
|
|
---@param c cmp.ConfigSchema
|
|
---@param filetypes string[]|string
|
|
config.set_filetype = function(c, filetypes)
|
|
for _, filetype in ipairs(type(filetypes) == 'table' and filetypes or { filetypes }) do
|
|
local revision = (config.filetypes[filetype] or {}).revision or 1
|
|
config.filetypes[filetype] = c or {}
|
|
config.filetypes[filetype].revision = revision + 1
|
|
end
|
|
end
|
|
|
|
---Set configuration for cmdline
|
|
---@param c cmp.ConfigSchema
|
|
---@param cmdtype string
|
|
config.set_cmdline = function(c, cmdtype)
|
|
local revision = (config.cmdline[cmdtype] or {}).revision or 1
|
|
config.cmdline[cmdtype] = c or {}
|
|
config.cmdline[cmdtype].revision = revision + 1
|
|
end
|
|
|
|
---Set configuration as oneshot completion.
|
|
---@param c cmp.ConfigSchema
|
|
config.set_onetime = function(c)
|
|
local revision = (config.onetime or {}).revision or 1
|
|
config.onetime = c or {}
|
|
config.onetime.revision = revision + 1
|
|
end
|
|
|
|
---@return cmp.ConfigSchema
|
|
config.get = function()
|
|
local global_config = config.global
|
|
if config.onetime.sources then
|
|
local onetime_config = config.onetime
|
|
return config.cache:ensure({
|
|
'get',
|
|
'onetime',
|
|
global_config.revision or 0,
|
|
onetime_config.revision or 0,
|
|
}, function()
|
|
return config.normalize(misc.merge(onetime_config, global_config))
|
|
end)
|
|
elseif api.is_cmdline_mode() then
|
|
local cmdtype = vim.fn.getcmdtype()
|
|
local cmdline_config = config.cmdline[cmdtype] or { revision = 1, sources = {} }
|
|
return config.cache:ensure({
|
|
'get',
|
|
'cmdline',
|
|
global_config.revision or 0,
|
|
cmdtype,
|
|
cmdline_config.revision or 0,
|
|
}, function()
|
|
return config.normalize(misc.merge(cmdline_config, global_config))
|
|
end)
|
|
else
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
|
|
local buffer_config = config.buffers[bufnr] or { revision = 1 }
|
|
local filetype_config = config.filetypes[filetype] or { revision = 1 }
|
|
return config.cache:ensure({
|
|
'get',
|
|
'default',
|
|
global_config.revision or 0,
|
|
filetype,
|
|
filetype_config.revision or 0,
|
|
bufnr,
|
|
buffer_config.revision or 0,
|
|
}, function()
|
|
local c = {}
|
|
c = config.normalize(misc.merge(c, buffer_config))
|
|
c = config.normalize(misc.merge(c, filetype_config))
|
|
c = config.normalize(misc.merge(c, global_config))
|
|
return c
|
|
end)
|
|
end
|
|
end
|
|
|
|
---Return cmp is enabled or not.
|
|
config.enabled = function()
|
|
local enabled = config.get().enabled
|
|
if type(enabled) == 'function' then
|
|
enabled = enabled()
|
|
end
|
|
return enabled and api.is_suitable_mode()
|
|
end
|
|
|
|
---Return source config
|
|
---@param name string
|
|
---@return cmp.SourceConfig
|
|
config.get_source_config = function(name)
|
|
local c = config.get()
|
|
for _, s in ipairs(c.sources) do
|
|
if s.name == name then
|
|
return s
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
---Return the current menu is native or not.
|
|
config.is_native_menu = function()
|
|
local c = config.get()
|
|
if c.experimental and c.experimental.native_menu then
|
|
return true
|
|
end
|
|
if c.view and c.view.entries then
|
|
return c.view.entries == 'native' or c.view.entries.name == 'native'
|
|
end
|
|
return false
|
|
end
|
|
|
|
---Normalize mapping key
|
|
---@param c cmp.ConfigSchema
|
|
---@return cmp.ConfigSchema
|
|
config.normalize = function(c)
|
|
-- make sure c is not 'nil'
|
|
c = c == nil and {} or c
|
|
|
|
-- Normalize mapping.
|
|
if c.mapping then
|
|
local normalized = {}
|
|
for k, v in pairs(c.mapping) do
|
|
normalized[keymap.normalize(k)] = mapping(v, { 'i' })
|
|
end
|
|
c.mapping = normalized
|
|
end
|
|
|
|
-- Notice experimental.native_menu.
|
|
if c.experimental and c.experimental.native_menu then
|
|
vim.api.nvim_echo({
|
|
{ '[nvim-cmp] ', 'Normal' },
|
|
{ 'experimental.native_menu', 'WarningMsg' },
|
|
{ ' is deprecated.\n', 'Normal' },
|
|
{ '[nvim-cmp] Please use ', 'Normal' },
|
|
{ 'view.entries = "native"', 'WarningMsg' },
|
|
{ ' instead.', 'Normal' },
|
|
}, true, {})
|
|
|
|
c.view = c.view or {}
|
|
c.view.entries = c.view.entries or 'native'
|
|
end
|
|
|
|
-- Notice sources.[n].opts
|
|
if c.sources then
|
|
for _, s in ipairs(c.sources) do
|
|
if s.opts and not s.option then
|
|
s.option = s.opts
|
|
s.opts = nil
|
|
vim.api.nvim_echo({
|
|
{ '[nvim-cmp] ', 'Normal' },
|
|
{ 'sources[number].opts', 'WarningMsg' },
|
|
{ ' is deprecated.\n', 'Normal' },
|
|
{ '[nvim-cmp] Please use ', 'Normal' },
|
|
{ 'sources[number].option', 'WarningMsg' },
|
|
{ ' instead.', 'Normal' },
|
|
}, true, {})
|
|
end
|
|
s.option = s.option or {}
|
|
end
|
|
end
|
|
|
|
return c
|
|
end
|
|
|
|
return config
|