From bbe9b034f07d345f9fea4c51b84dd1fba1c78851 Mon Sep 17 00:00:00 2001 From: Kim K Date: Sun, 17 Feb 2019 12:53:01 +0100 Subject: [PATCH] feat: Implement equal pair detection --- README.md | 29 +++++++++++++++++++++++++++++ doc/AutoPairs.txt | 38 ++++++++++++++++++++++++++++++++++++++ plugin/auto-pairs.vim | 29 +++++++++++++++++++++++++++-- 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d467cf9..288116c 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,35 @@ Features }| +* Auto detect when to close by checking the equality of pairs within the buffer: > + + input: + const user = getUser|id) + + (press ( at |) + + output: + const user = getUser(id) + + --- + + input: + |]] + + (press [ at |) + + output: + [|]] + + (press [ at | again) + + output: + [[|]] + + (press [ at | again) + + output: + [[[|]]] * Fly Mode input: if(a[3) diff --git a/doc/AutoPairs.txt b/doc/AutoPairs.txt index afe589e..4607797 100644 --- a/doc/AutoPairs.txt +++ b/doc/AutoPairs.txt @@ -103,6 +103,35 @@ Support ```, ''' and """: > output: '''|''' +Auto detect when to close by checking the equality of pairs within the buffer: > + input: + const user = getUser|id) + + (press ( at |) + + output: + const user = getUser(id) + + --- + + input: + |]] + + (press [ at |) + + output: + [|]] + + (press [ at | again) + + output: + [[|]] + + (press [ at | again) + + output: + [[[|]]] + Delete Repeated Pairs in one time: > input: """|""" (press at |) @@ -247,6 +276,15 @@ Default: Jump to the next closed pair. + *g:AutoPairsEqualPairDetection* +|g:AutoPairsEqualPairDetection| int + +Default: 1 + +Detects when to close a pair by detecting the equality of pairs within the +current buffer. (This feature is not supported for multibyte pairs) + + *g:AutoPairsShortcutBackInsert* |g:AutoPairsShortcutBackInsert| string diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index e630454..d58022b 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -79,6 +79,10 @@ if !exists('g:AutoPairsShortcutJump') let g:AutoPairsShortcutJump = '' endif +if !exists('g:AutoPairsEqualPairDetection') + let g:AutoPairsEqualPairDetection = 1 +endif + " Fly mode will for closed pair to jump to closed pair instead of insert. " also support AutoPairsBackInsert to insert pairs where jumped. if !exists('g:AutoPairsFlyMode') @@ -195,6 +199,20 @@ func! AutoPairsDefine(pairs, ...) return r endf +func! s:count(word) + " Because of the substitution the cursor is moved when there are matches and + " thus we have to preserve the cursor position. + let cpos = [line('.'), col('.')] + try + let result = execute('%s/\V' . a:word . '//gn') + call cursor(cpos) + let occurences = trim(strpart(result, 0, stridx(result, " "))) + return occurences + catch + return 0 + endtry +endfunction + func! AutoPairsInsert(key) if !b:autopairs_enabled return a:key @@ -246,7 +264,7 @@ func! AutoPairsInsert(key) end endfor if !found - " delete charactor + " delete character let ms = s:matchend(before, '\v.') if len(ms) let before = ms[1] @@ -254,7 +272,14 @@ func! AutoPairsInsert(key) end end endwhile - return bs.del.openPair.close.s:left(close) + + if strlen(open) == 1 && g:AutoPairsEqualPairDetection == 1 + if s:count(open) == s:count(close) + return bs.del.openPair.close.s:left(close) + endif + else + return bs.del.openPair.close.s:left(close) + endif end endfor