summaryrefslogtreecommitdiffstats
path: root/after/ftplugin/terraform.vim
blob: 1328931abcdd2f1dbe43cb97c1799b86f169ddc0 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'terraform') == -1
  
if !exists('g:terraform_align')
  let g:terraform_align = 0
endif

if !exists('g:terraform_remap_spacebar')
  let g:terraform_remap_spacebar = 0
endif

if !exists('g:terraform_fold_sections')
  let g:terraform_fold_sections = 0
endif

if g:terraform_align && exists(':Tabularize')
  inoremap <buffer> <silent> = =<Esc>:call <SID>terraformalign()<CR>a
  function! s:terraformalign()
    let p = '^.*=[^>]*$'
    if exists(':Tabularize') && getline('.') =~# '^.*=' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
      let column = strlen(substitute(getline('.')[0:col('.')],'[^=]','','g'))
      let position = strlen(matchstr(getline('.')[0:col('.')],'.*=\s*\zs.*'))
      Tabularize/=/l1
      normal! 0
      call search(repeat('[^=]*=',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
    endif
  endfunction
endif

if g:terraform_fold_sections
  function! TerraformFolds()
    let thisline = getline(v:lnum)
    if match(thisline, '^resource') >= 0
      return ">1"
    elseif match(thisline, '^provider') >= 0
      return ">1"
    elseif match(thisline, '^module') >= 0
      return ">1"
    elseif match(thisline, '^variable') >= 0
      return ">1"
    elseif match(thisline, '^output') >= 0
      return ">1"
    elseif match(thisline, '^data') >= 0
      return ">1"
    elseif match(thisline, '^terraform') >= 0
      return ">1"
    elseif match(thisline, '^locals') >= 0
      return ">1"
    else
      return "="
    endif
  endfunction
  setlocal foldmethod=expr
  setlocal foldexpr=TerraformFolds()
  setlocal foldlevel=1

  function! TerraformFoldText()
    let foldsize = (v:foldend-v:foldstart)
    return getline(v:foldstart).' ('.foldsize.' lines)'
  endfunction
  setlocal foldtext=TerraformFoldText()
endif

" Re-map the space bar to fold and unfold
if get(g:, "terraform_remap_spacebar", 1)
  "inoremap <space> <C-O>za
  nnoremap <space> za
  onoremap <space> <C-C>za
  vnoremap <space> zf
endif

" Match the identation put in place by Hashicorp and :TerraformFmt, https://github.com/hashivim/vim-terraform/issues/21
if get(g:, "terraform_align", 1)
  setlocal tabstop=2
  setlocal softtabstop=2
  setlocal shiftwidth=2
endif

endif