hcl.vim 1.1 KB

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