mirror of
https://github.com/junegunn/vim-easy-align.git
synced 2025-11-18 06:43:40 -05:00
Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a9f593fdf8 | ||
|
|
d87ea32cfe | ||
|
|
11a6b03e7e | ||
|
|
ba54df53ca | ||
|
|
d041e17bba | ||
|
|
2ab86911cb | ||
|
|
4929841a7a | ||
|
|
12792cd60f | ||
|
|
f7213f30f3 | ||
|
|
f44c325aa2 | ||
|
|
dc4786841e | ||
|
|
46abd8fb92 | ||
|
|
af39e544ca | ||
|
|
49c7133328 | ||
|
|
4298395085 | ||
|
|
e7fca89dde | ||
|
|
4b706efeb3 | ||
|
|
9bb987599a | ||
|
|
b8e93f96b8 | ||
|
|
6d85e93476 | ||
|
|
9271fc3f8f | ||
|
|
d3eec7fa8b | ||
|
|
62387b78e0 | ||
|
|
f400e4d8d1 | ||
|
|
9716fb3917 | ||
|
|
353ae9b071 | ||
|
|
ba4ba6e3c4 | ||
|
|
da284f908e | ||
|
|
8d4d5b7941 | ||
|
|
165bda1d95 | ||
|
|
ddc1e00b81 | ||
|
|
462a800064 | ||
|
|
3921d0bab3 | ||
|
|
ea1ea51ef1 |
13
.travis.yml
Normal file
13
.travis.yml
Normal file
@@ -0,0 +1,13 @@
|
||||
language: vim
|
||||
|
||||
before_script: |
|
||||
git clone https://github.com/junegunn/vader.vim.git
|
||||
|
||||
script: |
|
||||
vim -Nu <(cat << VIMRC
|
||||
filetype off
|
||||
set rtp+=vader.vim
|
||||
set rtp+=.
|
||||
set rtp+=after
|
||||
filetype plugin indent on
|
||||
VIMRC) -c 'Vader! test/*' > /dev/null
|
||||
177
README.md
177
README.md
@@ -8,7 +8,7 @@ Demo
|
||||
|
||||

