This commit is contained in:
zztrieuzz 2022-04-26 14:00:26 +07:00
parent 63779ea99e
commit 62519f7573
6 changed files with 36 additions and 19 deletions

View File

@ -407,17 +407,9 @@ M.autopairs_map = function(bufnr, char)
local rules = M.get_buf_rules(bufnr)
for _, rule in pairs(rules) do
if rule.start_pair then
if rule.is_regex and rule.key_map and rule.key_map ~= '' then
if rule.key_map and rule.key_map:match('<.*>') then
new_text = line:sub(1, col) .. line:sub(col + 1, #line)
add_char = 0
elseif rule.key_map and rule.key_map:match('<.*>') then
-- if it is a special key like <c-a>
if utils.esc(rule.key_map) ~= char then
new_text = ''
else
new_text = line:sub(1, col) .. line:sub(col + 1, #line)
add_char = 0
end
else
new_text = line:sub(1, col) .. char .. line:sub(col + 1, #line)
add_char = rule.key_map and #rule.key_map or 1

View File

@ -66,9 +66,16 @@ M.show = function(line)
then
local key = config.keys:sub(index, index)
index = index + 1
if utils.is_quote(char) then
if
utils.is_quote(char)
or (
utils.is_close_bracket(char)
and utils.is_in_quotes(line, col, prev_char)
)
then
offset = 0
end
if i == str_length then
is_end_key = false
key = config.end_key
@ -94,7 +101,7 @@ M.show = function(line)
vim.api.nvim_buf_clear_namespace(0, M.ns_fast_wrap, row, row + 1)
for _, pos in pairs(list_pos) do
if char == pos.key then
M.move_bracket(line, pos.col, end_pair,false)
M.move_bracket(line, pos.col, end_pair, false)
break
end
if char == string.upper(pos.key) then
@ -122,8 +129,8 @@ M.move_bracket = function(line, target_pos, end_pair, change_pos)
line = line:sub(1, target_pos) .. end_pair .. line:sub(target_pos + 1, #line)
vim.api.nvim_set_current_line(line)
if change_pos then
vim.api.nvim_win_set_cursor(0,{row + 1, target_pos})
if change_pos then
vim.api.nvim_win_set_cursor(0, { row + 1, target_pos })
end
end

View File

@ -49,6 +49,7 @@ function Rule.new(...)
end_pair_length = nil,
}, opt)
---@param rule Rule
local function constructor(rule)
-- check multibyte
if #rule.start_pair ~= vim.api.nvim_strwidth(rule.start_pair) then

View File

@ -62,7 +62,7 @@ M.is_in_quotes = function (line, pos, quote_type)
then
result = false
last_char = quote_type or ''
elseif result == false and M.is_quote(char)
elseif result == false and M.is_quote(char)
and (not quote_type or char == quote_type)
then
last_char = quote_type or char

View File

@ -596,6 +596,19 @@ local data = {
before = [[ | aaa]],
after = [[ "ab| aaa]]
},
{
setup_func = function()
npairs.add_rule(
Rule('struct%s[a-zA-Z]+%s?{$', '};' )
:use_regex(true, "{")
)
end,
filetype = 'javascript',
name = 'custom endwise rule',
key = [[{]],
before = [[struct abc | ]],
after = [[struct abc {|};]],
},
{
setup_func = function()
npairs.clear_rules()

View File

@ -143,11 +143,15 @@ _G.Test_withfile = function(test_data, cb)
0,
{ pos_before.linenr, pos_before.colnr - 1 }
)
log.debug('insert:' .. value.key)
helpers.insert(value.key, value.not_replace_term_code)
vim.wait(2)
helpers.feed('<esc>')
if type(value.key) == "function" then
log.debug("call key")
value.key()
else
log.debug('insert:' .. value.key)
helpers.insert(value.key, value.not_replace_term_code)
vim.wait(2)
helpers.feed('<esc>')
end
compare_text(
value.linenr,
value.after,