vim-polyglot/indent/terraform.vim

64 lines
1.6 KiB
VimL
Raw Normal View History

if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'terraform') != -1
finish
endif
2017-12-06 06:56:27 -05:00
" Only load this file if no other indent file was loaded
2019-06-08 06:44:15 -04:00
if exists('b:did_indent')
2017-02-02 15:44:42 -05:00
finish
endif
let b:did_indent = 1
2019-06-08 06:44:15 -04:00
let s:cpo_save = &cpoptions
set cpoptions&vim
2017-02-02 15:44:42 -05:00
setlocal nolisp
2019-06-08 06:44:15 -04:00
setlocal autoindent shiftwidth=2 tabstop=2 softtabstop=2
2017-02-02 15:44:42 -05:00
setlocal indentexpr=TerraformIndent(v:lnum)
setlocal indentkeys+=<:>,0=},0=)
2019-06-08 06:44:15 -04:00
let b:undo_indent = 'setlocal lisp< autoindent< shiftwidth< tabstop< softtabstop<'
\ . ' indentexpr< indentkeys<'
let &cpoptions = s:cpo_save
unlet s:cpo_save
2017-02-02 15:44:42 -05:00
2019-06-08 06:44:15 -04:00
if exists('*TerraformIndent')
2017-02-02 15:44:42 -05:00
finish
endif
2019-06-08 06:44:15 -04:00
let s:cpo_save = &cpoptions
set cpoptions&vim
2017-02-02 15:44:42 -05:00
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
2019-06-08 06:44:15 -04:00
if prevline =~# '[\[{\(]\s*$'
let thisindent += &shiftwidth
2017-02-02 15:44:42 -05:00
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
2019-06-08 06:44:15 -04:00
if thisline =~# '^\s*[\)}\]]'
let thisindent -= &shiftwidth
2017-02-02 15:44:42 -05:00
endif
return thisindent
endfunction
2019-06-08 06:44:15 -04:00
let &cpoptions = s:cpo_save
unlet s:cpo_save