1# Terraform configuration language
2# https://www.terraform.io/docs/configuration/
3
4# Detection
5# ‾‾‾‾‾‾‾‾‾
6
7hook global BufCreate .*[.](tf|tfvars) %{
8  set-option buffer filetype terraform
9}
10
11
12# Initialization
13# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
14
15hook global WinSetOption filetype=terraform %{
16    require-module terraform
17
18    set-option window static_words %opt{terraform_static_words}
19
20    hook -once -always window WinSetOption filetype=.* %{ remove-hooks window terraform-.+ }
21}
22
23
24hook -group terraform-highlight global WinSetOption filetype=terraform %{
25    add-highlighter window/terraform ref terraform
26    hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/terraform }
27}
28
29
30provide-module terraform %§
31
32# Highlighters
33# ‾‾‾‾‾‾‾‾‾‾‾‾
34
35add-highlighter shared/terraform regions
36add-highlighter shared/terraform/code  default-region group
37
38add-highlighter shared/terraform/comment1 region '#'    '$'  fill comment
39add-highlighter shared/terraform/comment2 region '\\'   '$'  fill comment
40add-highlighter shared/terraform/comment3 region /\*    \*/  fill comment
41
42# Strings can contain interpolated terraform expressions, which can contain
43# strings. Currently, we cannot support nesting of the same type of delimiter,
44# so instead we render the full interpolation as a value (otherwise, it
45# looks bad).
46# See https://github.com/mawww/kakoune/issues/1670
47add-highlighter shared/terraform/string  region '"' '(?<!\\)(?:\\\\)*"'  group
48add-highlighter shared/terraform/string/fill fill string
49add-highlighter shared/terraform/string/inter regex \$\{.+?\} 0:value
50
51add-highlighter shared/terraform/heredoc region -match-capture '<<-?(\w+)' '^\h*(\w+)$' regions
52add-highlighter shared/terraform/heredoc/fill default-region fill string
53add-highlighter shared/terraform/heredoc/inter region -recurse \{ (?<!\\)(\\\\)*\$\{ \} ref terraform
54
55
56add-highlighter shared/terraform/code/valueDec regex '\b[0-9]+([kKmMgG]b?)?\b' 0:value
57add-highlighter shared/terraform/code/valueHex regex '\b0x[0-9a-f]+([kKmMgG]b?)?\b' 0:value
58
59add-highlighter shared/terraform/code/operators regex [\[\]] 0:operator
60
61add-highlighter shared/terraform/code/field regex '^\h+(\w+)\s*(=)' 1:variable 2:keyword
62
63evaluate-commands %sh{
64  blocks="connection content data dynamic locals module output provider
65          provisioner resource terraform variable"
66
67  constants="true false null"
68
69  keywords="for for_each if in"
70
71  types="bool list map number object set string tuple"
72
73  var_subs="local module var"
74
75  # Builtin functions
76  fun_num="abs ceil floor log max min parseint pow signum"
77
78  fun_str="chomp format formatlist indent join lower regex regexall replace
79           split strrev substr title trimspace upper"
80
81  fun_coll="chunklist coalesce coalescelist compact concat contains
82            distinct element flatten index keys length lookup
83            matchkeys merge range reverse setintersection setproduct
84            setunion slice sort transpose values zipmap"
85
86  fun_enc="base64decode base64encode base64gzip csvdecode jsondecode
87           jsonencode urlencode yamldecode yamlencode"
88
89  fun_file="abspath dirname pathexpand basename file fileexists fileset
90            filebase64 templatefile"
91
92  fun_dt="formatdate timeadd timestamp"
93
94  fun_crypt="base64sha256 base64sha512 bcrypt filebase64sha256
95             filebase64sha512 filemd5 filesha1 filesha256 filesha512 md5
96             rsadecrypt sha1 sha256 sha512 uuid uuidv5"
97
98  fun_net="cidrhost cidrnetmask cidrsubnet"
99
100  fun_cast="tobool tolist tomap tonumber toset tostring"
101
102  functions="$fun_num $fun_str $fun_coll $fun_enc $fun_file $fun_dt $fun_crypt $fun_net $fun_cast"
103
104  join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
105
106  # Add grammar elements to the static completion list
107  printf %s\\n "declare-option str-list terraform_static_words $(join "$blocks $keywords $constants $types $var_subs $functions" ' ')"
108
109  # Highlight grammar elements
110  printf %s "
111    add-highlighter shared/terraform/code/ regex '\b($(join "$blocks"    '|'))\b[^.]' 1:keyword
112    add-highlighter shared/terraform/code/ regex '\b($(join "$keywords"  '|'))\b'     1:keyword
113    add-highlighter shared/terraform/code/ regex '\b($(join "$constants" '|'))\b'     1:value
114    add-highlighter shared/terraform/code/ regex '\b($(join "$types"     '|'))\b'     1:type
115    add-highlighter shared/terraform/code/ regex '\b($(join "$var_subs"  '|'))\b\.'   1:meta
116    add-highlighter shared/terraform/code/ regex '\b($(join "$functions" '|'))\s*\('  1:builtin
117  "
118}
119
120§
121