vim-polyglot/indent/terraform.vim

59 lines
1.4 KiB
VimL
Raw Permalink Normal View History

if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'terraform') != -1
finish
endif
2017-12-06 12:56:27 +01:00
" Only load this file if no other indent file was loaded
2019-06-08 12:44:15 +02:00
if exists('b:did_indent')
2017-02-02 21:44:42 +01:00
finish
endif
let b:did_indent = 1
2019-06-08 12:44:15 +02:00
let s:cpo_save = &cpoptions
set cpoptions&vim
2017-02-02 21:44:42 +01:00
setlocal nolisp
2019-06-08 12:44:15 +02:00
setlocal autoindent shiftwidth=2 tabstop=2 softtabstop=2
2017-02-02 21:44:42 +01:00
setlocal indentexpr=TerraformIndent(v:lnum)
setlocal indentkeys+=<:>,0=},0=)
2019-06-08 12:44:15 +02: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 21:44:42 +01:00
2019-06-08 12:44:15 +02:00
if exists('*TerraformIndent')
2017-02-02 21:44:42 +01:00
finish
endif
2019-06-08 12:44:15 +02:00
let s:cpo_save = &cpoptions
set cpoptions&vim
2017-02-02 21:44:42 +01:00
function! TerraformIndent(lnum)
2017-12-06 12:56:27 +01:00
" Beginning of the file should have no indent
if a:lnum == 0
2017-02-02 21:44:42 +01:00
return 0
endif
2019-07-01 16:25:37 +02:00
" Usual case is to continue at the same indent as the previous non-blank line.
2017-12-06 12:56:27 +01:00
let prevlnum = prevnonblank(a:lnum-1)
2019-07-01 16:25:37 +02:00
let thisindent = indent(prevlnum)
2017-12-06 12:56:27 +01:00
2019-07-01 16:25:37 +02:00
" If that previous line is a non-comment ending in [ { (, increase the
" indent level.
let prevline = getline(prevlnum)
if prevline !~# '^\s*\(#\|//\)' && prevline =~# '[\[{\(]\s*$'
2019-06-08 12:44:15 +02:00
let thisindent += &shiftwidth
2017-02-02 21:44:42 +01:00
endif
2019-07-01 16:25:37 +02:00
" If the current line ends a block, decrease the indent level.
let thisline = getline(a:lnum)
2019-06-08 12:44:15 +02:00
if thisline =~# '^\s*[\)}\]]'
let thisindent -= &shiftwidth
2017-02-02 21:44:42 +01:00
endif
return thisindent
endfunction
2019-06-08 12:44:15 +02:00
let &cpoptions = s:cpo_save
unlet s:cpo_save