on master help sign in
fix #238
commit62519f7573df5be7722bff091d589b7b72296307 authorzztrieuzz <trieu19@gmail.com> dateTue 26 Apr 2022, 14:00 (4 years ago) parent63779ea
6 files changed+36−19
lua/nvim-autopairs.lua+1−9 lua/nvim-autopairs/fastwrap.lua+11−4 lua/nvim-autopairs/rule.lua+1 lua/nvim-autopairs/utils.lua+1−1 tests/nvim-autopairs_spec.lua+13 tests/test_utils.lua+9−5
lua/nvim-autopairs.lua+1−9
@@ -407,17 +407,9 @@ M.autopairs_map = function(bufnr, char)
407407 local rules = M.get_buf_rules(bufnr)
408408 for _, rule in pairs(rules) do
409409 if rule.start_pair then
410− if rule.is_regex and rule.key_map and rule.key_map ~= '' then
410+ if rule.key_map and rule.key_map:match('<.*>') then
411411 new_text = line:sub(1, col) .. line:sub(col + 1, #line)
412412 add_char = 0
413− elseif rule.key_map and rule.key_map:match('<.*>') then
414− -- if it is a special key like <c-a>
415− if utils.esc(rule.key_map) ~= char then
416− new_text = ''
417− else
418− new_text = line:sub(1, col) .. line:sub(col + 1, #line)
419− add_char = 0
420− end
421413 else
422414 new_text = line:sub(1, col) .. char .. line:sub(col + 1, #line)
423415 add_char = rule.key_map and #rule.key_map or 1
lua/nvim-autopairs/fastwrap.lua+11−4
@@ -66,9 +66,16 @@ M.show = function(line)
6666 then
6767 local key = config.keys:sub(index, index)
6868 index = index + 1
69− if utils.is_quote(char) then
69+ if
70+ utils.is_quote(char)
71+ or (
72+ utils.is_close_bracket(char)
73+ and utils.is_in_quotes(line, col, prev_char)
74+ )
75+ then
7076 offset = 0
7177 end
78+
7279 if i == str_length then
7380 is_end_key = false
7481 key = config.end_key
@@ -94,7 +101,7 @@ M.show = function(line)
94101 vim.api.nvim_buf_clear_namespace(0, M.ns_fast_wrap, row, row + 1)
95102 for _, pos in pairs(list_pos) do
96103 if char == pos.key then
97− M.move_bracket(line, pos.col, end_pair,false)
104+ M.move_bracket(line, pos.col, end_pair, false)
98105 break
99106 end
100107 if char == string.upper(pos.key) then
@@ -122,8 +129,8 @@ M.move_bracket = function(line, target_pos, end_pair, change_pos)
122129
123130 line = line:sub(1, target_pos) .. end_pair .. line:sub(target_pos + 1, #line)
124131 vim.api.nvim_set_current_line(line)
125− if change_pos then
126− vim.api.nvim_win_set_cursor(0,{row + 1, target_pos})
132+ if change_pos then
133+ vim.api.nvim_win_set_cursor(0, { row + 1, target_pos })
127134 end
128135end
129136
lua/nvim-autopairs/rule.lua+1
@@ -49,6 +49,7 @@ function Rule.new(...)
4949 end_pair_length = nil,
5050 }, opt)
5151
52+ ---@param rule Rule
5253 local function constructor(rule)
5354 -- check multibyte
5455 if #rule.start_pair ~= vim.api.nvim_strwidth(rule.start_pair) then
lua/nvim-autopairs/utils.lua+1−1
@@ -62,7 +62,7 @@ M.is_in_quotes = function (line, pos, quote_type)
6262 then
6363 result = false
6464 last_char = quote_type or ''
65− elseif result == false and M.is_quote(char)
65+ elseif result == false and M.is_quote(char)
6666 and (not quote_type or char == quote_type)
6767 then
6868 last_char = quote_type or char
tests/nvim-autopairs_spec.lua+13
@@ -596,6 +596,19 @@ local data = {
596596 before = [[ | aaa]],
597597 after = [[ "ab| aaa]]
598598 },
599+ {
600+ setup_func = function()
601+ npairs.add_rule(
602+ Rule('struct%s[a-zA-Z]+%s?{$', '};' )
603+ :use_regex(true, "{")
604+ )
605+ end,
606+ filetype = 'javascript',
607+ name = 'custom endwise rule',
608+ key = [[{]],
609+ before = [[struct abc | ]],
610+ after = [[struct abc {|};]],
611+ },
599612 {
600613 setup_func = function()
601614 npairs.clear_rules()
tests/test_utils.lua+9−5
@@ -143,11 +143,15 @@ _G.Test_withfile = function(test_data, cb)
143143 0,
144144 { pos_before.linenr, pos_before.colnr - 1 }
145145 )
146− log.debug('insert:' .. value.key)
147−
148− helpers.insert(value.key, value.not_replace_term_code)
149− vim.wait(2)
150− helpers.feed('<esc>')
146+ if type(value.key) == "function" then
147+ log.debug("call key")
148+ value.key()
149+ else
150+ log.debug('insert:' .. value.key)
151+ helpers.insert(value.key, value.not_replace_term_code)
152+ vim.wait(2)
153+ helpers.feed('<esc>')
154+ end
151155 compare_text(
152156 value.linenr,
153157 value.after,