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) *cmp.scroll_docs* (delta: number)
Scroll docs if it visible. Scroll docs if it visible.
*cmp.complete* (option: { reason = cmp.ContextReason }) *cmp.complete* (option: { reason = cmp.ContextReason, config = cmp.ConfigSchema })
Invoke completion. Invoke completion.
The following configurations defines the key mapping to invoke only snippet completion. The following configurations defines the key mapping to invoke only snippet completion.
> >
cmp.setup { cmp.setup {
mapping = { 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) *cmp.confirm* (option: cmp.ConfirmOption, callback: function)
Accept current selected completion item. Accept current selected completion item.
If you didn't select any items and specified the `{ select = true }` for 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> ---@type table<string, cmp.ConfigSchema>
config.cmdline = {} config.cmdline = {}
---@type cmp.ConfigSchema
config.onetime = {}
---Set configuration for global. ---Set configuration for global.
---@param c cmp.ConfigSchema ---@param c cmp.ConfigSchema
config.set_global = function(c) config.set_global = function(c)
@@ -33,21 +36,36 @@ end
---@param bufnr number|nil ---@param bufnr number|nil
config.set_buffer = function(c, bufnr) config.set_buffer = function(c, bufnr)
local revision = (config.buffers[bufnr] or {}).revision or 1 local revision = (config.buffers[bufnr] or {}).revision or 1
config.buffers[bufnr] = c config.buffers[bufnr] = c or {}
config.buffers[bufnr].revision = revision + 1 config.buffers[bufnr].revision = revision + 1
end end
---Set configuration for cmdline ---Set configuration for cmdline
---@param c cmp.ConfigSchema
---@param cmdtype string
config.set_cmdline = function(c, cmdtype) config.set_cmdline = function(c, cmdtype)
local revision = (config.cmdline[cmdtype] or {}).revision or 1 local revision = (config.cmdline[cmdtype] or {}).revision or 1
config.cmdline[cmdtype] = c config.cmdline[cmdtype] = c or {}
config.cmdline[cmdtype].revision = revision + 1 config.cmdline[cmdtype].revision = revision + 1
end 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 ---@return cmp.ConfigSchema
config.get = function() config.get = function()
local global = config.global 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 cmdtype = vim.fn.getcmdtype()
local cmdline = config.cmdline[cmdtype] or { revision = 1, sources = {} } 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() 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 suspending boolean
---@field public view cmp.View ---@field public view cmp.View
---@field public sources cmp.Source[] ---@field public sources cmp.Source[]
---@field public source_configs cmp.SourceConfig[]
---@field public context cmp.Context ---@field public context cmp.Context
---@field public event cmp.Event ---@field public event cmp.Event
local core = {} local core = {}
@@ -29,7 +28,6 @@ core.new = function()
local self = setmetatable({}, { __index = core }) local self = setmetatable({}, { __index = core })
self.suspending = false self.suspending = false
self.sources = {} self.sources = {}
self.source_configs = {}
self.context = context.new() self.context = context.new()
self.event = event.new() self.event = event.new()
self.view = view.new() self.view = view.new()
@@ -90,8 +88,7 @@ core.get_sources = function(self, filter)
end end
local sources = {} local sources = {}
local source_configs = #self.source_configs > 0 and self.source_configs or config.get().sources for _, c in pairs(config.get().sources) do
for _, c in pairs(source_configs) do
for _, s in pairs(self.sources) do for _, s in pairs(self.sources) do
if c.name == s.name then if c.name == s.name then
if s:is_available() and f(s) then if s:is_available() and f(s) then
@@ -218,14 +215,12 @@ end
---Invoke completion ---Invoke completion
---@param ctx cmp.Context ---@param ctx cmp.Context
---@param source_configs? cmp.SourceConfig[] core.complete = function(self, ctx)
core.complete = function(self, ctx, source_configs)
if not api.is_suitable_mode() then if not api.is_suitable_mode() then
return return
end end
self:set_context(ctx) self:set_context(ctx)
self.source_configs = source_configs or self.source_configs
-- Invoke completion sources. -- Invoke completion sources.
local sources = self:get_sources() local sources = self:get_sources()
@@ -288,18 +283,16 @@ core.filter = async.throttle(
-- Display completion results. -- Display completion results.
self.view:open(ctx, sources) self.view:open(ctx, sources)
-- Check specific source config. -- Check onetime config.
if #self.source_configs > 0 then
if #self:get_sources(function(s) if #self:get_sources(function(s)
if s.status == source.SourceStatus.FETCHING then if s.status == source.SourceStatus.FETCHING then
return true return true
elseif s.status == source.SourceStatus.COMPLETED and #s:get_entries(ctx) > 0 then elseif #s:get_entries(ctx) > 0 then
return true return true
end end
return false return false
end) == 0 then end) == 0 then
self.source_configs = {} config.set_onetime({})
end
end end
end), end),
THROTTLE_TIME THROTTLE_TIME

View File

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

View File

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