mirror of
https://github.com/junegunn/vim-easy-align.git
synced 2025-11-10 02:43:49 -05:00
allows custom alignment rules
This commit is contained in:
16
README.md
16
README.md
@@ -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
|
||||||
------
|
------
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user