Skip to content

Commit

Permalink
fix: add length behavior in regex condition again
Browse files Browse the repository at this point in the history
fix #374
  • Loading branch information
zztrieuzz committed Jul 14, 2023
1 parent 228e64c commit e6adb87
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
38 changes: 18 additions & 20 deletions lua/nvim-autopairs/conds.lua
Expand Up @@ -37,14 +37,13 @@ cond.invert = function(func)
end
end

cond.before_regex = function(regex)
if not regex then
return cond.none()
end
cond.before_regex = function(regex, length)
length = length or 1
if length < 0 then length = nil end
---@param opts CondOpts
return function(opts)
log.debug('before_regex')
local str = utils.text_sub_char(opts.line, opts.col - 1, -opts.col)
local str = utils.text_sub_char(opts.line, opts.col - 1, length - opts.col)
if str:match(regex) then
return true
end
Expand Down Expand Up @@ -78,14 +77,13 @@ cond.after_text = function(text)
end
end

cond.after_regex = function(regex)
if not regex then
return cond.none()
end
cond.after_regex = function(regex, length)
length = length or 1
if length < 0 then length = nil end
---@param opts CondOpts
return function(opts)
log.debug('after_regex')
local str = utils.text_sub_char(opts.line, opts.col, #opts.line)
local str = utils.text_sub_char(opts.line, opts.col, length or #opts.line)
if str:match(regex) then
return true
end
Expand Down Expand Up @@ -116,28 +114,28 @@ cond.not_after_text = function(text)
end
end

cond.not_before_regex = function(regex)
if not regex then
return cond.none()
end
cond.not_before_regex = function(regex, length)
log.debug(length)
length = length or 1
if length < 0 then length = nil end
---@param opts CondOpts
return function(opts)
log.debug('not_before_regex')
local str = utils.text_sub_char(opts.line, opts.col - 1, -opts.col)
log.debug(length)
local str = utils.text_sub_char(opts.line, opts.col - 1, length or -opts.col)
if str:match(regex) then
return false
end
end
end

cond.not_after_regex = function(regex)
if not regex then
return cond.none()
end
cond.not_after_regex = function(regex, length)
length = length or 1
if length < 0 then length = nil end
---@param opts CondOpts
return function(opts)
log.debug('not_after_regex')
local str = utils.text_sub_char(opts.line, opts.col, #opts.line)
local str = utils.text_sub_char(opts.line, opts.col, length or #opts.line)
if str:match(regex) then
return false
end
Expand Down
9 changes: 4 additions & 5 deletions lua/nvim-autopairs/rules/basic.lua
Expand Up @@ -7,7 +7,7 @@ local function quote_creator(opt)
local rule = Rule(...):with_move(move_func()):with_pair(cond.not_add_quote_inside_quote())

if #opt.ignored_next_char > 1 then
rule:with_pair(cond.not_after_regex('^' .. opt.ignored_next_char))
rule:with_pair(cond.not_after_regex(opt.ignored_next_char))
end
rule:use_undo(opt.break_undo)
return rule
Expand All @@ -32,7 +32,6 @@ local function bracket_creator(opt)
end

local function setup(opt)
-- stylua: ignore
local quote = quote_creator(opt)
local bracket = bracket_creator(opt)
local rules = {
Expand All @@ -41,11 +40,11 @@ local function setup(opt)
Rule("```.*$", "```", { "markdown", "vimwiki", "rmarkdown", "rmd", "pandoc" }):only_cr():use_regex(true),
Rule('"""', '"""', { "python", "elixir", "julia", "kotlin" }):with_pair(cond.not_before_char('"', 3)),
Rule("'''", "'''", { "python" }):with_pair(cond.not_before_char('"', 3)),
quote("'", "'", "-rust"):with_pair(cond.not_before_regex("%w$")),
quote("'", "'", "rust"):with_pair(cond.not_before_regex("[%w<&]$")):with_pair(cond.not_after_text(">")),
quote("'", "'", "-rust"):with_pair(cond.not_before_regex("%w")),
quote("'", "'", "rust"):with_pair(cond.not_before_regex("[%w<&]")):with_pair(cond.not_after_text(">")),
quote("`", "`"),
quote('"', '"', "-vim"),
quote('"', '"', "vim"):with_pair(cond.not_before_regex("^%s*$")),
quote('"', '"', "vim"):with_pair(cond.not_before_regex("^%s*$", -1)),
bracket("(", ")"),
bracket("[", "]"),
bracket("{", "}"),
Expand Down
6 changes: 6 additions & 0 deletions tests/nvim-autopairs_spec.lua
Expand Up @@ -760,6 +760,12 @@ local data = {
after = [[{{("It doesn't name %s", ''), 'ErrorMsg''|' }}, ]],
end_cursor = 41
},
{
name = "80 add normal quote with '",
key = [["]],
before = [[aa| 'aa]],
after = [[aa"|" 'aa]]
},
}

local run_data = _G.Test_filter(data)
Expand Down

0 comments on commit e6adb87

Please sign in to comment.