Improve mappings

This commit is contained in:
hrsh7th
2021-08-12 16:14:23 +09:00
parent 04af63f742
commit 22f8ce1bd5
4 changed files with 68 additions and 128 deletions

View File

@@ -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('<C-n>', 'n')
return
end
elseif c.type == 'item.prev' then
if vim.fn.pumvisible() == 1 then
keymap.feedkeys('<C-p>', 'n')
return
end
end
return fallback()
return action(core, fallback)
end
end

View File

@@ -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('<C-n>', '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('<C-p>', '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

View File

@@ -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<string, cmp.MappingConfig>
---@field public mapping table<string, fun(core: cmp.Core, fallback: function)>
---@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