mirror of
https://github.com/junegunn/vim-easy-align.git
synced 2025-11-17 14:23:42 -05:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aad5012615 | ||
|
|
0166077e16 | ||
|
|
3c4b7af9b5 | ||
|
|
5e6e60d620 | ||
|
|
712bab8c72 | ||
|
|
0546969c6b | ||
|
|
8805ec3834 | ||
|
|
96608ee5ef | ||
|
|
7d031956ab | ||
|
|
c4dbfece32 | ||
|
|
a2811dc253 | ||
|
|
5e4ec85956 | ||
|
|
b3281fb19a | ||
|
|
10a4176159 |
173
EXAMPLES.md
173
EXAMPLES.md
@@ -1,7 +1,9 @@
|
||||
vim-easy-align examples
|
||||
=======================
|
||||
|
||||
The description in this document assumes that you have defined this mapping.
|
||||
Open this document in your Vim and try it yourself.
|
||||
|
||||
This document assumes that you have defined the following mapping.
|
||||
|
||||
```vim
|
||||
vnoremap <silent> <Enter> :EasyAlign<cr>
|
||||
@@ -12,20 +14,17 @@ function.
|
||||
|
||||
```vim
|
||||
function! GFM()
|
||||
let syntaxes = {
|
||||
\ 'ruby': 'syntax/ruby.vim',
|
||||
\ 'yaml': 'syntax/yaml.vim',
|
||||
\ 'vim': 'syntax/vim.vim',
|
||||
\ 'c': 'syntax/c.vim'
|
||||
\ }
|
||||
let langs = ['ruby', 'yaml', 'vim', 'c']
|
||||
|
||||
for [lang, syn] in items(syntaxes)
|
||||
for lang in langs
|
||||
unlet b:current_syntax
|
||||
silent! exec printf("syntax include @%s %s", lang, syn)
|
||||
silent! exec printf("syntax include @%s syntax/%s.vim", lang, lang)
|
||||
exec printf("syntax region %sSnip matchgroup=Snip start='```%s' end='```' contains=@%s",
|
||||
\ lang, lang, lang)
|
||||
endfor
|
||||
let b:current_syntax='mkd'
|
||||
|
||||
syntax sync fromstart
|
||||
endfunction
|
||||
```
|
||||
|
||||
@@ -130,7 +129,6 @@ Try `<Enter>:` here, to align text around only the first occurrences of colons.
|
||||
In this case, you don't want to align around all the colons: `<Enter>*:`.
|
||||
|
||||
```yaml
|
||||
|
||||
mysql:
|
||||
# JDBC driver for MySQL database:
|
||||
driver: com.mysql.jdbc.Driver
|
||||
@@ -138,7 +136,6 @@ mysql:
|
||||
url: jdbc:mysql://localhost/test
|
||||
database: test
|
||||
"user:pass":r00t:pa55
|
||||
|
||||
```
|
||||
|
||||
Formatting multi-line method chaining
|
||||
@@ -166,39 +163,35 @@ my_object
|
||||
```
|
||||
|
||||
|
||||
Partial alignment in block-visual mode / Negative field index
|
||||
-------------------------------------------------------------
|
||||
Using blockwise-visual mode or negative field index
|
||||
---------------------------------------------------
|
||||
|
||||
You can try one of these:
|
||||
- Select text around `=>` in block-wise visual mode (`<Ctrl>-V`) and `<Enter>=`
|
||||
- `<Enter>-=`
|
||||
You can try either:
|
||||
- select text around `=>` in blockwise-visual mode (`CTRL-V`) and `<Enter>=`
|
||||
- or `<Enter>-=`
|
||||
|
||||
```ruby
|
||||
|
||||
options = { :caching => nil,
|
||||
:versions => 3,
|
||||
"cache=blocks" => false }.merge(options)
|
||||
|
||||
```
|
||||
|
||||
Commas
|
||||
------
|
||||
|
||||
There is also a predefined rule for commas, try `<Enter>*,` for the following
|
||||
There is also a predefined rule for commas, try `<Enter>*,` on the following
|
||||
lines.
|
||||
|
||||
```
|
||||
|
||||
aaa, bb,c
|
||||
d,eeeeeee
|
||||
fffff, gggggggggg,
|
||||
h, , ii
|
||||
j,,k
|
||||
|
||||
```
|
||||
|
||||
Ignoring delimiters in comments and strings
|
||||
-------------------------------------------
|
||||
Ignoring delimiters in comments or strings
|
||||
------------------------------------------
|
||||
|
||||
Delimiters highlighted as comments or strings are ignored by default, try
|
||||
`<Enter>*=` on the following lines.
|
||||
@@ -213,6 +206,8 @@ aaaaa /* bbbbb */ == ccccc /* != eeeee = */ === fffff
|
||||
|
||||
```
|
||||
|
||||
This only works when syntax highlighting is enabled.
|
||||
|
||||
Aligning in-line comments
|
||||
-------------------------
|
||||
|
||||
@@ -222,10 +217,10 @@ banana = 'Gros Michel' # comment 2
|
||||
```
|
||||
|
||||
So, how do we align the trailing comments in the above lines?
|
||||
Simply try `<Enter>-<space>`! The spaces in the comments are ignored, so the
|
||||
Simply try `<Enter>-<space>`. The spaces in the comments are ignored, so the
|
||||
trailing comment in each line is considered to be a single chunk.
|
||||
|
||||
But this doesn't work in the following case.
|
||||
But that doesn't work in the following case.
|
||||
|
||||
```ruby
|
||||
apple = 1 # comment not aligned
|
||||
@@ -234,7 +229,7 @@ banana = 'Gros Michel' # comment 2
|
||||
```
|
||||
|
||||
That is because the second line doesn't have trailing comment, and
|
||||
the last space (`-`) for that line is the one just before `'F#AD'`.
|
||||
the last (`-`) space for that line is the one just before `'F#AD'`.
|
||||
|
||||
So, let's define a custom mapping for `#`.
|
||||
|
||||
@@ -256,7 +251,7 @@ apricot = 'DAD' + 'F#AD'
|
||||
banana = 'string' # comment 2
|
||||
```
|
||||
|
||||
If you don't want to define a rule, you can do the same with the following
|
||||
If you don't want to define the rule, you can do the same with the following
|
||||
command:
|
||||
|
||||
```vim
|
||||
@@ -265,17 +260,29 @@ command:
|
||||
:EasyAlign/#/{'is':['String']}
|
||||
```
|
||||
|
||||
In this case, the second line is ignored as it doesn't contain `#`. (The one
|
||||
In this case, the second line is ignored as it doesn't contain a `#`. (The one
|
||||
highlighted as String is ignored.) If you don't want the second line to be
|
||||
ignored, set `g:easy_align_ignore_unmatched` to 0, or use the following
|
||||
commands:
|
||||
ignored, there are three options:
|
||||
|
||||
1. Set global `g:easy_align_ignore_unmatched` flag to 0
|
||||
2. Use `:EasyAlign` command with `ignore_unmatched` option
|
||||
3. Update the alignment rule with `ignore_unmatched` option
|
||||
|
||||
```vim
|
||||
" Using predefined rule with delimiter key #
|
||||
:EasyAlign#{'iu':0}`
|
||||
" 1. Set global g:easy_align_ignore_unmatched to zero
|
||||
let g:easy_align_ignore_unmatched = 0
|
||||
|
||||
" Using regular expression /#/
|
||||
:EasyAlign/#/{'is':['String'],'iu':0}`
|
||||
" 2. Using :EasyAlign command with ignore_unmatched option
|
||||
" 2-1. Using predefined rule with delimiter key #
|
||||
" - "iu" is fuzzy-matched to "*i*gnore_*u*nmatched"
|
||||
:EasyAlign#{'iu':0}
|
||||
|
||||
" 2-2. Using regular expression /#/
|
||||
:EasyAlign/#/{'is':['String'],'iu':0}
|
||||
|
||||
" 3. Update the alignment rule with ignore_unmatched option
|
||||
let g:easy_align_delimiters['#'] = {
|
||||
\ 'pattern': '#', 'ignores': ['String'], 'ignore_unmatched': 0 } }
|
||||
```
|
||||
|
||||
Then we get,
|
||||
@@ -286,3 +293,101 @@ apricot = 'DAD' + 'F#AD'
|
||||
banana = 'string' # comment 2
|
||||
```
|
||||
|
||||
Aligning C-style variable definition
|
||||
------------------------------------
|
||||
|
||||
Take the following example:
|
||||
|
||||
```c
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
```
|
||||
|
||||
We can align these lines with the predefined `=` rule. Select the lines and
|
||||
press `<Enter>=`
|
||||
|
||||
```c
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
```
|
||||
|
||||
Not bad. However, the names of the variables, `str`, `count`, and `pi` are not
|
||||
aligned with each other. Can we do better? We can clearly see that simple
|
||||
`<Enter><space>` won't properly align those names.
|
||||
So let's define an alignment rule than can handle this case.
|
||||
|
||||
```vim
|
||||
let g:easy_align_delimiters['d'] = {
|
||||
\ 'pattern': '\(const\|static\)\@<! ',
|
||||
\ 'left_margin': 0, 'right_margin': 0
|
||||
\ }
|
||||
```
|
||||
|
||||
This new rule aligns text around spaces that are *not* preceded by
|
||||
`const` or `static`. Let's try it with `<Enter>d`.
|
||||
|
||||
```c
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
```
|
||||
|
||||
Okay, the names are now aligned. We select the lines again with `gv`, and then
|
||||
press `<Enter>=` to finish our alignment.
|
||||
|
||||
```c
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
```
|
||||
|
||||
So far, so good. However, this rule is not sufficient to handle more complex
|
||||
cases involving C++ templates or Java generics. Take the following example:
|
||||
|
||||
```c
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
static std::map<std::string, float>* scores = pointer;
|
||||
```
|
||||
|
||||
We see that our rule above doesn't work anymore.
|
||||
|
||||
```c
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
static std::map<std::string, float>* scores = pointer;
|
||||
```
|
||||
|
||||
So what do we do? Let's try to improve our alignment rule.
|
||||
|
||||
```vim
|
||||
let g:easy_align_delimiters['d'] = {
|
||||
\ 'pattern': ' \(\S\+\s*[;=]\)\@=',
|
||||
\ 'left_margin': 0, 'right_margin': 0
|
||||
\ }
|
||||
```
|
||||
|
||||
Now the new rule has changed to align text around spaces that are followed
|
||||
by some non-whitespace characters and then an equals sign or a semi-colon.
|
||||
Try `<Enter>d`
|
||||
|
||||
```c
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
static std::map<std::string, float>* scores = pointer;
|
||||
```
|
||||
|
||||
We're right on track, now press `gv<Enter>=` and voila!
|
||||
|
||||
```c
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
static std::map<std::string, float>* scores = pointer;
|
||||
```
|
||||
|
||||
|
||||
151
README.md
151
README.md
@@ -11,13 +11,14 @@ Demo
|
||||
Features
|
||||
--------
|
||||
|
||||
- Makes the common case easy
|
||||
- Comes with a predefined set of alignment rules
|
||||
- Provides a fast and intuitive interface
|
||||
- Extensible
|
||||
- You can define your own rules
|
||||
- Supports arbitrary regular expressions
|
||||
- Optimized for code editing
|
||||
- Designed to require minimal keystrokes
|
||||
- Extensible alignment rules
|
||||
- Supports arbitrary regular expressions
|
||||
- Aligns text around either _all or n-th_ occurrence(s) of the delimiter
|
||||
- Ignores delimiters in certain syntax highlight groups (e.g. comments, strings)
|
||||
- Ignores lines without a matching delimiter
|
||||
- Takes advantage of syntax highlighting feature to avoid unwanted alignments
|
||||
|
||||
Installation
|
||||
------------
|
||||
@@ -28,10 +29,14 @@ or [Pathogen](https://github.com/tpope/vim-pathogen).
|
||||
|
||||
### With Vundle
|
||||
|
||||
Add the following line to your .vimrc,
|
||||
|
||||
```vim
|
||||
Bundle 'junegunn/vim-easy-align'
|
||||
```
|
||||
|
||||
then execute `:BundleInstall` command.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
@@ -54,7 +59,7 @@ your `.vimrc`.
|
||||
vnoremap <silent> <Enter> :EasyAlign<cr>
|
||||
```
|
||||
|
||||
With the mapping, you can align selected lines of text with a few keystrokes.
|
||||
With the mapping, you can align selected lines of text with only a few keystrokes.
|
||||
|
||||
1. `<Enter>` key to start interactive EasyAlign command
|
||||
1. Optional Enter keys to toggle right-justification mode
|
||||
@@ -142,17 +147,7 @@ You can even omit spaces between the arguments, so concisely (or cryptically):
|
||||
|
||||
- `:EasyAlign*/[:;]\+/{'s':1,'l':0}`
|
||||
|
||||
Available options are as follows.
|
||||
|
||||
| Atrribute | Type | Default |
|
||||
| ---------------- | ---------------- | ----------------------- |
|
||||
| left_margin | number or string | 0 |
|
||||
| right_margin | number or string | 0 |
|
||||
| stick_to_left | boolean | 0 |
|
||||
| ignore_unmatched | boolean | 1 |
|
||||
| ignores | array | `['String', 'Comment']` |
|
||||
|
||||
(The last two options will be described shortly in the following sections.)
|
||||
Available options will be shown later in the document.
|
||||
|
||||
### Partial alignment in blockwise-visual mode
|
||||
|
||||
@@ -181,14 +176,35 @@ 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 field number: `<Enter>-=`
|
||||
|
||||
Global options
|
||||
--------------
|
||||
Alignment options
|
||||
-----------------
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
| ----------------------------- | ---------- | ----------------------- | -------------------------------------------------- |
|
||||
| g:easy_align_ignores | list | `['String', 'Comment']` | Ignore delimiters in these syntax highlight groups |
|
||||
| g:easy_align_ignore_unmatched | boolean | `1` | Ignore lines without matching delimiter |
|
||||
| g:easy_align_delimiters | dictionary | `{}` | Extend or override alignment rules |
|
||||
Options values can be 1) specified as global variables, 2) set on each alignment
|
||||
rule in `g:easy_align_delimiters`, 3) or given to every `:EasyAlign` command.
|
||||
|
||||
Command-line options have the highest precedence, and global variables have the
|
||||
lowest precedence.
|
||||
|
||||
### List of options
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
| ------------------ | ----------------- | ----------------------- | ------------------------------------------------------- |
|
||||
| `left_margin` | number | 0 | Number of spaces to attach before delimiter |
|
||||
| `left_margin` | string | `''` | String to attach before delimiter |
|
||||
| `right_margin` | number | 0 | Number of spaces to attach after delimiter |
|
||||
| `right_margin` | string | `''` | String to attach after delimiter |
|
||||
| `stick_to_left` | boolean | 0 | Whether to position delimiter on the left-side |
|
||||
| `ignore_unmatched` | boolean | 1 | Whether to ignore lines without matching delimiter |
|
||||
| `ignores` | array | `['String', 'Comment']` | Delimiters in these syntax highlight groups are ignored |
|
||||
| `delimiter_align` | string | `r` | Determines how to align delimiters of different lengths |
|
||||
|
||||
Some of the options can be specified using corresponding global variables.
|
||||
|
||||
| Option | Global variable |
|
||||
| ------------------ | ------------------------------- |
|
||||
| `ignore_unmatched` | `g:easy_align_ignore_unmatched` |
|
||||
| `ignores` | `g:easy_align_ignores` |
|
||||
| `delimiter_align` | `g:easy_align_delimiter_align` |
|
||||
|
||||
### Ignoring delimiters in comments or strings
|
||||
|
||||
@@ -231,21 +247,13 @@ becomes as follows on `<Enter>:` (or `:EasyAlign:`)
|
||||
|
||||
Naturally, this feature only works when syntax highlighting is enabled.
|
||||
|
||||
You can change the default rule by either defining global `g:easy_align_ignores`
|
||||
array,
|
||||
You can change the default rule by using one of these 3 methods.
|
||||
|
||||
```vim
|
||||
" Ignore nothing!
|
||||
let g:easy_align_ignores = []
|
||||
```
|
||||
1. Define global `g:easy_align_ignores` list
|
||||
2. Define a custom alignment rule in `g:easy_align_delimiters` with `ignores` option
|
||||
3. Provide `ignores` option to `:EasyAlign` command. e.g. `:EasyAlign:{'is':[]}`
|
||||
|
||||
or providing `ignores` option directly to `:EasyAlign` command
|
||||
|
||||
```vim
|
||||
:EasyAlign:{'is':[]}
|
||||
```
|
||||
|
||||
Then you get,
|
||||
For example if you set `ignores` option to be an empty list, you get
|
||||
|
||||
```ruby
|
||||
{
|
||||
@@ -289,20 +297,12 @@ this is usually what we want.
|
||||
}
|
||||
```
|
||||
|
||||
However, this default behavior is also configurable.
|
||||
However, this default behavior is also configurable by using one of these 3
|
||||
methods.
|
||||
|
||||
One way is to set the global `g:easy_align_ignore_unmatched` variable to 0.
|
||||
|
||||
```vim
|
||||
let g:easy_align_ignore_unmatched = 0
|
||||
```
|
||||
|
||||
Or in non-interactive mode, you can provide `ignore_unmatched` option to
|
||||
`:EasyAlign` command as follows.
|
||||
|
||||
```vim
|
||||
:EasyAlign:{'iu':0}
|
||||
```
|
||||
1. Set the global `g:easy_align_ignore_unmatched` variable to 0
|
||||
2. Define a custom alignment rule with `ignore_unmatched` option set to 0
|
||||
3. Provide `ignore_unmatched` option to `:EasyAlign` command. e.g. `:EasyAlign:{'iu':0}`
|
||||
|
||||
Then we get,
|
||||
|
||||
@@ -316,6 +316,42 @@ Then we get,
|
||||
}
|
||||
```
|
||||
|
||||
### Aligning delimiters of different lengths
|
||||
|
||||
Global `g:easy_align_delimiter_align` option and rule-wise/command-wise
|
||||
`delimiter_align` option determines how matched delimiters of different lengths
|
||||
are aligned.
|
||||
|
||||
```ruby
|
||||
apple = 1
|
||||
banana += apple
|
||||
cake ||= banana
|
||||
```
|
||||
|
||||
By default, delimiters are right-aligned as follows.
|
||||
|
||||
```ruby
|
||||
apple = 1
|
||||
banana += apple
|
||||
cake ||= banana
|
||||
```
|
||||
|
||||
However, with `:EasyAlign={'da':l}`, delimiters are left-aligned.
|
||||
|
||||
```ruby
|
||||
apple = 1
|
||||
banana += apple
|
||||
cake ||= banana
|
||||
```
|
||||
|
||||
And on `:EasyAlign={'da':c}`, center-aligned.
|
||||
|
||||
```ruby
|
||||
apple = 1
|
||||
banana += apple
|
||||
cake ||= banana
|
||||
```
|
||||
|
||||
### Extending alignment rules
|
||||
|
||||
Although the default rules should cover the most of the use cases,
|
||||
@@ -327,7 +363,7 @@ you can extend the rules by setting a dictionary named `g:easy_align_delimiters`
|
||||
let g:easy_align_delimiters = {
|
||||
\ '>': { 'pattern': '>>\|=>\|>' },
|
||||
\ '/': { 'pattern': '//\+\|/\*\|\*/', 'ignores': ['String'] },
|
||||
\ '#': { 'pattern': '#\+', 'ignores': ['String'] },
|
||||
\ '#': { 'pattern': '#\+', 'ignores': ['String'], 'delimiter_align': 'l' },
|
||||
\ ']': {
|
||||
\ 'pattern': '[\[\]]',
|
||||
\ 'left_margin': 0,
|
||||
@@ -339,14 +375,19 @@ let g:easy_align_delimiters = {
|
||||
\ 'left_margin': 0,
|
||||
\ 'right_margin': 0,
|
||||
\ 'stick_to_left': 0
|
||||
\ },
|
||||
\ 'd': {
|
||||
\ 'pattern': ' \(\S\+\s*[;=]\)\@=',
|
||||
\ 'left_margin': 0,
|
||||
\ 'right_margin': 0
|
||||
\ }
|
||||
\ }
|
||||
```
|
||||
|
||||
Examples and use cases
|
||||
----------------------
|
||||
Advanced examples and use cases
|
||||
-------------------------------
|
||||
|
||||
See the [link](https://github.com/junegunn/vim-easy-align/blob/master/EXAMPLES.md)
|
||||
See [EXAMPLES.md](https://github.com/junegunn/vim-easy-align/blob/master/EXAMPLES.md)
|
||||
for more examples.
|
||||
|
||||
Author
|
||||
|
||||
@@ -44,7 +44,7 @@ let s:just = ['', '[R]']
|
||||
let s:known_options = {
|
||||
\ 'margin_left': [0, 1], 'margin_right': [0, 1], 'stick_to_left': [0],
|
||||
\ 'left_margin': [0, 1], 'right_margin': [0, 1],
|
||||
\ 'ignores': [3], 'ignore_unmatched': [0]
|
||||
\ 'ignores': [3], 'ignore_unmatched': [0], 'delimiter_align': [1]
|
||||
\ }
|
||||
|
||||
if exists("*strwidth")
|
||||
@@ -139,54 +139,86 @@ function! s:validate_options(opts)
|
||||
return a:opts
|
||||
endfunction
|
||||
|
||||
function! s:do_align(just, all_tokens, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, ignore_unmatched, ignores, recursive)
|
||||
function! s:split_line(line, fc, lc, pattern, stick_to_left, ignore_unmatched, ignores)
|
||||
let left = a:lc ?
|
||||
\ strpart(getline(a:line), a:fc - 1, a:lc - a:fc + 1) :
|
||||
\ strpart(getline(a:line), a:fc - 1)
|
||||
let idx = 0
|
||||
" Do not allow \zs
|
||||
" 1: whole match
|
||||
" 2: token
|
||||
" 3: delimiter
|
||||
let pattern = '^\(\(.\{-}\s*\)\(' .a:pattern. '\)\s' . (a:stick_to_left ? '*' : '\{-}') . '\)'
|
||||
let tokens = []
|
||||
let delims = []
|
||||
|
||||
" Phase 1: split
|
||||
let ignorable = 0
|
||||
let token = ''
|
||||
while 1
|
||||
let matches = matchlist(left, pattern, idx)
|
||||
if empty(matches) | break | endif
|
||||
if empty(matches[1])
|
||||
let char = strpart(left, idx, 1)
|
||||
if empty(char) | break | endif
|
||||
let [match, part, delim] = [char, char, '']
|
||||
else
|
||||
let [match, part, delim] = matches[1 : 3]
|
||||
endif
|
||||
|
||||
let ignorable = s:highlighted_as(a:line, idx + len(part) + a:fc, a:ignores)
|
||||
if ignorable
|
||||
let token .= match
|
||||
else
|
||||
call add(tokens, token . match)
|
||||
call add(delims, delim)
|
||||
let token = ''
|
||||
endif
|
||||
|
||||
let idx += len(match)
|
||||
endwhile
|
||||
|
||||
let leftover = token . strpart(left, idx)
|
||||
if !empty(leftover)
|
||||
call add(tokens, leftover)
|
||||
call add(delims, '')
|
||||
endif
|
||||
|
||||
" Preserve indentation - merge first two tokens
|
||||
if len(tokens) > 1 && empty(s:rtrim(tokens[0]))
|
||||
let tokens[1] = tokens[0] . tokens[1]
|
||||
call remove(tokens, 0)
|
||||
call remove(delims, 0)
|
||||
endif
|
||||
|
||||
" Skip comment line
|
||||
if ignorable && len(tokens) == 1 && a:ignore_unmatched
|
||||
let tokens = []
|
||||
let delims = []
|
||||
endif
|
||||
|
||||
return [tokens, delims]
|
||||
endfunction
|
||||
|
||||
function! s:do_align(just, all_tokens, all_delims, fl, ll, fc, lc, pattern, nth, ml, mr, da, stick_to_left, ignore_unmatched, ignores, recursive)
|
||||
let lines = {}
|
||||
let max_just_len = 0
|
||||
let max_delim_len = 0
|
||||
let max_tokens = 0
|
||||
let min_indent = -1
|
||||
let pattern = '\s*\(' .a:pattern. '\)\s' . (a:stick_to_left ? '*' : '\{-}')
|
||||
|
||||
" Phase 1
|
||||
for line in range(a:fl, a:ll)
|
||||
if !has_key(a:all_tokens, line)
|
||||
" Split line into the tokens by the delimiters
|
||||
let raw_tokens = split(a:lc ?
|
||||
\ strpart(getline(line), a:fc - 1, a:lc - a:fc + 1) :
|
||||
\ strpart(getline(line), a:fc - 1),
|
||||
\ pattern.'\zs')
|
||||
let concat = 0
|
||||
if empty(a:ignores)
|
||||
let tokens = raw_tokens
|
||||
else
|
||||
" Concat adjacent tokens that are split by ignorable delimiters
|
||||
let tokens = []
|
||||
let idx = 0
|
||||
for token in raw_tokens
|
||||
let idx += len(token)
|
||||
if concat
|
||||
let tokens[len(tokens) - 1] .= token
|
||||
else
|
||||
call add(tokens, token)
|
||||
endif
|
||||
let concat = s:highlighted_as(line, idx + a:fc - 1, a:ignores)
|
||||
endfor
|
||||
endif
|
||||
|
||||
" Preserve indentation - merge first two tokens
|
||||
if !empty(tokens) && match(tokens[0], '^\s*$') != -1
|
||||
let tokens = extend([join(tokens[0:1], '')], tokens[2:-1])
|
||||
endif
|
||||
|
||||
" Skip comment line
|
||||
if concat && len(tokens) == 1 && a:ignore_unmatched
|
||||
let tokens = []
|
||||
endif
|
||||
let [tokens, delims] = s:split_line(line, a:fc, a:lc, a:pattern, a:stick_to_left, a:ignore_unmatched, a:ignores)
|
||||
|
||||
" Remember tokens for subsequent recursive calls
|
||||
let a:all_tokens[line] = tokens
|
||||
let a:all_delims[line] = delims
|
||||
else
|
||||
let tokens = a:all_tokens[line]
|
||||
let delims = a:all_delims[line]
|
||||
endif
|
||||
|
||||
" Skip empty lines
|
||||
@@ -204,7 +236,7 @@ function! s:do_align(just, all_tokens, fl, ll, fc, lc, pattern, nth, ml, mr, sti
|
||||
let nth = a:nth - 1 " make it 0-based
|
||||
else " Negative field number
|
||||
let nth = len(tokens) + a:nth
|
||||
if match(tokens[len(tokens) - 1], pattern.'$') == -1
|
||||
if empty(delims[len(delims) - 1])
|
||||
let nth -= 1
|
||||
endif
|
||||
|
||||
@@ -213,12 +245,11 @@ function! s:do_align(just, all_tokens, fl, ll, fc, lc, pattern, nth, ml, mr, sti
|
||||
endif
|
||||
endif
|
||||
|
||||
let last = tokens[nth]
|
||||
let prefix = (nth > 0 ? join(tokens[0 : nth - 1], '') : '')
|
||||
let token = substitute(last, pattern.'$', '', '')
|
||||
|
||||
let delim = get(matchlist(last, pattern.'$'), 1, '')
|
||||
if empty(delim) && a:just == 0 && a:ignore_unmatched
|
||||
let prefix = nth > 0 ? join(tokens[0 : nth - 1], '') : ''
|
||||
let delim = delims[nth]
|
||||
let token = s:rtrim( tokens[nth] )
|
||||
let token = s:rtrim( strpart(token, 0, len(token) - len(s:rtrim(delim))) )
|
||||
if empty(delim) && !exists('tokens[nth + 1]') && a:just == 0 && a:ignore_unmatched
|
||||
continue
|
||||
endif
|
||||
|
||||
@@ -234,6 +265,7 @@ function! s:do_align(just, all_tokens, fl, ll, fc, lc, pattern, nth, ml, mr, sti
|
||||
" Phase 2
|
||||
for [line, elems] in items(lines)
|
||||
let tokens = a:all_tokens[line]
|
||||
let delims = a:all_delims[line]
|
||||
let [nth, prefix, token, delim] = elems
|
||||
|
||||
" Remove the leading whitespaces of the next token
|
||||
@@ -256,11 +288,16 @@ function! s:do_align(just, all_tokens, fl, ll, fc, lc, pattern, nth, ml, mr, sti
|
||||
let tokens[nth] = token
|
||||
|
||||
" Pad the delimiter
|
||||
let dpad = repeat(' ', max_delim_len - s:strwidth(delim))
|
||||
if a:stick_to_left
|
||||
let rpad = rpad . dpad
|
||||
let dpadl = max_delim_len - s:strwidth(delim)
|
||||
if a:da ==? 'l'
|
||||
let [dl, dr] = ['', repeat(' ', dpadl)]
|
||||
elseif a:da ==? 'c'
|
||||
let dl = repeat(' ', dpadl / 2)
|
||||
let dr = repeat(' ', dpadl - dpadl / 2)
|
||||
elseif a:da ==? 'r'
|
||||
let [dl, dr] = [repeat(' ', dpadl), '']
|
||||
else
|
||||
let delim = dpad . delim
|
||||
call s:exit('Invalid delimiter_align: ' . a:da)
|
||||
endif
|
||||
|
||||
" Before and after the range (for blockwise visual mode)
|
||||
@@ -286,7 +323,7 @@ function! s:do_align(just, all_tokens, fl, ll, fc, lc, pattern, nth, ml, mr, sti
|
||||
endif
|
||||
|
||||
" Align the token
|
||||
let aligned = join([lpad, token, ml, delim, mr, rpad], '')
|
||||
let aligned = join([lpad, token, ml, dl, delim, dr, mr, rpad], '')
|
||||
let tokens[nth] = aligned
|
||||
|
||||
" Update the line
|
||||
@@ -296,8 +333,8 @@ function! s:do_align(just, all_tokens, fl, ll, fc, lc, pattern, nth, ml, mr, sti
|
||||
|
||||
if a:recursive && a:nth < max_tokens
|
||||
let just = a:recursive == 2 ? !a:just : a:just
|
||||
call s:do_align(just, a:all_tokens, a:fl, a:ll, a:fc, a:lc, a:pattern,
|
||||
\ a:nth + 1, a:ml, a:mr, a:stick_to_left, a:ignore_unmatched,
|
||||
call s:do_align(just, a:all_tokens, a:all_delims, a:fl, a:ll, a:fc, a:lc, a:pattern,
|
||||
\ a:nth + 1, a:ml, a:mr, a:da, a:stick_to_left, a:ignore_unmatched,
|
||||
\ a:ignores, a:recursive)
|
||||
endif
|
||||
endfunction
|
||||
@@ -352,14 +389,19 @@ function! s:parse_args(args)
|
||||
" Poor man's option parser
|
||||
let idx = 0
|
||||
while 1
|
||||
let midx = match(args, '{.*}\s*$', idx)
|
||||
let midx = match(args, '\s*{.*}\s*$', idx)
|
||||
if midx == -1 | break | endif
|
||||
|
||||
let cand = strpart(args, midx)
|
||||
try
|
||||
let [l, r, c] = ['l', 'r', 'c']
|
||||
let [L, R, C] = ['l', 'r', 'c']
|
||||
let o = eval(cand)
|
||||
if type(o) == 4
|
||||
let option = o
|
||||
if args[midx - 1 : midx] == '\ '
|
||||
let midx += 1
|
||||
endif
|
||||
let args = strpart(args, 0, midx)
|
||||
break
|
||||
endif
|
||||
@@ -384,10 +426,6 @@ function! s:parse_args(args)
|
||||
try | call matchlist('', regexp)
|
||||
catch | call s:exit("Invalid regular expression: ". regexp)
|
||||
endtry
|
||||
" Unsupported regular expression
|
||||
if match(regexp, '\\zs') != -1
|
||||
call s:exit("Using \\zs is not allowed. Use stick_to_left option instead.")
|
||||
endif
|
||||
return [matches[1], regexp, option, 1]
|
||||
else
|
||||
let tokens = matchlist(args, '^\([1-9][0-9]*\|-[0-9]*\|\*\*\?\)\?\s*\(.\{-}\)\?$')
|
||||
@@ -436,10 +474,15 @@ function! easy_align#align(just, expr) range
|
||||
if regexp
|
||||
let dict = { 'pattern': ch }
|
||||
else
|
||||
if ch =~ '^\\\s\+$'
|
||||
let ch = ' '
|
||||
elseif ch =~ '^\\\\\s\+$'
|
||||
let ch = '\'
|
||||
" Resolving command-line ambiguity
|
||||
if !empty(a:expr)
|
||||
" '\ ' => ' '
|
||||
if ch =~ '^\\\s\+$'
|
||||
let ch = ' '
|
||||
" '\\' => '\'
|
||||
elseif ch =~ '^\\\\\s*$'
|
||||
let ch = '\'
|
||||
endif
|
||||
endif
|
||||
if !has_key(delimiters, ch)
|
||||
echon "\rUnknown delimiter key: ". ch
|
||||
@@ -461,17 +504,28 @@ function! easy_align#align(just, expr) range
|
||||
if type(ml) == 0 | let ml = repeat(' ', ml) | endif
|
||||
if type(mr) == 0 | let mr = repeat(' ', mr) | endif
|
||||
|
||||
call s:do_align(just, {}, a:firstline, a:lastline,
|
||||
\ visualmode() == '' ? min([col("'<"), col("'>")]) : 1,
|
||||
\ visualmode() == '' ? max([col("'<"), col("'>")]) : 0,
|
||||
let bvisual = visualmode() == ''
|
||||
|
||||
if recur && bvisual
|
||||
echon "\rRecursive alignment is currently not supported in blockwise-visual mode"
|
||||
return
|
||||
endif
|
||||
|
||||
try
|
||||
call s:do_align(just, {}, {}, a:firstline, a:lastline,
|
||||
\ bvisual ? min([col("'<"), col("'>")]) : 1,
|
||||
\ bvisual ? max([col("'<"), col("'>")]) : 0,
|
||||
\ get(dict, 'pattern', ch),
|
||||
\ nth,
|
||||
\ ml,
|
||||
\ mr,
|
||||
\ get(dict, 'delimiter_align', get(g:, 'easy_align_delimiter_align', 'r')),
|
||||
\ get(dict, 'stick_to_left', 0),
|
||||
\ get(dict, 'ignore_unmatched', get(g:, 'easy_align_ignore_unmatched', 1)),
|
||||
\ get(dict, 'ignores', s:ignored_syntax()),
|
||||
\ recur)
|
||||
call s:echon(just, n, regexp ? '/'.ch.'/' : ch)
|
||||
call s:echon(just, n, regexp ? '/'.ch.'/' : ch)
|
||||
catch 'exit'
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
*easy_align.txt* A simple, easy-to-use Vim alignment plugin
|
||||
|
||||
vim-easy-align *vim-easy-align* *easy-align*
|
||||
=========================================================================
|
||||
|
||||
@@ -117,6 +119,7 @@ Available options are as follows.
|
||||
| left_margin | number or string | 0 |
|
||||
| right_margin | number or string | 0 |
|
||||
| stick_to_left | boolean | 0 |
|
||||
| delimiter_align | string | 'r' |
|
||||
| ignore_unmatched | boolean | 1 |
|
||||
| ignores | array | `['String', 'Comment']` |
|
||||
|
||||
@@ -235,6 +238,36 @@ Then we get,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Aligning delimiters of different lengths *g:easy_align_delimiter_align*
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
Global `g:easy_align_delimiter_align` option and rule-wise/command-wise
|
||||
`delimiter_align` option determines how matched delimiters of different
|
||||
lengths are aligned.
|
||||
|
||||
apple = 1
|
||||
banana += apple
|
||||
cake ||= banana
|
||||
|
||||
By default, delimiters are right-aligned as follows.
|
||||
|
||||
apple = 1
|
||||
banana += apple
|
||||
cake ||= banana
|
||||
|
||||
However, with `:EasyAlign={'da':l}`, delimiters are left-aligned.
|
||||
|
||||
apple = 1
|
||||
banana += apple
|
||||
cake ||= banana
|
||||
|
||||
And on `:EasyAlign={'da':c}`, center-aligned.
|
||||
|
||||
apple = 1
|
||||
banana += apple
|
||||
cake ||= banana
|
||||
|
||||
Extending alignment rules *g:easy_align_delimiters*
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -233,6 +233,14 @@ banana = 'Gros Michel' # comment 2
|
||||
|
||||
```yaml
|
||||
|
||||
mysql:
|
||||
# JDBC driver for MySQL database:
|
||||
driver: com.mysql.jdbc.Driver
|
||||
# JDBC URL for the connection (jdbc:mysql://HOSTNAME/DATABASE)
|
||||
url: jdbc:mysql://localhost/test
|
||||
database: test
|
||||
"user:pass": r00t:pa55
|
||||
|
||||
mysql:
|
||||
# JDBC driver for MySQL database:
|
||||
driver: com.mysql.jdbc.Driver
|
||||
@@ -336,6 +344,7 @@ my_object .
|
||||
| batch_size | Fixnum | nil | number of maximum items to be assigned at once |
|
||||
| logger | Logger | nil | logger instance for debug logs |
|
||||
|
||||
|
||||
|Option |Type |Default |Description |
|
||||
|-- |-- |-- |-- |
|
||||
|threads |Fixnum |1 |number of threads in the thread pool |
|
||||
@@ -356,3 +365,142 @@ my_object .
|
||||
| batch_size<| Fixnum <| nil <| number of maximum items to be assigned at once<|
|
||||
| logger <| Logger <| nil <| logger instance for debug logs <|
|
||||
|
||||
```c
|
||||
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
static std::map<std::string, float>* scores = pointer;
|
||||
|
||||
```
|
||||
|
||||
Paul<<< McCartney{{{ 1942
|
||||
George<<< Harrison {{{ 1943
|
||||
Ringo<<< Starr {{{ 1940
|
||||
Pete<<< Best {{{ 1941
|
||||
|
||||
Paul McCartney 1942
|
||||
George Harrison 1943
|
||||
Ringo Starr 1940
|
||||
Pete Best 1941
|
||||
|
||||
... - . -----
|
||||
.. -- .. ----
|
||||
. --- ... ---
|
||||
---- .... --
|
||||
. --- ..... -
|
||||
.. -- ......
|
||||
... - ..... -
|
||||
|
||||
... - . -----
|
||||
.. -- .. ----
|
||||
. --- ... ---
|
||||
---- .... --
|
||||
. --- ..... -
|
||||
.. -- ......
|
||||
... - ..... -
|
||||
|
||||
... - . -----
|
||||
.. -- .. ----
|
||||
. --- ... ---
|
||||
---- .... --
|
||||
. --- ..... -
|
||||
.. -- ......
|
||||
... - ..... -
|
||||
|
||||
... - . -----
|
||||
.. -- .. ----
|
||||
. --- ... ---
|
||||
---- .... --
|
||||
. --- ..... -
|
||||
.. -- ......
|
||||
... - ..... -
|
||||
|
||||
... - .-----
|
||||
.. -- ..----
|
||||
. --- ...---
|
||||
---- ....--
|
||||
. --- .....-
|
||||
.. -- ......
|
||||
... - .....-
|
||||
|
||||
... - . -----
|
||||
.. -- .. ----
|
||||
. --- ... ---
|
||||
---- .... --
|
||||
. --- ..... -
|
||||
.. -- ......
|
||||
... - ..... -
|
||||
|
||||
... - .-----
|
||||
.. -- ..----
|
||||
. --- ...---
|
||||
---- ....--
|
||||
. --- .....-
|
||||
.. -- ......
|
||||
... - .....-
|
||||
|
||||
... - .-----
|
||||
.. -- ..----
|
||||
. --- ...---
|
||||
---- ....--
|
||||
. --- .....-
|
||||
.. -- ......
|
||||
... - .....-
|
||||
|
||||
...-.-----
|
||||
..--..----
|
||||
.---...---
|
||||
----....--
|
||||
.---.....-
|
||||
..--......
|
||||
...-.....-
|
||||
|
||||
... - . -----
|
||||
.. -- .. ----
|
||||
. --- ... ---
|
||||
---- .... --
|
||||
. --- ..... -
|
||||
.. -- ......
|
||||
... - ..... -
|
||||
|
||||
... - .-----
|
||||
.. -- ..----
|
||||
. --- ...---
|
||||
---- ....--
|
||||
. --- .....-
|
||||
.. -- ......
|
||||
... - .....-
|
||||
|
||||
... - . -----
|
||||
.. -- .. ----
|
||||
. --- ... ---
|
||||
---- .... --
|
||||
. --- ..... -
|
||||
.. -- ......
|
||||
... - ..... -
|
||||
|
||||
... - . -----
|
||||
.. -- .. ----
|
||||
. --- ... ---
|
||||
---- .... --
|
||||
. --- ..... -
|
||||
.. -- ......
|
||||
... - ..... -
|
||||
|
||||
...-. -----
|
||||
..--.. ----
|
||||
.---... ---
|
||||
----.... --
|
||||
.---..... -
|
||||
..--......
|
||||
...-..... -
|
||||
|
||||
... - .-----
|
||||
.. -- ..----
|
||||
. --- ...---
|
||||
---- ....--
|
||||
. --- .....-
|
||||
.. -- ......
|
||||
... - .....-
|
||||
|
||||
|
||||
@@ -107,3 +107,26 @@ my_object.
|
||||
|batch_size | Fixnum | nil | number of maximum items to be assigned at once |
|
||||
|logger | Logger | nil | logger instance for debug logs |
|
||||
|
||||
|
||||
```c
|
||||
|
||||
const char* str = "Hello";
|
||||
int64_t count = 1 + 2;
|
||||
static double pi = 3.14;
|
||||
static std::map<std::string, float>* scores = pointer;
|
||||
|
||||
```
|
||||
|
||||
Paul McCartney 1942
|
||||
George Harrison 1943
|
||||
Ringo Starr 1940
|
||||
Pete Best 1941
|
||||
|
||||
...-.-----
|
||||
..--..----
|
||||
.---...---
|
||||
----....--
|
||||
.---.....-
|
||||
..--......
|
||||
...-.....-
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
4Gvipjyvip
|
||||
4Gvipjyvip
|
||||
|
||||
@@ -16,6 +16,8 @@ function! GFM()
|
||||
\ lang, lang, lang)
|
||||
endfor
|
||||
let b:current_syntax='mkd'
|
||||
|
||||
syntax sync fromstart
|
||||
endfunction
|
||||
|
||||
silent! unlet g:easy_align_delimiters
|
||||
|
||||
@@ -91,3 +91,6 @@ apricot = 'DAD' + 'F#AD'
|
||||
banana = 'Gros Michel' # comment 2
|
||||
```
|
||||
|
||||
a()p()p()l()e();():()b()a()n()a()n()a():():()c()a()k()e
|
||||
d()a()t()a();();()e()x()c()h()a()n()g()e():();()f()o()r()m()a()t
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
4Gvipjyvip:EasyAlign:
|
||||
4Gvipjyvip:EasyAlign:
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
e!
|
||||
execute 'source '. expand('%:p:h') . '/include.vim'
|
||||
|
||||
while line('.') < line('$')
|
||||
normal 30j
|
||||
redraw
|
||||
endwhile
|
||||
|
||||
normal gg
|
||||
let @b=system('cat '. expand('%:p:r') . '.script')
|
||||
let @a='@b:vert diffsplit ' . expand('%:p:r') . '.expected
|
||||
'
|
||||
" Syntax highlighting doesn't work
|
||||
'
|
||||
|
||||
Reference in New Issue
Block a user