allows custom alignment rules

This commit is contained in:
Junegunn Choi
2013-04-08 20:26:24 +09:00
parent 81f2565da3
commit 0a9656675b
2 changed files with 41 additions and 13 deletions

View File

@@ -52,6 +52,22 @@ Demo
[Screencast](https://vimeo.com/63506219) [Screencast](https://vimeo.com/63506219)
Defining custom alignment rules
-------------------------------
Define (or override) alignment rules.
```vim
let g:lesser_align_delimiters = {
\ '/': {
\ 'pattern': '/',
\ 'margin_left': ' <<<',
\ 'margin_right': '>>> ',
\ 'stick_to_left': 0,
\ }
\ }
```
Author Author
------ ------

View File

@@ -2,8 +2,22 @@ if exists("g:lesser_align_loaded")
finish finish
endif endif
let g:lesser_align_loaded = 1 let g:lesser_align_loaded = 1
let g:lesser_align_delimiters_merged = {
\ ' ': { 'pattern': ' ', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 },
\ '=': { 'pattern': '<=>\|&&=\|||=\|=\~\|=>\|[:+/*!%^=><&|-]\?=',
\ 'margin_left': ' ', 'margin_right': ' ', 'stick_to_left': 0 },
\ ':': { 'pattern': ':', 'margin_left': '', 'margin_right': ' ', 'stick_to_left': 1 },
\ ',': { 'pattern': ',', 'margin_left': '', 'margin_right': ' ', 'stick_to_left': 1 },
\ '|': { 'pattern': '|', 'margin_left': ' ', 'margin_right': ' ', 'stick_to_left': 0 }
\ }
function! s:do_align(fl, ll, pattern, nth, ml, mr, ljust, recursive) if !exists("g:lesser_align_delimiters")
let g:lesser_align_delimiters = {}
endif
call extend(g:lesser_align_delimiters_merged, g:lesser_align_delimiters)
function! s:do_align(fl, ll, pattern, nth, ml, mr, stick_to_left, recursive)
let lines = {} let lines = {}
let just_len = 0 let just_len = 0
let max_delim_len = 0 let max_delim_len = 0
@@ -41,7 +55,7 @@ function! s:do_align(fl, ll, pattern, nth, ml, mr, ljust, recursive)
let pad = just_len - len(prefix) let pad = just_len - len(prefix)
if pad > 0 if pad > 0
for i in range(pad) for i in range(pad)
if a:ljust if a:stick_to_left
let suffix = ' '. suffix let suffix = ' '. suffix
else else
let prefix = prefix . ' ' let prefix = prefix . ' '
@@ -60,7 +74,7 @@ function! s:do_align(fl, ll, pattern, nth, ml, mr, ljust, recursive)
endfor endfor
if a:recursive && a:nth < max_tokens if a:recursive && a:nth < max_tokens
call s:do_align(a:fl, a:ll, a:pattern, a:nth + 1, a:ml, a:mr, a:ljust, a:recursive) call s:do_align(a:fl, a:ll, a:pattern, a:nth + 1, a:ml, a:mr, a:stick_to_left, a:recursive)
endif endif
endfunction endfunction
@@ -96,16 +110,14 @@ function! lesser_align#align() range
let n = empty(n) ? 1 : n let n = empty(n) ? 1 : n
let error = 0 let error = 0
if ch == ' ' if has_key(g:lesser_align_delimiters_merged, ch)
call s:do_align(a:firstline, a:lastline, ' ', n, '', '', 0, recursive) let dict = g:lesser_align_delimiters_merged[ch]
elseif ch == '=' call s:do_align(a:firstline, a:lastline,
call s:do_align(a:firstline, a:lastline, '<=>\|&&=\|||=\|=\~\|=>\|[:+/*!%^=-]\?=', n, ' ', ' ', 0, recursive) \ get(dict, 'pattern', ch),
elseif ch == ':' \ n,
call s:do_align(a:firstline, a:lastline, ':', n, '', ' ', 1, recursive) \ get(dict, 'margin_left', ' '),
elseif ch == ',' \ get(dict, 'margin_right', ' '),
call s:do_align(a:firstline, a:lastline, ',', n, '', ' ', 1, recursive) \ get(dict, 'stick_to_left', 0), recursive)
elseif ch == '|'
call s:do_align(a:firstline, a:lastline, '|', n, ' ', ' ', 0, recursive)
else else
let error = 1 let error = 1
endif endif