diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index 2d441a7..5f7bbcd 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -253,7 +253,7 @@ function! s:split_line(line, nth, modes, cycle, fc, lc, pattern, stick_to_left, let delims = [] " Append an empty item to enable right/center alignment of the last token " - if the last token is not ignorable or ignorable but not the only token - elseif (mode == 'r' || mode == 'c') && (!ignorable || len(tokens) > 1) && a:nth >= 0 " includes -0 + elseif (mode ==? 'r' || mode ==? 'c') && (!ignorable || len(tokens) > 1) && a:nth >= 0 " includes -0 call add(tokens, '') call add(delims, '') endif @@ -275,7 +275,7 @@ function! s:do_align(modes, all_tokens, all_delims, fl, ll, fc, lc, pattern, nth let lines = {} let min_indent = -1 let max = { 'pivot_len': 0.0, 'token_len': 0, 'just_len': 0, 'delim_len': 0, - \ 'indent': 0, 'tokens': 0 } + \ 'indent': 0, 'tokens': 0, 'strip_len': 0 } " Phase 1 for line in range(a:fl, a:ll) @@ -308,7 +308,7 @@ function! s:do_align(modes, all_tokens, all_delims, fl, ll, fc, lc, pattern, nth endif let nth = a:nth - 1 " make it 0-based else " -0 or Negative field number - if a:nth == 0 && mode != 'l' + if a:nth == 0 && mode !=? 'l' let nth = len(tokens) - 1 else let nth = len(tokens) + a:nth @@ -334,9 +334,14 @@ function! s:do_align(modes, all_tokens, all_delims, fl, ll, fc, lc, pattern, nth if min_indent < 0 || indent < min_indent let min_indent = indent endif + if mode ==? 'c' | let token .= matchstr(token, '^\s*') | endif let [pw, tw] = [s:strwidth(prefix), s:strwidth(token)] call s:max(max, { 'indent': indent, 'token_len': tw, 'just_len': pw + tw, - \ 'delim_len': s:strwidth(delim), 'pivot_len': pw + tw / 2.0 }) + \ 'delim_len': s:strwidth(delim) }) + if mode ==? 'c' + call s:max(max, { 'pivot_len': pw + tw / 2.0, + \ 'strip_len': s:strwidth(s:trim(token)) }) + endif let lines[line] = [nth, prefix, token, delim] endfor @@ -361,9 +366,14 @@ function! s:do_align(modes, all_tokens, all_delims, fl, ll, fc, lc, pattern, nth let [nth, prefix, token, delim] = elems let token = substitute(token, '^\s*', indent, '') + if mode ==? 'c' + let token = substitute(token, '\s*$', indent, '') + endif let [pw, tw] = [s:strwidth(prefix), s:strwidth(token)] - call s:max(max, - \ { 'token_len': tw, 'just_len': pw + tw, 'pivot_len': pw + tw / 2.0 }) + call s:max(max, { 'token_len': tw, 'just_len': pw + tw }) + if mode ==? 'c' + call s:max(max, { 'pivot_len': pw + tw / 2.0 }) + endif let lines[line][2] = token endfor @@ -384,24 +394,26 @@ function! s:do_align(modes, all_tokens, all_delims, fl, ll, fc, lc, pattern, nth " Pad the token with spaces let [pw, tw] = [s:strwidth(prefix), s:strwidth(token)] let rpad = '' - if mode == 'l' + if mode ==? 'l' let pad = repeat(' ', max.just_len - pw - tw) if a:stick_to_left let rpad = pad else let token = token . pad endif - elseif mode == 'r' + elseif mode ==? 'r' let pad = repeat(' ', max.just_len - pw - tw) let token = pad . token - elseif mode == 'c' + elseif mode ==? 'c' let p1 = max.pivot_len - (pw + tw / 2.0) let p2 = (max.token_len - tw) / 2.0 let pf1 = floor(p1) if pf1 < p1 | let p2 = ceil(p2) else | let p2 = floor(p2) endif + let strip = float2nr(ceil((max.token_len - max.strip_len) / 2.0)) let token = repeat(' ', float2nr(pf1)) .token. repeat(' ', float2nr(p2)) + let token = substitute(token, repeat(' ', strip) . '$', '', '') endif let tokens[nth] = token @@ -433,7 +445,7 @@ function! s:do_align(modes, all_tokens, all_delims, fl, ll, fc, lc, pattern, nth let lpad = '' if nth == 0 let ipad = repeat(' ', min_indent - len(token.ml)) - if mode == 'l' + if mode ==? 'l' let token = ipad . token else let lpad = ipad @@ -664,7 +676,7 @@ function! easy_align#align(bang, expr) range endif let aseq = get(dict, 'mode_sequence', - \ recur == 2 ? (mode == 'r' ? ['r', 'l'] : ['l', 'r']) : [mode]) + \ recur == 2 ? (mode ==? 'r' ? ['r', 'l'] : ['l', 'r']) : [mode]) try call s:do_align( diff --git a/test/basic.expected b/test/basic.expected index 5f6b807..5bdcf11 100644 --- a/test/basic.expected +++ b/test/basic.expected @@ -971,3 +971,73 @@ queue_size | Fixnum | 1000 | size of each queue batch_size | Fixnum | nil | number of maximum items to be assigned at once | logger | Logger | nil | logger instance for debug logs | +``` + aaaaa = 123456778901234567890 = +cccccccccccccccccc =12345678 = + + aaaa = 123456778901234567890 = + cccccccccccccccccc = 12345678 = + + aaaa = 123456778901234567890 = +cccccccccccccccccc = 12345678 = + + aaaa = 123456778901234567890 = + cccccccccccccccccc = 12345678 = + + aaaa = 123456778901234567890 = + cccccccccccccccccc = 12345678 = + + aaaa = 123456778901234567890 = + cccccccccccccccccc = 12345678 = + + aaaaaaaaaaaaa = 123456778901234567890 = + cc = 12345678 = + + aaaaaaaaaaa = 123 + a = 123 + + aaaaaaaaaaaa = 123 + a = 123 + +aaaaaaaaaaaa = 123 +aaaaaaaaaaa = 123 + aaaaaaaaaa = 123 + aaa = 123 + aa = 123 + a = 123 + + aaaaaaaaaaaa = 123 + aaaaaaaaaaa = 123 + aaaaaaaaaa = 123 + aaa = 123 + aa = 123 + a = 123 + + aaaaaaaaaaaa = 123 + aaaaaaaaaaa = 123 + aaaaaaaaaa = 123 + aaa = 123 + aa = 123 + a = 123 + + aaaa = 123456778901234567890 = + cccccccccccccc = 12345678 = + + aaaa = 123456778901234567890 = + bbbbbb = 4 + cccccccccccccccccc = 12345678 = + + aaaa = 123456778901234567890 = + cccccccccccccccccc = 12345678 = + + aaaaa = 123456778901234567890 = + cc = 12345678 = + += aaaaa = 123456778901234567890 = += cccccccccccccccccc = 12345678 = + +=aaaaa = 123456778901234567890 = += cccccccccccccccccc = 12345678 = + +``` + diff --git a/test/basic.md b/test/basic.md index 04a7765..3b47fbf 100644 --- a/test/basic.md +++ b/test/basic.md @@ -215,5 +215,46 @@ bbbbbbbbbb = 123456778901234567890 123456 7890 aaaaa = 123456778901234567890 = + cccccccccccccccccc = 12345678 = + +``` + aaaaa = 123456778901234567890 = +cccccccccccccccccc =12345678 = + + aaaa = 123456778901234567890 = + cccccccccccccccccc = 12345678 = + + aaaaaaaaaaaaa = 123456778901234567890 = +cc = 12345678 = + + aaaaaaaaaaa= 123 + a = 123 + + aaaaaaaaaaaa= 123 + a = 123 + + aaaaaaaaaaaa = 123 + aaaaaaaaaaa = 123 + aaaaaaaaaa = 123 + aaa = 123 + aa = 123 + a = 123 + + aaaa = 123456778901234567890 = +cccccccccccccc = 12345678 = + + aaaa = 123456778901234567890 = + bbbbbb = 4 +cccccccccccccccccc = 12345678 = + + aaaa = 123456778901234567890 = cccccccccccccccccc = 12345678 = + aaaaa = 123456778901234567890 = + cc = 12345678 = + +=aaaaa = 123456778901234567890 = += cccccccccccccccccc = 12345678 = + +``` + diff --git a/test/basic.script b/test/basic.script index b377b76..5385c61 100644 --- a/test/basic.script +++ b/test/basic.script @@ -1 +1 @@ -4Gvipjyvip Pvip 2 Pvip * Pvip Pvip 2 Pvip * Pvip ** Pvip - Pvip -2 Pvip -1 Pvip ** 60zzvipjyvip *|Pvip *|Pvip **|80zzvip **|gv 3|vip *|90zzvip *,100zzvipjyvip =Pvip *=Pvip **=Pvip =vip 2=198Gvipjyvip =Pvip -=Pf:jj3E =209Gvip - 214zzvipjyvip #P:let g:easy_align_delimiters = { '#': {'pattern': '#\+', 'ignore_groups': ['String'] } } vip #227zzvip :239zzvip *=vipjyP:let g:easy_align_ignore_groups = [] vip *=:unlet g:easy_align_delimiters :unlet g:easy_align_ignore_groups 4Gvipy4GP7Gojkkvip:EasyAlign /1/{'ml':'{{', 'mr-r':'}}'} vipjyPvip:EasyAlign */../ 263zzvipjygv .Pvip *.Pvip * .Pvip .vip 2.Pvip **.Pvip **.Pvip -.G303zzvip .310zzvipjygv *|Pvip *|Pvip |gv -|gv **|gv *|gv **|jji jjjhi vip ** |339Gpvip:EasyAlign*|{'ml': 5, 'mrr': 0 } 349Gpvip:EasyAlign*/|/{'ml':'~', 'mrr': 4} 362G:let g:easy_align_delimiters = { 'd': { 'pattern': '\s\+\(\S\+\s*[;=]\)\@=', 'left_margin': 0, 'right_margin': 0 } } vip dgv =236GvipjyPvip :377Gvip gv 2 382Gvipjyvip:EasyAlign/-\+/ Pvip:EasyAlign2/-\+/ Pvip:EasyAlign*/-\+/ Pvip:EasyAlign*/-\+/{'da': L} Pvip:EasyAlign/-\+/{'da': c} Pvip:EasyAlign*/-\+/{'delimiter_align':'C'} Pvip:EasyAlign*/-\+/{'da': 'x'} 381Gpvip:EasyAlign/-\+/{'da':r} :let g:easy_align_delimiter_align = 'l' Pvip:EasyAlign/-\+/ Pvip:EasyAlign*/-\+/ :unlet g:easy_align_delimiter_align :let g:easy_align_delimiters['\'] = { 'pattern': '-\+', 'delimiter_align': 'c' } Pvip \Pvip *-vip *\Pvip:EasyAlign *\\ {'da':l} Pvip:EasyAlign*\\ Pvip:EasyAlign*\\{'da': 'R'} 377GvipjyPvip:EasyAlign\ {'l': '@@@'} gv:EasyAlign 2 \ {'l': '{{{'} 507Gvipjygv:EasyAlign= Pvip:EasyAlign = {'idt': s} Pvip:EasyAlign= {'idt': d} Pvip:EasyAlign**={'indentation': 'd'} Pvip:EasyAlign!= Pvip:EasyAlign! **= {'indent': s} Pvip:EasyAlign!* = {'idt': D} Pvip:EasyAlign! ={'idt':'S'} 507GPvip:EasyAlign-={'idt':d} PA = 2j.j.j.j.vip:EasyAlign2={'idt': s} 507GPljjjj$:EasyAlign = {'idt': s} Pljjjj$:EasyAlign={'indent':d} Phhxxvip:EasyAlign={'idt':'s'} 525Gvipjy507GPPvip:EasyAlign*={'idt':s} Pvip:EasyAlign!**={'idt':s} Pvip:EasyAlign = {'idt': D} 525G^hhr1jr1jr1jr1jr1llkkkk$:EasyAlign = { 'idt': s } 513GvipjyPvip:EasyAlign*={'idt':n} Pvip:EasyAlign = { 'idt': N } Pvip:EasyAlign!*= {'idt': 'n'} 630Gvipjygv =Pvip =656Gvipjygv =Pvip *=Pvip **=Pvip **=Pvip:EasyAlign**= {'iu':0} Pvip =vip 2=vip -0 =818Gvipjygv =Pvip 2=Pvip *=Pvip **=Pvip **=Pvip -0=Pvip -0=855Gvip ** 860Gvip 2=}863Gvip 2=866Gvip 2=869Gvip 2=872Gvip *=875Gvip 2 878Gvip 2 881Gvip:EasyAlign**={'m': 'c','idt':s} ggjjj70GvipjyGpvip *|vipjyGpvip:EasyAlign*|{'m': 'lcr'} Gpvip:EasyAlign**|{'m':'llc'} Gpvip:s/^|// vip:EasyAlign*|{'idt':n} vip:EasyAlign**|{'m':'cl'} 107GvipjyPvip  *=Pvip  *= +4Gvipjyvip Pvip 2 Pvip * Pvip Pvip 2 Pvip * Pvip ** Pvip - Pvip -2 Pvip -1 Pvip ** 60zzvipjyvip *|Pvip *|Pvip **|80zzvip **|gv 3|vip *|90zzvip *,100zzvipjyvip =Pvip *=Pvip **=Pvip =vip 2=198Gvipjyvip =Pvip -=Pf:jj3E =209Gvip - 214zzvipjyvip #P:let g:easy_align_delimiters = { '#': {'pattern': '#\+', 'ignore_groups': ['String'] } } vip #227zzvip :239zzvip *=vipjyP:let g:easy_align_ignore_groups = [] vip *=:unlet g:easy_align_delimiters :unlet g:easy_align_ignore_groups 4Gvipy4GP7Gojkkvip:EasyAlign /1/{'ml':'{{', 'mr-r':'}}'} vipjyPvip:EasyAlign */../ 263zzvipjygv .Pvip *.Pvip * .Pvip .vip 2.Pvip **.Pvip **.Pvip -.G303zzvip .310zzvipjygv *|Pvip *|Pvip |gv -|gv **|gv *|gv **|jji jjjhi vip ** |339Gpvip:EasyAlign*|{'ml': 5, 'mrr': 0 } 349Gpvip:EasyAlign*/|/{'ml':'~', 'mrr': 4} 362G:let g:easy_align_delimiters = { 'd': { 'pattern': '\s\+\(\S\+\s*[;=]\)\@=', 'left_margin': 0, 'right_margin': 0 } } vip dgv =236GvipjyPvip :377Gvip gv 2 382Gvipjyvip:EasyAlign/-\+/ Pvip:EasyAlign2/-\+/ Pvip:EasyAlign*/-\+/ Pvip:EasyAlign*/-\+/{'da': L} Pvip:EasyAlign/-\+/{'da': c} Pvip:EasyAlign*/-\+/{'delimiter_align':'C'} Pvip:EasyAlign*/-\+/{'da': 'x'} 381Gpvip:EasyAlign/-\+/{'da':r} :let g:easy_align_delimiter_align = 'l' Pvip:EasyAlign/-\+/ Pvip:EasyAlign*/-\+/ :unlet g:easy_align_delimiter_align :let g:easy_align_delimiters['\'] = { 'pattern': '-\+', 'delimiter_align': 'c' } Pvip \Pvip *-vip *\Pvip:EasyAlign *\\ {'da':l} Pvip:EasyAlign*\\ Pvip:EasyAlign*\\{'da': 'R'} 377GvipjyPvip:EasyAlign\ {'l': '@@@'} gv:EasyAlign 2 \ {'l': '{{{'} 507Gvipjygv:EasyAlign= Pvip:EasyAlign = {'idt': s} Pvip:EasyAlign= {'idt': d} Pvip:EasyAlign**={'indentation': 'd'} Pvip:EasyAlign!= Pvip:EasyAlign! **= {'indent': s} Pvip:EasyAlign!* = {'idt': D} Pvip:EasyAlign! ={'idt':'S'} 507GPvip:EasyAlign-={'idt':d} PA = 2j.j.j.j.vip:EasyAlign2={'idt': s} 507GPljjjj$:EasyAlign = {'idt': s} Pljjjj$:EasyAlign={'indent':d} Phhxxvip:EasyAlign={'idt':'s'} 525Gvipjy507GPPvip:EasyAlign*={'idt':s} Pvip:EasyAlign!**={'idt':s} Pvip:EasyAlign = {'idt': D} 525G^hhr1jr1jr1jr1jr1llkkkk$:EasyAlign = { 'idt': s } 513GvipjyPvip:EasyAlign*={'idt':n} Pvip:EasyAlign = { 'idt': N } Pvip:EasyAlign!*= {'idt': 'n'} 630Gvipjygv =Pvip =656Gvipjygv =Pvip *=Pvip **=Pvip **=Pvip:EasyAlign**= {'iu':0} Pvip =vip 2=vip -0 =818Gvipjygv =Pvip 2=Pvip *=Pvip **=Pvip **=Pvip -0=Pvip -0=855Gvip ** 860Gvip 2=}863Gvip 2=866Gvip 2=869Gvip 2=872Gvip *=875Gvip 2 878Gvip 2 881Gvip:EasyAlign**={'m': 'c','idt':s} ggjjj70Gvipjy883Gpvip *|vipjy893Gpvip:EasyAlign*|{'m': 'lcr'} 903Gpvip:EasyAlign**|{'m':'llc'} 913Gpvip:s/^|// vip:EasyAlign*|{'idt':n} vip:EasyAlign**|{'m':'cl'} 107GvipjyPvip  *=Pvip  *=975GVj 2=978Gvipjygv =Pvip =Pvip =Pvip =Pvip 2=993Gvip =996Gvip =999Gvip =1002Gvip =vipjyPvip =Pvip =1023Gvip =1026Gvip =1030Gvip =1033Gvip =1036Gvip 3=vipjyPvip 2= diff --git a/test/include.vim b/test/include.vim deleted file mode 100644 index cacb75e..0000000 --- a/test/include.vim +++ /dev/null @@ -1,45 +0,0 @@ -function! GFM() - let syntaxes = { - \ 'ruby': 'syntax/ruby.vim', - \ 'yaml': 'syntax/yaml.vim', - \ 'vim': 'syntax/vim.vim', - \ 'sh': 'syntax/sh.vim', - \ 'python': 'syntax/python.vim', - \ 'java': 'syntax/java.vim', - \ 'c': 'syntax/c.vim' - \ } - - for [lang, syn] in items(syntaxes) - unlet b:current_syntax - silent! exec printf("syntax include @%s %s", lang, syn) - exec printf("syntax region %sSnip matchgroup=Snip start='```%s' end='```' contains=@%s", - \ lang, lang, lang) - endfor - let b:current_syntax='mkd' - - syntax sync fromstart -endfunction - -silent! unlet g:easy_align_delimiters -silent! unlet g:easy_align_ignore_unmatched -silent! unlet g:easy_align_ignores - -vnoremap :EasyAlign - -noremap -noremap -noremap -noremap -vnoremap -vnoremap -vnoremap -vnoremap - -set nolazyredraw -set buftype=nofile -set colorcolumn= - -silent! ScrollPositionHide - -call GFM() - diff --git a/test/run.vim b/test/run.vim index e746629..4222159 100644 --- a/test/run.vim +++ b/test/run.vim @@ -1,5 +1,49 @@ e! -execute 'source '. expand('%:p:h') . '/include.vim' + +function! GFM() + let syntaxes = { + \ 'ruby': 'syntax/ruby.vim', + \ 'yaml': 'syntax/yaml.vim', + \ 'vim': 'syntax/vim.vim', + \ 'sh': 'syntax/sh.vim', + \ 'python': 'syntax/python.vim', + \ 'java': 'syntax/java.vim', + \ 'c': 'syntax/c.vim' + \ } + + for [lang, syn] in items(syntaxes) + unlet b:current_syntax + silent! exec printf("syntax include @%s %s", lang, syn) + exec printf("syntax region %sSnip matchgroup=Snip start='```%s' end='```' contains=@%s", + \ lang, lang, lang) + endfor + let b:current_syntax='mkd' + + syntax sync fromstart +endfunction + +silent! unlet g:easy_align_delimiters +silent! unlet g:easy_align_ignore_unmatched +silent! unlet g:easy_align_ignores + +vnoremap :EasyAlign + +noremap +noremap +noremap +noremap +vnoremap +vnoremap +vnoremap +vnoremap + +set nolazyredraw +set buftype=nofile +set colorcolumn= + +silent! ScrollPositionHide + +call GFM() normal gg let @b=system('cat '. expand('%:p:r') . '.script')