Implement enabled=false configuration

This commit is contained in:
hrsh7th
2021-09-13 00:59:48 +09:00
parent 1cec1ecb31
commit a78894a09a
9 changed files with 69 additions and 48 deletions

View File

@@ -40,6 +40,15 @@ config.get = function()
end)
end
---Return cmp is enabled or not.
config.enabled = function()
local enabled = config.get().enabled
if type(enabled) == 'function' then
return enabled()
end
return not not enabled
end
---Return source config
---@param name string
---@return cmp.SourceConfig

View File

@@ -6,6 +6,12 @@ local WIDE_HEIGHT = 40
---@return cmp.ConfigSchema
return function()
return {
enabled = function()
local enabled = true
enabled = enabled and vim.api.nvim_buf_get_option(0, 'buftype') ~= 'prompt'
enabled = enabled and string.sub(vim.api.nvim_get_mode().mode, 1, 1) == 'i'
return enabled
end,
completion = {
autocomplete = {
types.cmp.TriggerEvent.TextChanged,

View File

@@ -126,16 +126,25 @@ cmp.setup = setmetatable({
---Handle events
autocmd.subscribe('InsertEnter', function()
core.prepare()
core.on_change('InsertEnter')
-- Avoid unexpected mode detection (mode() function will returns `normal mode` on the InsertEnter event.)
vim.schedule(function()
if config.enabled() then
core.prepare()
core.on_change('InsertEnter')
end
end)
end)
autocmd.subscribe('TextChanged', function()
core.on_change('TextChanged')
if config.enabled() then
core.on_change('TextChanged')
end
end)
autocmd.subscribe('InsertLeave', function()
core.reset()
if config.enabled() then
core.reset()
end
end)
return cmp

View File

@@ -4,7 +4,6 @@ local async = require('cmp.utils.async')
local float = require('cmp.float')
local config = require('cmp.config')
local autocmd = require('cmp.utils.autocmd')
local check = require('cmp.utils.check')
---@class cmp.MenuOption
---@field on_select fun(e: cmp.Entry)
@@ -70,7 +69,7 @@ end
---@param ctx cmp.Context
---@param sources cmp.Source[]
---@return cmp.Menu
menu.update = check.wrap(function(self, ctx, sources)
menu.update = function(self, ctx, sources)
local entries = {}
-- check the source triggered by character
@@ -138,11 +137,11 @@ menu.update = check.wrap(function(self, ctx, sources)
self.preselect = preselect
self.context = ctx
self:show()
end)
end
---Restore previous menu
---@param ctx cmp.Context
menu.restore = check.wrap(function(self, ctx)
menu.restore = function(self, ctx)
if not ctx.pumvisible then
if #self.items > 0 then
if self.offset <= ctx.cursor.col then
@@ -151,7 +150,7 @@ menu.restore = check.wrap(function(self, ctx)
end
end
end
end)
end
---Show completion item
menu.show = function(self)

View File

@@ -47,6 +47,7 @@ cmp.PreselectMode.None = 'none'
---@class cmp.ConfigSchema
---@field private revision number
---@field public enabled fun():boolean|boolean
---@field public preselect cmp.PreselectMode
---@field public completion cmp.CompletionConfig
---@field public documentation cmp.DocumentationConfig|"false"

View File

@@ -1,18 +0,0 @@
local check = {}
check.ok = function()
local ng = false
ng = ng or vim.api.nvim_buf_get_option(0, 'buftype') == 'prompt'
ng = ng or string.sub(vim.api.nvim_get_mode().mode, 1, 1) ~= 'i'
return not ng
end
check.wrap = function(callback)
return function(...)
if check.ok() then
callback(...)
end
end
end
return check