Skip to content

Commit

Permalink
fix: prevent conds lambda from caching the first length parameter (#374)
Browse files Browse the repository at this point in the history
* fix: prevent returned conds lambda from caching
  • Loading branch information
ribru17 committed Jul 13, 2023
1 parent e8f7dd7 commit a16989a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
32 changes: 8 additions & 24 deletions lua/nvim-autopairs/conds.lua
Expand Up @@ -37,18 +37,14 @@ cond.invert = function(func)
end
end

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

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

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

cond.not_after_regex = function(regex, length)
length = length or 1
cond.not_after_regex = function(regex)
if not regex then
return cond.none()
end
---@param opts CondOpts
return function(opts)
log.debug('not_after_regex')
if length < 0 then
length = #opts.line
end
local str = utils.text_sub_char(opts.line, opts.col, length)
local str = utils.text_sub_char(opts.line, opts.col, #opts.line)
if str:match(regex) then
return false
end
Expand Down
8 changes: 4 additions & 4 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 Down Expand Up @@ -41,11 +41,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*$", -1)),
quote('"', '"', "vim"):with_pair(cond.not_before_regex("^%s*$")),
bracket("(", ")"),
bracket("[", "]"),
bracket("{", "}"),
Expand Down

0 comments on commit a16989a

Please sign in to comment.