From 1e627366e72df77da4297a6a21a6da1a4a27b02c Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 13 May 2013 02:43:29 +0900 Subject: [PATCH 01/10] support negative modifier (backward scan) --- README.md | 24 +++++++++++++++--------- autoload/easy_align.vim | 29 +++++++++++++++++++++++++---- doc/easy_align.txt | 3 +++ 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 136f711..8404237 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,16 @@ With the mapping, you can align selected lines with a few keystrokes. 1. `` key to start interactive EasyAlign command 1. Optional Enter keys to toggle right-justification mode 1. Optional field number (default: 1) - - `1` Alignment around 1st delimiters - - `2` Alignment around 2nd delimiters + - `1` Alignment around 1st delimiters + - `2` Alignment around 2nd delimiters + - ... + - `*` Alignment around all delimiters (recursive) + - `-` Alignment around the last delimiters (`-1`) + - `-2` Alignment around the one before the last delimiters - ... - - `*` Alignment around all delimiters (recursive) 1. Delimiter (``, `=`, `:`, `.`, `|`, `,`) -Alignment rules for the following delimiters have been crafted to meet the most needs. +Alignment rules for the following delimiters have been defined to meet the most needs. | Delimiter | Description/Use cases | | --------- | ---------------------------------------------------------- | @@ -53,6 +56,7 @@ Alignment rules for the following delimiters have been crafted to meet the most | `:` | Suitable for formatting JSON or YAML | | `.` | Multi-line method chaining | | `,` | Multi-line method arguments | +| `}` | Closing braces (use it with with a negative field number) | | | | Table markdown | ### Example command sequences @@ -64,9 +68,11 @@ Alignment rules for the following delimiters have been crafted to meet the most | `3=` | Alignment around 3rd equals signs (and the likes) | `:'<,'>EasyAlign3=` | | `*=` | Alignment around all equals signs (and the likes) | `:'<,'>EasyAlign*=` | | `=` | Right-justified alignment around 1st equals signs | `:'<,'>EasyAlignRight=` | -| `` | Alignment around 1st space | `:'<,'>EasyAlign\ ` | -| `2` | Alignment around 2nd space | `:'<,'>EasyAlign2\ ` | +| `` | Alignment around 1st whitespaces | `:'<,'>EasyAlign\ ` | +| `2` | Alignment around 2nd whitespaces | `:'<,'>EasyAlign2\ ` | +| `-` | Alignment around the last whitespaces | `:'<,'>EasyAlign-\ ` | | `:` | Alignment around 1st colon | `:'<,'>EasyAlign:` | +| `-}` | Alignment around the last closing braces | `:'<,'>EasyAlign-}` | | ... | ... | | ### Partial alignment in blockwise-visual mode @@ -100,9 +106,9 @@ Defining custom alignment rules let g:easy_align_delimiters = { \ '>': { 'pattern': '>>\|=>\|>' }, \ '/': { 'pattern': '//*' }, -\ '}': { -\ 'pattern': '}', -\ 'margin_left': ' ', +\ ')': { +\ 'pattern': ')', +\ 'margin_left': '', \ 'margin_right': '', \ 'stick_to_left': 0 \ } diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index a7b62f4..e36df10 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -10,7 +10,8 @@ let s:easy_align_delimiters_default = { \ ':': { '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 }, -\ '.': { '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': 0 } \ } let s:just = ['', '[R]'] @@ -36,10 +37,22 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, endif let max_tokens = max([len(tokens), max_tokens]) - if len(tokens) < a:nth - continue + if a:nth > 0 + if len(tokens) < a:nth + continue + endif + let nth = a:nth - 1 " 0-based + else + if match(tokens[len(tokens) - 1], pattern.'$') == -1 + let nth = len(tokens) + a:nth - 1 + else + let nth = len(tokens) + a:nth + endif + + if nth < 0 + continue + endif endif - let nth = a:nth - 1 " 0-based let last = tokens[nth] let prefix = (nth > 0 ? join(tokens[0 : nth - 1], '') : '') @@ -114,6 +127,12 @@ function! easy_align#align(just, ...) range return elseif c == 13 let just = (just + 1) % len(s:just) + elseif c == 45 + if !empty(n) + break + else + let n = '-' + endif elseif c >= 48 && c <= 57 if n == '*' break @@ -148,6 +167,8 @@ function! easy_align#align(just, ...) range if n == '*' let n = 1 let recursive = 1 + elseif n == '-' + let n = -1 elseif empty(n) let n = 1 elseif n != string(str2nr(n)) diff --git a/doc/easy_align.txt b/doc/easy_align.txt index 8cac149..65859cd 100644 --- a/doc/easy_align.txt +++ b/doc/easy_align.txt @@ -26,6 +26,8 @@ With this mapping, you can align selected lines with a few keystrokes. 2 Alignment around 2nd delimiters ... * Alignment around all delimiters (recursive) + - Alignment around the last delimieters + -2 Alignment around the one before the last delimieters 4. Delimiter General alignment around whitespaces = Operators containing equals sign (=, ==, !=, +=, &&=, ...) @@ -46,6 +48,7 @@ Examples: Alignment around 1st whitespace 2 Alignment around 2nd whitespace : Alignment around 1st colon + -} Alignment around the last closing braces EasyAlignRight *EasyAlignRight* From 9c9a16c7591bdd36d7ae2fab1e288daacbac6982 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 13 May 2013 02:59:01 +0900 Subject: [PATCH 02/10] add use case of negative field number --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 8404237..98cfcc1 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,9 @@ my_hash = { :a => 1, :aaa => 3 } ``` +However, in this case, we don't really need blockwise visual mode +since the same can be easily done using the negative field number: `-=` + Defining custom alignment rules ------------------------------- From 811ea63b11356062136e7307564ee0cab41478fa Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 13 May 2013 03:01:43 +0900 Subject: [PATCH 03/10] update help --- doc/easy_align.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/easy_align.txt b/doc/easy_align.txt index 65859cd..67f4eb5 100644 --- a/doc/easy_align.txt +++ b/doc/easy_align.txt @@ -45,9 +45,10 @@ Examples: 3= Alignment around 3rd equals signs (and the likes) *= Alignment around all equals signs (and the likes) = Right-justified alignment around 1st equals signs - Alignment around 1st whitespace - 2 Alignment around 2nd whitespace - : Alignment around 1st colon + Alignment around 1st whitespaces + 2 Alignment around 2nd whitespaces + - Alignment around the last whitespaces + : Alignment around 1st colons -} Alignment around the last closing braces From cb71847a113d095f4aa71844d7c6838b2c0c0ed5 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 13 May 2013 03:10:33 +0900 Subject: [PATCH 04/10] support backspace in interactive mode --- autoload/easy_align.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index e36df10..e67f4e8 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -36,7 +36,6 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, let tokens = extend([join(tokens[0:1], '')], tokens[2:-1]) endif let max_tokens = max([len(tokens), max_tokens]) - if a:nth > 0 if len(tokens) < a:nth continue @@ -125,6 +124,10 @@ function! easy_align#align(just, ...) range let ch = nr2char(c) if c == 3 || c == 27 return + elseif c == '€kb' + if len(n) > 0 + let n = strpart(n, 0, len(n) - 1) + endif elseif c == 13 let just = (just + 1) % len(s:just) elseif c == 45 From 5c870f60d95fe59c32c74e71305c7c08d138550e Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 13 May 2013 03:21:52 +0900 Subject: [PATCH 05/10] fix non-interactive EasyAlign to take negative field # --- autoload/easy_align.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index e67f4e8..cfe83bf 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -153,7 +153,7 @@ function! easy_align#align(just, ...) range endif endwhile elseif a:0 == 1 - let tokens = matchlist(a:1, '^\([1-9][0-9]*\|\*\)\?\(.\)$') + let tokens = matchlist(a:1, '^\(-\?[1-9][0-9]*\|-\|\*\)\?\(.\)$') if empty(tokens) echo "Invalid arguments: ". a:1 return From 27b95b4ce0cee60bab25d160c4c21ce5580a8546 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 13 May 2013 12:38:58 +0900 Subject: [PATCH 06/10] finish implementing negative field number --- autoload/easy_align.vim | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index cfe83bf..52e22d3 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -5,7 +5,7 @@ let g:easy_align_loaded = 1 let s:easy_align_delimiters_default = { \ ' ': { 'pattern': ' ', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 }, -\ '=': { 'pattern': '===\|<=>\|\(&&\|||\|<<\|>>\)=\|=\~\|=>\|[:+/*!%^=><&|-]\?=', +\ '=': { '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 }, @@ -21,7 +21,7 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, let max_just_len = 0 let max_delim_len = 0 let max_tokens = 0 - let pattern = '\s*\(' .a:pattern. '\)\s*' + let pattern = '\s*\(' .a:pattern. '\)\s\{-}' for line in range(a:fl, a:ll) let tokens = split(a:lc ? \ strpart(getline(line), a:fc - 1, a:lc - a:fc + 1) : @@ -56,7 +56,7 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, let last = tokens[nth] let prefix = (nth > 0 ? join(tokens[0 : nth - 1], '') : '') let token = substitute(last, pattern.'$', '', '') - let suffix = join(tokens[nth + 1: -1], '') + let suffix = substitute(join(tokens[nth + 1: -1], ''), '^\s*', '', '') if match(last, pattern.'$') == -1 if a:just == 0 && (!exists("g:easy_align_ignore_unmatched") || g:easy_align_ignore_unmatched) @@ -130,30 +130,30 @@ function! easy_align#align(just, ...) range endif elseif c == 13 let just = (just + 1) % len(s:just) - elseif c == 45 - if !empty(n) - break + elseif index(['-', '*'], ch) != -1 + if empty(n) + let n = ch else - let n = '-' + break endif - elseif c >= 48 && c <= 57 - if n == '*' - break + elseif c == 48 + if n == '-' + let n = '-0' else + break + endif + elseif c > 48 && c <= 57 + if n != '*' let n = n . nr2char(c) - endif - elseif ch == '*' - if !empty(n) - break else - let n = '*' + break endif else break endif endwhile elseif a:0 == 1 - let tokens = matchlist(a:1, '^\(-\?[1-9][0-9]*\|-\|\*\)\?\(.\)$') + let tokens = matchlist(a:1, '^\([1-9][0-9]*\|-[0-9]*\|\*\)\?\(.\)$') if empty(tokens) echo "Invalid arguments: ". a:1 return @@ -174,7 +174,7 @@ function! easy_align#align(just, ...) range let n = -1 elseif empty(n) let n = 1 - elseif n != string(str2nr(n)) + elseif n != '-0' && n != string(str2nr(n)) echon "\rInvalid field number: ". n return endif From 0fe7d1790806ab0de5b7e08b04c73731cf8662db Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 13 May 2013 12:44:42 +0900 Subject: [PATCH 07/10] add support for unicode characters --- autoload/easy_align.vim | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index 52e22d3..ddebbc5 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -16,6 +16,16 @@ let s:easy_align_delimiters_default = { let s:just = ['', '[R]'] +if exists("*strwidth") + function! s:strwidth(str) + return strwidth(a:str) + endfunction +else + function! s:strwidth(str) + return len(split(a:str, '\zs')) + endfunction +endif + function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, recursive) let lines = {} let max_just_len = 0 @@ -68,15 +78,15 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, let delim = matchlist(last, pattern)[1] endif - let max_just_len = max([len(token.prefix), max_just_len]) - let max_delim_len = max([len(delim), max_delim_len]) + let max_just_len = max([s:strwidth(token.prefix), max_just_len]) + let max_delim_len = max([s:strwidth(delim), max_delim_len]) let lines[line] = [prefix, token, delim, suffix] endfor for [line, tokens] in items(lines) let [prefix, token, delim, suffix] = tokens - let pad = repeat(' ', max_just_len - len(prefix) - len(token)) + let pad = repeat(' ', max_just_len - s:strwidth(prefix) - s:strwidth(token)) if a:just == 0 if a:stick_to_left let suffix = pad . suffix @@ -87,7 +97,7 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, let token = pad . token endif - let delim = repeat(' ', max_delim_len - len(delim)). delim + let delim = repeat(' ', max_delim_len - s:strwidth(delim)). delim let cline = getline(line) let before = strpart(cline, 0, a:fc - 1) let after = a:lc ? strpart(cline, a:lc) : '' From e3f7352ab887a10e326d2ff980ac5aa952846aa4 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 13 May 2013 15:02:44 +0900 Subject: [PATCH 08/10] remove unnecessary nr2char call --- autoload/easy_align.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index ddebbc5..6abe3e9 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -154,7 +154,7 @@ function! easy_align#align(just, ...) range endif elseif c > 48 && c <= 57 if n != '*' - let n = n . nr2char(c) + let n = n . ch else break endif From 8dd2daaa9e9940f335303734d2c68f7cd51b891f Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 14 May 2013 00:41:56 +0900 Subject: [PATCH 09/10] fix to preserve trailing whitespaces from prefix when delimiter sticks to left --- autoload/easy_align.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index 6abe3e9..9deb77d 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -31,7 +31,7 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, let max_just_len = 0 let max_delim_len = 0 let max_tokens = 0 - let pattern = '\s*\(' .a:pattern. '\)\s\{-}' + let pattern = '\s*\(' .a:pattern. '\)\s' . (a:stick_to_left ? '*' : '\{-}') for line in range(a:fl, a:ll) let tokens = split(a:lc ? \ strpart(getline(line), a:fc - 1, a:lc - a:fc + 1) : @@ -58,7 +58,7 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, let nth = len(tokens) + a:nth endif - if nth < 0 + if nth < 0 || nth == len(tokens) continue endif endif From 3ab67e28708dba5033c66f0bc9a287e5f536732c Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 14 May 2013 00:48:21 +0900 Subject: [PATCH 10/10] update doc --- README.md | 2 +- doc/easy_align.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 98cfcc1..9698b83 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Alignment rules for the following delimiters have been defined to meet the most | `:` | Suitable for formatting JSON or YAML | | `.` | Multi-line method chaining | | `,` | Multi-line method arguments | -| `}` | Closing braces (use it with with a negative field number) | +| `}` | Closing braces (Try using it with a negative field number) | | | | Table markdown | ### Example command sequences diff --git a/doc/easy_align.txt b/doc/easy_align.txt index 67f4eb5..59f9ed0 100644 --- a/doc/easy_align.txt +++ b/doc/easy_align.txt @@ -34,6 +34,7 @@ With this mapping, you can align selected lines with a few keystrokes. : Suitable for formatting JSON or YAML . Multi-line method chaining , Multi-line method arguments. CSV. + } Closing braces (Try using it with a negative field number) | Table markdown During the key sequence, key will toggle right-justification mode. @@ -65,7 +66,6 @@ In blockwise-visual mode (`CTRL-V`), EasyAlign command aligns only the selected parts, instead of the whole lines in the range. - Defining custom alignment rules *g:easy_align_delimiters* -------------------------------------------------------------------------