From a16989a5453e0a0a8250deab340f429353876da4 Mon Sep 17 00:00:00 2001 From: Riley Bruins Date: Thu, 13 Jul 2023 04:25:11 -0700 Subject: [PATCH] fix: prevent conds lambda from caching the first length parameter (#374) * fix: prevent returned conds lambda from caching --- lua/nvim-autopairs/conds.lua | 32 ++++++++---------------------- lua/nvim-autopairs/rules/basic.lua | 8 ++++---- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/lua/nvim-autopairs/conds.lua b/lua/nvim-autopairs/conds.lua index 429a7fce..90af2b08 100644 --- a/lua/nvim-autopairs/conds.lua +++ b/lua/nvim-autopairs/conds.lua @@ -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 @@ -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 @@ -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 diff --git a/lua/nvim-autopairs/rules/basic.lua b/lua/nvim-autopairs/rules/basic.lua index ff993210..e6361d35 100644 --- a/lua/nvim-autopairs/rules/basic.lua +++ b/lua/nvim-autopairs/rules/basic.lua @@ -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 @@ -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("{", "}"),