From 94524ec3d33505402b41162e56d638387feffd80 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 15 Jul 2013 20:53:30 +0900 Subject: [PATCH] Left-right alternating alignment (**) --- README.md | 43 ++++++++++++++++++----------------- autoload/easy_align.vim | 50 +++++++++++++++++++++++++---------------- doc/easy_align.txt | 33 ++++++++++++++------------- 3 files changed, 70 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 88a925a..4d9f4c6 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Features: - Optimized for code editing - Extensible alignment rules -- Aligns text around either _all or n-th_ appearance(s) of the delimiter +- Aligns text around either _all or n-th_ occurrence(s) of the delimiter - Ignores comment lines - Ignores lines without a matching delimiter @@ -46,14 +46,15 @@ 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` Around the 1st occurrences of delimiters + - `2` Around the 2nd occurrences of delimiters - ... - - `*` Alignment around all delimiters (recursive) - - `-` Alignment around the last delimiters (`-1`) - - `-2` Alignment around the one before the last delimiters + - `*` Around all occurrences of delimiters + - `**` Left-right alternating alignment around all delimiters + - `-` Around the last occurrences of delimiters (`-1`) + - `-2` Around the second to last occurrences of delimiters - ... -1. Delimiter (``, `=`, `:`, `.`, `|`, `,`, `}`) +1. Delimiter (``, `=`, `:`, `.`, `|`, `,`) Alignment rules for the following delimiters have been defined to meet the most needs. @@ -64,24 +65,24 @@ 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 (Try using it with a negative field number) | | | | Table markdown | ### Example command sequences -| With visual map | Description | Equivalent command | -| ----------------- | ------------------------------------------------- | ----------------------- | -| `=` | Alignment around 1st equals signs (and the likes) | `:'<,'>EasyAlign=` | -| `2=` | Alignment around 2nd equals signs (and the likes) | `:'<,'>EasyAlign2=` | -| `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 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-}` | -| ... | ... | | +| With visual map | Description | Equivalent command | +| ------------------- | -------------------------------------------------------- | ------------------------- | +| `` | Alignment around 1st whitespaces | `:'<,'>EasyAlign\ ` | +| `2` | Alignment around 2nd whitespaces | `:'<,'>EasyAlign2\ ` | +| `-` | Alignment around the last whitespaces | `:'<,'>EasyAlign-\ ` | +| `:` | Alignment around 1st colon | `:'<,'>EasyAlign:` | +| `=` | Alignment around 1st equals signs (and the likes) | `:'<,'>EasyAlign=` | +| `2=` | Alignment around 2nd equals signs (and the likes) | `:'<,'>EasyAlign2=` | +| `3=` | Alignment around 3rd equals signs (and the likes) | `:'<,'>EasyAlign3=` | +| `*=` | Alignment around all equals signs (and the likes) | `:'<,'>EasyAlign*=` | +| `**=` | Left-right alternating alignment around all equals signs | `:'<,'>EasyAlign**=` | +| `=` | Right-justified alignment around 1st equals signs | `:'<,'>EasyAlignRight=` | +| `**=` | Right-left alternating alignment around all equals signs | `:'<,'>EasyAlignRight**=` | +| ... | ... | | ### Partial alignment in blockwise-visual mode diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index 9ac57be..3cf5743 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -147,7 +147,8 @@ function! s:do_align(just, cl, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_le endfor if a:recursive && a:nth < max_tokens - call s:do_align(a:just, a:cl, a:fl, a:ll, a:fc, a:lc, a:pattern, a:nth + 1, a:ml, a:mr, a:stick_to_left, a:recursive) + let just = a:recursive == 2 ? !a:just : a:just + call s:do_align(just, a:cl, a:fl, a:ll, a:fc, a:lc, a:pattern, a:nth + 1, a:ml, a:mr, a:stick_to_left, a:recursive) endif endfunction @@ -168,32 +169,38 @@ function! easy_align#align(just, ...) range let c = getchar() let ch = nr2char(c) - if c == 3 || c == 27 + if c == 3 || c == 27 " CTRL-C / ESC return - elseif c == '€kb' + elseif c == '€kb' " Backspace if len(n) > 0 let n = strpart(n, 0, len(n) - 1) endif - elseif c == 13 + elseif c == 13 " Enter key let just = (just + 1) % len(s:just) - elseif index(['-', '*'], ch) != -1 + elseif ch == '-' if empty(n) - let n = ch + let n = '-' + elseif n == '-' + let n = '' else break endif - elseif c == 48 - if n == '-' - let n = '-0' + elseif ch == '*' + if empty(n) + let n = '*' + elseif n == '*' + let n = '**' + elseif n == '**' + let n = '' else break endif - elseif c > 48 && c <= 57 - if n != '*' + elseif c >= 48 && c <= 57 + if n[0] == '*' + break + else let n = n . ch - else - break - endif + end else break endif @@ -214,15 +221,20 @@ function! easy_align#align(just, ...) range endif if n == '*' - let n = 1 + let nth = 1 let recursive = 1 + elseif n == '**' + let nth = 1 + let recursive = 2 elseif n == '-' - let n = -1 + let nth = -1 elseif empty(n) - let n = 1 + let nth = 1 elseif n != '-0' && n != string(str2nr(n)) echon "\rInvalid field number: ". n return + else + let nth = n endif let delimiters = extend(copy(s:easy_align_delimiters_default), @@ -234,11 +246,11 @@ function! easy_align#align(just, ...) range \ visualmode() == '' ? min([col("'<"), col("'>")]) : 1, \ visualmode() == '' ? max([col("'<"), col("'>")]) : 0, \ get(dict, 'pattern', ch), - \ n, + \ nth, \ get(dict, 'margin_left', ' '), \ get(dict, 'margin_right', ' '), \ get(dict, 'stick_to_left', 0), recursive) - call s:echon(just, (recursive ? '*' : n), ch) + call s:echon(just, n, ch) else echon "\rUnknown delimiter: ". ch endif diff --git a/doc/easy_align.txt b/doc/easy_align.txt index 434e4ce..df662a9 100644 --- a/doc/easy_align.txt +++ b/doc/easy_align.txt @@ -22,35 +22,36 @@ With this mapping, you can align selected lines with a few keystrokes. 1. key to start interactive EasyAlign command 2. Optional Enter keys to switch justficiation mode (default: left) 3. Optional field number (default: 1) - 1 Alignment around 1st delimiters - 2 Alignment around 2nd delimiters + 1 Around the 1st occurrences of delimiters + 2 Around the 2nd occurrences of delimiters + * Around all occurrences of delimiters + ** Left-right alternating alignment around all delimiters + - Around the last occurrences of delimiters (`-1`) + -2 Around the second to last occurrences of 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 (=, ==, !=, +=, &&=, ...) : 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. Examples: - = Alignment around 1st equals signs (and the likes) - 2= Alignment around 2nd equals signs (and the likes) - 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 whitespaces - 2 Alignment around 2nd whitespaces - - Alignment around the last whitespaces - : Alignment around 1st colons - -} Alignment around the last closing braces + Alignment around 1st whitespaces + 2 Alignment around 2nd whitespaces + - Alignment around the last whitespaces + : Alignment around 1st colon + = Alignment around 1st equals signs (and the likes) + 2= Alignment around 2nd equals signs (and the likes) + 3= Alignment around 3rd equals signs (and the likes) + *= Alignment around all equals signs (and the likes) + **= Left-right alternating alignment around all equals signs + = Right-justified alignment around 1st equals signs + **= Right-left alternating alignment around all equals signs EasyAlignRight *EasyAlignRight*