Skip to content

Commit

Permalink
Add auto-alignment of class definition with tabular
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Mar 13, 2018
1 parent 921ccf8 commit 531ea98
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ Provides

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

Installation
------------
Expand Down
27 changes: 27 additions & 0 deletions after/plugin/puppet_tabular.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
if !exists('g:puppet_align_classes')
let g:puppet_align_classes = 1
endif

if exists(':AddTabularPipeline') && g:puppet_align_classes
" The unction is filtering definition by '$' symbol, then applies tabular to
" payload and join again. Source for this taken from
" https://unix.stackexchange.com/questions/35787/indent-the-middle-of-multiple-lines
function! AlignPuppetClass(lines)
" List of payload, should contain '$' AND not contain '=>'
let attributes = map(copy(a:lines), '(v:val =~ "[$]" && v:val !~ "=>") ? v:val : ""')
" List of noise, may be without '$' or with '=>'
let noise = map(copy(a:lines), '(v:val !~ "[$]" || v:val =~ "=>") ? v:val : ""')
" Splitting only by first '$attribute'
call tabular#TabularizeStrings(attributes, '^[^$]*\zs\s\+\$\w\+\>', 'l0l1')
call map(a:lines, 'remove(attributes, 0) . remove(noise, 0)')
endfunction

" The class definition could be interrupted with enum's multiline, selectors
" or whatever else, so tabular will search for any of `${['",#` symbols and
" pass them into AlignPuppetClass function
au FileType puppet AddTabularPipeline! puppet_class /[${['",#]/ AlignPuppetClass(a:lines)
au FileType puppet inoremap <buffer> <silent> ,<CR> ,<Esc>:Tabularize puppet_class<CR>o
" A comma should be at the last position in the line that's why 'norm A'
" is enough in this case
au FileType puppet inoremap <buffer> <silent> ,, ,<Esc>:Tabularize puppet_class<CR>A
endif

0 comments on commit 531ea98

Please sign in to comment.