vim-polyglot/indent/terraform.vim

50 lines
1.2 KiB
VimL
Raw Normal View History

2017-02-02 15:44:42 -05:00
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'terraform') == -1
2017-12-06 06:56:27 -05:00
" Only load this file if no other indent file was loaded
2017-02-02 15:44:42 -05:00
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
setlocal nolisp
2017-12-06 06:56:27 -05:00
setlocal autoindent sw=2 ts=2
2017-02-02 15:44:42 -05:00
setlocal indentexpr=TerraformIndent(v:lnum)
setlocal indentkeys+=<:>,0=},0=)
if exists("*TerraformIndent")
finish
endif
function! TerraformIndent(lnum)
2017-12-06 06:56:27 -05:00
" Beginning of the file should have no indent
if a:lnum == 0
2017-02-02 15:44:42 -05:00
return 0
endif
2017-12-06 06:56:27 -05:00
" Previous non-blank line should continue the indent level
let prevlnum = prevnonblank(a:lnum-1)
" Previous line without comments should continue the indent level
2017-02-02 15:44:42 -05:00
let prevline = substitute(getline(prevlnum), '//.*$', '', '')
let previndent = indent(prevlnum)
let thisindent = previndent
2017-12-06 06:56:27 -05:00
" Config block starting with [ { ( should increase the indent level
2017-09-27 13:57:29 -04:00
if prevline =~ '[\[{\(]\s*$'
2017-02-02 15:44:42 -05:00
let thisindent += &sw
endif
2017-12-06 06:56:27 -05:00
" Current line without comments should continue the indent level
2017-02-02 15:44:42 -05:00
let thisline = substitute(getline(a:lnum), '//.*$', '', '')
2017-12-06 06:56:27 -05:00
" Config block ending with ) } ] should get the indentation
" level from the initial config block
if thisline =~ '^\s*[\)}\]]'
2017-02-02 15:44:42 -05:00
let thisindent -= &sw
endif
return thisindent
endfunction
endif