mirror of
https://github.com/junegunn/vim-easy-align.git
synced 2025-11-09 02:13:49 -05:00
support negative modifier (backward scan)
This commit is contained in:
24
README.md
24
README.md
@@ -38,13 +38,16 @@ 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` Alignment around 1st delimiters
|
||||||
- `2` Alignment around 2nd 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 (`<space>`, `=`, `:`, `.`, `|`, `,`)
|
1. Delimiter (`<space>`, `=`, `:`, `.`, `|`, `,`)
|
||||||
|
|
||||||
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 |
|
| 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 |
|
| `:` | Suitable for formatting JSON or YAML |
|
||||||
| `.` | Multi-line method chaining |
|
| `.` | Multi-line method chaining |
|
||||||
| `,` | Multi-line method arguments |
|
| `,` | Multi-line method arguments |
|
||||||
|
| `}` | Closing braces (use it with with a negative field number) |
|
||||||
| | | Table markdown |
|
| | | Table markdown |
|
||||||
|
|
||||||
### Example command sequences
|
### Example command sequences
|
||||||
@@ -64,9 +68,11 @@ Alignment rules for the following delimiters have been crafted to meet the most
|
|||||||
| `<Enter>3=` | Alignment around 3rd equals signs (and the likes) | `:'<,'>EasyAlign3=` |
|
| `<Enter>3=` | Alignment around 3rd equals signs (and the likes) | `:'<,'>EasyAlign3=` |
|
||||||
| `<Enter>*=` | Alignment around all equals signs (and the likes) | `:'<,'>EasyAlign*=` |
|
| `<Enter>*=` | Alignment around all equals signs (and the likes) | `:'<,'>EasyAlign*=` |
|
||||||
| `<Enter><Enter>=` | Right-justified alignment around 1st equals signs | `:'<,'>EasyAlignRight=` |
|
| `<Enter><Enter>=` | Right-justified alignment around 1st equals signs | `:'<,'>EasyAlignRight=` |
|
||||||
| `<Enter><space>` | Alignment around 1st space | `:'<,'>EasyAlign\ ` |
|
| `<Enter><space>` | Alignment around 1st whitespaces | `:'<,'>EasyAlign\ ` |
|
||||||
| `<Enter>2<space>` | Alignment around 2nd space | `:'<,'>EasyAlign2\ ` |
|
| `<Enter>2<space>` | Alignment around 2nd whitespaces | `:'<,'>EasyAlign2\ ` |
|
||||||
|
| `<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-}` |
|
||||||
| ... | ... | |
|
| ... | ... | |
|
||||||
|
|
||||||
### Partial alignment in blockwise-visual mode
|
### Partial alignment in blockwise-visual mode
|
||||||
@@ -100,9 +106,9 @@ Defining custom alignment rules
|
|||||||
let g:easy_align_delimiters = {
|
let g:easy_align_delimiters = {
|
||||||
\ '>': { 'pattern': '>>\|=>\|>' },
|
\ '>': { 'pattern': '>>\|=>\|>' },
|
||||||
\ '/': { 'pattern': '//*' },
|
\ '/': { 'pattern': '//*' },
|
||||||
\ '}': {
|
\ ')': {
|
||||||
\ 'pattern': '}',
|
\ 'pattern': ')',
|
||||||
\ 'margin_left': ' ',
|
\ 'margin_left': '',
|
||||||
\ 'margin_right': '',
|
\ 'margin_right': '',
|
||||||
\ 'stick_to_left': 0
|
\ 'stick_to_left': 0
|
||||||
\ }
|
\ }
|
||||||
|
|||||||
@@ -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': 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 },
|
||||||
|
\ '}': { 'pattern': '}', 'margin_left': ' ', 'margin_right': '', 'stick_to_left': 0 }
|
||||||
\ }
|
\ }
|
||||||
|
|
||||||
let s:just = ['', '[R]']
|
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
|
endif
|
||||||
let max_tokens = max([len(tokens), max_tokens])
|
let max_tokens = max([len(tokens), max_tokens])
|
||||||
|
|
||||||
if len(tokens) < a:nth
|
if a:nth > 0
|
||||||
continue
|
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
|
endif
|
||||||
let nth = a:nth - 1 " 0-based
|
|
||||||
|
|
||||||
let last = tokens[nth]
|
let last = tokens[nth]
|
||||||
let prefix = (nth > 0 ? join(tokens[0 : nth - 1], '') : '')
|
let prefix = (nth > 0 ? join(tokens[0 : nth - 1], '') : '')
|
||||||
@@ -114,6 +127,12 @@ function! easy_align#align(just, ...) range
|
|||||||
return
|
return
|
||||||
elseif c == 13
|
elseif c == 13
|
||||||
let just = (just + 1) % len(s:just)
|
let just = (just + 1) % len(s:just)
|
||||||
|
elseif c == 45
|
||||||
|
if !empty(n)
|
||||||
|
break
|
||||||
|
else
|
||||||
|
let n = '-'
|
||||||
|
endif
|
||||||
elseif c >= 48 && c <= 57
|
elseif c >= 48 && c <= 57
|
||||||
if n == '*'
|
if n == '*'
|
||||||
break
|
break
|
||||||
@@ -148,6 +167,8 @@ function! easy_align#align(just, ...) range
|
|||||||
if n == '*'
|
if n == '*'
|
||||||
let n = 1
|
let n = 1
|
||||||
let recursive = 1
|
let recursive = 1
|
||||||
|
elseif n == '-'
|
||||||
|
let n = -1
|
||||||
elseif empty(n)
|
elseif empty(n)
|
||||||
let n = 1
|
let n = 1
|
||||||
elseif n != string(str2nr(n))
|
elseif n != string(str2nr(n))
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ With this mapping, you can align selected lines with a few keystrokes.
|
|||||||
2 Alignment around 2nd delimiters
|
2 Alignment around 2nd delimiters
|
||||||
...
|
...
|
||||||
* Alignment around all delimiters (recursive)
|
* 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 (=, ==, !=, +=, &&=, ...)
|
||||||
@@ -46,6 +48,7 @@ Examples:
|
|||||||
<Enter><space> Alignment around 1st whitespace
|
<Enter><space> Alignment around 1st whitespace
|
||||||
<Enter>2<space> Alignment around 2nd whitespace
|
<Enter>2<space> Alignment around 2nd whitespace
|
||||||
<Enter>: Alignment around 1st colon
|
<Enter>: Alignment around 1st colon
|
||||||
|
<Enter>-} Alignment around the last closing braces
|
||||||
|
|
||||||
|
|
||||||
EasyAlignRight *EasyAlignRight*
|
EasyAlignRight *EasyAlignRight*
|
||||||
|
|||||||
Reference in New Issue
Block a user