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

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
utils/stylua

View File

@@ -173,26 +173,32 @@ mapping = {
You can specify your own custom mapping function.
```lua
local check_back_space = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col == 0 or vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match('%s') ~= nil
end
mapping = {
['<Tab>'] = function(fallback)
if vim.fn.pumvisible() == 1 then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<C-n>', true, true, true), 'n', true)
elseif check_back_space() then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Tab>', true, true, true), 'n', true)
elseif vim.fn['vsnip#available']() == 1 then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Plug>(vsnip-expand-or-jump)', true, true, true), '', true)
if ...some_condition... then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('...', true, true, true), 'n', true)
else
fallback()
fallback() -- The fallback function is treated as original mapped key. In this case, it might be `<Tab>`.
end
end,
}
```
#### enabled (type: fun(): boolean|boolean)
The function or boolean value to specify all cmp's features enabled or not.
Default:
```lua
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
```
#### sources (type: table<cmp.SourceConfig>)
Globals source lists are listed in the `source` table. These are applied to all
@@ -468,24 +474,28 @@ cmp.setup {
}
```
#### How to disable nvim-cmp on the specific buffer?
You can specify `enabled = false` like this.
```vim
autocmd FileType TelescopePrompt lua require('cmp').setup.buffer { enabled = false }
```
#### nvim-cmp is slow.
I've optimized `nvim-cmp` as much as possible, but there are currently some known / unfixable issues.
1. `cmp-buffer` source and too large buffer.
The `cmp-buffer` source makes an index of the current buffer so if the current buffer is too large, it will slowdown the main UI thread.
The `cmp-buffer` source makes an index of the current buffer so if the current buffer is too large, it will slowdown the main UI thread.
1. Some language servers.
For example, `typescript-language-server` will returns 15k items to the client.
In such a case, it will take 100ms just to parse payloads as JSON.
For example, `typescript-language-server` will returns 15k items to the client.
In such a case, it will take 100ms just to parse payloads as JSON.
1. You set `vim.lsp.set_log_level` up by yourself.
This setting will cause the filesystem operation for each LSP payload.
This will greatly slow down nvim-cmp (and other LSP related features).
#### How to setup supertab-like mapping?
You can found the solution in [Example mappings](https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings).
This setting will cause the filesystem operation for each LSP payload.
This will greatly slow down nvim-cmp (and other LSP related features).
#### How to show name of item kind and source (like compe)?
@@ -508,6 +518,10 @@ formatting = {
},
```
#### How to setup supertab-like mapping?
You can found the solution in [Example mappings](https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings).
Source creation
====================

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()
-- 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()
if config.enabled() then
core.on_change('TextChanged')
end
end)
autocmd.subscribe('InsertLeave', function()
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

0
utils/install_stylua.sh Normal file → Executable file
View File