- fix mapping multiple rule same pair on keymap
- fix sort rule order
This commit is contained in:
windwp 2021-08-14 06:36:45 +07:00
parent 4a3d9426fd
commit b3d5d29163
2 changed files with 38 additions and 4 deletions

View File

@ -164,7 +164,19 @@ M.on_attach = function(bufnr)
end
-- sort by length
table.sort(rules, function(a, b)
return (#a.start_pair or 0) > (#b.start_pair or 0)
if a.start_pair == b.start_pair then
if not b.key_map then
return a.key_map and 1
end
if not a.key_map then
return b.key_map and -1
end
return #a.key_map < #b.key_map
end
if #a.start_pair == #b.start_pair then
return string.byte(a.start_pair) > string.byte(b.start_pair)
end
return #a.start_pair > #b.start_pair
end)
M.state.rules = rules
@ -325,9 +337,13 @@ M.autopairs_map = function(bufnr, char)
if rule.is_regex and rule.key_map and rule.key_map ~= '' then
new_text = line:sub(1, col) .. line:sub(col + 1, #line)
add_char = 0
elseif rule.key_map and #rule.key_map > 1 and utils.esc(rule.key_map) == char then
new_text = line:sub(1, col) .. line:sub(col + 1, #line)
add_char = 0
elseif rule.key_map and #rule.key_map > 1 then
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 = 1

View File

@ -403,6 +403,24 @@ local data = {
before = [[(o)=| ]],
after = [[(o)=> { | } ]]
},
{
setup_func = function()
npairs.add_rules({
Rule('(', ')'):use_key('<c-w>'):replace_endpair(function()
return '<bs><del><del><del>'
end, true),
Rule('(', ')'):use_key('<c-h>'):replace_endpair(function()
return '<bs><del>'
end, true),
})
end,
name="mapping same pair with different key",
filetype="typescript",
key="(",
before = [[(test|) ]],
after = [[(test(|)) ]]
}
}