Gregory Anders | 150b507 | 2024-09-04 22:19:45 +0200 | [diff] [blame] | 1 | " Language: HCL |
| 2 | " Maintainer: Gregory Anders |
| 3 | " Last Change: 2024-09-03 |
| 4 | " Based on: https://github.com/hashivim/vim-terraform |
| 5 | |
| 6 | function! hcl#indentexpr(lnum) |
| 7 | " Beginning of the file should have no indent |
| 8 | if a:lnum == 0 |
| 9 | return 0 |
| 10 | endif |
| 11 | |
| 12 | " Usual case is to continue at the same indent as the previous non-blank line. |
| 13 | let prevlnum = prevnonblank(a:lnum-1) |
| 14 | let thisindent = indent(prevlnum) |
| 15 | |
| 16 | " If that previous line is a non-comment ending in [ { (, increase the |
| 17 | " indent level. |
| 18 | let prevline = getline(prevlnum) |
| 19 | if prevline !~# '^\s*\(#\|//\)' && prevline =~# '[\[{\(]\s*$' |
| 20 | let thisindent += &shiftwidth |
| 21 | endif |
| 22 | |
| 23 | " If the current line ends a block, decrease the indent level. |
| 24 | let thisline = getline(a:lnum) |
| 25 | if thisline =~# '^\s*[\)}\]]' |
| 26 | let thisindent -= &shiftwidth |
| 27 | endif |
| 28 | |
| 29 | " If the previous line starts a block comment /*, increase by one |
| 30 | if prevline =~# '/\*' |
| 31 | let thisindent += 1 |
| 32 | endif |
| 33 | |
| 34 | " If the previous line ends a block comment */, decrease by one |
| 35 | if prevline =~# '\*/' |
| 36 | let thisindent -= 1 |
| 37 | endif |
| 38 | |
| 39 | return thisindent |
| 40 | endfunction |