Fix #463
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
local mapping = require('cmp.config.mapping')
|
||||||
local cache = require('cmp.utils.cache')
|
local cache = require('cmp.utils.cache')
|
||||||
local keymap = require('cmp.utils.keymap')
|
local keymap = require('cmp.utils.keymap')
|
||||||
local misc = require('cmp.utils.misc')
|
local misc = require('cmp.utils.misc')
|
||||||
@@ -91,9 +92,11 @@ end
|
|||||||
---@return cmp.ConfigSchema
|
---@return cmp.ConfigSchema
|
||||||
config.normalize = function(c)
|
config.normalize = function(c)
|
||||||
if c.mapping then
|
if c.mapping then
|
||||||
|
local normalized = {}
|
||||||
for k, v in pairs(c.mapping) do
|
for k, v in pairs(c.mapping) do
|
||||||
c.mapping[keymap.normalize(k)] = v
|
normalized[keymap.normalize(k)] = mapping(v, { 'i' })
|
||||||
end
|
end
|
||||||
|
c.mapping = normalized
|
||||||
end
|
end
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,35 +1,14 @@
|
|||||||
local api = require('cmp.utils.api')
|
|
||||||
|
|
||||||
local mapping
|
local mapping
|
||||||
mapping = setmetatable({}, {
|
mapping = setmetatable({}, {
|
||||||
__call = function(_, invoke, modes)
|
__call = function(_, invoke, modes)
|
||||||
if type(invoke) == 'function' then
|
if type(invoke) == 'function' then
|
||||||
return {
|
local map = {}
|
||||||
invoke = function(...)
|
for _, mode in ipairs(modes or { 'i' }) do
|
||||||
invoke(...)
|
map[mode] = invoke
|
||||||
end,
|
end
|
||||||
modes = modes or { 'i' },
|
return map
|
||||||
__type = 'mapping',
|
end
|
||||||
}
|
|
||||||
elseif type(invoke) == 'table' then
|
|
||||||
if invoke.__type == 'mapping' then
|
|
||||||
return invoke
|
return invoke
|
||||||
else
|
|
||||||
return mapping(function(fallback)
|
|
||||||
if api.is_insert_mode() and invoke.i then
|
|
||||||
return invoke.i(fallback)
|
|
||||||
elseif api.is_cmdline_mode() and invoke.c then
|
|
||||||
return invoke.c(fallback)
|
|
||||||
elseif api.is_select_mode() and invoke.s then
|
|
||||||
return invoke.s(fallback)
|
|
||||||
elseif api.is_visual_mode() and invoke.x then
|
|
||||||
return invoke.x(fallback)
|
|
||||||
else
|
|
||||||
fallback()
|
|
||||||
end
|
|
||||||
end, vim.tbl_keys(invoke))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -104,14 +104,10 @@ end
|
|||||||
|
|
||||||
---Keypress handler
|
---Keypress handler
|
||||||
core.on_keymap = function(self, keys, fallback)
|
core.on_keymap = function(self, keys, fallback)
|
||||||
for key, action in pairs(config.get().mapping) do
|
local mode = api.get_mode()
|
||||||
if keymap.equals(key, keys) then
|
for key, mapping in pairs(config.get().mapping) do
|
||||||
if type(action) == 'function' then
|
if keymap.equals(key, keys) and mapping[mode] then
|
||||||
action(fallback)
|
return mapping[mode](fallback)
|
||||||
else
|
|
||||||
action.invoke(fallback)
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -139,14 +135,8 @@ end
|
|||||||
|
|
||||||
---Prepare completion
|
---Prepare completion
|
||||||
core.prepare = function(self)
|
core.prepare = function(self)
|
||||||
for keys, action in pairs(config.get().mapping) do
|
for keys, mapping in pairs(config.get().mapping) do
|
||||||
if type(action) == 'function' then
|
for mode in pairs(mapping) do
|
||||||
action = {
|
|
||||||
modes = { 'i' },
|
|
||||||
action = action,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
for _, mode in ipairs(action.modes) do
|
|
||||||
keymap.listen(mode, keys, function(...)
|
keymap.listen(mode, keys, function(...)
|
||||||
self:on_keymap(...)
|
self:on_keymap(...)
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ cmp.ItemField.Menu = 'menu'
|
|||||||
---@field public __call fun(c: cmp.ConfigSchema)
|
---@field public __call fun(c: cmp.ConfigSchema)
|
||||||
---@field public buffer fun(c: cmp.ConfigSchema)
|
---@field public buffer fun(c: cmp.ConfigSchema)
|
||||||
---@field public global fun(c: cmp.ConfigSchema)
|
---@field public global fun(c: cmp.ConfigSchema)
|
||||||
|
---@field public cmdline fun(type: string, c: cmp.ConfigSchema)
|
||||||
|
|
||||||
---@class cmp.SourceBaseApiParams
|
---@class cmp.SourceBaseApiParams
|
||||||
---@field public option table
|
---@field public option table
|
||||||
@@ -59,6 +60,12 @@ cmp.ItemField.Menu = 'menu'
|
|||||||
---@field public offset number
|
---@field public offset number
|
||||||
---@field public completion_context lsp.CompletionContext
|
---@field public completion_context lsp.CompletionContext
|
||||||
|
|
||||||
|
---@class cmp.Mapping
|
||||||
|
---@field public i nil|function(fallback: function): void
|
||||||
|
---@field public c nil|function(fallback: function): void
|
||||||
|
---@field public x nil|function(fallback: function): void
|
||||||
|
---@field public s nil|function(fallback: function): void
|
||||||
|
|
||||||
---@class cmp.ConfigSchema
|
---@class cmp.ConfigSchema
|
||||||
---@field private revision number
|
---@field private revision number
|
||||||
---@field public enabled fun():boolean|boolean
|
---@field public enabled fun():boolean|boolean
|
||||||
@@ -69,7 +76,7 @@ cmp.ItemField.Menu = 'menu'
|
|||||||
---@field public sorting cmp.SortingConfig
|
---@field public sorting cmp.SortingConfig
|
||||||
---@field public formatting cmp.FormattingConfig
|
---@field public formatting cmp.FormattingConfig
|
||||||
---@field public snippet cmp.SnippetConfig
|
---@field public snippet cmp.SnippetConfig
|
||||||
---@field public mapping table<string, fun(core: cmp.Core, fallback: function)>
|
---@field public mapping table<string, cmp.Mapping>
|
||||||
---@field public sources cmp.SourceConfig[]
|
---@field public sources cmp.SourceConfig[]
|
||||||
---@field public experimental cmp.ExperimentalConfig
|
---@field public experimental cmp.ExperimentalConfig
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,17 @@
|
|||||||
local api = {}
|
local api = {}
|
||||||
|
|
||||||
|
api.get_mode = function()
|
||||||
|
if api.is_insert_mode() then
|
||||||
|
return 'i'
|
||||||
|
elseif api.is_visual_mode() then
|
||||||
|
return 'x'
|
||||||
|
elseif api.is_select_mode() then
|
||||||
|
return 's'
|
||||||
|
elseif api.is_cmdline_mode() then
|
||||||
|
return 'c'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
api.is_insert_mode = function()
|
api.is_insert_mode = function()
|
||||||
return vim.tbl_contains({
|
return vim.tbl_contains({
|
||||||
'i',
|
'i',
|
||||||
|
|||||||
Reference in New Issue
Block a user