diff --git a/autoload/puppet/align.vim b/autoload/puppet/align.vim index 1275613..2e6002f 100644 --- a/autoload/puppet/align.vim +++ b/autoload/puppet/align.vim @@ -9,9 +9,9 @@ function! puppet#align#LinesInBlock(lnum) abort 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' + let line_indent = puppet#align#IndentLevel(marker) if line_indent < indent_level break elseif line_indent == indent_level @@ -25,9 +25,9 @@ function! puppet#align#LinesInBlock(lnum) abort let marker = a:lnum while marker <= line('$') let line_text = getline(marker) - let line_indent = puppet#align#IndentLevel(marker) if line_text =~? '\v\S' + let line_indent = puppet#align#IndentLevel(marker) if line_indent < indent_level break elseif line_indent == indent_level @@ -77,4 +77,6 @@ function! puppet#align#AlignHashrockets(...) abort call setline(line_num, new_line) endif endfor + + return lines_in_block endfunction diff --git a/autoload/puppet/format.vim b/autoload/puppet/format.vim index 7eee7f8..6d01f5e 100644 --- a/autoload/puppet/format.vim +++ b/autoload/puppet/format.vim @@ -23,12 +23,20 @@ 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 + let processed_lines = [] while l:lnum <= a:end_lnum - call puppet#align#AlignHashrockets(l:lnum) + if index(processed_lines, l:lnum) ==# -1 + let line_text = getline(l:lnum) + if line_text =~? '\v\S' + let processed_lines += puppet#align#AlignHashrockets(l:lnum) + else + " empty lines make puppet#align#AlignHashrockets reprocess blocks with + " indentation that were already processed so we just skip them + call add(processed_lines, l:lnum) + endif + endif let l:lnum += 1 endwhile endfunction