diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index 7fdd30d..8c69812 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -701,14 +701,16 @@ function! s:test_regexp(regexp) return a:regexp endfunction -function! s:parse_shortcut_opts(expr) +let s:shorthand_regex = + \ '\s*\(' + \ .'\(lm\?[0-9]\+\)\|\(rm\?[0-9]\+\)\|\(iu[01]\)\|\(s\%(tl\)\?[01]\)\|' + \ .'\(da\?[clr]\)\|\(ms\?[lrc*]\+\)\|\(i\%(dt\)\?[kdsn]\)\|\(ig\[.*\]\)' + \ .'\)\+\s*$' + +function! s:parse_shorthand_opts(expr) let opts = {} let expr = substitute(a:expr, '\s', '', 'g') - let regex = - \ '^\(' - \ .'\(l[0-9]\+\)\|\(r[0-9]\+\)\|\(iu[01]\)\|\(s[01]\)\|' - \ .'\(d[clr]\)\|\(m[lrc*]\+\)\|\(i[kdsn]\)\|\(ig\[.*\]\)' - \ .'\)\+$' + let regex = '^'. s:shorthand_regex if empty(expr) return opts @@ -718,29 +720,30 @@ function! s:parse_shortcut_opts(expr) let match = matchlist(expr, regex) if empty(match) | break | endif for m in filter(match[ 2 : -1 ], '!empty(v:val)') - let k = tolower(m[0]) - let kk = tolower(m[0 : 1]) - let rest = m[1 : -1] - if index(['l', 'r', 's'], k) >= 0 - let opts[k] = str2nr(rest) - elseif kk == 'iu' - let opts['iu'] = str2nr(m[2 : -1]) - elseif kk == 'ig' - try - let arr = eval(m[2 : -1]) - if type(arr) == 3 - let opts['ig'] = arr + for key in ['lm', 'rm', 'l', 'r', 'stl', 's', 'iu', 'da', 'd', 'ms', 'm', 'ig', 'i'] + if stridx(tolower(m), key) == 0 + let rest = strpart(m, len(key)) + if key == 'i' | let key = 'idt' | endif + + if key == 'idt' || index(['d', 'm'], key[0]) >= 0 + let opts[key] = rest + elseif key == 'ig' + try + let arr = eval(rest) + if type(arr) == 3 + let opts[key] = arr + else + throw 'Not an array' + endif + catch + call s:exit("Invalid ignore_groups: ". a:expr) + endtry else - throw 'Not an array' + let opts[key] = str2nr(rest) endif - catch - call s:exit("Invalid ignore_groups: ". a:expr) - endtry - elseif k == 'i' - let opts['idt'] = rest - else - let opts[k] = rest - endif + break + endif + endfor endfor endif return s:normalize_options(opts) @@ -785,14 +788,18 @@ function! s:parse_args(args) let opts = s:normalize_options(opts) endif + " Shorthand option notation + let sopts = matchstr(args, s:shorthand_regex) + if !empty(sopts) + let args = strpart(args, 0, len(args) - len(sopts)) + let opts = extend(s:parse_shorthand_opts(sopts), opts) + endif + " Has /Regexp/? - let matches = matchlist(args, '^\(.\{-}\)\s*/\(.*\)/\(.\{-}\)$') + let matches = matchlist(args, '^\(.\{-}\)\s*/\(.*\)/\s*$') " Found regexp if !empty(matches) - if !empty(matches[3]) - let opts = extend(s:parse_shortcut_opts(matches[3]), opts) - endif return [matches[1], s:test_regexp(matches[2]), opts, 1] else let tokens = matchlist(args, '^\([1-9][0-9]*\|-[0-9]*\|\*\*\?\)\?\s*\(.\{-}\)\?$') diff --git a/test/regexp.expected b/test/regexp.expected index 312b731..0ceea49 100644 --- a/test/regexp.expected +++ b/test/regexp.expected @@ -106,3 +106,23 @@ data ;;exchange:;format apple ;: banana :: cake data ;; exchange :; format +a+= 1 +bb==# 2 +ccc= 3 +dddd=> 4 + +apple;: banana:: cake + data;; exchange:; format + +```ruby +apple = 1 # comment not aligned +apricot = 'DAD' + 'F#AD' +banana = 'Gros Michel' # comment 2 +``` + +```ruby +apple = 1 # comment not aligned +apricot = 'DAD' + 'F#AD' +banana = 'Gros Michel' # comment 2 +``` + diff --git a/test/regexp.script b/test/regexp.script index 6730fbe..159c0bd 100644 --- a/test/regexp.script +++ b/test/regexp.script @@ -1 +1 @@ -4Gvipjyvip:EasyAlign: Pvip:EasyAlign*: Pvip:EasyAlign-: Pvip:EasyAlign * : Pvip:EasyAlign -2 : Pvip:EasyAlign -2: Pvip:EasyAlign *: Pvip:EasyAlign; Pvip:EasyAlign /;/ Pvip:EasyAlign/;/ Pvip:EasyAlign*/;/ Pvip:EasyAlign/[:;]\+/ Pvip:EasyAlign*/[:;]\+/ Pvip:EasyAlign* /[:;]\+/ Pvip:EasyAlign */[:;]\+/ Pvip:EasyAlign * /[:;]\+/ Pvip:EasyAlign-/[:;]\+/ Pvip:EasyAlign-2/[:;]\+/ Pvip:EasyAlign/[:;]\+/{ 'l': '<<<', 'r': '>>>'} Pvip:EasyAlign/[:;]\+/{'l':'', 's': 1} Pvip:EasyAlign * /[:;]\+/ {'stick_to': 1, 'le': '???', 'marr': '~~~'} Pvip:EasyAlign /../ Pvip:EasyAlign*/../ Pvip:EasyAlign **/[:;\+]/ Pvip:EasyAlign/[:;]\+/{'rm':'____'} Pvip:EasyAlign**/[:;]\+/ Pvip:EasyAlign!**/[:;]\+/ Pfbjfm:EasyAlign/[:;]\+/ 90Gvip:EasyAlign/#/{'is':['String'],'iu':0} 93Gpvip:EasyAlign*/\s*/{'l':'(', 'r': ')'} 96Gpvip:EasyAlign*/[:;]\+/r3l3s1 Pvip:EasyAlign*/[:;]\+/r0l0s0 Pvip:EasyAlign*/[:;]\+/mrcr Pvip:EasyAlign*/[:;]\+/mrcrr0l2 +4Gvipjyvip:EasyAlign: Pvip:EasyAlign*: Pvip:EasyAlign-: Pvip:EasyAlign * : Pvip:EasyAlign -2 : Pvip:EasyAlign -2: Pvip:EasyAlign *: Pvip:EasyAlign; Pvip:EasyAlign /;/ Pvip:EasyAlign/;/ Pvip:EasyAlign*/;/ Pvip:EasyAlign/[:;]\+/ Pvip:EasyAlign*/[:;]\+/ Pvip:EasyAlign* /[:;]\+/ Pvip:EasyAlign */[:;]\+/ Pvip:EasyAlign * /[:;]\+/ Pvip:EasyAlign-/[:;]\+/ Pvip:EasyAlign-2/[:;]\+/ Pvip:EasyAlign/[:;]\+/{ 'l': '<<<', 'r': '>>>'} Pvip:EasyAlign/[:;]\+/{'l':'', 's': 1} Pvip:EasyAlign * /[:;]\+/ {'stick_to': 1, 'le': '???', 'marr': '~~~'} Pvip:EasyAlign /../ Pvip:EasyAlign*/../ Pvip:EasyAlign **/[:;\+]/ Pvip:EasyAlign/[:;]\+/{'rm':'____'} Pvip:EasyAlign**/[:;]\+/ Pvip:EasyAlign!**/[:;]\+/ Pfbjfm:EasyAlign/[:;]\+/ 90Gvip:EasyAlign/#/{'is':['String'],'iu':0} 93Gpvip:EasyAlign*/\s*/{'l':'(', 'r': ')'} 96Gpvip:EasyAlign*/[:;]\+/r3l3s1 Pvip:EasyAlign*/[:;]\+/r0l0s0 Pvip:EasyAlign*/[:;]\+/mrcr Pvip:EasyAlign*/[:;]\+/mrcrr0l2 108Goa+=1 bb==#2 ccc=3 dddd=>4vip:EasyAlign=dll0s1r5id 112Gopvip:EasyAlign/[:;]\+/msrrc**lm0rm3stl1dacidtd 88Gvipjy116Gpvip:EasyAlign3\ ig['String'] Pvip:EasyAlign3\ ig['string']