Improve macro & dot-repeat support (#363)
* manual support dot-repeat * cmdwin and terminal * cmdline only * Fix * fix * Improve * Fix test * Support macro * disable cmdline for now * Simplify * fmt * consume once * Ignore = type * cmdline * Remove cmdline features
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
local cache = require('cmp.utils.cache')
|
local cache = require('cmp.utils.cache')
|
||||||
local misc = require('cmp.utils.misc')
|
local misc = require('cmp.utils.misc')
|
||||||
|
local api = require('cmp.utils.api')
|
||||||
|
|
||||||
---@class cmp.Config
|
---@class cmp.Config
|
||||||
---@field public g cmp.ConfigSchema
|
---@field public g cmp.ConfigSchema
|
||||||
@@ -33,10 +34,10 @@ end
|
|||||||
|
|
||||||
---@return cmp.ConfigSchema
|
---@return cmp.ConfigSchema
|
||||||
config.get = function()
|
config.get = function()
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
|
||||||
local global = config.global
|
local global = config.global
|
||||||
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
local buffer = config.buffers[bufnr] or { revision = 1 }
|
local buffer = config.buffers[bufnr] or { revision = 1 }
|
||||||
return config.cache:ensure({ 'get', bufnr, global.revision or 0, buffer.revision or 0 }, function()
|
return config.cache:ensure({ 'get_buffer', bufnr, global.revision or 0, buffer.revision or 0 }, function()
|
||||||
return misc.merge(buffer, global)
|
return misc.merge(buffer, global)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -47,7 +48,7 @@ config.enabled = function()
|
|||||||
if type(enabled) == 'function' then
|
if type(enabled) == 'function' then
|
||||||
enabled = enabled()
|
enabled = enabled()
|
||||||
end
|
end
|
||||||
return enabled and misc.is_suitable_mode()
|
return enabled and api.is_suitable_mode()
|
||||||
end
|
end
|
||||||
|
|
||||||
---Return source config
|
---Return source config
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
local mapping = setmetatable({}, {
|
local api = require('cmp.utils.api')
|
||||||
|
|
||||||
|
local mapping
|
||||||
|
mapping = setmetatable({}, {
|
||||||
__call = function(_, invoke, modes)
|
__call = function(_, invoke, modes)
|
||||||
return {
|
if type(invoke) == 'function' then
|
||||||
invoke = function(...)
|
return {
|
||||||
invoke(...)
|
invoke = function(...)
|
||||||
end,
|
invoke(...)
|
||||||
modes = modes or { 'i' },
|
end,
|
||||||
}
|
modes = modes or { 'i' },
|
||||||
|
}
|
||||||
|
end
|
||||||
|
return invoke
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ local misc = require('cmp.utils.misc')
|
|||||||
local pattern = require('cmp.utils.pattern')
|
local pattern = require('cmp.utils.pattern')
|
||||||
local types = require('cmp.types')
|
local types = require('cmp.types')
|
||||||
local cache = require('cmp.utils.cache')
|
local cache = require('cmp.utils.cache')
|
||||||
|
local api = require('cmp.utils.api')
|
||||||
|
|
||||||
---@class cmp.Context
|
---@class cmp.Context
|
||||||
---@field public id string
|
---@field public id string
|
||||||
@@ -47,8 +48,8 @@ context.new = function(prev_context, option)
|
|||||||
self.mode = vim.api.nvim_get_mode().mode
|
self.mode = vim.api.nvim_get_mode().mode
|
||||||
self.bufnr = vim.api.nvim_get_current_buf()
|
self.bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
local cursor = api.get_cursor()
|
||||||
self.cursor_line = vim.api.nvim_get_current_line()
|
self.cursor_line = api.get_current_line()
|
||||||
self.cursor = {}
|
self.cursor = {}
|
||||||
self.cursor.row = cursor[1]
|
self.cursor.row = cursor[1]
|
||||||
self.cursor.col = cursor[2] + 1
|
self.cursor.col = cursor[2] + 1
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ local view = require('cmp.view')
|
|||||||
local misc = require('cmp.utils.misc')
|
local misc = require('cmp.utils.misc')
|
||||||
local config = require('cmp.config')
|
local config = require('cmp.config')
|
||||||
local types = require('cmp.types')
|
local types = require('cmp.types')
|
||||||
|
local api = require('cmp.utils.api')
|
||||||
|
|
||||||
local SOURCE_TIMEOUT = 500
|
local SOURCE_TIMEOUT = 500
|
||||||
local THROTTLE_TIME = 120
|
local THROTTLE_TIME = 120
|
||||||
@@ -196,14 +197,14 @@ end
|
|||||||
---@param callback function
|
---@param callback function
|
||||||
core.autoindent = function(self, event, callback)
|
core.autoindent = function(self, event, callback)
|
||||||
if event == types.cmp.TriggerEvent.TextChanged then
|
if event == types.cmp.TriggerEvent.TextChanged then
|
||||||
local cursor_before_line = misc.get_cursor_before_line()
|
local cursor_before_line = api.get_cursor_before_line()
|
||||||
local prefix = pattern.matchstr('[^[:blank:]]\\+$', cursor_before_line)
|
local prefix = pattern.matchstr('[^[:blank:]]\\+$', cursor_before_line)
|
||||||
if prefix then
|
if prefix then
|
||||||
for _, key in ipairs(vim.split(vim.bo.indentkeys, ',')) do
|
for _, key in ipairs(vim.split(vim.bo.indentkeys, ',')) do
|
||||||
if vim.tbl_contains({ '=' .. prefix, '0=' .. prefix }, key) then
|
if vim.tbl_contains({ '=' .. prefix, '0=' .. prefix }, key) then
|
||||||
local release = self:suspend()
|
local release = self:suspend()
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
if cursor_before_line == misc.get_cursor_before_line() then
|
if cursor_before_line == api.get_cursor_before_line() then
|
||||||
local indentkeys = vim.bo.indentkeys
|
local indentkeys = vim.bo.indentkeys
|
||||||
vim.bo.indentkeys = indentkeys .. ',!^F'
|
vim.bo.indentkeys = indentkeys .. ',!^F'
|
||||||
keymap.feedkeys(keymap.t('<C-f>'), 'n', function()
|
keymap.feedkeys(keymap.t('<C-f>'), 'n', function()
|
||||||
@@ -226,7 +227,7 @@ end
|
|||||||
---Invoke completion
|
---Invoke completion
|
||||||
---@param ctx cmp.Context
|
---@param ctx cmp.Context
|
||||||
core.complete = function(self, ctx)
|
core.complete = function(self, ctx)
|
||||||
if not misc.is_suitable_mode() then
|
if not api.is_suitable_mode() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self:set_context(ctx)
|
self:set_context(ctx)
|
||||||
@@ -258,7 +259,7 @@ end
|
|||||||
---Update completion menu
|
---Update completion menu
|
||||||
core.filter = async.throttle(
|
core.filter = async.throttle(
|
||||||
vim.schedule_wrap(function(self)
|
vim.schedule_wrap(function(self)
|
||||||
if not misc.is_suitable_mode() then
|
if not api.is_suitable_mode() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if self.view:get_active_entry() ~= nil then
|
if self.view:get_active_entry() ~= nil then
|
||||||
@@ -370,10 +371,10 @@ core.confirm = function(self, e, option, callback)
|
|||||||
|
|
||||||
local is_snippet = completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet
|
local is_snippet = completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet
|
||||||
if is_snippet then
|
if is_snippet then
|
||||||
table.insert(keys, keymap.t('<C-g>u') .. e:get_word() .. keymap.t('<C-g>u'))
|
table.insert(keys, keymap.undobreak() .. e:get_word() .. keymap.undobreak())
|
||||||
table.insert(keys, keymap.backspace(vim.str_utfindex(e:get_word())))
|
table.insert(keys, keymap.backspace(vim.str_utfindex(e:get_word())))
|
||||||
else
|
else
|
||||||
table.insert(keys, keymap.t('<C-g>u') .. completion_item.textEdit.newText .. keymap.t('<C-g>u'))
|
table.insert(keys, keymap.undobreak() .. completion_item.textEdit.newText .. keymap.undobreak())
|
||||||
end
|
end
|
||||||
keymap.feedkeys(table.concat(keys, ''), 'n', function()
|
keymap.feedkeys(table.concat(keys, ''), 'n', function()
|
||||||
if is_snippet then
|
if is_snippet then
|
||||||
|
|||||||
@@ -84,6 +84,12 @@ end
|
|||||||
---Select next item if possible
|
---Select next item if possible
|
||||||
cmp.select_next_item = function(option)
|
cmp.select_next_item = function(option)
|
||||||
option = option or {}
|
option = option or {}
|
||||||
|
|
||||||
|
-- Hack: Ignore when executing macro.
|
||||||
|
if vim.fn.reg_executing() ~= '' then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
if cmp.core.view:visible() then
|
if cmp.core.view:visible() then
|
||||||
local release = cmp.core:suspend()
|
local release = cmp.core:suspend()
|
||||||
cmp.core.view:select_next_item(option)
|
cmp.core.view:select_next_item(option)
|
||||||
@@ -103,6 +109,12 @@ end
|
|||||||
---Select prev item if possible
|
---Select prev item if possible
|
||||||
cmp.select_prev_item = function(option)
|
cmp.select_prev_item = function(option)
|
||||||
option = option or {}
|
option = option or {}
|
||||||
|
|
||||||
|
-- Hack: Ignore when executing macro.
|
||||||
|
if vim.fn.reg_executing() ~= '' then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
if cmp.core.view:visible() then
|
if cmp.core.view:visible() then
|
||||||
local release = cmp.core:suspend()
|
local release = cmp.core:suspend()
|
||||||
cmp.core.view:select_prev_item(option)
|
cmp.core.view:select_prev_item(option)
|
||||||
@@ -133,6 +145,11 @@ end
|
|||||||
cmp.confirm = function(option)
|
cmp.confirm = function(option)
|
||||||
option = option or {}
|
option = option or {}
|
||||||
|
|
||||||
|
-- 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)
|
local e = cmp.core.view:get_selected_entry() or (option.select and cmp.core.view:get_first_entry() or nil)
|
||||||
if e then
|
if e then
|
||||||
cmp.core:confirm(e, {
|
cmp.core:confirm(e, {
|
||||||
|
|||||||
56
lua/cmp/utils/api.lua
Normal file
56
lua/cmp/utils/api.lua
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
local api = {}
|
||||||
|
|
||||||
|
api.is_insert_mode = function()
|
||||||
|
return vim.tbl_contains({
|
||||||
|
'i',
|
||||||
|
'ic',
|
||||||
|
'ix',
|
||||||
|
}, vim.api.nvim_get_mode().mode)
|
||||||
|
end
|
||||||
|
|
||||||
|
api.is_cmdline_mode = function()
|
||||||
|
return vim.tbl_contains({
|
||||||
|
'c',
|
||||||
|
'cv',
|
||||||
|
}, vim.api.nvim_get_mode().mode)
|
||||||
|
end
|
||||||
|
|
||||||
|
api.is_select_mode = function()
|
||||||
|
return vim.tbl_contains({
|
||||||
|
's',
|
||||||
|
'S',
|
||||||
|
}, vim.api.nvim_get_mode().mode)
|
||||||
|
end
|
||||||
|
|
||||||
|
api.is_suitable_mode = function()
|
||||||
|
return api.is_insert_mode() or api.is_cmdline_mode()
|
||||||
|
end
|
||||||
|
|
||||||
|
api.get_current_line = function()
|
||||||
|
if api.is_cmdline_mode() then
|
||||||
|
return vim.fn.getcmdline()
|
||||||
|
end
|
||||||
|
return vim.api.nvim_get_current_line()
|
||||||
|
end
|
||||||
|
|
||||||
|
api.get_cursor = function()
|
||||||
|
if api.is_cmdline_mode() then
|
||||||
|
return { vim.o.lines - (vim.api.nvim_get_option('cmdheight') or 1) + 1, vim.fn.getcmdpos() - 1 }
|
||||||
|
end
|
||||||
|
return vim.api.nvim_win_get_cursor(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
api.get_screen_cursor = function()
|
||||||
|
local cursor = api.get_cursor()
|
||||||
|
if api.is_cmdline_mode() then
|
||||||
|
return cursor
|
||||||
|
end
|
||||||
|
local pos = vim.fn.screenpos(0, cursor[1], cursor[2] + 1)
|
||||||
|
return { pos.row, pos.col - 1 }
|
||||||
|
end
|
||||||
|
|
||||||
|
api.get_cursor_before_line = function()
|
||||||
|
return string.sub(api.get_current_line(), 1, api.get_cursor()[2] + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
return api
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
local misc = require('cmp.utils.misc')
|
local misc = require('cmp.utils.misc')
|
||||||
local str = require('cmp.utils.str')
|
local str = require('cmp.utils.str')
|
||||||
local cache = require('cmp.utils.cache')
|
local cache = require('cmp.utils.cache')
|
||||||
|
local api = require('cmp.utils.api')
|
||||||
|
|
||||||
local keymap = {}
|
local keymap = {}
|
||||||
|
|
||||||
@@ -72,6 +73,22 @@ keymap.to_keymap = function(s)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Mode safe break undo
|
||||||
|
keymap.undobreak = function()
|
||||||
|
if api.is_cmdline_mode() then
|
||||||
|
return ''
|
||||||
|
end
|
||||||
|
return keymap.t('<C-g>u')
|
||||||
|
end
|
||||||
|
|
||||||
|
---Mode safe join undo
|
||||||
|
keymap.undojoin = function()
|
||||||
|
if api.is_cmdline_mode() then
|
||||||
|
return ''
|
||||||
|
end
|
||||||
|
return keymap.t('<C-g>U')
|
||||||
|
end
|
||||||
|
|
||||||
---Create backspace keys.
|
---Create backspace keys.
|
||||||
---@param count number
|
---@param count number
|
||||||
---@return string
|
---@return string
|
||||||
@@ -80,9 +97,7 @@ keymap.backspace = function(count)
|
|||||||
return ''
|
return ''
|
||||||
end
|
end
|
||||||
local keys = {}
|
local keys = {}
|
||||||
table.insert(keys, keymap.t('<Cmd>set backspace=start<CR>'))
|
|
||||||
table.insert(keys, keymap.t(string.rep('<BS>', count)))
|
table.insert(keys, keymap.t(string.rep('<BS>', count)))
|
||||||
table.insert(keys, keymap.t(('<Cmd>set backspace=%s<CR>'):format(vim.o.backspace)))
|
|
||||||
return table.concat(keys, '')
|
return table.concat(keys, '')
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -95,31 +110,48 @@ keymap.equals = function(a, b)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---Feedkeys with callback
|
---Feedkeys with callback
|
||||||
|
---@param keys string
|
||||||
|
---@param mode string
|
||||||
|
---@param callback function
|
||||||
keymap.feedkeys = setmetatable({
|
keymap.feedkeys = setmetatable({
|
||||||
callbacks = {},
|
callbacks = {},
|
||||||
}, {
|
}, {
|
||||||
__call = function(self, keys, mode, callback)
|
__call = function(self, keys, mode, callback)
|
||||||
if #keys == 0 then
|
if vim.fn.reg_recording() ~= '' then
|
||||||
return callback and callback() or nil
|
return keymap.feedkeys_macro_safe(keys, mode, callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_feedkeys(keys, mode, true)
|
local is_typed = string.match(mode, 't') ~= nil
|
||||||
|
local is_insert = string.match(mode, 'i') ~= nil
|
||||||
|
|
||||||
if callback then
|
local queue = {}
|
||||||
if vim.fn.reg_recording() == '' then
|
if #keys > 0 then
|
||||||
local id = misc.id('cmp.utils.keymap.feedkeys')
|
table.insert(queue, { keymap.t('<Cmd>set backspace=start<CR>'), 'n' })
|
||||||
self.callbacks[id] = callback
|
table.insert(queue, { keymap.t('<Cmd>set eventignore=all<CR>'), 'n' })
|
||||||
vim.api.nvim_feedkeys(keymap.t('<Cmd>call v:lua.cmp.utils.keymap.feedkeys.run(%s)<CR>'):format(id), 'n', true)
|
table.insert(queue, { keys, string.gsub(mode, '[it]', ''), true })
|
||||||
else
|
table.insert(queue, { keymap.t('<Cmd>set backspace=%s<CR>'):format(vim.o.backspace or ''), 'n' })
|
||||||
-- Does not feed extra keys if macro recording.
|
table.insert(queue, { keymap.t('<Cmd>set eventignore=%s<CR>'):format(vim.o.eventignore or ''), 'n' })
|
||||||
local wait
|
end
|
||||||
wait = vim.schedule_wrap(function()
|
if #keys > 0 or callback then
|
||||||
if vim.fn.getchar(1) == 0 then
|
local id = misc.id('cmp.utils.keymap.feedkeys')
|
||||||
return callback()
|
self.callbacks[id] = function()
|
||||||
end
|
if is_typed then
|
||||||
vim.defer_fn(wait, 1)
|
vim.fn.setreg('".', vim.fn.getreg('".') .. keys)
|
||||||
end)
|
end
|
||||||
wait()
|
if callback then
|
||||||
|
callback()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.insert(queue, { keymap.t('<Cmd>call v:lua.cmp.utils.keymap.feedkeys.run(%s)<CR>'):format(id), 'n', true })
|
||||||
|
end
|
||||||
|
|
||||||
|
if is_insert then
|
||||||
|
for i = #queue, 1, -1 do
|
||||||
|
vim.api.nvim_feedkeys(queue[i][1], queue[i][2] .. 'i', queue[i][3])
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i = 1, #queue do
|
||||||
|
vim.api.nvim_feedkeys(queue[i][1], queue[i][2], queue[i][3])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
@@ -132,6 +164,63 @@ misc.set(_G, { 'cmp', 'utils', 'keymap', 'feedkeys', 'run' }, function(id)
|
|||||||
return ''
|
return ''
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
---Macro safe feedkeys.
|
||||||
|
---@param keys string
|
||||||
|
---@param mode string
|
||||||
|
---@param callback function
|
||||||
|
keymap.feedkeys_macro_safe = setmetatable({
|
||||||
|
queue = {},
|
||||||
|
current = nil,
|
||||||
|
timer = vim.loop.new_timer(),
|
||||||
|
running = false,
|
||||||
|
}, {
|
||||||
|
__call = function(self, keys, mode, callback)
|
||||||
|
local is_insert = string.match(mode, 'i') ~= nil
|
||||||
|
table.insert(self.queue, is_insert and 1 or #self.queue + 1, {
|
||||||
|
keys = keys,
|
||||||
|
mode = mode,
|
||||||
|
callback = callback,
|
||||||
|
})
|
||||||
|
|
||||||
|
if not self.running then
|
||||||
|
self.running = true
|
||||||
|
local consume
|
||||||
|
consume = vim.schedule_wrap(function()
|
||||||
|
if vim.fn.getchar(1) == 0 then
|
||||||
|
if self.current then
|
||||||
|
vim.cmd(('set backspace=%s'):format(self.current.backspace or ''))
|
||||||
|
vim.cmd(('set eventignore=%s'):format(self.current.eventignore or ''))
|
||||||
|
if self.current.callback then
|
||||||
|
self.current.callback()
|
||||||
|
end
|
||||||
|
self.current = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local current = table.remove(self.queue, 1)
|
||||||
|
if current then
|
||||||
|
self.current = {
|
||||||
|
keys = current.keys,
|
||||||
|
callback = current.callback,
|
||||||
|
backspace = vim.o.backspace,
|
||||||
|
eventignore = vim.o.eventignore,
|
||||||
|
}
|
||||||
|
vim.api.nvim_feedkeys(keymap.t('<Cmd>set backspace=start<CR>'), 'n', true)
|
||||||
|
vim.api.nvim_feedkeys(keymap.t('<Cmd>set eventignore=all<CR>'), 'n', true)
|
||||||
|
vim.api.nvim_feedkeys(current.keys, string.gsub(current.mode, '[i]', ''), true) -- 'i' flag is manually resolved.
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if #self.queue ~= 0 or self.current then
|
||||||
|
vim.defer_fn(consume, 1)
|
||||||
|
else
|
||||||
|
self.running = false
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
vim.defer_fn(consume, 1)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
---Register keypress handler.
|
---Register keypress handler.
|
||||||
keymap.listen = setmetatable({
|
keymap.listen = setmetatable({
|
||||||
cache = cache.new(),
|
cache = cache.new(),
|
||||||
@@ -169,9 +258,15 @@ keymap.listen = setmetatable({
|
|||||||
})
|
})
|
||||||
misc.set(_G, { 'cmp', 'utils', 'keymap', 'listen', 'run' }, function(id)
|
misc.set(_G, { 'cmp', 'utils', 'keymap', 'listen', 'run' }, function(id)
|
||||||
local definition = keymap.listen.cache:get({ 'definition', id })
|
local definition = keymap.listen.cache:get({ 'definition', id })
|
||||||
definition.callback(definition.keys, misc.once(function()
|
if definition.mode == 'c' and vim.fn.getcmdtype() == '=' then
|
||||||
keymap.feedkeys(keymap.t(definition.fallback), 'i')
|
return vim.api.nvim_feedkeys(keymap.t(definition.fallback), 'i', true)
|
||||||
end))
|
end
|
||||||
|
definition.callback(
|
||||||
|
definition.keys,
|
||||||
|
misc.once(function()
|
||||||
|
vim.api.nvim_feedkeys(keymap.t(definition.fallback), 'i', true)
|
||||||
|
end)
|
||||||
|
)
|
||||||
return keymap.t('<Ignore>')
|
return keymap.t('<Ignore>')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -252,4 +347,21 @@ keymap.find_map_by_lhs = function(mode, lhs)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
keymap.spec = function()
|
||||||
|
vim.fn.setreg('q', '')
|
||||||
|
vim.cmd([[normal! qq]])
|
||||||
|
vim.schedule(function()
|
||||||
|
keymap.feedkeys('i', 'nt', function()
|
||||||
|
keymap.feedkeys(keymap.t('foo2'), 'n')
|
||||||
|
keymap.feedkeys(keymap.t('bar2'), 'nt')
|
||||||
|
keymap.feedkeys(keymap.t('baz2'), 'n', function()
|
||||||
|
vim.cmd[[normal! q]]
|
||||||
|
end)
|
||||||
|
keymap.feedkeys(keymap.t('baz1'), 'ni')
|
||||||
|
keymap.feedkeys(keymap.t('bar1'), 'nti')
|
||||||
|
keymap.feedkeys(keymap.t('foo1'), 'ni')
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
return keymap
|
return keymap
|
||||||
|
|||||||
@@ -17,6 +17,26 @@ describe('keymap', function()
|
|||||||
assert.are.equal(keymap.escape('<LT>C-d>'), '<LT>C-d>')
|
assert.are.equal(keymap.escape('<LT>C-d>'), '<LT>C-d>')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('feedkeys', function()
|
||||||
|
it('dot-repeat', function()
|
||||||
|
vim.fn.setreg('".', '')
|
||||||
|
keymap.feedkeys(keymap.t('i'), 'nt')
|
||||||
|
keymap.feedkeys(keymap.t('aiueo<Esc>'), 'nt')
|
||||||
|
keymap.feedkeys(keymap.t('<Ignore>'), 'nx')
|
||||||
|
assert.are.equal(vim.fn.getreg('".'), keymap.t('iaiueo<Esc>'))
|
||||||
|
end)
|
||||||
|
it('macro', function()
|
||||||
|
vim.fn.setreg('q', '')
|
||||||
|
vim.cmd([[normal! qq]])
|
||||||
|
keymap.feedkeys(keymap.t('iaiueo'), 'nt')
|
||||||
|
keymap.feedkeys(keymap.t('<Esc>'), 'nt', function()
|
||||||
|
vim.cmd([[normal! q]])
|
||||||
|
assert.are.equal(vim.fn.getreg('q'), keymap.t('iaiueo<Esc>'))
|
||||||
|
print(vim.fn.getreg('q'))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe('evacuate', function()
|
describe('evacuate', function()
|
||||||
before_each(spec.before)
|
before_each(spec.before)
|
||||||
|
|
||||||
|
|||||||
@@ -29,24 +29,6 @@ misc.concat = function(list1, list2)
|
|||||||
return new_list
|
return new_list
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get cursor before line
|
|
||||||
---@return string
|
|
||||||
misc.get_cursor_before_line = function()
|
|
||||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
|
||||||
return string.sub(vim.api.nvim_get_current_line(), 1, cursor[2])
|
|
||||||
end
|
|
||||||
|
|
||||||
---Return current mode is insert-mode or not.
|
|
||||||
---@return boolean
|
|
||||||
misc.is_suitable_mode = function()
|
|
||||||
local mode = vim.api.nvim_get_mode().mode
|
|
||||||
return vim.tbl_contains({
|
|
||||||
'i',
|
|
||||||
'ic',
|
|
||||||
'ix',
|
|
||||||
}, mode)
|
|
||||||
end
|
|
||||||
|
|
||||||
---Merge two tables recursively
|
---Merge two tables recursively
|
||||||
---@generic T
|
---@generic T
|
||||||
---@param v1 T
|
---@param v1 T
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
local cache = require('cmp.utils.cache')
|
local cache = require('cmp.utils.cache')
|
||||||
local misc = require('cmp.utils.misc')
|
local misc = require('cmp.utils.misc')
|
||||||
local buffer = require('cmp.utils.buffer')
|
local buffer = require('cmp.utils.buffer')
|
||||||
|
local api = require('cmp.utils.api')
|
||||||
|
|
||||||
---@class cmp.WindowStyle
|
---@class cmp.WindowStyle
|
||||||
---@field public relative string
|
---@field public relative string
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ local window = require('cmp.utils.window')
|
|||||||
local config = require('cmp.config')
|
local config = require('cmp.config')
|
||||||
local types = require('cmp.types')
|
local types = require('cmp.types')
|
||||||
local keymap = require('cmp.utils.keymap')
|
local keymap = require('cmp.utils.keymap')
|
||||||
local misc = require('cmp.utils.misc')
|
local api = require('cmp.utils.api')
|
||||||
|
|
||||||
---@class cmp.CustomEntriesView
|
---@class cmp.CustomEntriesView
|
||||||
---@field private entries_win cmp.Window
|
---@field private entries_win cmp.Window
|
||||||
@@ -129,16 +129,16 @@ custom_entries_view.open = function(self, offset, entries)
|
|||||||
width = width + self.column_width.kind + (self.column_width.menu > 0 and 1 or 0)
|
width = width + self.column_width.kind + (self.column_width.menu > 0 and 1 or 0)
|
||||||
width = width + self.column_width.menu + 1
|
width = width + self.column_width.menu + 1
|
||||||
|
|
||||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
local cursor = api.get_cursor()
|
||||||
local pos = vim.fn.screenpos(0, cursor[1], cursor[2] + 1)
|
local pos = api.get_screen_cursor()
|
||||||
local height = vim.api.nvim_get_option('pumheight')
|
local height = vim.api.nvim_get_option('pumheight')
|
||||||
height = height == 0 and #self.entries or height
|
height = height == 0 and #self.entries or height
|
||||||
height = math.min(height, #self.entries)
|
height = math.min(height, #self.entries)
|
||||||
if (vim.o.lines - pos.row) <= 8 and pos.row - 8 > 0 then
|
if (vim.o.lines - pos[1]) <= 8 and pos[1] - 8 > 0 then
|
||||||
height = math.min(height, pos.row - 1)
|
height = math.min(height, pos[1] - 1)
|
||||||
pos.row = pos.row - height - 1
|
pos[1] = pos[1] - height - 1
|
||||||
else
|
else
|
||||||
height = math.min(height, vim.o.lines - pos.row)
|
height = math.min(height, vim.o.lines - pos[1])
|
||||||
end
|
end
|
||||||
|
|
||||||
if width < 1 or height < 1 then
|
if width < 1 or height < 1 then
|
||||||
@@ -149,8 +149,8 @@ custom_entries_view.open = function(self, offset, entries)
|
|||||||
self.entries_win:open({
|
self.entries_win:open({
|
||||||
relative = 'editor',
|
relative = 'editor',
|
||||||
style = 'minimal',
|
style = 'minimal',
|
||||||
row = pos.row,
|
row = pos[1],
|
||||||
col = pos.col - 1 - delta - 1,
|
col = pos[2] - delta - 1,
|
||||||
width = width,
|
width = width,
|
||||||
height = height,
|
height = height,
|
||||||
zindex = 1001,
|
zindex = 1001,
|
||||||
@@ -270,7 +270,7 @@ custom_entries_view._select = function(self, cursor, option)
|
|||||||
local is_insert = (option.behavior or types.cmp.SelectBehavior.Insert) == types.cmp.SelectBehavior.Insert
|
local is_insert = (option.behavior or types.cmp.SelectBehavior.Insert) == types.cmp.SelectBehavior.Insert
|
||||||
if is_insert then
|
if is_insert then
|
||||||
if vim.api.nvim_win_get_cursor(self.entries_win.win)[2] == 1 then
|
if vim.api.nvim_win_get_cursor(self.entries_win.win)[2] == 1 then
|
||||||
self.prefix = string.sub(vim.api.nvim_get_current_line(), self.offset, vim.api.nvim_win_get_cursor(0)[2]) or ''
|
self.prefix = string.sub(api.get_current_line(), self.offset, api.get_cursor()[2]) or ''
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -286,18 +286,12 @@ custom_entries_view._select = function(self, cursor, option)
|
|||||||
end
|
end
|
||||||
|
|
||||||
custom_entries_view._insert = function(self, word)
|
custom_entries_view._insert = function(self, word)
|
||||||
vim.api.nvim_buf_set_keymap(0, 'i', '<Plug>(cmp.view.custom_entries_view._insert.remove)', ('v:lua.cmp.view.custom_entries_view._insert.remove(%s)'):format(self.offset), {
|
keymap.feedkeys('', 'n', function()
|
||||||
expr = true,
|
local release = require('cmp').core:suspend()
|
||||||
noremap = true,
|
local cursor = api.get_cursor()
|
||||||
})
|
local length = vim.str_utfindex(string.sub(api.get_current_line(), self.offset, cursor[2]))
|
||||||
keymap.feedkeys(keymap.t('<Plug>(cmp.view.custom_entries_view._insert.remove)'), 't')
|
keymap.feedkeys(keymap.backspace(length) .. word, 'int', vim.schedule_wrap(release))
|
||||||
keymap.feedkeys(word, 'nt')
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
misc.set(_G, { 'cmp', 'view', 'custom_entries_view', '_insert', 'remove' }, function(offset)
|
|
||||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
|
||||||
local length = vim.str_utfindex(string.sub(vim.api.nvim_get_current_line(), offset, cursor[2]))
|
|
||||||
return keymap.backspace(length)
|
|
||||||
end)
|
|
||||||
|
|
||||||
return custom_entries_view
|
return custom_entries_view
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
local config = require('cmp.config')
|
local config = require('cmp.config')
|
||||||
local str = require('cmp.utils.str')
|
local str = require('cmp.utils.str')
|
||||||
local types = require('cmp.types')
|
local types = require('cmp.types')
|
||||||
|
local api = require('cmp.utils.api')
|
||||||
|
|
||||||
---@class cmp.GhostTextView
|
---@class cmp.GhostTextView
|
||||||
local ghost_text_view = {}
|
local ghost_text_view = {}
|
||||||
@@ -53,6 +54,9 @@ end
|
|||||||
---Show ghost text
|
---Show ghost text
|
||||||
---@param e cmp.Entry
|
---@param e cmp.Entry
|
||||||
ghost_text_view.show = function(self, e)
|
ghost_text_view.show = function(self, e)
|
||||||
|
if not api.is_insert_mode() then
|
||||||
|
return
|
||||||
|
end
|
||||||
local changed = e ~= self.entry
|
local changed = e ~= self.entry
|
||||||
self.win = vim.api.nvim_get_current_win()
|
self.win = vim.api.nvim_get_current_win()
|
||||||
self.entry = e
|
self.entry = e
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ local autocmd = require('cmp.utils.autocmd')
|
|||||||
local keymap = require('cmp.utils.keymap')
|
local keymap = require('cmp.utils.keymap')
|
||||||
local types = require('cmp.types')
|
local types = require('cmp.types')
|
||||||
local config = require('cmp.config')
|
local config = require('cmp.config')
|
||||||
local misc = require('cmp.utils.misc')
|
local api = require('cmp.utils.api')
|
||||||
|
|
||||||
---@class cmp.NativeEntriesView
|
---@class cmp.NativeEntriesView
|
||||||
---@field private offset number
|
---@field private offset number
|
||||||
@@ -70,7 +70,7 @@ native_entries_view.open = function(self, offset, entries)
|
|||||||
end
|
end
|
||||||
|
|
||||||
native_entries_view.close = function(self)
|
native_entries_view.close = function(self)
|
||||||
if misc.is_suitable_mode() then
|
if api.is_suitable_mode() then
|
||||||
vim.fn.complete(1, {})
|
vim.fn.complete(1, {})
|
||||||
end
|
end
|
||||||
self.offset = -1
|
self.offset = -1
|
||||||
@@ -80,7 +80,7 @@ native_entries_view.close = function(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
native_entries_view.abort = function(_)
|
native_entries_view.abort = function(_)
|
||||||
if misc.is_suitable_mode() then
|
if api.is_suitable_mode() then
|
||||||
vim.api.nvim_select_popupmenu_item(-1, true, true, {})
|
vim.api.nvim_select_popupmenu_item(-1, true, true, {})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user