Skip to content

Commit eede960

Browse files
authored
Merge pull request #98 from emacs-tree-sitter/hcl
Improve HCL support !bump-version 0.12.0
2 parents 0dd5e56 + 86080fb commit eede960

File tree

7 files changed

+145
-31
lines changed

7 files changed

+145
-31
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
branch = master
160160
[submodule "repos/hcl"]
161161
path = repos/hcl
162-
url = https://github.com/mitchellh/tree-sitter-hcl.git
162+
url = https://github.com/MichaHoffmann/tree-sitter-hcl
163163
update = none
164164
ignore = dirty
165165
branch = master

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22

33
## Unreleased
4+
- Used a different `hcl` grammar.
5+
- Added support for mode-specific highlighting patterns.
6+
- Added highlighting patterns for `terraform-mode`.
47

58
## 0.11.6 - 2022-03-28
69
- Updated `bash` grammar.

README.org

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For each supported language, this package provides:
1919
** Highlighting Queries
2020
# *Note*: Highlighting styles are a mattter of taste.
2121

22-
Highlighting query patterns for a language are in the file ~queries/{lang}/highlights.scm~. Most of them are *intentionally different* from those from upstream repositories, which are more geared towards /GitHub's use cases/. We try to be more consistent with /Emacs's existing conventions/. (For some languages, this is WIP, so their patterns may look similar to upstream's.)
22+
Highlighting query patterns for a language are in the file ~queries/<lang>/highlights.scm~. Most of them are *intentionally different* from those from upstream repositories, which are more geared towards /GitHub's use cases/. We try to be more consistent with /Emacs's existing conventions/. (For some languages, this is WIP, so their patterns may look similar to upstream's.)
2323

2424
In general, try to follow what the docstrings of ~tree-sitter-hl-face:~ faces say. Most importantly:
2525
- Definitions and uses should be differentiated:
@@ -30,6 +30,9 @@ In general, try to follow what the docstrings of ~tree-sitter-hl-face:~ faces sa
3030
- Special faces should have high priority (placed earlier in the pattern list): ~@function.macro~, ~@type.builtin~, ~@variable.special~.
3131
- Patterns whose internals may be highlighted should have low priority (placed towards the end). Example: strings with interpolation.
3232

33+
*** Mode-specific highlighting
34+
Some languages are associated with multiple major modes. Mode-specific highlighting patterns are provided by the files ~queries/<lang>/highlights.<major-mode>.scm~. These are combined with the base highlighting patterns in ~queries/<lang>/highlights.scm~, but have higher precedence.
35+
3336
** Building Grammars from Source
3437
Note: If you also plan to work on [[https://github.com/emacs-tree-sitter/elisp-tree-sitter#building-grammars-from-source][elisp-tree-sitter]], it might be more convenient to work with this repository as a submodule.
3538

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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 (quoted_template_start) @noise
33+
(template_literal) @variable
34+
(quoted_template_end) @noise)])
35+
(.eq? @keyword "dynamic"))
36+
37+
;; ((block (identifier) @keyword
38+
;; [(string_lit (template_literal) @variable)]
39+
;; (body ((attribute (identifier) @keyword)
40+
;; (.eq? @keyword "for_each"))))
41+
;; (.eq? @keyword "dynamic"))
42+
43+
((block (identifier) @keyword
44+
. [(string_lit (quoted_template_start) @noise
45+
(template_literal) @function
46+
(quoted_template_end) @noise)
47+
(identifier) @function])
48+
(.eq? @keyword "output"))
49+
50+
((block (identifier) @keyword
51+
[(string_lit (quoted_template_start) @noise
52+
(template_literal) @variable.special
53+
(quoted_template_end) @noise)
54+
(identifier) @variable.special])
55+
(.eq? @keyword "variable"))
56+
57+
((block (identifier) @keyword
58+
[(string_lit (quoted_template_start) @noise
59+
(template_literal) @type
60+
(quoted_template_end) @noise)
61+
(identifier) @type])
62+
(.eq? @keyword "module"))
63+
64+
;; resource "aws_launch_template" "default"
65+
(block (identifier)
66+
[(string_lit (quoted_template_start) @noise
67+
(template_literal) @type
68+
(quoted_template_end) @noise)
69+
(identifier) @type]
70+
. [(string_lit (quoted_template_start) @noise
71+
(template_literal) @function
72+
(quoted_template_end) @noise)
73+
(identifier) @function]
74+
. (block_start))
75+
76+
((block (identifier) @keyword)
77+
(.match? @keyword "(resource|data|output|locals|lifecycle)"))
78+
79+
((attribute (identifier) @keyword)
80+
(.match? @keyword "^(count|depends_on|for_each)$"))
81+
82+
(attribute (identifier) @variable)
83+
(object_elem key: (_) @variable)
84+
85+
(block (identifier) @label . (block_start))

repos/hcl

Submodule hcl updated from a8bd256 to b048a42

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)