From da4c071f6bdb1edfa1debc7e1f7842c2d620bb04 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Tue, 14 Dec 2021 22:41:45 +0900 Subject: [PATCH] Fix #648 --- doc/cmp.txt | 14 +++++++++++--- lua/cmp/config.lua | 24 +++++++++++++++++++++--- lua/cmp/core.lua | 29 +++++++++++------------------ lua/cmp/init.lua | 4 ++-- lua/cmp/types/cmp.lua | 2 +- 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/doc/cmp.txt b/doc/cmp.txt index 9dded23..1907bc5 100644 --- a/doc/cmp.txt +++ b/doc/cmp.txt @@ -176,19 +176,27 @@ NOTE: You can call these functions in mapping via `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 = { - [''] = cmp.mapping.complete({ sources = { { name = 'vsnip' } } }) + [''] = cmp.mapping.complete({ + config = { + sources = { + { name = 'vsnip' } + } + } + }) } } < > - inoremap lua require('cmp').complete({ sources = { { name = 'vsnip' } } }) + inoremap lua require('cmp').complete({ config = { sources = { { name = 'vsnip' } } } }) < + 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 diff --git a/lua/cmp/config.lua b/lua/cmp/config.lua index 7d1bcde..7984b57 100644 --- a/lua/cmp/config.lua +++ b/lua/cmp/config.lua @@ -20,6 +20,9 @@ config.buffers = {} ---@type table 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() diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index e41ebfa..8e94352 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -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 diff --git a/lua/cmp/init.lua b/lua/cmp/init.lua index d11600d..aa8c81f 100644 --- a/lua/cmp/init.lua +++ b/lua/cmp/init.lua @@ -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 diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index 7d7ab7d..a3aedd8 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -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)