From 6bc0ddb58bc9768f525b5a04170ea1581e0bceb8 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Wed, 25 Aug 2021 21:23:54 +0900 Subject: [PATCH] Add `preselect` configuration option --- Makefile | 2 +- README.md | 11 +++++++++++ lua/cmp/config/default.lua | 2 ++ lua/cmp/init.lua | 2 +- lua/cmp/menu.lua | 3 ++- lua/cmp/types/cmp.lua | 9 +++++---- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 84ee29f..ca8887b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: fmt fmt: - ./utils/stylua --config-path stylua.toml --glob lua/**/*.lua -- lua + stylua --config-path stylua.toml --glob lua/**/*.lua -- lua .PHONY: lint lint: diff --git a/README.md b/README.md index 016b168..134e760 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,17 @@ Default: } ``` +#### preselect (type: cmp.PreselectMode) + +Specify preselect mode. The following modes are available. + +- cmp.Preselect.Item + - If the item has `preselect = true`, nvim-cmp will preselect it. +- cmp.Preselect.None + - Disable preselect feature. + +Default: `cmp.PreselectMode.Item` + FAQ ==================== diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index 9903c70..01bcfe2 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -21,6 +21,8 @@ return function() end, }, + preselect = types.cmp.PreselectMode.Item, + documentation = { border = { '', '', '', ' ', '', '', '', ' ' }, winhighlight = 'NormalFloat:CmpDocumentation,FloatBorder:CmpDocumentationBorder', diff --git a/lua/cmp/init.lua b/lua/cmp/init.lua index a2bf00c..5f0b3af 100644 --- a/lua/cmp/init.lua +++ b/lua/cmp/init.lua @@ -54,7 +54,7 @@ cmp.abort = function() keymap.feedkeys(keymap.t(''), 'n', function() core.reset() end) - return true; + return true else return false end diff --git a/lua/cmp/menu.lua b/lua/cmp/menu.lua index 9f5bf31..d64f6c0 100644 --- a/lua/cmp/menu.lua +++ b/lua/cmp/menu.lua @@ -1,4 +1,5 @@ local debug = require('cmp.utils.debug') +local types = require('cmp.types') local async = require('cmp.utils.async') local float = require('cmp.float') local config = require('cmp.config') @@ -116,7 +117,7 @@ menu.update = check.wrap(function(self, ctx, sources) local abbrs = {} local preselect = 0 for i, e in ipairs(entries) do - if preselect == 0 and e.completion_item.preselect then + if preselect == 0 and e.completion_item.preselect and config.get().preselect ~= types.cmp.PreselectMode.None then preselect = i end diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index 2b134b0..5eb2b16 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -17,10 +17,10 @@ cmp.TriggerEvent = {} cmp.TriggerEvent.InsertEnter = 'InsertEnter' cmp.TriggerEvent.TextChanged = 'TextChanged' ----@alias cmp.ScrollDirection "'up'" | "'down'" -cmp.ScrollDirection = {} -cmp.ScrollDirection.Up = 'up' -cmp.ScrollDirection.Down = 'down' +---@alias cmp.PreselectMode "'item'" | "'None'" +cmp.PreselectMode = {} +cmp.PreselectMode.Item = 'item' +cmp.PreselectMode.None = 'none' ---@class cmp.ContextOption ---@field public reason cmp.ContextReason|nil @@ -47,6 +47,7 @@ cmp.ScrollDirection.Down = 'down' ---@class cmp.ConfigSchema ---@field private revision number +---@field public preselect cmp.PreselectMode ---@field public completion cmp.CompletionConfig ---@field public documentation cmp.DocumentationConfig ---@field public confirmation cmp.ConfirmationConfig