Skip to content

Commit

Permalink
Add option to use Parens Decorators
Browse files Browse the repository at this point in the history
Parens decorators are symbols that can be used inside parens and create
a custom compound open-close sequence.
It is useful for some templating languages such as [Jinja](http://jinja.pocoo.org/)/[Twig](twig.sensiolabs.org)/[Liquid](https://github.com/Shopify/liquid)
and must be opted in with a config option.

Closes jiangmiao#144
  • Loading branch information
Kasama committed Sep 9, 2018
1 parent f0019fc commit f07cffd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ Features
input: '|' (press <SPACE> at |)
output: ' |'

* Insert Parens decorators for [], (), {}. Useful for templating languages such as [Jinja](http://jinja.pocoo.org/)/[Twig](twig.sensiolabs.org)/[Liquid](https://github.com/Shopify/liquid)

input: {|} (press % at |)
output {%|%}

input: {|} (press #<SPACE>foo at |)
output {# foo| #}

* Skip ' when inside a word

input: foo| (press ' at |)
Expand Down Expand Up @@ -183,6 +191,30 @@ Options

Buffer level pairs set.

* g:AutoPairsEnableParensDecorators

Default: 0

Enables parens decorators, such as %, #, =.

* g:AutoPairsParens

Default: {'(':')', '[':']', '{':'}'}

Which pairs should be considered paren.

* g:AutoPairsParensDecorators

Default: {'%':'%', '#':'#', '=':'='}

Parens decorator value pairs.

* b:AutoPairsParensDecorators

Default: g:AutoPairsParensDecorators

Buffer level parens decorator set.

* g:AutoPairsShortcutToggle

Default: '<M-p>'
Expand Down
42 changes: 35 additions & 7 deletions plugin/auto-pairs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ if !exists('g:AutoPairsParens')
let g:AutoPairsParens = {'(':')', '[':']', '{':'}'}
end

if !exists('g:AutoPairsEnableParensDecorators')
let g:AutoPairsEnableParensDecorators = 0
end

if !exists('g:AutoPairsParensDecorators')
let g:AutoPairsParensDecorators = {'%':'%', '#':'#', '=':'='}
end

if !exists('g:AutoPairsMapBS')
let g:AutoPairsMapBS = 1
end
Expand Down Expand Up @@ -245,15 +253,19 @@ function! AutoPairsDelete()
endif

" Delete Repeated Pair eg: '''|''' [[|]] {{|}}
if has_key(b:AutoPairs, prev_char)
if (has_key(b:AutoPairs, prev_char) || has_key(b:AutoPairsParensDecorators, prev_char))
let times = 0
let p = -1
while get(prev_chars, p, '') == prev_char
let p = p - 1
let times = times + 1
endwhile

let close = b:AutoPairs[prev_char]
if has_key(b:AutoPairs, prev_char)
let close = b:AutoPairs[prev_char]
else
let close = b:AutoPairsParensDecorators[prev_char]
endif
let left = repeat(prev_char, times)
let right = repeat(close, times)

Expand Down Expand Up @@ -413,15 +425,20 @@ function! AutoPairsReturn()
return ''
endfunction

function! AutoPairsSpace()
function! AutoPairsDecorator(decorator, closing)
let line = getline('.')
let prev_char = line[col('.')-2]
let cmd = ''
let cur_char =line[col('.')-1]
if has_key(g:AutoPairsParens, prev_char) && g:AutoPairsParens[prev_char] == cur_char
let cmd = "\<SPACE>".s:Left
let cur_char = line[col('.')-1]
if ((has_key(g:AutoPairsParens, prev_char) && g:AutoPairsParens[prev_char] == cur_char)
\|| (has_key(b:AutoPairsParensDecorators, prev_char) && b:AutoPairsParensDecorators[prev_char] == cur_char))
let cmd = a:closing.s:Left
endif
return "\<SPACE>".cmd
return a:decorator.cmd
endfunction

function! AutoPairsSpace()
return AutoPairsDecorator("\<SPACE>", "\<SPACE>")
endfunction

function! AutoPairsBackInsert()
Expand All @@ -445,6 +462,10 @@ function! AutoPairsInit()
let b:AutoPairs = g:AutoPairs
end

if !exists('b:AutoPairsParensDecorators')
let b:AutoPairsParensDecorators = g:AutoPairsParensDecorators
end

if !exists('b:AutoPairsMoveCharacter')
let b:AutoPairsMoveCharacter = g:AutoPairsMoveCharacter
end
Expand Down Expand Up @@ -482,6 +503,13 @@ function! AutoPairsInit()
execute 'inoremap <buffer> <silent> <SPACE> '.do_abbrev.'<C-R>=AutoPairsSpace()<CR>'
end

if g:AutoPairsEnableParensDecorators
let decorators = items(b:AutoPairsParensDecorators)
for decorator in decorators
execute 'inoremap <buffer> <silent> '.decorator[0]." <C-R>=AutoPairsDecorator('".decorator[0]."', '".decorator[1]."')<CR>"
endfor
end

if g:AutoPairsShortcutFastWrap != ''
execute 'inoremap <buffer> <silent> '.g:AutoPairsShortcutFastWrap.' <C-R>=AutoPairsFastWrap()<CR>'
end
Expand Down

0 comments on commit f07cffd

Please sign in to comment.