From 801a9f98bbae14135be3a04515b9350b145ce25b Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Fri, 8 Apr 2022 22:04:08 +0900 Subject: [PATCH] Improve macro handling --- lua/cmp/config/default.lua | 6 +++++- lua/cmp/core.lua | 4 ++-- lua/cmp/init.lua | 29 +++++++---------------------- lua/cmp/utils/keymap.lua | 12 +++++++----- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index 365b68678..f45d7ebe4 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -8,7 +8,11 @@ local WIDE_HEIGHT = 40 return function() return { enabled = function() - return vim.api.nvim_buf_get_option(0, 'buftype') ~= 'prompt' + local disabled = false + disabled = disabled or (vim.api.nvim_buf_get_option(0, 'buftype') == 'prompt') + disabled = disabled or (vim.fn.reg_recording() ~= '') + disabled = disabled or (vim.fn.reg_executing() ~= '') + return not disabled end, preselect = types.cmp.PreselectMode.Item, diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index ba0659caf..169319dd9 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -374,7 +374,7 @@ core.confirm = function(self, e, option, callback) local keys = {} table.insert(keys, keymap.backspace(ctx.cursor.character - misc.to_utfindex(ctx.cursor_line, e:get_offset()))) table.insert(keys, string.sub(e.context.cursor_before_line, e:get_offset())) - feedkeys.call(table.concat(keys, ''), 'int') + feedkeys.call(table.concat(keys, ''), 'in') else vim.api.nvim_buf_set_text(0, ctx.cursor.row - 1, e:get_offset() - 1, ctx.cursor.row - 1, ctx.cursor.col - 1, { string.sub(e.context.cursor_before_line, e:get_offset()), @@ -462,7 +462,7 @@ core.confirm = function(self, e, option, callback) table.insert(keys, string.rep(keymap.t(''), diff_before)) table.insert(keys, string.rep(keymap.t(''), diff_after)) table.insert(keys, new_text) - feedkeys.call(table.concat(keys, ''), 'int') + feedkeys.call(table.concat(keys, ''), 'in') end end) feedkeys.call(keymap.indentkeys(vim.bo.indentkeys), 'n') diff --git a/lua/cmp/init.lua b/lua/cmp/init.lua index e780596e7..dbcd8668b 100644 --- a/lua/cmp/init.lua +++ b/lua/cmp/init.lua @@ -130,22 +130,17 @@ end) cmp.select_next_item = cmp.sync(function(option) option = option or {} - -- Hack: Ignore when executing macro. - if vim.fn.reg_executing() ~= '' then - return true - end - if cmp.core.view:visible() then local release = cmp.core:suspend() cmp.core.view:select_next_item(option) vim.schedule(release) return true elseif vim.fn.pumvisible() == 1 then - -- Special handling for native puma. Required to facilitate key mapping processing. + -- Special handling for native pum. Required to facilitate key mapping processing. if (option.behavior or cmp.SelectBehavior.Insert) == cmp.SelectBehavior.Insert then - feedkeys.call(keymap.t(''), 'n') + feedkeys.call(keymap.t(''), 'in') else - feedkeys.call(keymap.t(''), 'n') + feedkeys.call(keymap.t(''), 'in') end return true end @@ -156,22 +151,17 @@ end) cmp.select_prev_item = cmp.sync(function(option) option = option or {} - -- Hack: Ignore when executing macro. - if vim.fn.reg_executing() ~= '' then - return true - end - if cmp.core.view:visible() then local release = cmp.core:suspend() cmp.core.view:select_prev_item(option) vim.schedule(release) return true elseif vim.fn.pumvisible() == 1 then - -- Special handling for native puma. Required to facilitate key mapping processing. + -- Special handling for native pum. Required to facilitate key mapping processing. if (option.behavior or cmp.SelectBehavior.Insert) == cmp.SelectBehavior.Insert then - feedkeys.call(keymap.t(''), 'n') + feedkeys.call(keymap.t(''), 'in') else - feedkeys.call(keymap.t(''), 'n') + feedkeys.call(keymap.t(''), 'in') end return true end @@ -193,11 +183,6 @@ cmp.confirm = cmp.sync(function(option, callback) option = option or {} callback = callback or function() end - -- Hack: Ignore when executing macro. - if vim.fn.reg_executing() ~= '' then - return true - end - local e = cmp.core.view:get_selected_entry() or (option.select and cmp.core.view:get_first_entry() or nil) if e then cmp.core:confirm(e, { @@ -210,7 +195,7 @@ cmp.confirm = cmp.sync(function(option, callback) else -- Special handling for native puma. Required to facilitate key mapping processing. if vim.fn.complete_info({ 'selected' }).selected ~= -1 then - feedkeys.call(keymap.t(''), 'n') + feedkeys.call(keymap.t(''), 'in') return true end return false diff --git a/lua/cmp/utils/keymap.lua b/lua/cmp/utils/keymap.lua index 68a53d92c..5727cd096 100644 --- a/lua/cmp/utils/keymap.lua +++ b/lua/cmp/utils/keymap.lua @@ -106,7 +106,9 @@ keymap.listen = function(mode, lhs, callback) local bufnr = existing.buffer and vim.api.nvim_get_current_buf() or -1 local fallback = keymap.fallback(bufnr, mode, existing) keymap.set_map(bufnr, mode, lhs, function() - if mode == 'c' and vim.fn.getcmdtype() == '=' then + local ignore = false + ignore = ignore or (mode == 'c' and vim.fn.getcmdtype() == '=') + if ignore then fallback() else callback(lhs, misc.once(fallback)) @@ -132,7 +134,7 @@ keymap.fallback = function(bufnr, mode, map) nowait = map.nowait, silent = map.silent and mode ~= 'c', }) - vim.api.nvim_feedkeys(keymap.t(fallback_expr), 'itm', true) + vim.api.nvim_feedkeys(keymap.t(fallback_expr), 'im', true) elseif not map.callback then local solved = keymap.solve(bufnr, mode, map) vim.api.nvim_feedkeys(solved.keys, solved.mode, true) @@ -148,7 +150,7 @@ keymap.solve = function(bufnr, mode, map) local rhs = map.expr and (map.callback and map.callback() or vim.api.nvim_eval(keymap.t(map.rhs))) or keymap.t(map.rhs) if map.noremap then - return { keys = rhs, mode = 'itn' } + return { keys = rhs, mode = 'in' } end if string.find(rhs, lhs, 1, true) == 1 then @@ -159,9 +161,9 @@ keymap.solve = function(bufnr, mode, map) nowait = map.nowait, silent = map.silent and mode ~= 'c', }) - return { keys = keymap.t(recursive) .. string.gsub(rhs, '^' .. vim.pesc(lhs), ''), mode = 'itm' } + return { keys = keymap.t(recursive) .. string.gsub(rhs, '^' .. vim.pesc(lhs), ''), mode = 'im' } end - return { keys = rhs, mode = 'itm' } + return { keys = rhs, mode = 'im' } end ---Get map