Skip to content

Commit 531ea98

Browse files
committed
Add auto-alignment of class definition with tabular
1 parent 921ccf8 commit 531ea98

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ Provides
88

99
* Formatting based on the latest Puppetlabs Style Guide
1010
* Syntax highlighting compatible with puppet 4.x
11-
* Automatic => alignment
11+
* Automatic '=>' alignment
1212
* If you don't like that, add `let g:puppet_align_hashes = 0` to your vimrc.
13+
* Automatic '=' alignment in class definition ([tabular](https://github.com/godlygeek/tabular) is required)
14+
* If you don't like that, add `let g:puppet_align_classes = 0` to your vimrc.
1315
* Ctags support
1416
* Doesn't require a bloated JRE
1517
* Doesn't take minutes to open
@@ -24,6 +26,7 @@ Additional useful plugins
2426
[snipmate](https://github.com/garbas/vim-snipmate) and
2527
[ultisnips](https://github.com/SirVer/ultisnips).
2628
* [Tagbar](https://github.com/majutsushi/tagbar) plugin for Ctags support.
29+
* [Tabular](https://github.com/godlygeek/tabular) plugin for automatical align
2730

2831
Installation
2932
------------

after/plugin/puppet_tabular.vim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
if !exists('g:puppet_align_classes')
2+
let g:puppet_align_classes = 1
3+
endif
4+
5+
if exists(':AddTabularPipeline') && g:puppet_align_classes
6+
" The unction is filtering definition by '$' symbol, then applies tabular to
7+
" payload and join again. Source for this taken from
8+
" https://unix.stackexchange.com/questions/35787/indent-the-middle-of-multiple-lines
9+
function! AlignPuppetClass(lines)
10+
" List of payload, should contain '$' AND not contain '=>'
11+
let attributes = map(copy(a:lines), '(v:val =~ "[$]" && v:val !~ "=>") ? v:val : ""')
12+
" List of noise, may be without '$' or with '=>'
13+
let noise = map(copy(a:lines), '(v:val !~ "[$]" || v:val =~ "=>") ? v:val : ""')
14+
" Splitting only by first '$attribute'
15+
call tabular#TabularizeStrings(attributes, '^[^$]*\zs\s\+\$\w\+\>', 'l0l1')
16+
call map(a:lines, 'remove(attributes, 0) . remove(noise, 0)')
17+
endfunction
18+
19+
" The class definition could be interrupted with enum's multiline, selectors
20+
" or whatever else, so tabular will search for any of `${['",#` symbols and
21+
" pass them into AlignPuppetClass function
22+
au FileType puppet AddTabularPipeline! puppet_class /[${['",#]/ AlignPuppetClass(a:lines)
23+
au FileType puppet inoremap <buffer> <silent> ,<CR> ,<Esc>:Tabularize puppet_class<CR>o
24+
" A comma should be at the last position in the line that's why 'norm A'
25+
" is enough in this case
26+
au FileType puppet inoremap <buffer> <silent> ,, ,<Esc>:Tabularize puppet_class<CR>A
27+
endif

0 commit comments

Comments
 (0)