mirror of
https://github.com/junegunn/vim-easy-align.git
synced 2025-11-10 02:43:49 -05:00
132 lines
3.8 KiB
VimL
132 lines
3.8 KiB
VimL
if exists("g:lesser_align_loaded")
|
|
finish
|
|
endif
|
|
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 }
|
|
\ }
|
|
|
|
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 just_len = 0
|
|
let max_delim_len = 0
|
|
let max_tokens = 0
|
|
let pattern = '\s*\(' .a:pattern. '\)\s*'
|
|
for line in range(a:fl, a:ll)
|
|
let tokens = split(getline(line), pattern.'\zs')
|
|
if empty(tokens)
|
|
continue
|
|
endif
|
|
|
|
let max_tokens = len(tokens) > max_tokens ? len(tokens) : max_tokens
|
|
let nth = match(tokens[0], '^\s*$') != -1 ? a:nth + 1 : a:nth
|
|
|
|
if len(tokens) < nth
|
|
continue
|
|
endif
|
|
|
|
let last = tokens[nth - 1]
|
|
let before = (nth > 1 ? join(tokens[0 : nth - 2], '') : '') . substitute(last, pattern.'$', '', '')
|
|
let after = join(tokens[nth : -1], '')
|
|
|
|
if match(last, pattern.'$') == -1
|
|
continue
|
|
endif
|
|
|
|
let delim = matchlist(tokens[nth - 1], pattern)[1]
|
|
let just_len = len(before) > just_len ? len(before) : just_len
|
|
let max_delim_len = len(delim) > max_delim_len ? len(delim) : max_delim_len
|
|
let lines[line] = [before, after, delim]
|
|
endfor
|
|
|
|
for [line, tokens] in items(lines)
|
|
let [prefix, suffix, delim] = tokens
|
|
let pad = just_len - len(prefix)
|
|
if pad > 0
|
|
for i in range(pad)
|
|
if a:stick_to_left
|
|
let suffix = ' '. suffix
|
|
else
|
|
let prefix = prefix . ' '
|
|
endif
|
|
endfor
|
|
endif
|
|
let pad = max_delim_len - len(delim)
|
|
if pad > 0
|
|
for i in range(pad)
|
|
let delim = ' '. delim
|
|
endfor
|
|
endif
|
|
let ml = empty(prefix) ? '' : a:ml
|
|
let mr = empty(suffix) ? '' : a:mr
|
|
call setline(line, substitute(join([prefix, ml, delim, mr, suffix], ''), '\s*$', '', ''))
|
|
endfor
|
|
|
|
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:stick_to_left, a:recursive)
|
|
endif
|
|
endfunction
|
|
|
|
function! lesser_align#align() range
|
|
echon "\rlesser-align ()"
|
|
let n = ''
|
|
let recursive = 0
|
|
|
|
while 1
|
|
let c = getchar()
|
|
let ch = nr2char(c)
|
|
if c == 3 || c == 27
|
|
return
|
|
elseif c >= 48 && c <= 57
|
|
if recursive
|
|
echo "Number(*) already specified"
|
|
return
|
|
endif
|
|
let n = n . nr2char(c)
|
|
echon "\rlesser-align (". n .")"
|
|
elseif ch == '*'
|
|
if !empty(n)
|
|
echo "Number already specified"
|
|
return
|
|
endif
|
|
let recursive = 1
|
|
echon "\rlesser-align (*)"
|
|
else
|
|
break
|
|
endif
|
|
endwhile
|
|
|
|
let n = empty(n) ? 1 : n
|
|
|
|
let error = 0
|
|
if has_key(g:lesser_align_delimiters_merged, ch)
|
|
let dict = g:lesser_align_delimiters_merged[ch]
|
|
call s:do_align(a:firstline, a:lastline,
|
|
\ get(dict, 'pattern', ch),
|
|
\ n,
|
|
\ get(dict, 'margin_left', ' '),
|
|
\ get(dict, 'margin_right', ' '),
|
|
\ get(dict, 'stick_to_left', 0), recursive)
|
|
else
|
|
let error = 1
|
|
endif
|
|
|
|
if error
|
|
echon "\rUnknown delimiter: ". ch
|
|
else
|
|
echon "\rlesser-align (". (recursive ? '*' : n) . ch .")"
|
|
endif
|
|
endfunction
|
|
|