diff --git a/EXAMPLES.md b/EXAMPLES.md index 28f6c01..e42c01c 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -286,3 +286,101 @@ apricot = 'DAD' + 'F#AD' banana = 'string' # comment 2 ``` +Aligning C-style variable definition +------------------------------------ + +Take the following example: + +```c +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +``` + +We can align these lines with the predefined `=` rule. Select the lines and +press `=` + +```c +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +``` + +Not bad. However, the names of the variables, `str`, `count`, and `pi` are not +aligned with each other. Can we do better? You can clearly see that simple +`` won't properly align those names. So we define a rule for such +cases. + +```vim +let g:easy_align_delimiters['d'] = { +\ 'pattern': '\(const\|static\)\@d`. + +```c +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +``` + +Okay, now the names are aligned. We select the lines again with `gv`, and then +press `=` to finalize our alignment. + +```c +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +``` + +Looks good. However, this rule is not sufficient to handle more complex cases +such as C++ templates or Java generics. Take the following examples. + +```c +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +static std::map* scores = pointer; +``` + +We see that our rule fails with the new fourth line. + +```c +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +static std::map* scores = pointer; +``` + +So what do we do? Let's try to improve our alignment rule. + +```vim +let g:easy_align_delimiters['d'] = { +\ 'pattern': '\s\+\(\S\+\s*[;=]\)\@=', +\ 'left_margin': 0, 'right_margin': 0 +\ } +``` + +Now the new rule has changed to align text around whitespaces that are followed +by some non-whitespace characters and then an equals sign or a semi-colon. +Try `d` + +```c +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +static std::map* scores = pointer; +``` + +Okay, now press `gv=` and voila! + +```c +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +static std::map* scores = pointer; +``` + diff --git a/README.md b/README.md index c1c6bc5..3fe8105 100644 --- a/README.md +++ b/README.md @@ -340,14 +340,19 @@ let g:easy_align_delimiters = { \ 'left_margin': 0, \ 'right_margin': 0, \ 'stick_to_left': 0 +\ }, +\ 'd': { +\ 'pattern': '\s\+\(\S\+\s*[;=]\)\@=', +\ 'left_margin': 0, +\ 'right_margin': 0 \ } \ } ``` -Examples and use cases ----------------------- +Advanced examples and use cases +------------------------------- -See the [link](https://github.com/junegunn/vim-easy-align/blob/master/EXAMPLES.md) +See [EXAMPLES.md](https://github.com/junegunn/vim-easy-align/blob/master/EXAMPLES.md) for more examples. Author diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index 58944ca..5903078 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -218,7 +218,7 @@ function! s:do_align(just, all_tokens, fl, ll, fc, lc, pattern, nth, ml, mr, sti let token = substitute(last, pattern.'$', '', '') let delim = get(matchlist(last, pattern.'$'), 1, '') - if empty(delim) && a:just == 0 && a:ignore_unmatched + if empty(delim) && !exists('tokens[nth + 1]') && a:just == 0 && a:ignore_unmatched continue endif diff --git a/test/basic.expected b/test/basic.expected index e96978b..0c9e388 100644 --- a/test/basic.expected +++ b/test/basic.expected @@ -336,6 +336,16 @@ my_object . | batch_size | Fixnum | nil | number of maximum items to be assigned at once | | logger | Logger | nil | logger instance for debug logs | + +```c + +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +static std::map* scores = pointer; + +``` + |Option |Type |Default |Description | |-- |-- |-- |-- | |threads |Fixnum |1 |number of threads in the thread pool | diff --git a/test/basic.md b/test/basic.md index c967c76..25f23ba 100644 --- a/test/basic.md +++ b/test/basic.md @@ -107,3 +107,13 @@ my_object. |batch_size | Fixnum | nil | number of maximum items to be assigned at once | |logger | Logger | nil | logger instance for debug logs | + +```c + +const char* str = "Hello"; +int64_t count = 1 + 2; +static double pi = 3.14; +static std::map* scores = pointer; + +``` + diff --git a/test/basic.script b/test/basic.script index b590950..2a4dcf8 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 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 ** |Gpvip:EasyAlign*|{'ml': 5, 'mrr': 0 } Gpvip:EasyAlign*/|/{'ml':'<', 'mrr': 4} +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 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 ** |Gpvip:EasyAlign*|{'ml': 5, 'mrr': 0 } Gpvip:EasyAlign*/|/{'ml':'<', 'mrr': 4} 342G:let g:easy_align_delimiters = { 'd': { 'pattern': '\s\+\(\S\+\s*[;=]\)\@=', 'left_margin': 0, 'right_margin': 0 } } vip dgv =