blob: e45a4e31ba96afc5e33649bb4108cb30c8aa176c [file] [log] [blame]
Gregory Anders150b5072024-09-04 22:19:45 +02001" Language: HCL
2" Maintainer: Gregory Anders
3" Last Change: 2024-09-03
4" Based on: https://github.com/hashivim/vim-terraform
Gregory Andersc8b7e612025-05-25 17:01:45 +02005" License: ISC
6"
7" Copyright (c) 2014-2016 Mark Cornick <mark@markcornick.com>
8"
9" Permission to use, copy, modify, and/or distribute this software for any purpose
10" with or without fee is hereby granted, provided that the above copyright notice
11" and this permission notice appear in all copies.
12"
13" THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
15" FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
17" OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
18" TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
19" THIS SOFTWARE.
Gregory Anders150b5072024-09-04 22:19:45 +020020
21function! hcl#indentexpr(lnum)
22 " Beginning of the file should have no indent
23 if a:lnum == 0
24 return 0
25 endif
26
27 " Usual case is to continue at the same indent as the previous non-blank line.
28 let prevlnum = prevnonblank(a:lnum-1)
29 let thisindent = indent(prevlnum)
30
31 " If that previous line is a non-comment ending in [ { (, increase the
32 " indent level.
33 let prevline = getline(prevlnum)
34 if prevline !~# '^\s*\(#\|//\)' && prevline =~# '[\[{\(]\s*$'
35 let thisindent += &shiftwidth
36 endif
37
38 " If the current line ends a block, decrease the indent level.
39 let thisline = getline(a:lnum)
40 if thisline =~# '^\s*[\)}\]]'
41 let thisindent -= &shiftwidth
42 endif
43
44 " If the previous line starts a block comment /*, increase by one
45 if prevline =~# '/\*'
46 let thisindent += 1
47 endif
48
49 " If the previous line ends a block comment */, decrease by one
50 if prevline =~# '\*/'
51 let thisindent -= 1
52 endif
53
54 return thisindent
55endfunction