fix: check pairs inside quote

This commit is contained in:
windwp 2021-03-25 08:49:08 +07:00
parent b405f7ccf9
commit 228c4bc517
2 changed files with 46 additions and 29 deletions

View File

@ -109,22 +109,24 @@ MPairs.check_jump = function(char)
return esc(char)
end
local is_quote = function (char)
return char == "'" or char == '"' or char == '`'
end
local function is_in_quote(line, pos, quote)
local cIndex = 0
local result = false
local function is_in_quote(line, pos)
local cIndex = 0
local last_quote = ''
local result = false
while cIndex < string.len(line) and cIndex < pos do
cIndex = cIndex + 1
local char = line:sub(cIndex, cIndex)
if
result == true and
char == last_quote and
char == quote and
line:sub(cIndex -1, cIndex -1) ~= "\\"
then
result = false
elseif result == false and (char == "'" or char == '"' or char == '`') then
last_quote = char
elseif result == false and char == quote then
result = true
end
end
@ -154,28 +156,23 @@ MPairs.check_add = function(char)
-- move right when have quote on end line or in quote
-- situtaion |" => "|
if (next_char == "'" or next_char == '"' or next_char == '`') and next_char == char then
if is_quote(next_char) and next_char == char then
if next_col == string.len(line) then
return 2
end
-- ("|") => (""|)
-- "" |" " => "" "| "
if is_in_quote(line, next_col - 1) then
if is_in_quote(line, next_col - 1, char) then
return 2
end
end
-- don' t add single quote if prev char is word
-- don' t add single quote if prev char is a word
-- a| => not add
if char == "'" and prev_char:match("%w")then
return 0
end
-- when on end line col not work with autocomplete method so we need to skip it
if next_col == string.len(line) + 1 then
-- need to update completion nvim for check
return 1
end
-- situtaion |( => not add
if next_char == char then
@ -189,21 +186,28 @@ MPairs.check_add = function(char)
return 0
end
local char_end = pairs_map[char]
if check_line_pair and next_char == char_end then
if check_line_pair then
local char_end = pairs_map[char]
-- (( many char |)) => add
-- ( many char |)) => not add
local count_prev_char = 0
local count_next_char = 0
for i = 1, #line, 1 do
local c = line:sub(i, i)
if c == char then
count_prev_char = count_prev_char + 1
elseif c == char_end then
count_next_char = count_next_char + 1
if next_char == char_end then
local count_prev_char = 0
local count_next_char = 0
for i = 1, #line, 1 do
local c = line:sub(i, i)
if c == char then
count_prev_char = count_prev_char + 1
elseif c == char_end then
count_next_char = count_next_char + 1
end
end
if count_prev_char ~= count_next_char then
return 0
end
end
if count_prev_char ~= count_next_char then
-- " abc | xyz => not add
if is_quote(char) and is_in_quote(line, next_col -1, char) then
return 0
end
end

View File

@ -51,6 +51,18 @@ local data = {
key = [["]],
before = [[aa |aa]],
after = [[aa "|aa]]
},
{
name = "don't add quote inside quote" ,
key = [["]],
before = [["aa | aa]],
after = [["aa "| aa]]
},
{
name = "add quote if not inside quote" ,
key = [["]],
before = [["aa " | aa]],
after = [["aa " "|" aa]]
},
{
name = "don't add pair after alphabet char" ,
@ -167,9 +179,10 @@ end
describe('autopairs ', function()
Test(run_data)
if isOnly then return end
run_data = {
{
name = "nil breaklin_file_type " ,
name = "nil breakline_file_type " ,
filetype="javascript",
key = [[<cr>]],
before = [[a[|] ]],
@ -184,7 +197,7 @@ describe('autopairs ', function()
Test(run_data)
run_data = {
{
name = "regex file tye" ,
name = "regex file type" ,
filetype="javascript",
key = [[<cr>]],
before = [[a[|] ]],