diff --git a/lua/nvim-autopairs.lua b/lua/nvim-autopairs.lua index ee740cc..81e6698 100644 --- a/lua/nvim-autopairs.lua +++ b/lua/nvim-autopairs.lua @@ -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 - 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 diff --git a/lua/nvim-autopairs/fastwrap.lua b/lua/nvim-autopairs/fastwrap.lua index 838adaa..d6d4458 100644 --- a/lua/nvim-autopairs/fastwrap.lua +++ b/lua/nvim-autopairs/fastwrap.lua @@ -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 diff --git a/lua/nvim-autopairs/rule.lua b/lua/nvim-autopairs/rule.lua index 9a41fe6..3cb8f66 100644 --- a/lua/nvim-autopairs/rule.lua +++ b/lua/nvim-autopairs/rule.lua @@ -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 diff --git a/lua/nvim-autopairs/utils.lua b/lua/nvim-autopairs/utils.lua index 242fd43..d53ef25 100644 --- a/lua/nvim-autopairs/utils.lua +++ b/lua/nvim-autopairs/utils.lua @@ -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 diff --git a/tests/nvim-autopairs_spec.lua b/tests/nvim-autopairs_spec.lua index ab4e777..a64a751 100644 --- a/tests/nvim-autopairs_spec.lua +++ b/tests/nvim-autopairs_spec.lua @@ -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() diff --git a/tests/test_utils.lua b/tests/test_utils.lua index 6dd1eb5..47b1e68 100644 --- a/tests/test_utils.lua +++ b/tests/test_utils.lua @@ -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('') + 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('') + end compare_text( value.linenr, value.after,