fix: add bracket afterquote inside a string

This commit is contained in:
windwp 2021-05-25 10:38:40 +07:00
parent a61ed0eb42
commit c7192e6fcc
3 changed files with 33 additions and 16 deletions

View File

@ -403,30 +403,34 @@ 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)
if utils.is_bracket(prev_char) and utils.is_quote(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
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)
M.state.expr_quote = new_text
local append = "a";
if col > 0 then append = "la" end
return utils.esc('<esc><cmd>lua MPairs.autopairs_closequote_expr()<cr>' .. append)
if
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
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)
M.state.expr_quote = new_text
local append = "a";
if col > 0 then append = "la" end
return utils.esc('<esc><cmd>lua MPairs.autopairs_closequote_expr()<cr>' .. append)
end
end
end
is_prev_slash = char == '\\'
end
is_prev_slash = char == '\\'
end
end
end
return utils.esc(key_char)
end

View File

@ -51,8 +51,10 @@ M.is_in_quote = function(line, pos, quote)
char == quote and
line:sub(cIndex -1, cIndex -1) ~= "\\"
then
log.debug('match')
result = false
elseif result == false and char == quote then
log.debug('match')
result = true
end
end

View File

@ -62,6 +62,17 @@ local data = {
before = [[const abc=|"visu\"dsa" ]],
after = [[const abc=(|"visu\"dsa") ]],
},
{
-- only=true,
name = 'test add close quote inside string',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = '5',
key = [[(]],
before = [[const abc="visu|"dsa"" ]],
after = [[const abc="visu(|"dsa"" ]],
},
}
local run_data = _G.Test_filter(data)