summaryrefslogtreecommitdiffstats
path: root/indent/terraform.vim
blob: 5a29dfb40243f5f2e12042490abdb5c5f97a70df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'terraform') == -1
  
if exists("b:did_indent")
  finish
endif

let b:did_indent = 1

setlocal nolisp
setlocal autoindent
setlocal indentexpr=TerraformIndent(v:lnum)
setlocal indentkeys+=<:>,0=},0=)

if exists("*TerraformIndent")
  finish
endif

function! TerraformIndent(lnum)
  " previous non-blank line
  let prevlnum = prevnonblank(a:lnum-1)

  " beginning of file?
  if prevlnum == 0
    return 0
  endif

  " previous line without comments
  let prevline = substitute(getline(prevlnum), '//.*$', '', '')
  let previndent = indent(prevlnum)
  let thisindent = previndent

  " block open?
  if prevline =~ '[\[{\(]\s*$'
    let thisindent += &sw
  endif

  " current line without comments
  let thisline = substitute(getline(a:lnum), '//.*$', '', '')

  " block close?
  if thisline =~ '^\s*[\)\]}]'
    let thisindent -= &sw
  endif

  return thisindent
endfunction

endif