From 76ba56ce962db88f8ca71c554c568106ca076dc3 Mon Sep 17 00:00:00 2001 From: hrsh7th <629908+hrsh7th@users.noreply.github.com> Date: Sun, 24 Jul 2022 13:14:01 +0900 Subject: [PATCH] Decrease waits --- doc/cmp.txt | 6 ++++++ lua/cmp/config/default.lua | 5 +++-- lua/cmp/core.lua | 12 +++++------- lua/cmp/types/cmp.lua | 1 + lua/cmp/utils/api.lua | 1 + lua/cmp/utils/keymap.lua | 4 ++-- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/doc/cmp.txt b/doc/cmp.txt index 89eea88..c19d51c 100644 --- a/doc/cmp.txt +++ b/doc/cmp.txt @@ -383,6 +383,12 @@ performance.throttle~ Sets throttle time This is used to delay filtering and displaying completions. + *cmp-config.performance.fetching_timeout* + performance.fetching_timeout~ + `number` + Sets the timeout of candidate fetching process. + The nvim-cmp will wait to display the most prioritized source. + *cmp-config.preselect* preselect~ `cmp.PreselectMode` diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index 7516ae8..0098079 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -15,8 +15,9 @@ return function() end, performance = { - debounce = 80, - throttle = 40, + debounce = 60, + throttle = 30, + fetching_timeout = 200, }, preselect = types.cmp.PreselectMode.Item, diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index aec6269..635fc85 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -14,8 +14,6 @@ local types = require('cmp.types') local api = require('cmp.utils.api') local event = require('cmp.utils.event') -local SOURCE_TIMEOUT = 500 - ---@class cmp.Core ---@field public suspending boolean ---@field public view cmp.View @@ -55,7 +53,7 @@ core.unregister_source = function(self, source_id) end ---Get new context ----@param option cmp.ContextOption +---@param option? cmp.ContextOption ---@return cmp.Context core.get_context = function(self, option) local prev = self.context:clone() @@ -81,7 +79,7 @@ core.suspend = function(self) end ---Get sources that sorted by priority ----@param filter cmp.SourceStatus[]|fun(s: cmp.Source): boolean +---@param filter? cmp.SourceStatus[]|fun(s: cmp.Source): boolean ---@return cmp.Source[] core.get_sources = function(self, filter) local f = function(s) @@ -241,7 +239,7 @@ core.complete_common_string = function(self) config.set_onetime({}) local cursor = api.get_cursor() - local offset = self.view:get_offset() + local offset = self.view:get_offset() or cursor[2] local common_string for _, e in ipairs(self.view:get_entries()) do local vim_item = e:get_vim_item(offset) @@ -309,8 +307,8 @@ core.filter = async.throttle(function(self) local sources = {} for _, s in ipairs(self:get_sources({ source.SourceStatus.FETCHING, source.SourceStatus.COMPLETED })) do -- Reserve filter call for timeout. - if not s.incomplete and SOURCE_TIMEOUT > s:get_fetching_time() then - self.filter.timeout = SOURCE_TIMEOUT - s:get_fetching_time() + if not s.incomplete and config.get().performance.fetching_timeout > s:get_fetching_time() then + self.filter.timeout = config.get().performance.fetching_timeout - s:get_fetching_time() self:filter() if #sources == 0 then return diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index f73e774..451b83c 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -97,6 +97,7 @@ cmp.ItemField = { ---@class cmp.PerformanceConfig ---@field public debounce integer ---@field public throttle integer +---@field public fetching_timeout integer ---@class cmp.WindowConfig ---@field completion cmp.WindowConfig diff --git a/lua/cmp/utils/api.lua b/lua/cmp/utils/api.lua index d053409..381482e 100644 --- a/lua/cmp/utils/api.lua +++ b/lua/cmp/utils/api.lua @@ -44,6 +44,7 @@ api.get_current_line = function() return vim.api.nvim_get_current_line() end +---@return { [1]: integer, [2]: integer } api.get_cursor = function() if api.is_cmdline_mode() then return { vim.o.lines - (vim.api.nvim_get_option('cmdheight') or 1) + 1, vim.fn.getcmdpos() - 1 } diff --git a/lua/cmp/utils/keymap.lua b/lua/cmp/utils/keymap.lua index fba0f94..4cefb54 100644 --- a/lua/cmp/utils/keymap.lua +++ b/lua/cmp/utils/keymap.lua @@ -68,7 +68,7 @@ keymap.undojoin = function() end ---Create backspace keys. ----@param count integer +---@param count string|integer ---@return string keymap.backspace = function(count) if type(count) == 'string' then @@ -83,7 +83,7 @@ keymap.backspace = function(count) end ---Update indentkeys. ----@param expr string +---@param expr? string ---@return string keymap.indentkeys = function(expr) return string.format(keymap.t('set indentkeys=%s'), expr and vim.fn.escape(expr, '| \t\\') or '')