Skip to content

Commit

Permalink
Add aligning of selectors blocks in class definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Mar 14, 2018
1 parent b943152 commit a28915d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +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
* [Tabular](https://github.com/godlygeek/tabular) plugin for an automatical aligninig.

Installation
------------
Expand Down
53 changes: 46 additions & 7 deletions after/plugin/puppet_tabular.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,62 @@ if !exists('g:puppet_align_classes')
endif

if exists(':AddTabularPipeline') && g:puppet_align_classes
" The unction is filtering definition by '$' symbol, then applies tabular to
" The function for an aligning each block of selectors separately
function! AlignSelectorsByBlock(selectors)
let i = 0
let blStart = 0
let blocks = {}
" Write each block of selectors to dict {'idx_of_block_start':
" ['selector', 'strings']}
while i < len(a:selectors)
if a:selectors[i] == ''
let i += 1
continue
endif
if i == 0 || a:selectors[i-1] == ''
" Start new block
let blStart = i
let blocks[blStart] = [a:selectors[i]]
let i += 1
continue
else
" Or append to existing
let blocks[blStart] += [a:selectors[i]]
let i += 1
continue
endif
endwhile
" Align each block separately and write back to selectors list
for k in keys(blocks)
call tabular#TabularizeStrings(blocks[k], '=>')
for str in blocks[k]
let a:selectors[k] = str
let k += 1
endfor
endfor
endfunction


" The unction is filtering definition by '$' or '=>', 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 '=>'
" List of payload, must 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' and '='
" List of selectors, each block will be aligned separately
let selectors = map(copy(a:lines), 'v:val =~ "=>" ? v:val : ""')
" List of noise, haven't '$' or '=>'
let noise = map(copy(a:lines), '(v:val !~ "[$]" && v:val !~ "=>") ? v:val : ""')
call AlignSelectorsByBlock(selectors)
" Splitting by first '$attribute' and '='
call tabular#TabularizeStrings(attributes, '^[^$]*\zs\s\+\$\w\+\>\|=', 'l0l1')
call map(a:lines, 'remove(attributes, 0) . remove(noise, 0)')
call map(a:lines, 'remove(attributes, 0) . remove(noise, 0) . remove(selectors, 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 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
Expand Down

0 comments on commit a28915d

Please sign in to comment.