This commit is contained in:
hrsh7th
2021-12-14 22:41:45 +09:00
parent 91b6f503c1
commit da4c071f6b
5 changed files with 46 additions and 27 deletions

View File

@@ -176,19 +176,27 @@ NOTE: You can call these functions in mapping via `<Cmd>lua require('cmp').compl
*cmp.scroll_docs* (delta: number)
Scroll docs if it visible.
*cmp.complete* (option: { reason = cmp.ContextReason })
*cmp.complete* (option: { reason = cmp.ContextReason, config = cmp.ConfigSchema })
Invoke completion.
The following configurations defines the key mapping to invoke only snippet completion.
>
cmp.setup {
mapping = {
['<C-s>'] = cmp.mapping.complete({ sources = { { name = 'vsnip' } } })
['<C-s>'] = cmp.mapping.complete({
config = {
sources = {
{ name = 'vsnip' }
}
}
})
}
}
< >
inoremap <C-S> <Cmd>lua require('cmp').complete({ sources = { { name = 'vsnip' } } })<CR>
inoremap <C-S> <Cmd>lua require('cmp').complete({ config = { sources = { { name = 'vsnip' } } } })<CR>
<
NOTE: The `config` means a temporary setting, but the `config.mapping` remains permanent.
*cmp.confirm* (option: cmp.ConfirmOption, callback: function)
Accept current selected completion item.
If you didn't select any items and specified the `{ select = true }` for

View File

@@ -20,6 +20,9 @@ config.buffers = {}
---@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)
@@ -33,21 +36,36 @@ end
---@param bufnr number|nil
config.set_buffer = function(c, bufnr)
local revision = (config.buffers[bufnr] or {}).revision or 1
config.buffers[bufnr] = c
config.buffers[bufnr] = c or {}
config.buffers[bufnr].revision = revision + 1
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
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.global
if api.is_cmdline_mode() then
if config.onetime.sources then
local onetime = config.onetime
return config.cache:ensure({ 'get_onetime', global.revision or 0, onetime.revision or 0 }, function()
return misc.merge(config.normalize(onetime), config.normalize(global))
end)
elseif api.is_cmdline_mode() then
local cmdtype = vim.fn.getcmdtype()
local cmdline = config.cmdline[cmdtype] or { revision = 1, sources = {} }
return config.cache:ensure({ 'get_cmdline', cmdtype, global.revision or 0, cmdline.revision or 0 }, function()

View File

@@ -20,7 +20,6 @@ local THROTTLE_TIME = 120
---@field public suspending boolean
---@field public view cmp.View
---@field public sources cmp.Source[]
---@field public source_configs cmp.SourceConfig[]
---@field public context cmp.Context
---@field public event cmp.Event
local core = {}
@@ -29,7 +28,6 @@ core.new = function()
local self = setmetatable({}, { __index = core })
self.suspending = false
self.sources = {}
self.source_configs = {}
self.context = context.new()
self.event = event.new()
self.view = view.new()
@@ -90,8 +88,7 @@ core.get_sources = function(self, filter)
end
local sources = {}
local source_configs = #self.source_configs > 0 and self.source_configs or config.get().sources
for _, c in pairs(source_configs) do
for _, c in pairs(config.get().sources) do
for _, s in pairs(self.sources) do
if c.name == s.name then
if s:is_available() and f(s) then
@@ -218,14 +215,12 @@ end
---Invoke completion
---@param ctx cmp.Context
---@param source_configs? cmp.SourceConfig[]
core.complete = function(self, ctx, source_configs)
core.complete = function(self, ctx)
if not api.is_suitable_mode() then
return
end
self:set_context(ctx)
self.source_configs = source_configs or self.source_configs
-- Invoke completion sources.
local sources = self:get_sources()
@@ -288,18 +283,16 @@ core.filter = async.throttle(
-- Display completion results.
self.view:open(ctx, sources)
-- Check specific source config.
if #self.source_configs > 0 then
if #self:get_sources(function(s)
if s.status == source.SourceStatus.FETCHING then
return true
elseif s.status == source.SourceStatus.COMPLETED and #s:get_entries(ctx) > 0 then
return true
end
return false
end) == 0 then
self.source_configs = {}
-- Check onetime config.
if #self:get_sources(function(s)
if s.status == source.SourceStatus.FETCHING then
return true
elseif #s:get_entries(ctx) > 0 then
return true
end
return false
end) == 0 then
config.set_onetime({})
end
end),
THROTTLE_TIME

View File

@@ -55,8 +55,8 @@ end
---@param option cmp.CompleteParams
cmp.complete = function(option)
option = option or {}
cmp.core:complete(cmp.core:get_context({ reason = option.reason or cmp.ContextReason.Manual }), option.sources)
config.set_onetime(option.config)
cmp.core:complete(cmp.core:get_context({ reason = option.reason or cmp.ContextReason.Manual }))
return true
end

View File

@@ -48,7 +48,7 @@ cmp.ItemField.Menu = 'menu'
---@class cmp.CompleteParams
---@field public reason? cmp.ContextReason
---@field public sources? cmp.SourceConfig[]
---@field public config? cmp.ConfigSchema
---@class cmp.Setup
---@field public __call fun(c: cmp.ConfigSchema)