Skip to content

Commit

Permalink
Implement g:easy_align_bypass_fold switch (#14) with a small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Oct 15, 2013
1 parent 37fa908 commit 831cecd
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 11 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,42 @@ let g:easy_align_delimiters = {
\ }
```

Other options
-------------

### Disabling &foldmethod during alignment

[It is reported](https://github.com/junegunn/vim-easy-align/issues/14) that
&foldmethod of `expr` or `syntax` can significantly slow down the alignment when
editing a large, complex file with many folds. To alleviate this issue,
EasyAlign provides an option to temporarily set &foldmethod to manual during the
alignment task. In order to enable this feature, set `g:easy_align_bypass_fold`
switch to 1.

```vim
let g:easy_align_bypass_fold = 1
```

### Left/right/center mode switch in interactive mode

In interactive mode, you can choose the alignment mode you want by pressing
enter keys. The non-bang command, `:EasyAlign` starts in left-alignment mode
and changes to right and center mode as you press enter keys, while the bang
version first starts in right-alignment mode.

- `:EasyAlign`
- Left, Right, Center
- `:EasyAlign!`
- Right, Left, Center

If you do not prefer this default mode transition, you can define your own
settings as follows.

```vim
let g:easy_align_interactive_modes = ['l', 'r']
let g:easy_align_bang_interactive_modes = ['c', 'r']
```

Advanced examples and use cases
-------------------------------

Expand Down
21 changes: 16 additions & 5 deletions autoload/easy_align.vim
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ function! s:split_line(line, nth, modes, cycle, fc, lc, pattern, stick_to_left,
" Phase 1: split
let ignorable = 0
let token = ''
let phantom = 0
while 1
let matches = matchlist(string, pattern, idx)
" No match
Expand Down Expand Up @@ -284,8 +285,7 @@ function! s:split_line(line, nth, modes, cycle, fc, lc, pattern, stick_to_left,
" If the string is non-empty and ends with the delimiter,
" append an empty token to the list
if idx == len(string)
call add(tokens, '')
call add(delims, '')
let phantom = 1
break
endif
endwhile
Expand All @@ -295,6 +295,9 @@ function! s:split_line(line, nth, modes, cycle, fc, lc, pattern, stick_to_left,
let ignorable = s:highlighted_as(a:line, len(string) + a:fc - 1, a:ignore_groups)
call add(tokens, leftover)
call add(delims, '')
elseif phantom
call add(tokens, '')
call add(delims, '')
endif
let [pmode, mode] = [mode, s:shift(a:modes, a:cycle)]

Expand Down Expand Up @@ -926,9 +929,17 @@ function! s:align(bang, first_line, last_line, expr)
while len(args) > 1
let args = call('s:do_align', args)
endwhile
for [line, content] in items(todo)
call setline(line, s:rtrim(content))
endfor

let bypass_fold = get(g:, 'easy_align_bypass_fold', 0)
let ofm = &l:foldmethod
try
if bypass_fold | let &l:foldmethod = 'manual' | endif
for [line, content] in items(todo)
call setline(line, s:rtrim(content))
endfor
finally
if bypass_fold | let &l:foldmethod = ofm | endif
endtry

let copts = s:compact_options(opts)
let nbmode = s:modes(0)[0]
Expand Down
11 changes: 11 additions & 0 deletions doc/easy_align.txt
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,14 @@ you can extend the rules by setting a dictionary named
\ }


Disabling &foldmethod during alignment *g:easy_align_bypass_fold*
-------------------------------------------------------------------------

It is reported that &foldmethod of expr or syntax can significantly slow
down the alignment when editing a large, complex file with many folds. To
alleviate this issue, EasyAlign provides an option to temporarily set
&foldmethod to manual during the alignment task. In order to enable this
feature, set g:easy_align_bypass_fold to 1.

let g:easy_align_bypass_fold = 1

7 changes: 7 additions & 0 deletions test/commandline.vader
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
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
Expand Down Expand Up @@ -155,3 +160,5 @@ Expect javascript:
"user: pass": "r00t: pa55"
};

Execute:
Restore
8 changes: 8 additions & 0 deletions test/fixme.vader
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ Do (FIXME invalid judgement - block-wise visual mode):
Expect:
a | b | c

Do (TODO Workaround: reset visualmode() on error):
\<C-V>\<Esc>
:%EasyAlign|\<CR>
:%EasyAlign|\<CR>

Expect:
a | b | c

7 changes: 7 additions & 0 deletions test/interactive.vader
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
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

" TODO: revert after test
silent! unlet g:easy_align_ignore_groups
silent! unlet g:easy_align_ignore_unmatched
Expand Down Expand Up @@ -1283,3 +1288,5 @@ Expect:
a = b = c
aabba = bbbbb

Execute:
Restore
75 changes: 69 additions & 6 deletions test/tex.vader
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 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

Given tex (table with escaped &):
\begin{tabular}{ l c r }
Expand All @@ -9,8 +11,9 @@ Given tex (table with escaped &):
777&8\&8&999\\
\end{tabular}

# FIXME vip doesn't work if folded
Do (Align around all &s and \\s):
vip\<Enter>*&
VG\<Enter>*&

Expect tex:
\begin{tabular}{ l c r }
Expand All @@ -20,7 +23,7 @@ Expect tex:
\end{tabular}

Do (right-align with explicit ignore_unmatched):
vip\<Enter>\<Enter>\<C-U>\<C-U>*&
VG\<Enter>\<Enter>\<C-U>\<C-U>*&

Expect tex:
\begin{tabular}{ l c r }
Expand All @@ -30,7 +33,7 @@ Expect tex:
\end{tabular}

Do (center-align with explicit ignore_unmatched):
vip\<Enter>\<Enter>\<Enter>\<C-U>\<C-U>*&
VG\<Enter>\<Enter>\<Enter>\<C-U>\<C-U>*&

Expect tex:
\begin{tabular}{ l c r }
Expand All @@ -47,7 +50,7 @@ Given tex (simple table with \hline):
\end{tabular}

Do:
vip\<Enter>*&
VG\<Enter>*&

Expect tex:
\begin{tabular}{ l c r }
Expand All @@ -68,7 +71,7 @@ Given tex (table with lines w/o &s):
\end{tabular}

Do (left-align*):
vip\<Enter>*&
VG\<Enter>*&

Expect tex:
\begin{tabular}{|r|l|}
Expand All @@ -82,7 +85,7 @@ Expect tex:
\end{tabular}

Do(left-align* and right-align around 2nd):
vip\<Enter>*&
VG\<Enter>*&
gv\<Enter>\<Enter>2&

Expect tex:
Expand All @@ -96,3 +99,63 @@ Expect tex:
\hline
\end{tabular}

Given tex:
\begin{tabular}{}
32&1.14\e1&&5.65\e2&&&&1.16\e1&&1.28\e1&\\
64&1.03\e1&0.1&4.98\e2&0.2&&&9.21\e2&0.3&1.02\e1&0.3\\
128&9.86\e2&0.1&4.69\e2&0.1&&&8.46\e2&0.1&9.45\e2&0.1\\
256&9.65\e2&0.0&4.59\e2&0.0&&&8.15\e2&0.1&9.11\e2&0.1\\
% 512&9.55\e2&0.0&4.56\e2&0.0&&&8.01\e2&0.0&8.96\e2&0.0\\
1024&9.49\e2&0.0&4.53\e2&0.0&&&7.94\e2&0.0&8.89\e2&0.0\\
2048&9.47\e2&0.0&4.52\e2&0.0&&&7.91\e2&0.0&8.85\e2&0.0\\
4096&9.46\e2&0.0&4.51\e2&0.0%&&&7.90\e2&0.0&8.83\e2&0.0\\
8192&9.45\e2&0.0&4.51\e2&0.0&&&&&&\\
\end{tabular}

Execute (Alignment around &s, foldmethod should not change):
setlocal foldmethod=syntax
%EasyAlign*&
AssertEqual 'syntax', &l:foldmethod

setlocal foldmethod=manual
%EasyAlign*&
AssertEqual 'manual', &l:foldmethod

Expect tex:
\begin{tabular}{}
32 & 1.14\e1 & & 5.65\e2 & & & & 1.16\e1 & & 1.28\e1 & \\
64 & 1.03\e1 & 0.1 & 4.98\e2 & 0.2 & & & 9.21\e2 & 0.3 & 1.02\e1 & 0.3 \\
128 & 9.86\e2 & 0.1 & 4.69\e2 & 0.1 & & & 8.46\e2 & 0.1 & 9.45\e2 & 0.1 \\
256 & 9.65\e2 & 0.0 & 4.59\e2 & 0.0 & & & 8.15\e2 & 0.1 & 9.11\e2 & 0.1 \\
% 512&9.55\e2&0.0&4.56\e2&0.0&&&8.01\e2&0.0&8.96\e2&0.0\\
1024 & 9.49\e2 & 0.0 & 4.53\e2 & 0.0 & & & 7.94\e2 & 0.0 & 8.89\e2 & 0.0 \\
2048 & 9.47\e2 & 0.0 & 4.52\e2 & 0.0 & & & 7.91\e2 & 0.0 & 8.85\e2 & 0.0 \\
4096 & 9.46\e2 & 0.0 & 4.51\e2 & 0.0%&&&7.90\e2&0.0&8.83\e2&0.0\\
8192 & 9.45\e2 & 0.0 & 4.51\e2 & 0.0 & & & & & & \\
\end{tabular}

Execute (g:easy_align_bypass_fold set, foldmethod should not change):
let g:easy_align_bypass_fold = 1
setlocal foldmethod=syntax
%EasyAlign*&
AssertEqual 'syntax', &l:foldmethod

setlocal foldmethod=manual
%EasyAlign*&
AssertEqual 'manual', &l:foldmethod

Expect tex:
\begin{tabular}{}
32 & 1.14\e1 & & 5.65\e2 & & & & 1.16\e1 & & 1.28\e1 & \\
64 & 1.03\e1 & 0.1 & 4.98\e2 & 0.2 & & & 9.21\e2 & 0.3 & 1.02\e1 & 0.3 \\
128 & 9.86\e2 & 0.1 & 4.69\e2 & 0.1 & & & 8.46\e2 & 0.1 & 9.45\e2 & 0.1 \\
256 & 9.65\e2 & 0.0 & 4.59\e2 & 0.0 & & & 8.15\e2 & 0.1 & 9.11\e2 & 0.1 \\
% 512&9.55\e2&0.0&4.56\e2&0.0&&&8.01\e2&0.0&8.96\e2&0.0\\
1024 & 9.49\e2 & 0.0 & 4.53\e2 & 0.0 & & & 7.94\e2 & 0.0 & 8.89\e2 & 0.0 \\
2048 & 9.47\e2 & 0.0 & 4.52\e2 & 0.0 & & & 7.91\e2 & 0.0 & 8.85\e2 & 0.0 \\
4096 & 9.46\e2 & 0.0 & 4.51\e2 & 0.0%&&&7.90\e2&0.0&8.83\e2&0.0\\
8192 & 9.45\e2 & 0.0 & 4.51\e2 & 0.0 & & & & & & \\
\end{tabular}

Execute:
Restore

0 comments on commit 831cecd

Please sign in to comment.