mirror of
https://github.com/ilyakooo0/nvim-autopairs.git
synced 2024-10-26 08:41:46 +03:00
fix #238
This commit is contained in:
parent
63779ea99e
commit
62519f7573
@ -407,17 +407,9 @@ M.autopairs_map = function(bufnr, char)
|
|||||||
local rules = M.get_buf_rules(bufnr)
|
local rules = M.get_buf_rules(bufnr)
|
||||||
for _, rule in pairs(rules) do
|
for _, rule in pairs(rules) do
|
||||||
if rule.start_pair then
|
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)
|
new_text = line:sub(1, col) .. line:sub(col + 1, #line)
|
||||||
add_char = 0
|
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
|
else
|
||||||
new_text = line:sub(1, col) .. char .. line:sub(col + 1, #line)
|
new_text = line:sub(1, col) .. char .. line:sub(col + 1, #line)
|
||||||
add_char = rule.key_map and #rule.key_map or 1
|
add_char = rule.key_map and #rule.key_map or 1
|
||||||
|
@ -66,9 +66,16 @@ M.show = function(line)
|
|||||||
then
|
then
|
||||||
local key = config.keys:sub(index, index)
|
local key = config.keys:sub(index, index)
|
||||||
index = index + 1
|
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
|
offset = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
if i == str_length then
|
if i == str_length then
|
||||||
is_end_key = false
|
is_end_key = false
|
||||||
key = config.end_key
|
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)
|
vim.api.nvim_buf_clear_namespace(0, M.ns_fast_wrap, row, row + 1)
|
||||||
for _, pos in pairs(list_pos) do
|
for _, pos in pairs(list_pos) do
|
||||||
if char == pos.key then
|
if char == pos.key then
|
||||||
M.move_bracket(line, pos.col, end_pair,false)
|
M.move_bracket(line, pos.col, end_pair, false)
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
if char == string.upper(pos.key) then
|
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)
|
line = line:sub(1, target_pos) .. end_pair .. line:sub(target_pos + 1, #line)
|
||||||
vim.api.nvim_set_current_line(line)
|
vim.api.nvim_set_current_line(line)
|
||||||
if change_pos then
|
if change_pos then
|
||||||
vim.api.nvim_win_set_cursor(0,{row + 1, target_pos})
|
vim.api.nvim_win_set_cursor(0, { row + 1, target_pos })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -49,6 +49,7 @@ function Rule.new(...)
|
|||||||
end_pair_length = nil,
|
end_pair_length = nil,
|
||||||
}, opt)
|
}, opt)
|
||||||
|
|
||||||
|
---@param rule Rule
|
||||||
local function constructor(rule)
|
local function constructor(rule)
|
||||||
-- check multibyte
|
-- check multibyte
|
||||||
if #rule.start_pair ~= vim.api.nvim_strwidth(rule.start_pair) then
|
if #rule.start_pair ~= vim.api.nvim_strwidth(rule.start_pair) then
|
||||||
|
@ -62,7 +62,7 @@ M.is_in_quotes = function (line, pos, quote_type)
|
|||||||
then
|
then
|
||||||
result = false
|
result = false
|
||||||
last_char = quote_type or ''
|
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)
|
and (not quote_type or char == quote_type)
|
||||||
then
|
then
|
||||||
last_char = quote_type or char
|
last_char = quote_type or char
|
||||||
|
@ -596,6 +596,19 @@ local data = {
|
|||||||
before = [[ | aaa]],
|
before = [[ | aaa]],
|
||||||
after = [[ "ab| 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()
|
setup_func = function()
|
||||||
npairs.clear_rules()
|
npairs.clear_rules()
|
||||||
|
@ -143,11 +143,15 @@ _G.Test_withfile = function(test_data, cb)
|
|||||||
0,
|
0,
|
||||||
{ pos_before.linenr, pos_before.colnr - 1 }
|
{ pos_before.linenr, pos_before.colnr - 1 }
|
||||||
)
|
)
|
||||||
log.debug('insert:' .. value.key)
|
if type(value.key) == "function" then
|
||||||
|
log.debug("call key")
|
||||||
helpers.insert(value.key, value.not_replace_term_code)
|
value.key()
|
||||||
vim.wait(2)
|
else
|
||||||
helpers.feed('<esc>')
|
log.debug('insert:' .. value.key)
|
||||||
|
helpers.insert(value.key, value.not_replace_term_code)
|
||||||
|
vim.wait(2)
|
||||||
|
helpers.feed('<esc>')
|
||||||
|
end
|
||||||
compare_text(
|
compare_text(
|
||||||
value.linenr,
|
value.linenr,
|
||||||
value.after,
|
value.after,
|
||||||
|
Loading…
Reference in New Issue
Block a user