From a63a1a23e9a7e62b21a5c151c771ed6ca21a0990 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Wed, 29 Sep 2021 21:20:35 +0900 Subject: [PATCH] Fix #262 --- lua/cmp/core.lua | 17 ++++++++++++++--- lua/cmp/utils/misc.lua | 7 +++++++ plugin/cmp.lua | 11 ----------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index 0af87d7..f8d5996 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -226,12 +226,23 @@ end ---@param callback function core.autoindent = function(event, callback) if event == types.cmp.TriggerEvent.TextChanged then - local ctx = context.new() - local prefix = pattern.matchstr('[^[:blank:]]\\+$', ctx.cursor_before_line) + local cursor_before_line = misc.get_cursor_before_line() + local prefix = pattern.matchstr('[^[:blank:]]\\+$', cursor_before_line) if prefix then for _, key in ipairs(vim.split(vim.bo.indentkeys, ',')) do if vim.tbl_contains({ '=' .. prefix, '0=' .. prefix }, key) then - return keymap.feedkeys(keymap.t('(cmp-autoindent)'), '', callback) + return vim.schedule(function() + if cursor_before_line == misc.get_cursor_before_line() then + local indentkeys = vim.bo.indentkeys + vim.bo.indentkeys = indentkeys .. ',!^F' + keymap.feedkeys(keymap.t(''), 'n', function() + vim.bo.indentkeys = indentkeys + callback() + end) + else + callback() + end + end) end end end diff --git a/lua/cmp/utils/misc.lua b/lua/cmp/utils/misc.lua index 36396fc..cb3ebf6 100644 --- a/lua/cmp/utils/misc.lua +++ b/lua/cmp/utils/misc.lua @@ -15,6 +15,13 @@ misc.concat = function(list1, list2) return new_list 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 + ---Merge two tables recursively ---@generic T ---@param v1 T diff --git a/plugin/cmp.lua b/plugin/cmp.lua index 5a2b19c..6d30e66 100644 --- a/plugin/cmp.lua +++ b/plugin/cmp.lua @@ -17,17 +17,6 @@ vim.cmd [[ augroup END ]] -vim.cmd [[inoremap (cmp-autoindent) call v:lua.cmp.autoindent()]] -misc.set(_G, { 'cmp', 'autoindent' }, function() - local startofline = vim.o.startofline - local virtualedit = vim.o.virtualedit - vim.o.startofline = false - vim.o.virtualedit = 'all' - vim.cmd [[normal! ==]] - vim.o.startofline = startofline - vim.o.virtualedit = virtualedit -end) - vim.cmd [[command! CmpStatus lua require('cmp').status()]] vim.cmd [[doautocmd User cmp#ready]]