on master help sign in
Use replace_endpair when expanding with endwise
commit0ebfef16ec942fe86535c3c4c40e2d998b33b897 authorYarden Sod-Moriah <yardnsm@gmail.com> dateWed 20 Apr 2022, 11:55 (4 years ago) parente9b47f0
2 files changed+54−12
lua/nvim-autopairs.lua+18−12 tests/nvim-autopairs_spec.lua+36
lua/nvim-autopairs.lua+18−12
@@ -553,26 +553,32 @@ M.autopairs_cr = function(bufnr)
553553 #rule.end_pair,
554554 rule.is_regex
555555 )
556+
557+ local cond_opt = {
558+ ts_node = M.state.ts_node,
559+ check_endwise_ts = true,
560+ rule = rule,
561+ bufnr = bufnr,
562+ col = col + 1,
563+ line = line,
564+ prev_char = prev_char,
565+ next_char = next_char,
566+ }
567+
568+ local end_pair = rule:get_end_pair(cond_opt)
569+ local end_pair_length = rule:get_end_pair_length(end_pair)
570+
556571 -- log.debug('prev_char' .. rule.start_pair)
557572 -- log.debug('prev_char' .. prev_char)
558573 -- log.debug('next_char' .. next_char)
559574 if
560575 rule.is_endwise
561576 and utils.compare(rule.start_pair, prev_char, rule.is_regex)
562− and rule:can_cr({
563− ts_node = M.state.ts_node,
564− check_endwise_ts = true,
565− bufnr = bufnr,
566− rule = rule,
567− col = col,
568− prev_char = prev_char,
569− next_char = next_char,
570− line = line,
571− })
577+ and rule:can_cr(cond_opt)
572578 then
573579 return utils.esc(
574− rule.end_pair
575− .. utils.repeat_key(utils.key.join_left, #rule.end_pair)
580+ end_pair
581+ .. utils.repeat_key(utils.key.join_left, end_pair_length)
576582 -- FIXME do i need to re indent twice #118
577583 .. '<cr><esc>====O'
578584 )
tests/nvim-autopairs_spec.lua+36
@@ -596,6 +596,42 @@ local data = {
596596 before = [[ | aaa]],
597597 after = [[ "ab| aaa]]
598598 },
599+ {
600+ setup_func = function()
601+ npairs.clear_rules()
602+ npairs.add_rule(Rule("{", "}"):end_wise())
603+ end,
604+ filetype = 'javascript',
605+ name = 'custom endwise rule',
606+ key = [[<cr>]],
607+ before = [[function () {| ]],
608+ after = {
609+ [[function () {]],
610+ [[|]],
611+ [[}]],
612+ },
613+ },
614+ {
615+ setup_func = function()
616+ npairs.clear_rules()
617+ npairs.add_rule(
618+ Rule("{", "")
619+ :replace_endpair(function ()
620+ return "}"
621+ end)
622+ :end_wise()
623+ )
624+ end,
625+ filetype = 'javascript',
626+ name = 'custom endwise rule with custom end_pair',
627+ key = [[<cr>]],
628+ before = [[function () {| ]],
629+ after = {
630+ [[function () {]],
631+ [[|]],
632+ [[}]],
633+ },
634+ },
599635}
600636
601637local run_data = _G.Test_filter(data)