Add cmp.event:on() (#418)
This commit is contained in:
10
README.md
10
README.md
@@ -414,10 +414,6 @@ cmp.setup {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### event.on_confirm_done (type: fun(entry: cmp.Entry))
|
|
||||||
|
|
||||||
A callback function called when the item is confirmed.
|
|
||||||
|
|
||||||
#### experimental.native_menu (type: boolean)
|
#### experimental.native_menu (type: boolean)
|
||||||
|
|
||||||
Use vim's native completion menu instead of custom floating menu.
|
Use vim's native completion menu instead of custom floating menu.
|
||||||
@@ -476,6 +472,12 @@ Programatic API
|
|||||||
|
|
||||||
You can use the following APIs.
|
You can use the following APIs.
|
||||||
|
|
||||||
|
#### `cmp.event:on(name: string, callback: string)`
|
||||||
|
|
||||||
|
Subscribe the following events.
|
||||||
|
|
||||||
|
- `confirm_done`
|
||||||
|
|
||||||
#### `cmp.visible()`
|
#### `cmp.visible()`
|
||||||
|
|
||||||
Return the completion menu is visible or not.
|
Return the completion menu is visible or not.
|
||||||
|
|||||||
@@ -99,6 +99,32 @@ return function()
|
|||||||
fallback()
|
fallback()
|
||||||
end,
|
end,
|
||||||
}),
|
}),
|
||||||
|
['<Tab>'] = mapping({
|
||||||
|
c = function(fallback)
|
||||||
|
local c = require('cmp.config')
|
||||||
|
local cmp = require('cmp')
|
||||||
|
if cmp.visible() then
|
||||||
|
cmp.select_next_item()
|
||||||
|
elseif not c.get().experimental.native_menu then
|
||||||
|
cmp.complete()
|
||||||
|
else
|
||||||
|
fallback()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}),
|
||||||
|
['<S-Tab>'] = mapping({
|
||||||
|
c = function(fallback)
|
||||||
|
local c = require('cmp.config')
|
||||||
|
local cmp = require('cmp')
|
||||||
|
if cmp.visible() then
|
||||||
|
cmp.select_prev_item()
|
||||||
|
elseif not c.get().experimental.native_menu then
|
||||||
|
cmp.complete()
|
||||||
|
else
|
||||||
|
fallback()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}),
|
||||||
['<C-n>'] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }),
|
['<C-n>'] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }),
|
||||||
['<C-p>'] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }),
|
['<C-p>'] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }),
|
||||||
['<C-y>'] = mapping.confirm({ select = false }),
|
['<C-y>'] = mapping.confirm({ select = false }),
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ local misc = require('cmp.utils.misc')
|
|||||||
local config = require('cmp.config')
|
local config = require('cmp.config')
|
||||||
local types = require('cmp.types')
|
local types = require('cmp.types')
|
||||||
local api = require('cmp.utils.api')
|
local api = require('cmp.utils.api')
|
||||||
|
local event = require('cmp.utils.event')
|
||||||
|
|
||||||
local SOURCE_TIMEOUT = 500
|
local SOURCE_TIMEOUT = 500
|
||||||
local THROTTLE_TIME = 120
|
local THROTTLE_TIME = 120
|
||||||
@@ -21,6 +22,7 @@ local DEBOUNCE_TIME = 20
|
|||||||
---@field public sources cmp.Source[]
|
---@field public sources cmp.Source[]
|
||||||
---@field public sources_by_name table<string, cmp.Source>
|
---@field public sources_by_name table<string, cmp.Source>
|
||||||
---@field public context cmp.Context
|
---@field public context cmp.Context
|
||||||
|
---@field public event cmp.Event
|
||||||
local core = {}
|
local core = {}
|
||||||
|
|
||||||
core.new = function()
|
core.new = function()
|
||||||
@@ -29,6 +31,7 @@ core.new = function()
|
|||||||
self.sources = {}
|
self.sources = {}
|
||||||
self.sources_by_name = {}
|
self.sources_by_name = {}
|
||||||
self.context = context.new()
|
self.context = context.new()
|
||||||
|
self.event = event.new()
|
||||||
self.view = view.new()
|
self.view = view.new()
|
||||||
self.view.event:on('keymap', function(...)
|
self.view.event:on('keymap', function(...)
|
||||||
self:on_keymap(...)
|
self:on_keymap(...)
|
||||||
@@ -403,7 +406,8 @@ core.confirm = function(self, e, option, callback)
|
|||||||
end
|
end
|
||||||
e:execute(vim.schedule_wrap(function()
|
e:execute(vim.schedule_wrap(function()
|
||||||
release()
|
release()
|
||||||
|
self.event:emit('confirm_done', e)
|
||||||
|
--For backward compatibility
|
||||||
if config.get().event.on_confirm_done then
|
if config.get().event.on_confirm_done then
|
||||||
config.get().event.on_confirm_done(e)
|
config.get().event.on_confirm_done(e)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ cmp.config.disable = misc.none
|
|||||||
cmp.config.compare = require('cmp.config.compare')
|
cmp.config.compare = require('cmp.config.compare')
|
||||||
cmp.config.sources = require('cmp.config.sources')
|
cmp.config.sources = require('cmp.config.sources')
|
||||||
|
|
||||||
|
---Expose event
|
||||||
|
cmp.event = cmp.core.event
|
||||||
|
|
||||||
---Export mapping
|
---Export mapping
|
||||||
cmp.mapping = require('cmp.config.mapping')
|
cmp.mapping = require('cmp.config.mapping')
|
||||||
|
|
||||||
|
|||||||
@@ -69,7 +69,6 @@ cmp.ItemField.Menu = 'menu'
|
|||||||
---@field public sorting cmp.SortingConfig
|
---@field public sorting cmp.SortingConfig
|
||||||
---@field public formatting cmp.FormattingConfig
|
---@field public formatting cmp.FormattingConfig
|
||||||
---@field public snippet cmp.SnippetConfig
|
---@field public snippet cmp.SnippetConfig
|
||||||
---@field public event cmp.EventConfig
|
|
||||||
---@field public mapping table<string, fun(core: cmp.Core, fallback: function)>
|
---@field public mapping table<string, fun(core: cmp.Core, fallback: function)>
|
||||||
---@field public sources cmp.SourceConfig[]
|
---@field public sources cmp.SourceConfig[]
|
||||||
---@field public experimental cmp.ExperimentalConfig
|
---@field public experimental cmp.ExperimentalConfig
|
||||||
@@ -103,9 +102,6 @@ cmp.ItemField.Menu = 'menu'
|
|||||||
---@class cmp.SnippetConfig
|
---@class cmp.SnippetConfig
|
||||||
---@field public expand fun(args: cmp.SnippetExpansionParams)
|
---@field public expand fun(args: cmp.SnippetExpansionParams)
|
||||||
|
|
||||||
---@class cmp.EventConfig
|
|
||||||
---@field on_confirm_done function(e: cmp.Entry)
|
|
||||||
|
|
||||||
---@class cmp.ExperimentalConfig
|
---@class cmp.ExperimentalConfig
|
||||||
---@field public native_menu boolean
|
---@field public native_menu boolean
|
||||||
---@field public ghost_text cmp.GhostTextConfig|"false"
|
---@field public ghost_text cmp.GhostTextConfig|"false"
|
||||||
|
|||||||
Reference in New Issue
Block a user