diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ad5459 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +utils/stylua diff --git a/README.md b/README.md index a797535..7aa5491 100644 --- a/README.md +++ b/README.md @@ -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 = { [''] = function(fallback) - if vim.fn.pumvisible() == 1 then - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, true, true), 'n', true) - elseif check_back_space() then - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, true, true), 'n', true) - elseif vim.fn['vsnip#available']() == 1 then - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('(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 ``. 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) 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 ==================== diff --git a/lua/cmp/config.lua b/lua/cmp/config.lua index eae3c19..f6f26b2 100644 --- a/lua/cmp/config.lua +++ b/lua/cmp/config.lua @@ -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 diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index ec85a09..9852273 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -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, diff --git a/lua/cmp/init.lua b/lua/cmp/init.lua index a19fcfc..0ba4e75 100644 --- a/lua/cmp/init.lua +++ b/lua/cmp/init.lua @@ -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 diff --git a/lua/cmp/menu.lua b/lua/cmp/menu.lua index 2135764..4f74292 100644 --- a/lua/cmp/menu.lua +++ b/lua/cmp/menu.lua @@ -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) diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index 6a88569..aa68f2a 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -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" diff --git a/lua/cmp/utils/check.lua b/lua/cmp/utils/check.lua deleted file mode 100644 index e5a9cbe..0000000 --- a/lua/cmp/utils/check.lua +++ /dev/null @@ -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 diff --git a/utils/install_stylua.sh b/utils/install_stylua.sh old mode 100644 new mode 100755