diff options
author | Adam Stankiewicz <sheerun@sher.pl> | 2019-09-04 16:04:21 +0200 |
---|---|---|
committer | Adam Stankiewicz <sheerun@sher.pl> | 2019-09-04 16:04:21 +0200 |
commit | 933e42ea1f2d615c8ce5aa6daa2994e6369de3cf (patch) | |
tree | dd8398c36a6645585288247283f7cc52934268e4 /autoload/puppet | |
parent | cdd6d73e39c85feccdcace5c32b375de7ba25bae (diff) | |
download | vim-polyglot-933e42ea1f2d615c8ce5aa6daa2994e6369de3cf.tar.gz vim-polyglot-933e42ea1f2d615c8ce5aa6daa2994e6369de3cf.zip |
Change provider for puppet, closes #424
Diffstat (limited to 'autoload/puppet')
-rw-r--r-- | autoload/puppet/align.vim | 72 | ||||
-rw-r--r-- | autoload/puppet/ctags.vim | 40 | ||||
-rw-r--r-- | autoload/puppet/format.vim | 61 |
3 files changed, 173 insertions, 0 deletions
diff --git a/autoload/puppet/align.vim b/autoload/puppet/align.vim new file mode 100644 index 00000000..54a57599 --- /dev/null +++ b/autoload/puppet/align.vim @@ -0,0 +1,72 @@ +if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'puppet') != -1 + finish +endif + +function! puppet#align#IndentLevel(lnum) + return indent(a:lnum) / &shiftwidth +endfunction + +function! puppet#align#LinesInBlock(lnum) + let lines = [] + let indent_level = puppet#align#IndentLevel(a:lnum) + + let marker = a:lnum - 1 + while marker >= 1 + let line_text = getline(marker) + let line_indent = puppet#align#IndentLevel(marker) + + if line_text =~? '\v\S' + if line_indent < indent_level + break + elseif line_indent == indent_level + call add(lines, marker) + endif + endif + + let marker -= 1 + endwhile + + let marker = a:lnum + while marker <= line('$') + let line_text = getline(marker) + let line_indent = puppet#align#IndentLevel(marker) + + if line_text =~? '\v\S' + if line_indent < indent_level + break + elseif line_indent == indent_level + call add(lines, marker) + endif + endif + + let marker += 1 + endwhile + + return lines +endfunction + +"" +" Format lines with hashrocket (=>) +" @param a:1 a line where function should search for first hashrocket +" expression, if param is not given, line with active cursor is used +function! puppet#align#AlignHashrockets(...) abort + let l:lnum = get(a:, 1, line('.')) + let lines_in_block = puppet#align#LinesInBlock(l:lnum) + let max_left_len = 0 + let indent_str = printf('%' . indent(l:lnum) . 's', '') + + for line_num in lines_in_block + let data = matchlist(getline(line_num), '^\s*\(.\{-}\S\)\s*=>\s*\(.*\)$') + if !empty(data) + let max_left_len = max([max_left_len, strlen(data[1])]) + endif + endfor + + for line_num in lines_in_block + let data = matchlist(getline(line_num), '^\s*\(.\{-}\S\)\s*=>\s*\(.*\)$') + if !empty(data) + let new_line = printf('%s%-' . max_left_len . 's => %s', indent_str, data[1], data[2]) + call setline(line_num, new_line) + endif + endfor +endfunction diff --git a/autoload/puppet/ctags.vim b/autoload/puppet/ctags.vim new file mode 100644 index 00000000..5547f42e --- /dev/null +++ b/autoload/puppet/ctags.vim @@ -0,0 +1,40 @@ +if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'puppet') != -1 + finish +endif + + +if !exists('s:ctags_type') + let s:ctags_type = 0 +endif + +let s:ctags_options_dir = expand('<sfile>:p:h:h:h') . '/ctags/' + +" Return full path to option file for ctags application +function! puppet#ctags#OptionFile() + + if puppet#ctags#Type() == 'universal' + let l:ctags_options = 'puppet_u.ctags' + else + let l:ctags_options = 'puppet.ctags' + endif + return s:ctags_options_dir . l:ctags_options +endfunction + +" Return type of installed ctags application, +" can be 'universal' or 'exuberant' +function! puppet#ctags#Type() + + if !s:ctags_type + let l:version = system('ctags --version') + if l:version =~ 'Universal Ctags' + let s:ctags_type = 'universal' + elseif l:version =~ 'Exuberant Ctags' + let s:ctags_type = 'exuberant' + else + echoerr 'Unknown version of Ctags' + endif + endif + + return s:ctags_type +endfunction + diff --git a/autoload/puppet/format.vim b/autoload/puppet/format.vim new file mode 100644 index 00000000..f60bd782 --- /dev/null +++ b/autoload/puppet/format.vim @@ -0,0 +1,61 @@ +if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'puppet') != -1 + finish +endif + +" +" Simple format using puppet's l:indents and align hashrockets function +function! puppet#format#Format() abort + let l:start_lnum = v:lnum + let l:end_lnum = v:lnum + v:count - 1 + call puppet#format#Indention(l:start_lnum, l:end_lnum) + call puppet#format#Hashrocket(l:start_lnum, l:end_lnum) + call puppet#format#Fallback(l:start_lnum, l:end_lnum) +endfunction + +"" +" Format hashrockets expressions in every line in range start_lnum and +" end_lnum, both ends included +" +" TODO way of using AlignHashrockets function is ineffective, because it +" formats same lines again and again, find better way to do it +function! puppet#format#Hashrocket(start_lnum, end_lnum) abort + let l:lnum = a:start_lnum + while l:lnum <= a:end_lnum + call puppet#align#AlignHashrockets(l:lnum) + let l:lnum += 1 + endwhile +endfunction + +"" +" Format indention in every line in range start_lnum and end_lnum, both ends +" included +" +function! puppet#format#Indention(start_lnum, end_lnum) abort + execute 'normal! ' . a:start_lnum . 'gg=' . a:end_lnum . 'gg' +endfunction + +"" +" Use internal vim default autoformat method for every line in range, only +" lines which exeed &widthline are formated +" +function! puppet#format#Fallback(start_lnum, end_lnum) abort + " I'm using it to check if autoformat expand range + let l:eof_lnum = line('$') + let l:lnum = a:start_lnum + let l:end_lnum = a:end_lnum + while l:lnum <= l:end_lnum + if strlen(getline(l:lnum)) > &textwidth + call cursor(l:lnum) + execute 'normal! gww' + " Checking if autoformat expand number of lines if yes, I will extend + " range too + if l:eof_lnum < line('$') + let l:end_lnum += line('$') - l:eof_lnum + let l:eof_lnum = line('$') + endif + endif + let l:lnum += 1 + endwhile + +endfunction + |