From b09ab7495e55f09293500aacf460d176f942504d Mon Sep 17 00:00:00 2001 From: windwp Date: Sat, 5 Jun 2021 13:57:28 +0700 Subject: [PATCH] disable add bracket after quote with comma --- lua/nvim-autopairs.lua | 32 ++++++++++++++++++++------------ tests/afterquote_spec.lua | 10 +++++++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lua/nvim-autopairs.lua b/lua/nvim-autopairs.lua index 68ed2b3..7afc785 100644 --- a/lua/nvim-autopairs.lua +++ b/lua/nvim-autopairs.lua @@ -404,7 +404,7 @@ end --- add bracket pairs after quote (|"aaaaa" => (|"aaaaaa") M.autopairs_afterquote = function(line, key_char) - if M.config.enable_afterquote then + if M.config.enable_afterquote then line = line or utils.text_get_current_line(0) local _, col = utils.get_cursor() local prev_char, next_char = utils.text_cusor_line(line, col + 1, 1, 1, false) @@ -412,26 +412,34 @@ M.autopairs_afterquote = function(line, key_char) utils.is_bracket(prev_char) and utils.is_quote(next_char) and not utils.is_in_quote(line, col, next_char) - then - local is_prev_slash = false - for i = col + 2 + #next_char, #line, 1 do - local char = line:sub(i, i + #next_char -1) - local char_end = line:sub(i + 1, i + #next_char ) - if not is_prev_slash and char == next_char then + then + local is_prev_slash = false + for i = col + 2 + #next_char, #line, 1 do + local char = line:sub(i, i + #next_char - 1) + local char_end = line:sub(i + 1, i + #next_char) + if not is_prev_slash and char == next_char then + if char_end ~= ',' then for _, rule in pairs(M.state.rules) do if rule.start_pair == prev_char and char_end ~= rule.end_pair then - local new_text = line:sub(0, i) .. rule.end_pair .. line:sub(i + 1,#line) + local new_text = line:sub(0, i) + .. rule.end_pair + .. line:sub(i + 1, #line) M.state.expr_quote = new_text - local append = "a"; - if col > 0 then append = "la" end - return utils.esc('lua MPairs.autopairs_closequote_expr()' .. append) + local append = 'a' + if col > 0 then + append = 'la' + end + return utils.esc( + 'lua MPairs.autopairs_closequote_expr()' .. append + ) end end end - is_prev_slash = char == '\\' end + is_prev_slash = char == '\\' end end + end return utils.esc(key_char) end diff --git a/tests/afterquote_spec.lua b/tests/afterquote_spec.lua index 6f85d37..488c85d 100644 --- a/tests/afterquote_spec.lua +++ b/tests/afterquote_spec.lua @@ -64,7 +64,6 @@ local data = { }, { - -- only=true, name = 'test add close quote inside string', filepath = './tests/endwise/init.lua', filetype = 'lua', @@ -73,6 +72,15 @@ local data = { before = [[const abc="visu|"dsa"" ]], after = [[const abc="visu(|"dsa"" ]], }, + { + name = 'not add bracket with quote have comma', + filepath = './tests/endwise/init.lua', + filetype = 'lua', + linenr = '5', + key = [[(]], + before = [[|"data", abcdef]], + after = [[(|"data", abcdef]], + }, } local run_data = _G.Test_filter(data)