Skip to content

Commit 4d23b68

Browse files
committed
hcl: Add terraform-specific highlighting patterns
1 parent 98d2fdf commit 4d23b68

File tree

3 files changed

+124
-28
lines changed

3 files changed

+124
-28
lines changed

queries/hcl/highlights.scm

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1-
[
2-
"for"
3-
] @keyword
1+
("for" @keyword (identifier) @variable)
42

3+
;; (attribute (identifier) @variable)
4+
;; (object_elem key: (_) @variable)
55
(attribute (identifier) @property)
6-
(object_elem (identifier) @property)
6+
(object_elem key: (_) @property)
7+
8+
(get_attr (identifier) @property)
79

810
(block (identifier) @type)
9-
(one_line_block (identifier) @type)
11+
;; (one_line_block (identifier) @type)
1012

11-
(function_call (identifier) @function)
13+
(function_call (identifier) @function.call)
1214

1315
[
14-
(string_literal)
15-
(quoted_template)
16-
(heredoc)
17-
] @string
16+
;; (string_lit)
17+
(quoted_template_start)
18+
(quoted_template_end)
19+
(template_literal)
20+
;; (heredoc)
21+
] @string
1822

19-
(numeric_literal) @number
23+
(numeric_lit) @number
2024

21-
[
22-
(true)
23-
(false)
24-
(null)
25-
] @constant.builtin
25+
[(bool_lit)
26+
(null_lit)
27+
] @constant.builtin
2628

2729
(comment) @comment
2830

29-
[
30-
"("
31-
")"
32-
"["
33-
"]"
34-
"{"
35-
"}"
36-
] @punctuation.bracket
31+
["("
32+
")"
33+
"["
34+
"]"
35+
"{"
36+
"}"
37+
] @punctuation.bracket
38+
39+
["&&" "?" ":" "!=" "==" "=>"] @operator
40+
41+
["if" "in"] @keyword
42+
43+
;; "." @punctuation.special
44+
45+
(template_interpolation
46+
(template_interpolation_start) @punctuation.special
47+
(expression) @embedded
48+
(template_interpolation_end) @punctuation.special)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
;; data.aws_iam_policy_document.cluster_assume_role_policy.json
2+
(expression
3+
((variable_expr (identifier)) @keyword
4+
(.eq? @keyword "data"))
5+
. (get_attr (identifier) @type)
6+
. (get_attr (identifier) @function.call)
7+
. (get_attr (identifier) @property)?
8+
)
9+
10+
;; module.path
11+
((expression
12+
((variable_expr (identifier)) @keyword
13+
(.eq? @keyword "module"))
14+
. (get_attr) @type
15+
))
16+
17+
((expression
18+
((variable_expr (identifier)) @keyword
19+
(.match? @keyword "^(var|local|count|each|path)$"))
20+
. (get_attr)? @property
21+
))
22+
23+
;; aws_iam_role.shared-sagemaker-execution.name
24+
(expression
25+
(variable_expr (identifier)) @type
26+
. (get_attr) @function.call
27+
. (get_attr)? @property
28+
.)
29+
30+
;; TODO: Highlight `content'.
31+
((block (identifier) @keyword
32+
[(string_lit (template_literal) @variable)])
33+
(.eq? @keyword "dynamic"))
34+
35+
;; ((block (identifier) @keyword
36+
;; [(string_lit (template_literal) @variable)]
37+
;; (body ((attribute (identifier) @keyword)
38+
;; (.eq? @keyword "for_each"))))
39+
;; (.eq? @keyword "dynamic"))
40+
41+
((block (identifier) @keyword
42+
. [(string_lit (template_literal) @function)
43+
(identifier) @function])
44+
(.eq? @keyword "output"))
45+
46+
((block (identifier) @keyword
47+
[(string_lit (template_literal) @variable.special)
48+
(identifier) @variable.special])
49+
(.eq? @keyword "variable"))
50+
51+
((block (identifier) @keyword
52+
[(string_lit (template_literal) @type)
53+
(identifier) @type])
54+
(.eq? @keyword "module"))
55+
56+
;; resource "aws_launch_template" "default"
57+
(block (identifier)
58+
[(string_lit (template_literal) @type)
59+
(identifier) @type]
60+
. [(string_lit (template_literal) @function)
61+
(identifier) @function]
62+
. (block_start))
63+
64+
((block (identifier) @keyword)
65+
(.match? @keyword "(resource|data|output|locals|lifecycle)"))
66+
67+
((attribute (identifier) @keyword)
68+
(.match? @keyword "^(count|depends_on|for_each)$"))
69+
70+
(attribute (identifier) @variable)
71+
(object_elem key: (_) @variable)
72+
73+
(block (identifier) @label . (block_start))

tree-sitter-langs.el

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ See `tree-sitter-langs-repos'."
110110
(elixir-mode . elixir)
111111
(go-mode . go)
112112
(hcl-mode . hcl)
113+
(terraform-mode . hcl)
113114
(html-mode . html)
114115
(mhtml-mode . html)
115116
(nix-mode . nix)
@@ -144,13 +145,18 @@ See `tree-sitter-langs-repos'."
144145
;;; Normal load.
145146
(tree-sitter-langs--init-major-mode-alist)
146147

147-
(defun tree-sitter-langs--hl-query-path (lang-symbol)
148+
(defun tree-sitter-langs--hl-query-path (lang-symbol &optional mode)
149+
"Return the highlighting query file for LANG-SYMBOL.
150+
If MODE is non-nil, return the file containing additional MODE-specfic patterns
151+
instead. An example is `terraform-mode'-specific highlighting patterns for HCL."
148152
(concat (file-name-as-directory
149153
(concat tree-sitter-langs--queries-dir
150154
(symbol-name lang-symbol)))
151-
"highlights.scm"))
155+
(if mode
156+
(format "highlights.%s.scm" mode)
157+
"highlights.scm")))
152158

153-
(defun tree-sitter-langs--hl-default-patterns (lang-symbol)
159+
(defun tree-sitter-langs--hl-default-patterns (lang-symbol &optional mode)
154160
"Return the bundled default syntax highlighting patterns for LANG-SYMBOL.
155161
Return nil if there are no bundled patterns."
156162
(condition-case nil
@@ -162,6 +168,11 @@ Return nil if there are no bundled patterns."
162168
('typescript '(javascript))
163169
('tsx '(typescript javascript))
164170
(_ nil))))
171+
(when mode
172+
(ignore-error 'file-missing
173+
(insert-file-contents (tree-sitter-langs--hl-query-path sym mode))
174+
(goto-char (point-max))
175+
(insert "\n")))
165176
(insert-file-contents (tree-sitter-langs--hl-query-path sym))
166177
(goto-char (point-max))
167178
(insert "\n"))
@@ -174,7 +185,7 @@ Return nil if there are no bundled patterns."
174185
(unless tree-sitter-hl-default-patterns
175186
(let ((lang-symbol (tsc--lang-symbol tree-sitter-language)))
176187
(setq tree-sitter-hl-default-patterns
177-
(tree-sitter-langs--hl-default-patterns lang-symbol)))))
188+
(tree-sitter-langs--hl-default-patterns lang-symbol major-mode)))))
178189

179190
;;;###autoload
180191
(advice-add 'tree-sitter-hl--setup :before

0 commit comments

Comments
 (0)