From b7f79a19c6ffbdf513c4fd5ded798fad15a0ed37 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Tue, 2 Nov 2021 01:52:51 +0900 Subject: [PATCH] Fix #443 --- lua/cmp/core.lua | 38 +++++++++++++++++++++----------------- lua/cmp/utils/keymap.lua | 10 ++++++++++ 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index 9bd5e6a..01f683c 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -207,28 +207,32 @@ core.autoindent = function(self, trigger_event, callback) return callback() end + -- Check prefix local cursor_before_line = api.get_cursor_before_line() local prefix = pattern.matchstr('[^[:blank:]]\\+$', cursor_before_line) or '' - if #prefix > 0 then - for _, key in ipairs(vim.split(vim.bo.indentkeys, ',')) do - if vim.tbl_contains({ '=' .. prefix, '0=' .. prefix }, key) then - local release = self:suspend() - vim.schedule(function() - if cursor_before_line == api.get_cursor_before_line() then - feedkeys.call(keymap.t('setlocal cindent'), 'n') - feedkeys.call(keymap.t(''), 'n') - feedkeys.call(keymap.t('setlocal %scindent'):format(vim.bo.cindent and '' or 'no'), 'n', function() - release() - callback() - end) - else + if #prefix == 0 then + return callback() + end + + -- Scan indentkeys. + for _, key in ipairs(vim.split(vim.bo.indentkeys, ',')) do + if vim.tbl_contains({ '=' .. prefix, '0=' .. prefix }, key) then + local release = self:suspend() + vim.schedule(function() -- Check autoindent already applied. + if cursor_before_line == api.get_cursor_before_line() then + feedkeys.call(keymap.autoindent(), 'n', function() + release() callback() - end - end) - return - end + end) + else + callback() + end + end) + return end end + + -- indentkeys does not matched. callback() end diff --git a/lua/cmp/utils/keymap.lua b/lua/cmp/utils/keymap.lua index 272708e..c894aa9 100644 --- a/lua/cmp/utils/keymap.lua +++ b/lua/cmp/utils/keymap.lua @@ -90,6 +90,16 @@ keymap.backspace = function(count) return table.concat(keys, '') end +---Create autoindent keys +---@return string +keymap.autoindent = function() + local keys = {} + table.insert(keys, keymap.t('setlocal indentkeys+=!^F')) + table.insert(keys, keymap.t('')) + table.insert(keys, keymap.t('setlocal indentkeys=%s'):format(vim.bo.indentkeys)) + return table.concat(keys, '') +end + ---Return two key sequence are equal or not. ---@param a string ---@param b string