Skip to content

Commit

Permalink
Improve macro handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th committed Apr 8, 2022
1 parent 27970d8 commit 801a9f9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
6 changes: 5 additions & 1 deletion lua/cmp/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

This comment has been minimized.

Copy link
@asmodeus812

asmodeus812 Nov 20, 2022

@hrsh7th I am facing some issue here maybe a race condition issues with telescope getting overridden with bindings from cmp. (for and Tab for example). For some reason the other cmp bindings are not getting applied, since this method returns enabled = false, but i can still see in the telescope buffer that only CR and Tab and Esc are getting bound by cmp.utils.keymaps_set_map

This comment has been minimized.

Copy link
@asmodeus812

asmodeus812 Nov 20, 2022

if i add this locally, it seems to solve the issue

enabled = function()
            return vim.api.nvim_buf_get_option(0, "buftype") ~= "prompt"
        end,`
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,
Expand Down
4 changes: 2 additions & 2 deletions lua/cmp/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -462,7 +462,7 @@ core.confirm = function(self, e, option, callback)
table.insert(keys, string.rep(keymap.t('<BS>'), diff_before))
table.insert(keys, string.rep(keymap.t('<Del>'), 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')
Expand Down
29 changes: 7 additions & 22 deletions lua/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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('<C-n>'), 'n')
feedkeys.call(keymap.t('<C-n>'), 'in')
else
feedkeys.call(keymap.t('<Down>'), 'n')
feedkeys.call(keymap.t('<Down>'), 'in')
end
return true
end
Expand All @@ -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('<C-p>'), 'n')
feedkeys.call(keymap.t('<C-p>'), 'in')
else
feedkeys.call(keymap.t('<Up>'), 'n')
feedkeys.call(keymap.t('<Up>'), 'in')
end
return true
end
Expand All @@ -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, {
Expand All @@ -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('<C-y>'), 'n')
feedkeys.call(keymap.t('<C-y>'), 'in')
return true
end
return false
Expand Down
12 changes: 7 additions & 5 deletions lua/cmp/utils/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 801a9f9

Please sign in to comment.