Skip to content

Commit

Permalink
Use floating window for completion menus (#224)
Browse files Browse the repository at this point in the history
* WIP

* WIP

* Fix #226

* Insert text

* Emulate vim native

* テキトウ

* Tekito

* Move scrollbar impl

* aaa

* Ignore unexpected event

* fix

* fix scroll

* Refactor (conflict...)

* Fix bug

* Positive integer

* Refactor a bit

* Fix for pumheight=0

* fx

* Improve matching highlight

* Improve colorscheme handling

* fmt

* Add cmp.visible

* Fix pum pos

* ABBR_MARGIN

* Fix cel calculation

* up

* refactor

* fix

* a

* a

* compat

* Remove current completion state

* Fix ghost text

* Add feature toggle

* highlight customization

* Update

* Add breaking change announcement

* Add README.md

* Remove unused function

* extmark ephemeral ghost text

* Support native comp

* Fix docs  pos

* a

* Remove if native menu visible

* theme async

* Improvement idea: option to disables insert on select item (#240)

* use ghost text instead of insertion on prev/next item

* add disables_insert_on_selection option

* move disable_insert_on_select option as argumet on

* update README

* use an enum behavior to disable insert on select

* Adopt contribution

* Preselect

* Improve

* Change configuration option

* a

* Improve

* Improve

* Implement proper <C-e> behavior to native/custom

* Support <C-c> maybe

* Improve docs view

* Improve

* Avoid syntax leak

* TODO: refactor

* Fix

* Revert win pos

* fmt

* ghost text remaining

* Don't use italic by default

* bottom

* dedup by label

* Ignore events

* up

* Hacky native view partial support

* up

* perf

* improve

* more cache

* fmt

* Fix format option

* fmt

* recheck

* Fix

* Improve

* Improve

* compat

* implement redraw

* improve

* up

* fmt/lint

* immediate ghost text

* source timeout

* up

* Support multibyte

* disable highlight

* up

* improve

* fmt

* fmt

* fix

* fix

* up

* up

* Use screenpos

* Add undojoin check

* Fix height

* matcher bug

* Fix dot-repeat

* Remove undojoin

* macro

* Support dot-repeat

* MacroSafe

* Default item count is 200

* fmt

Co-authored-by: Eric Puentes <[email protected]>
  • Loading branch information
hrsh7th and Eric Puentes authored Oct 8, 2021
1 parent 5bed2dc commit ada9dde
Show file tree
Hide file tree
Showing 31 changed files with 1,803 additions and 719 deletions.
64 changes: 49 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
A completion engine plugin for neovim written in Lua.
Completion sources are installed from external repositories and "sourced".

Readme!
====================

nvim-cmp's breaking change are [here](https://github.com/hrsh7th/nvim-cmp/issues/231).


Status
====================
Expand Down Expand Up @@ -67,7 +72,7 @@ lua <<EOF
snippet = {
expand = function(args)
-- For `vsnip` user.
vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` user.
vim.fn["vsnip#anonymous"](args.body)
-- For `luasnip` user.
-- require('luasnip').lsp_expand(args.body)
Expand Down Expand Up @@ -125,8 +130,8 @@ If you want to remove an option, you can set it to `false` instead.

Built in helper `cmd.mappings` are:

- *cmp.mapping.select_prev_item()*
- *cmp.mapping.select_next_item()*
- *cmp.mapping.select_prev_item({ cmp.SelectBehavior.{Insert,Select} } })*
- *cmp.mapping.select_next_item({ cmp.SelectBehavior.{Insert,Select} })*
- *cmp.mapping.scroll_docs(number)*
- *cmp.mapping.complete()*
- *cmp.mapping.close()*
Expand All @@ -137,6 +142,10 @@ You can configure `nvim-cmp` to use these `cmd.mappings` like this:

```lua
mapping = {
['<C-n>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
['<C-p>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
['<Down>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }),
['<Up>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
Expand Down Expand Up @@ -344,14 +353,6 @@ The documentation window's max width.

The documentation window's max height.

#### formatting.deprecated (type: boolean)

Specify deprecated candidate should be marked as deprecated or not.

This option is useful but disabled by default because sometimes, this option can break your terminal appearance.

Default: `false`

#### formatting.format (type: fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem)

A function to customize completion menu.
Expand All @@ -375,12 +376,17 @@ cmp.setup {

A callback function called when the item is confirmed.

#### experimental.ghost_text (type: boolean)
#### experimental.native_menu (type: boolean)

Specify whether to display ghost text.
Use vim's native completion menu instead of custom floating menu.

Default: `false`

#### experimental.ghost_text (type: cmp.GhostTextConfig | false)

Specify whether to display ghost text.

Default: `false`

Commands
====================
Expand All @@ -396,6 +402,34 @@ Autocmds

Invoke after nvim-cmp setup.

Highlights
====================

※ The following highlights are used only when enabling `experimental.cusom_menu = true`.

#### `CmpItemAbbr`

The abbr field.

#### `CmpItemAbbrDeprecated`

The deprecated item's abbr field.

#### `CmpItemAbbrMatch`

The matched characters highlight.

#### `CmpItemAbbrMatchFuzzy`

The fuzzy matched characters highlight.

#### `CmpItemKind`

The kind field.

#### `CmpItemMenu`

The menu field.

Programatic API
====================
Expand All @@ -418,11 +452,11 @@ Close current completion menu.

Close current completion menu and restore current line (similar to native `<C-e>` behavior).

#### `cmp.select_next_item()`
#### `cmp.select_next_item({ cmp.SelectBehavior.{Insert,Select} })`

Select next completion item if possible.

#### `cmp.select_prev_item()`
#### `cmp.select_prev_item({ cmp.SelectBehavior.{Insert,Select} })`

Select prev completion item if possible.

Expand Down
2 changes: 1 addition & 1 deletion autoload/vital/_cmp/VS/Vim/Buffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ endfunction
function! s:ensure(expr) abort
if !bufexists(a:expr)
if type(a:expr) == type(0)
throw printf('VS.Vim.Buffer: `%s` is not valid expr.', l:bufnr)
throw printf('VS.Vim.Buffer: `%s` is not valid expr.', a:expr)
endif
badd `=a:expr`
endif
Expand Down
49 changes: 40 additions & 9 deletions lua/cmp/config/default.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local compare = require('cmp.config.compare')
local mapping = require('cmp.config.mapping')
local types = require('cmp.types')

local WIDE_HEIGHT = 40
Expand Down Expand Up @@ -46,28 +47,58 @@ return function()
sorting = {
priority_weight = 2,
comparators = {
compare.offset,
compare.exact,
compare.score,
compare.kind,
compare.sort_text,
compare.length,
compare.order,
function(e1, e2)
local diff
diff = compare.offset(e1, e2)
if diff ~= nil then
return diff
end
diff = compare.exact(e1, e2)
if diff ~= nil then
return diff
end
diff = compare.score(e1, e2)
if diff ~= nil then
return diff
end
diff = compare.kind(e1, e2)
if diff ~= nil then
return diff
end
diff = compare.sort_text(e1, e2)
if diff ~= nil then
return diff
end
diff = compare.length(e1, e2)
if diff ~= nil then
return diff
end
return compare.order(e1, e2)
end,
},
},

event = {},

mapping = {},
mapping = {
['<Down>'] = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }),
['<Up>'] = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Select }),
['<C-n>'] = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }),
['<C-p>'] = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }),
['<C-c>'] = function(fallback)
require('cmp').close()
fallback()
end,
},

formatting = {
deprecated = false,
format = function(_, vim_item)
return vim_item
end,
},

experimental = {
native_menu = false,
ghost_text = false,
},

Expand Down
14 changes: 5 additions & 9 deletions lua/cmp/config/mapping.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local misc = require('cmp.utils.misc')

local mapping = setmetatable({}, {
__call = function(_, invoke, modes)
return {
Expand All @@ -10,6 +8,7 @@ local mapping = setmetatable({}, {
}
end,
})

---Invoke completion
mapping.complete = function()
return function(fallback)
Expand Down Expand Up @@ -45,27 +44,24 @@ mapping.scroll_docs = function(delta)
end
end
end
mapping.scroll = misc.deprecated(mapping.scroll_docs, '`cmp.mapping.scroll` is deprecated. Please change it to `cmp.mapping.scroll_docs` instead.')

---Select next completion item.
mapping.select_next_item = function()
mapping.select_next_item = function(option)
return function(fallback)
if not require('cmp').select_next_item() then
if not require('cmp').select_next_item(option) then
fallback()
end
end
end
mapping.next_item = misc.deprecated(mapping.select_next_item, '`cmp.mapping.next_item` is deprecated. Please change it to `cmp.mapping.select_next_item` instead.')

---Select prev completion item.
mapping.select_prev_item = function()
mapping.select_prev_item = function(option)
return function(fallback)
if not require('cmp').select_prev_item() then
if not require('cmp').select_prev_item(option) then
fallback()
end
end
end
mapping.prev_item = misc.deprecated(mapping.select_prev_item, '`cmp.mapping.prev_item` is deprecated. Please change it to `cmp.mapping.select_prev_item` instead.')

---Confirm selection
mapping.confirm = function(option)
Expand Down
12 changes: 0 additions & 12 deletions lua/cmp/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ local cache = require('cmp.utils.cache')
---@field public cache cmp.Cache
---@field public prev_context cmp.Context
---@field public option cmp.ContextOption
---@field public pumvisible boolean
---@field public pumselect boolean
---@field public filetype string
---@field public time number
---@field public mode string
Expand Down Expand Up @@ -41,13 +39,10 @@ context.new = function(prev_context, option)
option = option or {}

local self = setmetatable({}, { __index = context })
local completeinfo = vim.fn.complete_info({ 'selected', 'mode', 'pum_visible' })
self.id = misc.id('context')
self.cache = cache.new()
self.prev_context = prev_context or context.empty()
self.option = option or { reason = types.cmp.ContextReason.None }
self.pumvisible = completeinfo.pum_visible ~= 0
self.pumselect = completeinfo.selected ~= -1
self.filetype = vim.api.nvim_buf_get_option(0, 'filetype')
self.time = vim.loop.now()
self.mode = vim.api.nvim_get_mode().mode
Expand Down Expand Up @@ -110,13 +105,6 @@ end
context.changed = function(self, ctx)
local curr = self

if self.pumvisible then
local completed_item = vim.v.completed_item or {}
if completed_item.word then
return false
end
end

if curr.bufnr ~= ctx.bufnr then
return true
end
Expand Down
Loading

1 comment on commit ada9dde

@alexzanderr
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the highlighths in the read me

or you can use my example

vim.cmd[[
	" scrollbar
	highlight PmenuThumb guibg=#e86671 gui=bold

	" menu selection when you use arrows
	hi PmenuSel guibg=#e86671

	" fuzzy match for what you typed
	hi CmpItemAbbrMatchFuzzy guifg=#4695DFa

	" matched item (what you typed until present)
	hi CmpItemAbbrMatch guifg=#e86671

	" [LSP] section
 	hi CmpItemMenu guifg=#61afef

	" uncompleted item that may be good for completion
	" hi CmpItemAbbr guifg=#808080
	hi CmpItemAbbr guifg=#9C9C9C
]]

but the people need to now that the colors can be customizable. btw. thanks for this feature; i really wanted to get a colored completion menu.

Please sign in to comment.