This commit is contained in:
hrsh7th
2021-11-02 01:52:51 +09:00
parent 674d2b1389
commit b7f79a19c6
2 changed files with 31 additions and 17 deletions

View File

@@ -207,17 +207,20 @@ core.autoindent = function(self, trigger_event, callback)
return callback() return callback()
end end
-- Check prefix
local cursor_before_line = api.get_cursor_before_line() local cursor_before_line = api.get_cursor_before_line()
local prefix = pattern.matchstr('[^[:blank:]]\\+$', cursor_before_line) or '' local prefix = pattern.matchstr('[^[:blank:]]\\+$', cursor_before_line) or ''
if #prefix > 0 then if #prefix == 0 then
return callback()
end
-- Scan indentkeys.
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() -- Check autoindent already applied.
if cursor_before_line == api.get_cursor_before_line() then if cursor_before_line == api.get_cursor_before_line() then
feedkeys.call(keymap.t('<Cmd>setlocal cindent<CR>'), 'n') feedkeys.call(keymap.autoindent(), 'n', function()
feedkeys.call(keymap.t('<C-f>'), 'n')
feedkeys.call(keymap.t('<Cmd>setlocal %scindent<CR>'):format(vim.bo.cindent and '' or 'no'), 'n', function()
release() release()
callback() callback()
end) end)
@@ -228,7 +231,8 @@ core.autoindent = function(self, trigger_event, callback)
return return
end end
end end
end
-- indentkeys does not matched.
callback() callback()
end end

View File

@@ -90,6 +90,16 @@ keymap.backspace = function(count)
return table.concat(keys, '') return table.concat(keys, '')
end end
---Create autoindent keys
---@return string
keymap.autoindent = function()
local keys = {}
table.insert(keys, keymap.t('<Cmd>setlocal indentkeys+=!^F<CR>'))
table.insert(keys, keymap.t('<C-f>'))
table.insert(keys, keymap.t('<Cmd>setlocal indentkeys=%s<CR>'):format(vim.bo.indentkeys))
return table.concat(keys, '')
end
---Return two key sequence are equal or not. ---Return two key sequence are equal or not.
---@param a string ---@param a string
---@param b string ---@param b string