mirror of
https://github.com/junegunn/vim-easy-align.git
synced 2025-11-11 19:33:50 -05:00
Merge pull request #3 from junegunn/alternating
Left-right alternating alignment (**)
This commit is contained in:
31
README.md
31
README.md
@@ -7,7 +7,7 @@ Features:
|
|||||||
|
|
||||||
- Optimized for code editing
|
- Optimized for code editing
|
||||||
- Extensible alignment rules
|
- 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 comment lines
|
||||||
- Ignores lines without a matching delimiter
|
- Ignores lines without a matching delimiter
|
||||||
|
|
||||||
@@ -46,14 +46,15 @@ With the mapping, you can align selected lines with a few keystrokes.
|
|||||||
1. `<Enter>` key to start interactive EasyAlign command
|
1. `<Enter>` key to start interactive EasyAlign command
|
||||||
1. Optional Enter keys to toggle right-justification mode
|
1. Optional Enter keys to toggle right-justification mode
|
||||||
1. Optional field number (default: 1)
|
1. Optional field number (default: 1)
|
||||||
- `1` Alignment around 1st delimiters
|
- `1` Around the 1st occurrences of delimiters
|
||||||
- `2` Alignment around 2nd delimiters
|
- `2` Around the 2nd occurrences of delimiters
|
||||||
- ...
|
- ...
|
||||||
- `*` Alignment around all delimiters (recursive)
|
- `*` Around all occurrences of delimiters
|
||||||
- `-` Alignment around the last delimiters (`-1`)
|
- `**` Left-right alternating alignment around all delimiters
|
||||||
- `-2` Alignment around the one before the last delimiters
|
- `-` Around the last occurrences of delimiters (`-1`)
|
||||||
|
- `-2` Around the second to last occurrences of delimiters
|
||||||
- ...
|
- ...
|
||||||
1. Delimiter (`<space>`, `=`, `:`, `.`, `|`, `,`, `}`)
|
1. Delimiter (`<space>`, `=`, `:`, `.`, `|`, `,`)
|
||||||
|
|
||||||
Alignment rules for the following delimiters have been defined to meet the most needs.
|
Alignment rules for the following delimiters have been defined to meet the most needs.
|
||||||
|
|
||||||
@@ -64,23 +65,23 @@ Alignment rules for the following delimiters have been defined to meet the most
|
|||||||
| `:` | Suitable for formatting JSON or YAML |
|
| `:` | Suitable for formatting JSON or YAML |
|
||||||
| `.` | Multi-line method chaining |
|
| `.` | Multi-line method chaining |
|
||||||
| `,` | Multi-line method arguments |
|
| `,` | Multi-line method arguments |
|
||||||
| `}` | Closing braces (Try using it with a negative field number) |
|
|
||||||
| | | Table markdown |
|
| | | Table markdown |
|
||||||
|
|
||||||
### Example command sequences
|
### Example command sequences
|
||||||
|
|
||||||
| With visual map | Description | Equivalent command |
|
| With visual map | Description | Equivalent command |
|
||||||
| ----------------- | ------------------------------------------------- | ----------------------- |
|
| ------------------- | -------------------------------------------------------- | ------------------------- |
|
||||||
| `<Enter>=` | Alignment around 1st equals signs (and the likes) | `:'<,'>EasyAlign=` |
|
|
||||||
| `<Enter>2=` | Alignment around 2nd equals signs (and the likes) | `:'<,'>EasyAlign2=` |
|
|
||||||
| `<Enter>3=` | Alignment around 3rd equals signs (and the likes) | `:'<,'>EasyAlign3=` |
|
|
||||||
| `<Enter>*=` | Alignment around all equals signs (and the likes) | `:'<,'>EasyAlign*=` |
|
|
||||||
| `<Enter><Enter>=` | Right-justified alignment around 1st equals signs | `:'<,'>EasyAlignRight=` |
|
|
||||||
| `<Enter><space>` | Alignment around 1st whitespaces | `:'<,'>EasyAlign\ ` |
|
| `<Enter><space>` | Alignment around 1st whitespaces | `:'<,'>EasyAlign\ ` |
|
||||||
| `<Enter>2<space>` | Alignment around 2nd whitespaces | `:'<,'>EasyAlign2\ ` |
|
| `<Enter>2<space>` | Alignment around 2nd whitespaces | `:'<,'>EasyAlign2\ ` |
|
||||||
| `<Enter>-<space>` | Alignment around the last whitespaces | `:'<,'>EasyAlign-\ ` |
|
| `<Enter>-<space>` | Alignment around the last whitespaces | `:'<,'>EasyAlign-\ ` |
|
||||||
| `<Enter>:` | Alignment around 1st colon | `:'<,'>EasyAlign:` |
|
| `<Enter>:` | Alignment around 1st colon | `:'<,'>EasyAlign:` |
|
||||||
| `<Enter>-}` | Alignment around the last closing braces | `:'<,'>EasyAlign-}` |
|
| `<Enter>=` | Alignment around 1st equals signs (and the likes) | `:'<,'>EasyAlign=` |
|
||||||
|
| `<Enter>2=` | Alignment around 2nd equals signs (and the likes) | `:'<,'>EasyAlign2=` |
|
||||||
|
| `<Enter>3=` | Alignment around 3rd equals signs (and the likes) | `:'<,'>EasyAlign3=` |
|
||||||
|
| `<Enter>*=` | Alignment around all equals signs (and the likes) | `:'<,'>EasyAlign*=` |
|
||||||
|
| `<Enter>**=` | Left-right alternating alignment around all equals signs | `:'<,'>EasyAlign**=` |
|
||||||
|
| `<Enter><Enter>=` | Right-justified alignment around 1st equals signs | `:'<,'>EasyAlignRight=` |
|
||||||
|
| `<Enter><Enter>**=` | Right-left alternating alignment around all equals signs | `:'<,'>EasyAlignRight**=` |
|
||||||
| ... | ... | |
|
| ... | ... | |
|
||||||
|
|
||||||
### Partial alignment in blockwise-visual mode
|
### Partial alignment in blockwise-visual mode
|
||||||
|
|||||||
@@ -147,7 +147,8 @@ function! s:do_align(just, cl, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_le
|
|||||||
endfor
|
endfor
|
||||||
|
|
||||||
if a:recursive && a:nth < max_tokens
|
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
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
@@ -168,32 +169,38 @@ function! easy_align#align(just, ...) range
|
|||||||
|
|
||||||
let c = getchar()
|
let c = getchar()
|
||||||
let ch = nr2char(c)
|
let ch = nr2char(c)
|
||||||
if c == 3 || c == 27
|
if c == 3 || c == 27 " CTRL-C / ESC
|
||||||
return
|
return
|
||||||
elseif c == '<27>kb'
|
elseif c == '<27>kb' " Backspace
|
||||||
if len(n) > 0
|
if len(n) > 0
|
||||||
let n = strpart(n, 0, len(n) - 1)
|
let n = strpart(n, 0, len(n) - 1)
|
||||||
endif
|
endif
|
||||||
elseif c == 13
|
elseif c == 13 " Enter key
|
||||||
let just = (just + 1) % len(s:just)
|
let just = (just + 1) % len(s:just)
|
||||||
elseif index(['-', '*'], ch) != -1
|
elseif ch == '-'
|
||||||
if empty(n)
|
if empty(n)
|
||||||
let n = ch
|
let n = '-'
|
||||||
|
elseif n == '-'
|
||||||
|
let n = ''
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
endif
|
endif
|
||||||
elseif c == 48
|
elseif ch == '*'
|
||||||
if n == '-'
|
if empty(n)
|
||||||
let n = '-0'
|
let n = '*'
|
||||||
|
elseif n == '*'
|
||||||
|
let n = '**'
|
||||||
|
elseif n == '**'
|
||||||
|
let n = ''
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
endif
|
endif
|
||||||
elseif c > 48 && c <= 57
|
elseif c >= 48 && c <= 57
|
||||||
if n != '*'
|
if n[0] == '*'
|
||||||
|
break
|
||||||
|
else
|
||||||
let n = n . ch
|
let n = n . ch
|
||||||
else
|
end
|
||||||
break
|
|
||||||
endif
|
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
endif
|
endif
|
||||||
@@ -214,15 +221,20 @@ function! easy_align#align(just, ...) range
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
if n == '*'
|
if n == '*'
|
||||||
let n = 1
|
let nth = 1
|
||||||
let recursive = 1
|
let recursive = 1
|
||||||
|
elseif n == '**'
|
||||||
|
let nth = 1
|
||||||
|
let recursive = 2
|
||||||
elseif n == '-'
|
elseif n == '-'
|
||||||
let n = -1
|
let nth = -1
|
||||||
elseif empty(n)
|
elseif empty(n)
|
||||||
let n = 1
|
let nth = 1
|
||||||
elseif n != '-0' && n != string(str2nr(n))
|
elseif n != '-0' && n != string(str2nr(n))
|
||||||
echon "\rInvalid field number: ". n
|
echon "\rInvalid field number: ". n
|
||||||
return
|
return
|
||||||
|
else
|
||||||
|
let nth = n
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let delimiters = extend(copy(s:easy_align_delimiters_default),
|
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() == '' ? min([col("'<"), col("'>")]) : 1,
|
||||||
\ visualmode() == '' ? max([col("'<"), col("'>")]) : 0,
|
\ visualmode() == '' ? max([col("'<"), col("'>")]) : 0,
|
||||||
\ get(dict, 'pattern', ch),
|
\ get(dict, 'pattern', ch),
|
||||||
\ n,
|
\ nth,
|
||||||
\ get(dict, 'margin_left', ' '),
|
\ get(dict, 'margin_left', ' '),
|
||||||
\ get(dict, 'margin_right', ' '),
|
\ get(dict, 'margin_right', ' '),
|
||||||
\ get(dict, 'stick_to_left', 0), recursive)
|
\ get(dict, 'stick_to_left', 0), recursive)
|
||||||
call s:echon(just, (recursive ? '*' : n), ch)
|
call s:echon(just, n, ch)
|
||||||
else
|
else
|
||||||
echon "\rUnknown delimiter: ". ch
|
echon "\rUnknown delimiter: ". ch
|
||||||
endif
|
endif
|
||||||
|
|||||||
@@ -22,35 +22,36 @@ With this mapping, you can align selected lines with a few keystrokes.
|
|||||||
1. <Enter> key to start interactive EasyAlign command
|
1. <Enter> key to start interactive EasyAlign command
|
||||||
2. Optional Enter keys to switch justficiation mode (default: left)
|
2. Optional Enter keys to switch justficiation mode (default: left)
|
||||||
3. Optional field number (default: 1)
|
3. Optional field number (default: 1)
|
||||||
1 Alignment around 1st delimiters
|
1 Around the 1st occurrences of delimiters
|
||||||
2 Alignment around 2nd 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
|
4. Delimiter
|
||||||
<space> General alignment around whitespaces
|
<space> General alignment around whitespaces
|
||||||
= Operators containing equals sign (=, ==, !=, +=, &&=, ...)
|
= Operators containing equals sign (=, ==, !=, +=, &&=, ...)
|
||||||
: Suitable for formatting JSON or YAML
|
: Suitable for formatting JSON or YAML
|
||||||
. Multi-line method chaining
|
. Multi-line method chaining
|
||||||
, Multi-line method arguments. CSV.
|
, Multi-line method arguments. CSV.
|
||||||
} Closing braces (Try using it with a negative field number)
|
|
||||||
| Table markdown
|
| Table markdown
|
||||||
|
|
||||||
During the key sequence, <Enter> key will toggle right-justification mode.
|
During the key sequence, <Enter> key will toggle right-justification mode.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
|
<Enter><space> Alignment around 1st whitespaces
|
||||||
|
<Enter>2<space> Alignment around 2nd whitespaces
|
||||||
|
<Enter>-<space> Alignment around the last whitespaces
|
||||||
|
<Enter>: Alignment around 1st colon
|
||||||
<Enter>= Alignment around 1st equals signs (and the likes)
|
<Enter>= Alignment around 1st equals signs (and the likes)
|
||||||
<Enter>2= Alignment around 2nd equals signs (and the likes)
|
<Enter>2= Alignment around 2nd equals signs (and the likes)
|
||||||
<Enter>3= Alignment around 3rd equals signs (and the likes)
|
<Enter>3= Alignment around 3rd equals signs (and the likes)
|
||||||
<Enter>*= Alignment around all equals signs (and the likes)
|
<Enter>*= Alignment around all equals signs (and the likes)
|
||||||
|
<Enter>**= Left-right alternating alignment around all equals signs
|
||||||
<Enter><Enter>= Right-justified alignment around 1st equals signs
|
<Enter><Enter>= Right-justified alignment around 1st equals signs
|
||||||
<Enter><space> Alignment around 1st whitespaces
|
<Enter><Enter>**= Right-left alternating alignment around all equals signs
|
||||||
<Enter>2<space> Alignment around 2nd whitespaces
|
|
||||||
<Enter>-<space> Alignment around the last whitespaces
|
|
||||||
<Enter>: Alignment around 1st colons
|
|
||||||
<Enter>-} Alignment around the last closing braces
|
|
||||||
|
|
||||||
|
|
||||||
EasyAlignRight *EasyAlignRight*
|
EasyAlignRight *EasyAlignRight*
|
||||||
|
|||||||
Reference in New Issue
Block a user