From 7c8876330df6e11cf91ca42ab6e52f37d81cc003 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Sun, 19 Sep 2021 02:23:58 +0900 Subject: [PATCH] Fix #220 --- lua/cmp/utils/keymap.lua | 45 +++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/lua/cmp/utils/keymap.lua b/lua/cmp/utils/keymap.lua index ce8ac2b..7d75da2 100644 --- a/lua/cmp/utils/keymap.lua +++ b/lua/cmp/utils/keymap.lua @@ -49,6 +49,37 @@ keymap.to_keymap = function(s) end) end +---Return two key sequence are equal or not. +---@param a string +---@param b string +---@return boolean +keymap.equals = function(a, b) + return keymap.to_upper(a) == keymap.to_upper(b) +end + +---Return upper case key sequence. +---@param keys string +---@return string +keymap.to_upper = function(keys) + local result = {} + local ctrl = false + for i = 1, #keys do + local c = string.sub(keys, i, i) + if c == '<' then + table.insert(result, c) + ctrl = true + elseif ctrl and c ~= '>' then + table.insert(result, string.upper(c)) + elseif ctrl and c == '>' then + table.insert(result, c) + ctrl = false + else + table.insert(result, c) + end + end + return table.concat(result, '') +end + ---Feedkeys with callback keymap.feedkeys = setmetatable({ callbacks = {}, @@ -90,7 +121,7 @@ keymap.listen = setmetatable({ cache = cache.new(), }, { __call = function(self, mode, keys, callback) - keys = keymap.to_keymap(keys) + keys = keymap.to_upper(keymap.to_keymap(keys)) local existing = keymap.find_map_by_lhs(mode, keys) if string.match(existing.rhs, '^.*' .. vim.pesc('v:lua.cmp.utils.keymap.listen.run') .. '.*$') then @@ -161,18 +192,22 @@ end ---@return table keymap.find_map_by_lhs = function(mode, lhs) for _, map in ipairs(vim.api.nvim_buf_get_keymap(0, mode)) do - if map.lhs == lhs then + if keymap.equals(map.lhs, lhs) then + map.lhs = keymap.to_upper(map.lhs) + map.rhs = keymap.to_upper(map.rhs) return map end end for _, map in ipairs(vim.api.nvim_get_keymap(mode)) do - if map.lhs == lhs then + if keymap.equals(map.lhs, lhs) then + map.lhs = keymap.to_upper(map.lhs) + map.rhs = keymap.to_upper(map.rhs) return map end end return { - lhs = lhs, - rhs = lhs, + lhs = keymap.to_upper(lhs), + rhs = keymap.to_upper(lhs), expr = 0, script = 0, noremap = 1,