mirror of
https://github.com/junegunn/vim-easy-align.git
synced 2025-11-12 03:43:48 -05:00
Merge pull request #1 from junegunn/negative-modifier
Add support for negative field number (scan backwards)
This commit is contained in:
19
README.md
19
README.md
@@ -42,9 +42,12 @@ With the 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 delimiters (`-1`)
|
||||||
|
- `-2` Alignment around the one before the last delimiters
|
||||||
|
- ...
|
||||||
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 (Try using it 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
|
||||||
@@ -93,6 +99,9 @@ my_hash = { :a => 1,
|
|||||||
:aaa => 3 }
|
: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: `<Enter>-=`
|
||||||
|
|
||||||
Defining custom alignment rules
|
Defining custom alignment rules
|
||||||
-------------------------------
|
-------------------------------
|
||||||
|
|
||||||
@@ -100,8 +109,8 @@ 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
|
||||||
|
|||||||
@@ -5,22 +5,33 @@ let g:easy_align_loaded = 1
|
|||||||
|
|
||||||
let s:easy_align_delimiters_default = {
|
let s:easy_align_delimiters_default = {
|
||||||
\ ' ': { 'pattern': ' ', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 },
|
\ ' ': { 'pattern': ' ', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 },
|
||||||
\ '=': { 'pattern': '===\|<=>\|\(&&\|||\|<<\|>>\)=\|=\~\|=>\|[:+/*!%^=><&|-]\?=',
|
\ '=': { 'pattern': '===\|<=>\|\(&&\|||\|<<\|>>\)=\|=\~\|=>\|[:+/*!%^=><&|-]\?=[#?]\?',
|
||||||
\ 'margin_left': ' ', 'margin_right': ' ', 'stick_to_left': 0 },
|
\ '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 },
|
||||||
\ ',': { '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]']
|
||||||
|
|
||||||
|
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)
|
function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, recursive)
|
||||||
let lines = {}
|
let lines = {}
|
||||||
let max_just_len = 0
|
let max_just_len = 0
|
||||||
let max_delim_len = 0
|
let max_delim_len = 0
|
||||||
let max_tokens = 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)
|
for line in range(a:fl, a:ll)
|
||||||
let tokens = split(a:lc ?
|
let tokens = split(a:lc ?
|
||||||
\ strpart(getline(line), a:fc - 1, a:lc - a:fc + 1) :
|
\ strpart(getline(line), a:fc - 1, a:lc - a:fc + 1) :
|
||||||
@@ -35,16 +46,27 @@ 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])
|
let tokens = extend([join(tokens[0:1], '')], tokens[2:-1])
|
||||||
endif
|
endif
|
||||||
let max_tokens = max([len(tokens), max_tokens])
|
let max_tokens = max([len(tokens), max_tokens])
|
||||||
|
if a:nth > 0
|
||||||
if len(tokens) < a:nth
|
if len(tokens) < a:nth
|
||||||
continue
|
continue
|
||||||
endif
|
endif
|
||||||
let nth = a:nth - 1 " 0-based
|
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 || nth == len(tokens)
|
||||||
|
continue
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
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], '') : '')
|
||||||
let token = substitute(last, pattern.'$', '', '')
|
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 match(last, pattern.'$') == -1
|
||||||
if a:just == 0 && (!exists("g:easy_align_ignore_unmatched") || g:easy_align_ignore_unmatched)
|
if a:just == 0 && (!exists("g:easy_align_ignore_unmatched") || g:easy_align_ignore_unmatched)
|
||||||
@@ -56,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]
|
let delim = matchlist(last, pattern)[1]
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let max_just_len = max([len(token.prefix), max_just_len])
|
let max_just_len = max([s:strwidth(token.prefix), max_just_len])
|
||||||
let max_delim_len = max([len(delim), max_delim_len])
|
let max_delim_len = max([s:strwidth(delim), max_delim_len])
|
||||||
let lines[line] = [prefix, token, delim, suffix]
|
let lines[line] = [prefix, token, delim, suffix]
|
||||||
endfor
|
endfor
|
||||||
|
|
||||||
for [line, tokens] in items(lines)
|
for [line, tokens] in items(lines)
|
||||||
let [prefix, token, delim, suffix] = tokens
|
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:just == 0
|
||||||
if a:stick_to_left
|
if a:stick_to_left
|
||||||
let suffix = pad . suffix
|
let suffix = pad . suffix
|
||||||
@@ -75,7 +97,7 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left,
|
|||||||
let token = pad . token
|
let token = pad . token
|
||||||
endif
|
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 cline = getline(line)
|
||||||
let before = strpart(cline, 0, a:fc - 1)
|
let before = strpart(cline, 0, a:fc - 1)
|
||||||
let after = a:lc ? strpart(cline, a:lc) : ''
|
let after = a:lc ? strpart(cline, a:lc) : ''
|
||||||
@@ -112,26 +134,36 @@ function! easy_align#align(just, ...) range
|
|||||||
let ch = nr2char(c)
|
let ch = nr2char(c)
|
||||||
if c == 3 || c == 27
|
if c == 3 || c == 27
|
||||||
return
|
return
|
||||||
|
elseif c == '<27>kb'
|
||||||
|
if len(n) > 0
|
||||||
|
let n = strpart(n, 0, len(n) - 1)
|
||||||
|
endif
|
||||||
elseif c == 13
|
elseif c == 13
|
||||||
let just = (just + 1) % len(s:just)
|
let just = (just + 1) % len(s:just)
|
||||||
elseif c >= 48 && c <= 57
|
elseif index(['-', '*'], ch) != -1
|
||||||
if n == '*'
|
if empty(n)
|
||||||
break
|
let n = ch
|
||||||
else
|
else
|
||||||
let n = n . nr2char(c)
|
break
|
||||||
endif
|
endif
|
||||||
elseif ch == '*'
|
elseif c == 48
|
||||||
if !empty(n)
|
if n == '-'
|
||||||
break
|
let n = '-0'
|
||||||
else
|
else
|
||||||
let n = '*'
|
break
|
||||||
|
endif
|
||||||
|
elseif c > 48 && c <= 57
|
||||||
|
if n != '*'
|
||||||
|
let n = n . ch
|
||||||
|
else
|
||||||
|
break
|
||||||
endif
|
endif
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
endif
|
endif
|
||||||
endwhile
|
endwhile
|
||||||
elseif a:0 == 1
|
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)
|
if empty(tokens)
|
||||||
echo "Invalid arguments: ". a:1
|
echo "Invalid arguments: ". a:1
|
||||||
return
|
return
|
||||||
@@ -148,9 +180,11 @@ 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 != '-0' && n != string(str2nr(n))
|
||||||
echon "\rInvalid field number: ". n
|
echon "\rInvalid field number: ". n
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|||||||
@@ -26,12 +26,15 @@ 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 (=, ==, !=, +=, &&=, ...)
|
||||||
: 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.
|
||||||
@@ -43,9 +46,11 @@ Examples:
|
|||||||
<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><Enter>= Right-justified alignment around 1st equals signs
|
<Enter><Enter>= Right-justified alignment around 1st equals signs
|
||||||
<Enter><space> Alignment around 1st whitespace
|
<Enter><space> Alignment around 1st whitespaces
|
||||||
<Enter>2<space> Alignment around 2nd whitespace
|
<Enter>2<space> Alignment around 2nd whitespaces
|
||||||
<Enter>: Alignment around 1st colon
|
<Enter>-<space> Alignment around the last whitespaces
|
||||||
|
<Enter>: Alignment around 1st colons
|
||||||
|
<Enter>-} Alignment around the last closing braces
|
||||||
|
|
||||||
|
|
||||||
EasyAlignRight *EasyAlignRight*
|
EasyAlignRight *EasyAlignRight*
|
||||||
@@ -61,7 +66,6 @@ In blockwise-visual mode (`CTRL-V`), EasyAlign command aligns only
|
|||||||
the selected parts, instead of the whole lines in the range.
|
the selected parts, instead of the whole lines in the range.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Defining custom alignment rules *g:easy_align_delimiters*
|
Defining custom alignment rules *g:easy_align_delimiters*
|
||||||
-------------------------------------------------------------------------
|
-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user