From 0fb997d5aba5afa15809c84c08b4a63bf886c7a0 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 16 Aug 2013 01:38:50 +0900 Subject: [PATCH] Add g:easy_align_indentation. Add `n` indentation option. --- README.md | 27 ++++++++++++++++++++------- autoload/easy_align.vim | 8 +++++--- doc/easy_align.txt | 23 ++++++++++++++++------- test/basic.expected | 18 ++++++++++++++++++ test/basic.script | 2 +- 5 files changed, 60 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index bf99815..3b36f36 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,9 @@ Alignment rules for the following delimiters have been defined to meet the most | `,` | Multi-line method arguments | | | | Table markdown | +You can override these default rules or define your own rules with +`g:easy_align_delimiters`, which will be described in the later section. + #### Example command sequences | With visual map | Description | Equivalent command | @@ -196,7 +199,7 @@ lowest precedence. | `stick_to_left` | boolean | 0 | Whether to position delimiter on the left-side | | `ignore_unmatched` | boolean | 1 | Whether to ignore lines without matching delimiter | | `ignores` | list | ['String', 'Comment'] | Delimiters in these syntax highlight groups are ignored | -| `indentation` | string | `k` | Indentation method (*k*eep, *d*eep, *s*hallow) | +| `indentation` | string | `k` | Indentation method (*k*eep, *d*eep, *s*hallow, *n*one) | | `delimiter_align` | string | `r` | Determines how to align delimiters of different lengths | Some of the options can be specified using corresponding global variables. @@ -206,6 +209,7 @@ Some of the options can be specified using corresponding global variables. | `ignore_unmatched` | `g:easy_align_ignore_unmatched` | | `ignores` | `g:easy_align_ignores` | | `delimiter_align` | `g:easy_align_delimiter_align` | +| `indentation` | `g:easy_align_indentation` | ### Ignoring delimiters in comments or strings @@ -359,35 +363,44 @@ By default :EasyAlign command keeps the original indentation of the lines. But then again we have `indentation` option. See the following example. ```ruby +# Lines with different indentation apple = 1 banana = 2 cake = 3 daisy = 4 eggplant = 5 - # Default: _k_eep the original indentation - # :EasyAlign= +# Default: _k_eep the original indentation +# :EasyAlign= apple = 1 banana = 2 cake = 3 daisy = 4 eggplant = 5 - # Use the _s_hallowest indentation among the lines - # :EasyAlign={'idt':s} +# Use the _s_hallowest indentation among the lines +# :EasyAlign={'idt':s} apple = 1 banana = 2 cake = 3 daisy = 4 eggplant = 5 - # Use the _d_eepest indentation among the lines - # :EasyAlign={'idt':d} +# Use the _d_eepest indentation among the lines +# :EasyAlign={'idt':d} apple = 1 banana = 2 cake = 3 daisy = 4 eggplant = 5 + + # Indentation: _n_one + # :EasyAlign={'idt':n} +apple = 1 +banana = 2 +cake = 3 +daisy = 4 +eggplant = 5 ``` Notice that `idt` is fuzzy-matched to `indentation`. diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index 76b3372..4de8f5d 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -271,6 +271,8 @@ function! s:do_align(just, all_tokens, all_delims, fl, ll, fc, lc, pattern, nth, let indent = repeat(' ', max_indent) elseif a:indentation ==? 's' let indent = repeat(' ', min_indent) + elseif a:indentation ==? 'n' + let indent = '' elseif a:indentation !=? 'k' call s:exit('Invalid indentation: ' . a:indentation) end @@ -420,8 +422,8 @@ function! s:parse_args(args) let cand = strpart(args, midx) try - let [l, r, c, k, s, d] = ['l', 'r', 'c', 'k', 's', 'd'] - let [L, R, C, K, S, D] = ['l', 'r', 'c', 'k', 's', 'd'] + let [l, r, c, k, s, d, n] = ['l', 'r', 'c', 'k', 's', 'd', 'n'] + let [L, R, C, K, S, D, N] = ['l', 'r', 'c', 'k', 's', 'd', 'n'] let o = eval(cand) if type(o) == 4 let option = o @@ -546,7 +548,7 @@ function! easy_align#align(just, expr) range \ ml, \ mr, \ get(dict, 'delimiter_align', get(g:, 'easy_align_delimiter_align', 'r')), - \ get(dict, 'indentation', 'k'), + \ get(dict, 'indentation', get(g:, 'easy_align_indentation', 'k')), \ get(dict, 'stick_to_left', 0), \ get(dict, 'ignore_unmatched', get(g:, 'easy_align_ignore_unmatched', 1)), \ get(dict, 'ignores', s:ignored_syntax()), diff --git a/doc/easy_align.txt b/doc/easy_align.txt index 76652f8..b40fa67 100644 --- a/doc/easy_align.txt +++ b/doc/easy_align.txt @@ -269,42 +269,51 @@ And on ':EasyAlign={'da':c}', center-aligned. cake ||= banana -Adjusting indentation +Adjusting indentation *g:easy_align_indentation* ------------------------------------------------------------------------- By default :EasyAlign command keeps the original indentation of the lines. But then again we have 'indentation' option. See the following example. + # Lines with different indentation apple = 1 banana = 2 cake = 3 daisy = 4 eggplant = 5 - # Default: _k_eep the original indentation - # :EasyAlign= + # Default: _k_eep the original indentation + # :EasyAlign= apple = 1 banana = 2 cake = 3 daisy = 4 eggplant = 5 - # Use the _s_hallowest indentation among the lines - # :EasyAlign={'idt':s} + # Use the _s_hallowest indentation among the lines + # :EasyAlign={'idt':s} apple = 1 banana = 2 cake = 3 daisy = 4 eggplant = 5 - # Use the _d_eepest indentation among the lines - # :EasyAlign={'idt':d} + # Use the _d_eepest indentation among the lines + # :EasyAlign={'idt':d} apple = 1 banana = 2 cake = 3 daisy = 4 eggplant = 5 + # Indentation: _n_one + # :EasyAlign={'idt':n} + apple = 1 + banana = 2 + cake = 3 + daisy = 4 + eggplant = 5 + Notice that 'idt' is fuzzy-matched to 'indentation'. diff --git a/test/basic.expected b/test/basic.expected index 23fdc74..d407b60 100644 --- a/test/basic.expected +++ b/test/basic.expected @@ -510,6 +510,24 @@ George Harrison 1943 daisy = 4 = 2 eggplant = 5 = 2 + apple = 1 = 2 + banana = 2 = 2 + cake = 3 = 2 + daisy = 4 = 2 +eggplant = 5 = 2 + +apple = 1 = 2 +banana = 2 = 2 +cake = 3 = 2 +daisy = 4 = 2 +eggplant = 5 = 2 + +apple = 1 = 2 +banana = 2 = 2 +cake = 3 = 2 +daisy = 4 = 2 +eggplant = 5 = 2 + apple = 1 = 2 banana = 2 = 2 cake = 3 = 2 diff --git a/test/basic.script b/test/basic.script index 4d58507..bbecbc8 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': '#\+', 'ignores': ['String'] } } vip #227zzvip :239zzvip *=vipjyP:let g:easy_align_ignores = [] vip *=:unlet g:easy_align_delimiters :unlet g:easy_align_ignores 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 } +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': '#\+', 'ignores': ['String'] } } vip #227zzvip :239zzvip *=vipjyP:let g:easy_align_ignores = [] vip *=:unlet g:easy_align_delimiters :unlet g:easy_align_ignores 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'}