|
||||
|
||||
(Too fast? Slower GIF is [here](https://raw.github.com/junegunn/i/master/vim-easy-align.gif))
|
||||
(Too fast? Slower GIF is [here](https://raw.github.com/junegunn/i/master/vim-easy-align-slow.gif))
|
||||
|
||||
Features
|
||||
--------
|
||||
@@ -49,11 +49,47 @@ and extract in ~/.vim or
|
||||
1. Add `Plug 'junegunn/vim-easy-align'` to .vimrc
|
||||
2. Run `:PlugInstall`
|
||||
|
||||
TL;DR - One-minute guide
|
||||
------------------------
|
||||
|
||||
Add the following mappings to your .vimrc.
|
||||
|
||||
```vim
|
||||
" Start interactive EasyAlign in visual mode
|
||||
vmap <Enter> <Plug>(EasyAlign)
|
||||
|
||||
" Start interactive EasyAlign with a Vim movement
|
||||
nmap <Leader>a <Plug>(EasyAlign)
|
||||
```
|
||||
|
||||
And with the following lines of text,
|
||||
|
||||
```
|
||||
apple =red
|
||||
grass+=green
|
||||
sky-= blue
|
||||
```
|
||||
|
||||
try these commands:
|
||||
|
||||
- `vip<Enter>=`
|
||||
- `v`isual-select `i`nner `p`aragraph
|
||||
- Start EasyAlign command (`<Enter>`)
|
||||
- Align around `=`
|
||||
- `<Leader>aip=`
|
||||
- Start EasyAlign command (`<Leader>a`) for `i`nner `p`aragraph
|
||||
- Align around `=`
|
||||
|
||||
Notice that the commands are repeatable with `.` key if you have installed
|
||||
[repeat.vim](https://github.com/tpope/vim-repeat). Install
|
||||
[visualrepeat](https://github.com/vim-scripts/visualrepeat) as well if you want
|
||||
to repeat in visual mode.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
_vim-easy-align_ defines `:EasyAlign` command (and the right-align
|
||||
variant `:EasyAlign!`) for visual mode.
|
||||
variant `:EasyAlign!`).
|
||||
|
||||
| Mode | Command |
|
||||
| ------------------------- | ------------------------------------------------ |
|
||||
@@ -65,7 +101,7 @@ variant `:EasyAlign!`) for visual mode.
|
||||
### Concept of _alignment rule_
|
||||
|
||||
An *alignment rule* is a predefined set of options for common alignment tasks,
|
||||
which is identified by a single character, *DELIMITER KEY*, such as `<space>`,
|
||||
which is identified by a single character, *DELIMITER KEY*, such as `<Space>`,
|
||||
`=`, `:`, `.`, `|`, `&`, and `,`.
|
||||
|
||||
Think of it as a shortcut. Instead of writing regular expression and setting
|
||||
@@ -74,18 +110,35 @@ several options, you can just type in a single character.
|
||||
### Interactive mode
|
||||
|
||||
The command will go into the interactive mode when no argument is given.
|
||||
For convenience, it is advised that you define a mapping for triggering it in
|
||||
your `.vimrc`.
|
||||
|
||||
```vim
|
||||
vnoremap <silent> <Enter> :EasyAlign<Enter>
|
||||
:EasyAlign
|
||||
```
|
||||
|
||||
(Of course you can use any key combination as the trigger. e.g. `<Leader>a`)
|
||||
However, it is strongly advised that you define mappings for triggering it in
|
||||
your `.vimrc` as follows. (Of course you can use any key combination as the
|
||||
trigger.)
|
||||
|
||||
```vim
|
||||
" For visual mode (e.g. vip<Enter>)
|
||||
vmap <Enter> <Plug>(EasyAlign)
|
||||
|
||||
" For normal mode, with Vim movement (e.g. <Leader>aip)
|
||||
nmap <Leader>a <Plug>(EasyAlign)
|
||||
```
|
||||
|
||||
The advantages of using the above mappings are two-fold:
|
||||
|
||||
- They require less keystrokes
|
||||
- They make alignments repeatable with
|
||||
[repeat.vim](https://github.com/tpope/vim-repeat) and
|
||||
[visualrepeat](https://github.com/vim-scripts/visualrepeat)
|
||||
|
||||
|
||||
With the mapping, you can align selected lines of text with only a few keystrokes.
|
||||
|
||||
1. `<Enter>` key to start interactive EasyAlign command
|
||||
1. `<Enter>` key in visual mode, or `<Leader>a` followed by a Vim movement to
|
||||
start interactive EasyAlign command
|
||||
1. Optional: Enter keys to select alignment mode (left, right, or center)
|
||||
1. Optional: N-th delimiter (default: 1)
|
||||
- `1` Around the 1st occurrences of delimiters
|
||||
@@ -96,19 +149,19 @@ With the mapping, you can align selected lines of text with only a few keystroke
|
||||
- `-` Around the last occurrences of delimiters (`-1`)
|
||||
- `-2` Around the second to last occurrences of delimiters
|
||||
- ...
|
||||
1. Delimiter key (a single keystroke; `<space>`, `=`, `:`, `.`, `|`, `&`, `,`)
|
||||
1. Delimiter key (a single keystroke; `<Space>`, `=`, `:`, `.`, `|`, `&`, `,`)
|
||||
|
||||
Alignment rules for the following delimiters have been defined to meet the most needs.
|
||||
|
||||
| Delimiter key | Description/Use cases |
|
||||
| ------------- | -------------------------------------------------------------------- |
|
||||
| `<space>` | General alignment around whitespaces |
|
||||
| `<Space>` | General alignment around whitespaces |
|
||||
| `=` | Operators containing equals sign (`=`, `==,` `!=`, `+=`, `&&=`, ...) |
|
||||
| `:` | Suitable for formatting JSON or YAML |
|
||||
| `.` | Multi-line method chaining |
|
||||
| `,` | Multi-line method arguments |
|
||||
| `&` | LaTeX tables (matches `&` and `\\`) |
|
||||
| | | Table markdown |
|
||||
| `<Bar>` | Table markdown |
|
||||
|
||||
You can override these default rules or define your own rules with
|
||||
`g:easy_align_delimiters`, which will be described in
|
||||
@@ -116,21 +169,21 @@ You can override these default rules or define your own rules with
|
||||
|
||||
#### Example command sequences
|
||||
|
||||
| With visual map | Description | Equivalent command |
|
||||
| ------------------- | ------------------------------------------------------- | ---------------------- |
|
||||
| `<Enter><space>` | Alignment around 1st whitespaces | `:'<,'>EasyAlign\ ` |
|
||||
| `<Enter>2<space>` | Alignment around 2nd whitespaces | `:'<,'>EasyAlign2\ ` |
|
||||
| `<Enter>-<space>` | Alignment around the last whitespaces | `:'<,'>EasyAlign-\ ` |
|
||||
| `<Enter>-2<space>` | Alignment around the 2nd to last whitespaces | `:'<,'>EasyAlign-2\ ` |
|
||||
| `<Enter>:` | Alignment around 1st colon (`key: value`) | `:'<,'>EasyAlign:` |
|
||||
| `<Enter><Right>:` | Alignment around 1st colon (`key : value`) | `:'<,'>EasyAlign:s0l1` |
|
||||
| `<Enter>=` | Alignment around 1st operators with = | `:'<,'>EasyAlign=` |
|
||||
| `<Enter>2=` | Alignment around 2nd operators with = | `:'<,'>EasyAlign2=` |
|
||||
| `<Enter>3=` | Alignment around 3rd operators with = | `:'<,'>EasyAlign3=` |
|
||||
| `<Enter>*=` | Alignment around all operators with = | `:'<,'>EasyAlign*=` |
|
||||
| `<Enter>**=` | Left-right alternating alignment around all = operators | `:'<,'>EasyAlign**=` |
|
||||
| `<Enter><Enter>=` | Right alignment around 1st equals signs | `:'<,'>EasyAlign!=` |
|
||||
| `<Enter><Enter>**=` | Right-left alternating alignment around all = operators | `:'<,'>EasyAlign!**=` |
|
||||
| With visual map | Description | Equivalent command |
|
||||
| ------------------- | ---------------------------------- | ---------------------- |
|
||||
| `<Enter><Space>` | Around 1st whitespaces | `:'<,'>EasyAlign\ ` |
|
||||
| `<Enter>2<Space>` | Around 2nd whitespaces | `:'<,'>EasyAlign2\ ` |
|
||||
| `<Enter>-<Space>` | Around the last whitespaces | `:'<,'>EasyAlign-\ ` |
|
||||
| `<Enter>-2<Space>` | Around the 2nd to last whitespaces | `:'<,'>EasyAlign-2\ ` |
|
||||
| `<Enter>:` | Around 1st colon (`key: value`) | `:'<,'>EasyAlign:` |
|
||||
| `<Enter><Right>:` | Around 1st colon (`key : value`) | `:'<,'>EasyAlign:s0l1` |
|
||||
| `<Enter>=` | Around 1st operators with = | `:'<,'>EasyAlign=` |
|
||||
| `<Enter>2=` | Around 2nd operators with = | `:'<,'>EasyAlign2=` |
|
||||
| `<Enter>3=` | Around 3rd operators with = | `:'<,'>EasyAlign3=` |
|
||||
| `<Enter>*=` | Around all operators with = | `:'<,'>EasyAlign*=` |
|
||||
| `<Enter>**=` | Left-right alternating around = | `:'<,'>EasyAlign**=` |
|
||||
| `<Enter><Enter>=` | Right alignment around 1st = | `:'<,'>EasyAlign!=` |
|
||||
| `<Enter><Enter>**=` | Right-left alternating around = | `:'<,'>EasyAlign!**=` |
|
||||
|
||||
#### Using regular expressions
|
||||
|
||||
@@ -150,7 +203,7 @@ keys listed below. The meaning of each option will be described in
|
||||
[the following sections](https://github.com/junegunn/vim-easy-align#alignment-options).
|
||||
|
||||
| Key | Option | Values |
|
||||
| -------- | ------------------ | -------------------------------------------------- |
|
||||
| --------- | ------------------ | -------------------------------------------------- |
|
||||
| `CTRL-F` | `filter` | Input string (`[gv]/.*/?`) |
|
||||
| `CTRL-I` | `indentation` | shallow, deep, none, keep |
|
||||
| `CTRL-L` | `left_margin` | Input number or string |
|
||||
@@ -170,6 +223,20 @@ repeatable, non-interactive command recorded in `g:easy_align_last_command`.
|
||||
:<C-R>=g:easy_align_last_command<Enter><Enter>
|
||||
```
|
||||
|
||||
### EasyAlign as Vim operator
|
||||
|
||||
With normal-mode map to `<Plug>(EasyAlign)`, EasyAlign command becomes a Vim
|
||||
operator that can be used with any Vim movement.
|
||||
|
||||
```vim
|
||||
nmap <Leader>a <Plug>(EasyAlign)
|
||||
```
|
||||
|
||||
Now without going into visual mode, you can align the lines in the paragraph
|
||||
with `<Leader>aip=`, `<Leader>aip*|`, or `<Leader>aip:`. And if you have
|
||||
installed [repeat.vim](https://github.com/tpope/vim-repeat) by Tim Pope, the
|
||||
exact alignment can be repeated with `.` key.
|
||||
|
||||
### Live interactive mode
|
||||
|
||||
If you're performing a complex alignment where multiple options should be
|
||||
@@ -177,11 +244,12 @@ carefully adjusted, try "live interactive mode" where you can preview the result
|
||||
of the alignment on-the-fly as you type in.
|
||||
|
||||
Live interactive mode can be started with `:LiveEasyAlign` command which takes
|
||||
the same parameters as `:EasyAlign`. I suggest you define a mapping such as
|
||||
follows in addition to the one for `:EasyAlign` command.
|
||||
the same parameters as `:EasyAlign`. I suggest you define mappings such as
|
||||
follows in addition to the ones for `:EasyAlign` command.
|
||||
|
||||
```vim
|
||||
vnoremap <silent> <Leader><Enter> :LiveEasyAlign<Enter>
|
||||
vmap <Leader><Enter> <Plug>(LiveEasyAlign)
|
||||
nmap <Leader><Leader>a <Plug>(LiveEasyAlign)
|
||||
```
|
||||
|
||||
In live interactive mode, you have to type in the same delimiter (or `CTRL-X` on
|
||||
@@ -241,17 +309,17 @@ notation.
|
||||
|
||||
The following table summarizes the shorthand notation.
|
||||
|
||||
| Option | Expression |
|
||||
| ---------------- | ---------- |
|
||||
| filter | `[gv]/.*/` |
|
||||
| left_margin | `l[0-9]+` |
|
||||
| right_margin | `r[0-9]+` |
|
||||
| stick_to_left | `s[01]` |
|
||||
| ignore_unmatched | `iu[01]` |
|
||||
| ignore_groups | `ig\[.*\]` |
|
||||
| delimiter_align | `d[lrc]` |
|
||||
| mode_sequence | `m[lrc*]*` |
|
||||
| indentation | `i[ksdn]` |
|
||||
| Option | Expression |
|
||||
| ------------------ | ---------- |
|
||||
| `filter` | `[gv]/.*/` |
|
||||
| `left_margin` | `l[0-9]+` |
|
||||
| `right_margin` | `r[0-9]+` |
|
||||
| `stick_to_left` | `s[01]` |
|
||||
| `ignore_unmatched` | `iu[01]` |
|
||||
| `ignore_groups` | `ig\[.*\]` |
|
||||
| `delimiter_align` | `d[lrc]` |
|
||||
| `mode_sequence` | `m[lrc*]*` |
|
||||
| `indentation` | `i[ksdn]` |
|
||||
|
||||
For your information, the same thing can be done in the interactive mode as well
|
||||
with the following key combination.
|
||||
@@ -289,25 +357,6 @@ my_hash = { :a => 1,
|
||||
However, in this case, we don't really need blockwise visual mode
|
||||
since the same can be easily done using the negative N-th parameter: `<Enter>-=`
|
||||
|
||||
### EasyAlign as Vim operator
|
||||
|
||||
You can define an operator function which executes EasyAlign command, so that it
|
||||
can be used with a Vim movement.
|
||||
|
||||
```vim
|
||||
function! s:easy_align_1st_eq(type, ...)
|
||||
'[,']EasyAlign=
|
||||
endfunction
|
||||
nnoremap <Leader>= :set opfunc=<SID>easy_align_1st_eq<Enter>g@
|
||||
|
||||
function! s:easy_align_1st_colon(type, ...)
|
||||
'[,']EasyAlign:
|
||||
endfunction
|
||||
nnoremap <Leader>: :set opfunc=<SID>easy_align_1st_colon<Enter>g@
|
||||
```
|
||||
|
||||
Now without going into visual mode, you can align the lines in the paragraph
|
||||
by `<Leader>=ip` or `<Leader>:ip`.
|
||||
|
||||
Alignment options
|
||||
-----------------
|
||||
@@ -576,8 +625,6 @@ daisy = 4
|
||||
eggplant = 5
|
||||
```
|
||||
|
||||
Notice that `idt` is fuzzy-matched to `indentation`.
|
||||
|
||||
In interactive mode, you can change the option value with `CTRL-I` key.
|
||||
|
||||
### Alignments over multiple occurrences of delimiters
|
||||
@@ -647,9 +694,9 @@ Although the default rules should cover the most of the use cases,
|
||||
you can extend the rules by setting a dictionary named `g:easy_align_delimiters`.
|
||||
|
||||
You may refer to the definitions of the default alignment rules
|
||||
[here](https://github.com/junegunn/vim-easy-align/blob/2.6.1/autoload/easy_align.vim#L29).
|
||||
[here](https://github.com/junegunn/vim-easy-align/blob/2.9.0/autoload/easy_align.vim#L29).
|
||||
|
||||
#### Example
|
||||
#### Examples
|
||||
|
||||
```vim
|
||||
let g:easy_align_delimiters = {
|
||||
@@ -745,7 +792,7 @@ It even feels like a native Vim command!
|
||||
|
||||
- *Right-align*: `<Enter><Enter>`
|
||||
- around the *second* occurrences: `2`
|
||||
- of *whitespaces*: `<space>`
|
||||
- of *whitespaces*: `<Space>`
|
||||
|
||||
For the simplest cases, Tabular and Align are also easy to use. But sooner or
|
||||
later, you will find yourself scratching your head, trying to come up with some
|
||||
|
||||
@@ -26,6 +26,9 @@ if exists("g:loaded_easy_align")
|
||||
endif
|
||||
let g:loaded_easy_align = 1
|
||||
|
||||
let s:cpo_save = &cpo
|
||||
set cpo&vim
|
||||
|
||||
let s:easy_align_delimiters_default = {
|
||||
\ ' ': { 'pattern': ' ', 'left_margin': 0, 'right_margin': 0, 'stick_to_left': 0 },
|
||||
\ '=': { 'pattern': '===\|<=>\|\(&&\|||\|<<\|>>\)=\|=\~[#?]\?\|=>\|[:+/*!%^=><&|.-]\?=[#?]\?',
|
||||
@@ -64,9 +67,9 @@ let s:shorthand = {
|
||||
\ 'mode_sequence': 'm', 'ignores': 'ig', 'filter': 'f'
|
||||
\ }
|
||||
|
||||
if exists("*strwidth")
|
||||
if exists("*strdisplaywidth")
|
||||
function! s:strwidth(str)
|
||||
return strwidth(a:str) + len(matchstr(a:str, '^\t*')) * (&tabstop - 1)
|
||||
return strdisplaywidth(a:str)
|
||||
endfunction
|
||||
else
|
||||
function! s:strwidth(str)
|
||||
@@ -74,6 +77,14 @@ else
|
||||
endfunction
|
||||
endif
|
||||
|
||||
function! s:ceil2(v)
|
||||
return a:v % 2 == 0 ? a:v : a:v + 1
|
||||
endfunction
|
||||
|
||||
function! s:floor2(v)
|
||||
return a:v % 2 == 0 ? a:v : a:v - 1
|
||||
endfunction
|
||||
|
||||
function! s:highlighted_as(line, col, groups)
|
||||
if empty(a:groups) | return 0 | endif
|
||||
let hl = synIDattr(synID(a:line, a:col, 0), 'name')
|
||||
@@ -332,7 +343,7 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r
|
||||
let mode = a:modes[0]
|
||||
let lines = {}
|
||||
let min_indent = -1
|
||||
let max = { 'pivot_len': 0.0, 'token_len': 0, 'just_len': 0, 'delim_len': 0,
|
||||
let max = { 'pivot_len2': 0, 'token_len': 0, 'just_len': 0, 'delim_len': 0,
|
||||
\ 'indent': 0, 'tokens': 0, 'strip_len': 0 }
|
||||
let d = a:dict
|
||||
let [f, fx] = s:parse_filter(d.filter)
|
||||
@@ -410,8 +421,9 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r
|
||||
let max.delim_len = max([max.delim_len, s:strwidth(delim)])
|
||||
|
||||
if mode ==? 'c'
|
||||
if max.pivot_len < pw + tw / 2.0
|
||||
let max.pivot_len = pw + tw / 2.0
|
||||
let pivot_len2 = pw * 2 + tw
|
||||
if max.pivot_len2 < pivot_len2
|
||||
let max.pivot_len2 = pivot_len2
|
||||
endif
|
||||
let max.strip_len = max([max.strip_len, s:strwidth(s:trim(token))])
|
||||
endif
|
||||
@@ -432,9 +444,9 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r
|
||||
end
|
||||
|
||||
if idt !=? 'k'
|
||||
let max.just_len = 0
|
||||
let max.token_len = 0
|
||||
let max.pivot_len = 0
|
||||
let max.just_len = 0
|
||||
let max.token_len = 0
|
||||
let max.pivot_len2 = 0
|
||||
|
||||
for [line, elems] in items(lines)
|
||||
let [nth, prefix, token, delim] = elems
|
||||
@@ -460,8 +472,9 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r
|
||||
let max.token_len = max([max.token_len, tw])
|
||||
let max.just_len = max([max.just_len, pw + tw])
|
||||
if mode ==? 'c'
|
||||
if max.pivot_len < pw + tw / 2.0
|
||||
let max.pivot_len = pw + tw / 2.0
|
||||
let pivot_len2 = pw * 2 + tw
|
||||
if max.pivot_len2 < pivot_len2
|
||||
let max.pivot_len2 = pivot_len2
|
||||
endif
|
||||
endif
|
||||
|
||||
@@ -496,15 +509,15 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r
|
||||
let indent = matchstr(token, '^\s*')
|
||||
let token = indent . pad . s:ltrim(token)
|
||||
elseif mode ==? 'c'
|
||||
let p1 = max.pivot_len - (pw + tw / 2.0)
|
||||
let p2 = (max.token_len - tw) / 2.0
|
||||
let pf1 = floor(p1)
|
||||
if pf1 < p1 | let p2 = ceil(p2)
|
||||
else | let p2 = floor(p2)
|
||||
let p1 = max.pivot_len2 - (pw * 2 + tw)
|
||||
let p2 = max.token_len - tw
|
||||
let pf1 = s:floor2(p1)
|
||||
if pf1 < p1 | let p2 = s:ceil2(p2)
|
||||
else | let p2 = s:floor2(p2)
|
||||
endif
|
||||
let strip = float2nr(ceil((max.token_len - max.strip_len) / 2.0))
|
||||
let strip = s:ceil2(max.token_len - max.strip_len) / 2
|
||||
let indent = matchstr(token, '^\s*')
|
||||
let token = indent. repeat(' ', float2nr(pf1)) .s:ltrim(token). repeat(' ', float2nr(p2))
|
||||
let token = indent. repeat(' ', pf1 / 2) .s:ltrim(token). repeat(' ', p2 / 2)
|
||||
let token = substitute(token, repeat(' ', strip) . '$', '', '')
|
||||
|
||||
if d.stick_to_left
|
||||
@@ -546,7 +559,7 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r
|
||||
" Adjust indentation of the lines starting with a delimiter
|
||||
let lpad = ''
|
||||
if nth == 0
|
||||
let ipad = repeat(' ', min_indent - len(token.ml))
|
||||
let ipad = repeat(' ', min_indent - s:strwidth(token.ml))
|
||||
if mode ==? 'l'
|
||||
let token = ipad . token
|
||||
else
|
||||
@@ -1041,10 +1054,16 @@ function s:summarize(opts, recur, mode_sequence)
|
||||
return copts
|
||||
endfunction
|
||||
|
||||
function! s:align(bang, live, first_line, last_line, expr)
|
||||
function! s:align(bang, live, visualmode, first_line, last_line, expr)
|
||||
" Heuristically determine if the user was in visual mode
|
||||
let vis = a:first_line == line("'<") && a:last_line == line("'>")
|
||||
let bvis = vis && char2nr(visualmode()) == 22 " ^V
|
||||
if empty(a:visualmode)
|
||||
let vis = a:first_line == line("'<") && a:last_line == line("'>")
|
||||
let bvis = vis && visualmode() == "\<C-V>"
|
||||
" Visual-mode explicitly given
|
||||
else
|
||||
let vis = 1
|
||||
let bvis = a:visualmode == "\<C-V>"
|
||||
end
|
||||
let range = [a:first_line, a:last_line]
|
||||
let modes = s:interactive_modes(a:bang)
|
||||
let mode = modes[0]
|
||||
@@ -1080,10 +1099,13 @@ function! s:align(bang, live, first_line, last_line, expr)
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
function! easy_align#align(bang, live, expr) range
|
||||
function! easy_align#align(bang, live, visualmode, expr) range
|
||||
try
|
||||
call s:align(a:bang, a:live, a:firstline, a:lastline, a:expr)
|
||||
call s:align(a:bang, a:live, a:visualmode, a:firstline, a:lastline, a:expr)
|
||||
catch 'exit'
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
let &cpo = s:cpo_save
|
||||
unlet s:cpo_save
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -26,5 +26,116 @@ if exists("g:loaded_easy_align_plugin")
|
||||
endif
|
||||
let g:loaded_easy_align_plugin = 1
|
||||
|
||||
command! -nargs=* -range -bang EasyAlign <line1>,<line2>call easy_align#align('<bang>' == '!', 0, <q-args>)
|
||||
command! -nargs=* -range -bang LiveEasyAlign <line1>,<line2>call easy_align#align('<bang>' == '!', 1, <q-args>)
|
||||
command! -nargs=* -range -bang EasyAlign <line1>,<line2>call easy_align#align('<bang>' == '!', 0, '', <q-args>)
|
||||
command! -nargs=* -range -bang LiveEasyAlign <line1>,<line2>call easy_align#align('<bang>' == '!', 1, '', <q-args>)
|
||||
|
||||
let s:last_command = 'EasyAlign'
|
||||
|
||||
function! s:abs(v)
|
||||
return a:v >= 0 ? a:v : - a:v
|
||||
endfunction
|
||||
|
||||
function! s:remember_visual(mode)
|
||||
let s:last_visual = [a:mode, s:abs(line("'>") - line("'<")), s:abs(col("'>") - col("'<"))]
|
||||
endfunction
|
||||
|
||||
function! s:repeat_visual()
|
||||
let [mode, ldiff, cdiff] = s:last_visual
|
||||
let cmd = 'normal! '.mode
|
||||
if ldiff > 0
|
||||
let cmd .= ldiff . 'j'
|
||||
endif
|
||||
|
||||
let ve_save = &virtualedit
|
||||
try
|
||||
if mode == "\<C-V>"
|
||||
if cdiff > 0
|
||||
let cmd .= cdiff . 'l'
|
||||
endif
|
||||
set virtualedit+=block
|
||||
endif
|
||||
execute cmd.":\<C-r>=g:easy_align_last_command\<Enter>\<Enter>"
|
||||
call s:set_repeat()
|
||||
finally
|
||||
if ve_save != &virtualedit
|
||||
let &virtualedit = ve_save
|
||||
endif
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
function! s:repeat_in_visual()
|
||||
if exists('g:easy_align_last_command')
|
||||
call s:remember_visual(visualmode())
|
||||
call s:repeat_visual()
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:set_repeat()
|
||||
silent! call repeat#set("\<Plug>(EasyAlignRepeat)")
|
||||
endfunction
|
||||
|
||||
function! s:generic_easy_align_op(type, vmode, live)
|
||||
let sel_save = &selection
|
||||
let &selection = "inclusive"
|
||||
|
||||
if a:vmode
|
||||
let vmode = a:type
|
||||
call s:remember_visual(vmode)
|
||||
else
|
||||
let tail = "\<C-c>"
|
||||
if a:type == 'line'
|
||||
silent execute "normal! '[V']".tail
|
||||
elseif a:type == 'block'
|
||||
silent execute "normal! `[\<C-V>`]".tail
|
||||
else
|
||||
silent execute "normal! `[v`]".tail
|
||||
endif
|
||||
let vmode = ''
|
||||
unlet! s:last_visual
|
||||
endif
|
||||
|
||||
try
|
||||
if get(g:, 'easy_align_need_repeat', 0)
|
||||
execute "'<,'>". g:easy_align_last_command
|
||||
else
|
||||
'<,'>call easy_align#align('<bang>' == '!', a:live, vmode, '')
|
||||
end
|
||||
call s:set_repeat()
|
||||
finally
|
||||
let &selection = sel_save
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
function! s:easy_align_op(type, ...)
|
||||
call s:generic_easy_align_op(a:type, a:0, 0)
|
||||
endfunction
|
||||
|
||||
function! s:live_easy_align_op(type, ...)
|
||||
call s:generic_easy_align_op(a:type, a:0, 1)
|
||||
endfunction
|
||||
|
||||
function! s:easy_align_repeat()
|
||||
if exists('s:last_visual')
|
||||
call s:repeat_visual()
|
||||
else
|
||||
try
|
||||
let g:easy_align_need_repeat = 1
|
||||
normal! .
|
||||
finally
|
||||
unlet! g:easy_align_need_repeat
|
||||
endtry
|
||||
endif
|
||||
endfunction
|
||||
|
||||
nnoremap <silent> <Plug>(EasyAlign) :set opfunc=<SID>easy_align_op<Enter>g@
|
||||
vnoremap <silent> <Plug>(EasyAlign) :<C-U>call <SID>easy_align_op(visualmode(), 1)<Enter>
|
||||
nnoremap <silent> <Plug>(LiveEasyAlign) :set opfunc=<SID>live_easy_align_op<Enter>g@
|
||||
vnoremap <silent> <Plug>(LiveEasyAlign) :<C-U>call <SID>live_easy_align_op(visualmode(), 1)<Enter>
|
||||
|
||||
" vim-repeat support
|
||||
nnoremap <silent> <Plug>(EasyAlignRepeat) :call <SID>easy_align_repeat()<Enter>
|
||||
vnoremap <silent> <Plug>(EasyAlignRepeat) :<C-U>call <SID>repeat_in_visual()<Enter>
|
||||
|
||||
" Backward-compatibility (deprecated)
|
||||
nnoremap <silent> <Plug>(EasyAlignOperator) :set opfunc=<SID>easy_align_op<Enter>g@
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ Test cases for vim-easy-align
|
||||
|
||||
### Run
|
||||
|
||||
```vim
|
||||
:Vader*
|
||||
```
|
||||
./run
|
||||
```
|
||||
|
||||
|
||||
@@ -1,14 +1,4 @@
|
||||
Execute (Clean up test environment):
|
||||
Save g:easy_align_ignore_groups, g:easy_align_ignore_unmatched
|
||||
Save g:easy_align_indentation, g:easy_align_delimiter_align
|
||||
Save g:easy_align_interactive_modes, g:easy_align_bang_interactive_modes
|
||||
Save g:easy_align_delimiters
|
||||
|
||||
let g:easy_align_delimiters = {}
|
||||
silent! unlet g:easy_align_ignore_groups
|
||||
silent! unlet g:easy_align_ignore_unmatched
|
||||
silent! unlet g:easy_align_indentation
|
||||
silent! unlet g:easy_align_delimiter_align
|
||||
Include: include/setup.vader
|
||||
|
||||
Given (fruits):
|
||||
apple;:;;banana::cake
|
||||
@@ -161,5 +151,4 @@ Expect javascript:
|
||||
"user: pass": "r00t: pa55"
|
||||
};
|
||||
|
||||
Execute:
|
||||
Restore
|
||||
Include: include/teardown.vader
|
||||
|
||||
100
test/fixed.vader
100
test/fixed.vader
@@ -1,3 +1,5 @@
|
||||
Include: include/setup.vader
|
||||
|
||||
Given (Table):
|
||||
|a|b|c|d|
|
||||
| -|-|>-|-|
|
||||
@@ -40,3 +42,101 @@ Execute:
|
||||
Expect:
|
||||
a <|>b <|>c <|
|
||||
aa<|>bb<|>cc<|
|
||||
|
||||
Given (Tab-indented code (#20)):
|
||||
class MyUnitTest(unittest.TestCase):
|
||||
def test_base(self):
|
||||
n2f = {}
|
||||
n2v = {}
|
||||
f2v = {}
|
||||
n2gv = {}
|
||||
n2vt = {}
|
||||
|
||||
Execute:
|
||||
set tabstop=1
|
||||
%EasyAlign=
|
||||
|
||||
Expect:
|
||||
class MyUnitTest(unittest.TestCase):
|
||||
def test_base(self):
|
||||
n2f = {}
|
||||
n2v = {}
|
||||
f2v = {}
|
||||
n2gv = {}
|
||||
n2vt = {}
|
||||
|
||||
Execute:
|
||||
set tabstop=2
|
||||
%EasyAlign=
|
||||
|
||||
Expect:
|
||||
class MyUnitTest(unittest.TestCase):
|
||||
def test_base(self):
|
||||
n2f = {}
|
||||
n2v = {}
|
||||
f2v = {}
|
||||
n2gv = {}
|
||||
n2vt = {}
|
||||
|
||||
Execute:
|
||||
set tabstop=4
|
||||
%EasyAlign=
|
||||
|
||||
Expect:
|
||||
class MyUnitTest(unittest.TestCase):
|
||||
def test_base(self):
|
||||
n2f = {}
|
||||
n2v = {}
|
||||
f2v = {}
|
||||
n2gv = {}
|
||||
n2vt = {}
|
||||
|
||||
Execute:
|
||||
set tabstop=8
|
||||
%EasyAlign=
|
||||
|
||||
Expect:
|
||||
class MyUnitTest(unittest.TestCase):
|
||||
def test_base(self):
|
||||
n2f = {}
|
||||
n2v = {}
|
||||
f2v = {}
|
||||
n2gv = {}
|
||||
n2vt = {}
|
||||
|
||||
Execute:
|
||||
set tabstop=12
|
||||
%EasyAlign=
|
||||
|
||||
Expect:
|
||||
class MyUnitTest(unittest.TestCase):
|
||||
def test_base(self):
|
||||
n2f = {}
|
||||
n2v = {}
|
||||
f2v = {}
|
||||
n2gv = {}
|
||||
n2vt = {}
|
||||
|
||||
Given (Tab-indented code (#20)):
|
||||
class MyUnitTest(unittest.TestCase):
|
||||
def test_base(self):
|
||||
# n2f= {}
|
||||
## n2v= {}
|
||||
# f2v = {}
|
||||
## n2gv = {}
|
||||
# n2vt = {}
|
||||
|
||||
Execute:
|
||||
set tabstop=12
|
||||
%EasyAlign=
|
||||
|
||||
Expect:
|
||||
class MyUnitTest(unittest.TestCase):
|
||||
def test_base(self):
|
||||
# n2f = {}
|
||||
## n2v = {}
|
||||
# f2v = {}
|
||||
## n2gv = {}
|
||||
# n2vt = {}
|
||||
|
||||
Include: include/teardown.vader
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
Include: include/setup.vader
|
||||
|
||||
# It is currently possible that EasyAlign command incorrectly judges
|
||||
# that it was executed in block-wise visual mode
|
||||
Given:
|
||||
@@ -18,3 +20,5 @@ Do (TODO Workaround: reset visualmode() on error):
|
||||
Expect:
|
||||
a | b | c
|
||||
|
||||
Include: include/teardown.vader
|
||||
|
||||
|
||||
32
test/include/setup.vader
Normal file
32
test/include/setup.vader
Normal file
@@ -0,0 +1,32 @@
|
||||
Execute (Clean up test environment):
|
||||
Save g:easy_align_ignore_groups, g:easy_align_ignore_unmatched
|
||||
Save g:easy_align_indentation, g:easy_align_delimiter_align
|
||||
Save g:easy_align_interactive_modes, g:easy_align_bang_interactive_modes
|
||||
Save g:easy_align_delimiters, g:easy_align_bypass_fold
|
||||
Save &tabstop, mapleader
|
||||
|
||||
unlet! g:easy_align_ignore_groups
|
||||
unlet! g:easy_align_ignore_unmatched
|
||||
unlet! g:easy_align_indentation
|
||||
unlet! g:easy_align_delimiter_align
|
||||
unlet! g:easy_align_interactive_modes
|
||||
unlet! g:easy_align_bang_interactive_modes
|
||||
unlet! g:easy_align_bypass_fold
|
||||
|
||||
let g:easy_align_delimiters = {}
|
||||
let mapleader = ' '
|
||||
vnoremap <silent> r<Enter> :EasyAlign!<Enter>
|
||||
vnoremap <silent> <Leader>r<Enter> :LiveEasyAlign!<Enter>
|
||||
|
||||
" " Legacy
|
||||
" vnoremap <silent> <Enter> :EasyAlign<Enter>
|
||||
" vnoremap <silent> <Leader><Enter> :LiveEasyAlign<Enter>
|
||||
" nmap <leader>A <Plug>(EasyAlignOperator)
|
||||
|
||||
set ts=2
|
||||
|
||||
vmap <Enter> <Plug>(EasyAlign)
|
||||
vmap <leader><Enter> <Plug>(LiveEasyAlign)
|
||||
nmap <leader>A <Plug>(EasyAlign)
|
||||
vmap <leader>. <Plug>(EasyAlignRepeat)
|
||||
|
||||
3
test/include/teardown.vader
Normal file
3
test/include/teardown.vader
Normal file
@@ -0,0 +1,3 @@
|
||||
Execute:
|
||||
Restore
|
||||
|
||||
@@ -1,24 +1,4 @@
|
||||
Execute (Clean up test environment):
|
||||
Save g:easy_align_ignore_groups, g:easy_align_ignore_unmatched
|
||||
Save g:easy_align_indentation, g:easy_align_delimiter_align
|
||||
Save g:easy_align_interactive_modes, g:easy_align_bang_interactive_modes
|
||||
Save g:easy_align_delimiters
|
||||
Save mapleader
|
||||
|
||||
" TODO: revert after test
|
||||
silent! unlet g:easy_align_ignore_groups
|
||||
silent! unlet g:easy_align_ignore_unmatched
|
||||
silent! unlet g:easy_align_indentation
|
||||
silent! unlet g:easy_align_delimiter_align
|
||||
silent! unlet g:easy_align_interactive_modes
|
||||
silent! unlet g:easy_align_bang_interactive_modes
|
||||
|
||||
let g:easy_align_delimiters = {}
|
||||
let mapleader = ' '
|
||||
vnoremap <silent> <Enter> :EasyAlign<Enter>
|
||||
vnoremap <silent> r<Enter> :EasyAlign!<Enter>
|
||||
vnoremap <silent> <Leader><Enter> :LiveEasyAlign<Enter>
|
||||
vnoremap <silent> <Leader>r<Enter> :LiveEasyAlign!<Enter>
|
||||
Include: include/setup.vader
|
||||
|
||||
###########################################################
|
||||
|
||||
@@ -39,6 +19,15 @@ Expect:
|
||||
111 22 333 444 55555 6666 7 88888
|
||||
1111 2 33 444 555 66 777 8
|
||||
|
||||
Do (left-align using operator map):
|
||||
\<Space>Aip*\<Space>
|
||||
|
||||
Expect:
|
||||
1 22222 33 444 555 6666 7 888
|
||||
11 222 3333 4 55 6666 77 888
|
||||
111 22 333 444 55555 6666 7 88888
|
||||
1111 2 33 444 555 66 777 8
|
||||
|
||||
Do (right-align):
|
||||
vip
|
||||
\<Enter>\<Enter>
|
||||
@@ -1501,6 +1490,106 @@ Expect:
|
||||
'url': 'http://localhost:8090/display/TEST'}
|
||||
|
||||
###########################################################
|
||||
Execute:
|
||||
Restore
|
||||
|
||||
Given (Two paragraphs (requires vim-repeat)):
|
||||
a = 1
|
||||
bb = 2
|
||||
ccc = 3
|
||||
dddd = 4
|
||||
|
||||
d = 1
|
||||
cc = 2
|
||||
bbb = 3
|
||||
aaaa = 4
|
||||
_____ = 5
|
||||
|
||||
Do (Align and repeat):
|
||||
\<Space>Aip\<Enter>=
|
||||
6G
|
||||
.
|
||||
|
||||
Expect:
|
||||
a = 1
|
||||
bb = 2
|
||||
ccc = 3
|
||||
dddd = 4
|
||||
|
||||
d = 1
|
||||
cc = 2
|
||||
bbb = 3
|
||||
aaaa = 4
|
||||
_____ = 5
|
||||
|
||||
Do (Visual-mode operator is also repeatable):
|
||||
vip\<Enter>\<Enter>=
|
||||
6G
|
||||
.
|
||||
|
||||
Expect:
|
||||
a = 1
|
||||
bb = 2
|
||||
ccc = 3
|
||||
dddd = 4
|
||||
|
||||
d = 1
|
||||
cc = 2
|
||||
bbb = 3
|
||||
aaaa = 4
|
||||
_____ = 5
|
||||
|
||||
Do (Repeatable in visual mode):
|
||||
2GvG\<Space>.
|
||||
|
||||
Expect:
|
||||
a = 1
|
||||
bb = 2
|
||||
ccc = 3
|
||||
dddd = 4
|
||||
|
||||
d = 1
|
||||
cc = 2
|
||||
bbb = 3
|
||||
aaaa = 4
|
||||
_____ = 5
|
||||
|
||||
Given:
|
||||
:: a : 1
|
||||
:: bb : 2
|
||||
:: ccc : 3
|
||||
:: dd : 4
|
||||
:: e : 5
|
||||
|
||||
:: :: a:1
|
||||
:: :: b :2
|
||||
:: :: ccc : 3
|
||||
:: :: dd : 4
|
||||
:: :: e : 5
|
||||
:: :: f : 6
|
||||
|
||||
Do (Blockwise-visual-operator is also repeatable):
|
||||
fa
|
||||
\<C-V>
|
||||
f1
|
||||
4j
|
||||
\<Enter>:
|
||||
7G
|
||||
fa
|
||||
.
|
||||
|
||||
Expect:
|
||||
:: a: 1
|
||||
:: bb: 2
|
||||
:: ccc: 3
|
||||
:: dd: 4
|
||||
:: e: 5
|
||||
|
||||
:: :: a: 1
|
||||
:: :: b: 2
|
||||
:: :: ccc: 3
|
||||
:: :: dd: 4
|
||||
:: :: e: 5
|
||||
:: :: f : 6
|
||||
|
||||
###########################################################
|
||||
|
||||
Include: include/teardown.vader
|
||||
|
||||
11
test/run
Executable file
11
test/run
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd $(dirname $BASH_SOURCE)
|
||||
|
||||
vim -Nu <(cat << EOF
|
||||
syntax on
|
||||
for dep in ['vader.vim', 'vim-repeat']
|
||||
execute 'set rtp+=' . finddir(dep, expand('~/.vim').'/**')
|
||||
endfor
|
||||
set rtp+=..
|
||||
EOF) +Vader*
|
||||
@@ -1,8 +1,6 @@
|
||||
# http://en.wikibooks.org/wiki/LaTeX/Tables
|
||||
Execute:
|
||||
Save g:easy_align_delimiters, g:easy_align_bypass_fold
|
||||
let g:easy_align_delimiters = {}
|
||||
silent! unlet g:easy_align_bypass_fold
|
||||
|
||||
Include: include/setup.vader
|
||||
|
||||
Given tex (table with escaped &):
|
||||
\begin{tabular}{ l c r }
|
||||
@@ -157,5 +155,4 @@ Expect tex:
|
||||
8192 & 9.45\e2 & 0.0 & 4.51\e2 & 0.0 & & & & & & \\
|
||||
\end{tabular}
|
||||
|
||||
Execute:
|
||||
Restore
|
||||
Include: include/teardown.vader
|
||||
|
||||
Reference in New Issue
Block a user