diff --git a/README.md b/README.md index 3f3319c..5097677 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,10 @@ lua <'] = cmp.mapping.item.prev(), - [''] = cmp.mapping.item.next(), - [''] = cmp.mapping.scroll.up(), - [''] = cmp.mapping.scroll.down(), + [''] = cmp.mapping.prev_item(), + [''] = cmp.mapping.next_item(), + [''] = cmp.mapping.scroll(-4), + [''] = cmp.mapping.scroll(4), [''] = cmp.mapping.complete(), [''] = cmp.mapping.close(), [''] = cmp.mapping.confirm({ @@ -84,10 +84,10 @@ cmp.setup { comparators = { ... }, }, mapping = { - [''] = cmp.mapping.item.prev(), - [''] = cmp.mapping.item.next(), - [''] = cmp.mapping.scroll.up(), - [''] = cmp.mapping.scroll.down(), + [''] = cmp.mapping.prev_item(), + [''] = cmp.mapping.next_item(), + [''] = cmp.mapping.scroll(-4), + [''] = cmp.mapping.scroll(4), [''] = cmp.mapping.complete(), [''] = cmp.mapping.close(), [''] = cmp.mapping.confirm({ @@ -106,13 +106,12 @@ Define mappings with `cmp.mapping` helper. The `cmp.mapping` helper has the below functions. -*cmp.mapping.confirm({ select = true or false, behavior = cmp.ConfirmBehavior.Insert or cmp.ConfirmBehavior.Replace })* -*cmp.mapping.complete()* -*cmp.mapping.close()* -*cmp.mapping.item.next()* -*cmp.mapping.item.prev()* -*cmp.mapping.scroll.up()* -*cmp.mapping.scroll.down()* +- *cmp.mapping.confirm({ select = true or false, behavior = cmp.ConfirmBehavior.Insert or cmp.ConfirmBehavior.Replace })* +- *cmp.mapping.complete()* +- *cmp.mapping.close()* +- *cmp.mapping.next_item()* +- *cmp.mapping.prev_item()* +- *cmp.mapping.scroll(delta = number)* ### completion.autocomplete (type: cmp.TriggerEvent[]) diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index 18bff2b..67efc0b 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -9,6 +9,7 @@ local misc = require('cmp.utils.misc') local config = require('cmp.config') local types = require('cmp.types') +---@class cmp.Core local core = {} core.SOURCE_TIMEOUT = 500 @@ -71,48 +72,9 @@ end ---Keypress handler core.on_keymap = function(keys, fallback) - for key, c in pairs(config.get().mapping) do + for key, action in pairs(config.get().mapping) do if key == keys then - if c.type == 'confirm' then - local e = core.menu:get_selected_entry() or (c.select and core.menu:get_first_entry()) - if e then - core.confirm(e, { - behavior = c.behavior, - }, function() - core.complete(core.get_context({ reason = types.cmp.ContextReason.TriggerOnly })) - end) - return - end - elseif c.type == 'complete' then - core.complete(core.get_context({ reason = types.cmp.ContextReason.Manual })) - return - elseif c.type == 'close' then - if vim.fn.pumvisible() == 1 then - core.reset() - return - end - elseif c.type == 'scroll.up' then - if core.menu.float:is_visible() then - core.menu.float:scroll(-c.delta) - return - end - elseif c.type == 'scroll.down' then - if core.menu.float:is_visible() then - core.menu.float:scroll(c.delta) - return - end - elseif c.type == 'item.next' then - if vim.fn.pumvisible() == 1 then - keymap.feedkeys('', 'n') - return - end - elseif c.type == 'item.prev' then - if vim.fn.pumvisible() == 1 then - keymap.feedkeys('', 'n') - return - end - end - return fallback() + return action(core, fallback) end end diff --git a/lua/cmp/mapping.lua b/lua/cmp/mapping.lua index f5a8133..372108e 100644 --- a/lua/cmp/mapping.lua +++ b/lua/cmp/mapping.lua @@ -2,58 +2,66 @@ local types = require('cmp.types') local mapping = {} ----Create complete mapping mapping.complete = function() - return { - type = 'complete', - } + return function(core) + core.complete(core.get_context({ reason = types.cmp.ContextReason.Manual })) + end end ----Create close mapping mapping.close = function() - return { - type = 'close', - } + return function(core, fallback) + if vim.fn.pumvisible() == 1 then + core.close() + else + fallback() + end + end end ----Create scroll mapping -mapping.scroll = { - up = function(delta) - return { - type = 'scroll.up', - delta = delta or 4, - } - end, - down = function(delta) - return { - type = 'scroll.down', - delta = delta or 4, - } - end, -} +mapping.scroll = function(delta) + return function(core, fallback) + if core.menu.float:is_visible() then + core.menu.float:scroll(delta) + else + fallback() + end + end +end ----Create item mapping -mapping.item = { - prev = function() - return { - type = 'item.prev', - } - end, - next = function() - return { - type = 'item.next', - } - end, -} +mapping.next_item = function() + return function(_, fallback) + if vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('', 'n')) + else + fallback() + end + end +end + +mapping.prev_item = function() + return function(_, fallback) + if vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('', 'n')) + else + fallback() + end + end +end ----Create confirm mapping mapping.confirm = function(option) option = option or {} - return { - type = 'confirm', - select = option.select or false, - behavior = option.behavior or types.cmp.ConfirmBehavior.Insert, - } + return function(core, fallback) + local e = core.menu:get_selected_entry() or (option.select and core.menu:get_first_entry() or nil) + if e then + core.confirm(e, { + behavior = option.behavior, + }, function() + core.complete(core.get_context({ reason = types.cmp.ContextReason.TriggerOnly })) + end) + else + fallback() + end + end end return mapping diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index 334cca0..bf01e85 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -51,7 +51,7 @@ cmp.ScrollDirection.Down = 'down' ---@field public sorting cmp.SortingConfig ---@field public formatting cmp.FormattingConfig ---@field public snippet cmp.SnippetConfig ----@field public mapping table +---@field public mapping table ---@field public sources cmp.SourceConfig[] ---@class cmp.CompletionConfig @@ -83,34 +83,5 @@ cmp.ScrollDirection.Down = 'down' ---@field public name string ---@field public opts table ----@alias cmp.MappingConfig cmp.ConfirmMapping | cmp.CompleteMapping | cmp.CloseMapping | cmp.ItemNextMapping | cmp.ItemPrevMapping | cmp.ScrollUpMapping | cmp.ScrollDownMapping - ----@class cmp.ConfirmMapping ----@field public type '"confirm"' ----@field public select boolean ----@field public behavior cmp.ConfirmBehavior - ----@class cmp.CompleteMapping ----@field public type '"complete"' - ----@class cmp.CloseMapping ----@field public type '"close"' - ----@class cmp.ItemNextMapping ----@field public type '"item.next"' ----@field public delta number - ----@class cmp.ItemPrevMapping ----@field public type '"item.prev"' ----@field public delta number - ----@class cmp.ScrollUpMapping ----@field public type '"scroll.up"' ----@field public delta number - ----@class cmp.ScrollDownMapping ----@field public type '"scroll.down"' ----@field public delta number - return cmp