mirror of
https://github.com/ilyakooo0/nvim-autopairs.git
synced 2024-09-11 07:15:44 +03:00
fix: add bracket afterquote inside a string
This commit is contained in:
parent
a61ed0eb42
commit
c7192e6fcc
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user