-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterraform-ts-mode.el
325 lines (256 loc) · 11.3 KB
/
terraform-ts-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
;;; terraform-ts-mode.el --- Terraform major mode using Treesitter and eglot -*- lexical-binding: t -*-
;;; Copyright (C) 2022-2027 Kai Grotelüschen
;; Author: Kai Grotelueschen <kgr@gnotes.de>
;; Maintainer: Kai Grotelueschen <kgr@gnotes.de>
;; Version: 0.4
;; Keywords: elisp, extensions
;; Homepage: https://github.com/kgrotel/terraform-ts-mode
;; Package-Requires: ((emacs "29.1"))
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of
;; the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see http://www.gnu.org/licenses.
;;; Commentary:
;; This is a terraform mode using treesit. There are still quite some
;; Isues with using Treesitter for imenu and Highlight so any kind of
;; help is greatly appreaciated
;;; Code:
(require 'treesit)
(require 'eglot)
(eval-when-compile (require 'rx))
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-query-capture "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-child "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-node-type "treesit.c")
(defgroup terraform nil
"Support terraform code."
:link '(url-link "https://www.terraform.io/")
:group 'languages)
;; module customizions
(defcustom terraform-ts-mode-hook nil
"Hook called by `terraform-ts-mode'."
:type 'hook
:group 'terraform)
(defcustom terraform-ts-indent-level 2
"The tab width to use when indenting."
:type 'integer
:group 'terraform)
(defcustom terraform-ts-format-on-save t
"Format buffer on save using eglot-format"
:type 'boolean
:group 'terraform)
(defcustom terraform-ts-eglot-debug nil
"enable debugging of eglot (mostly eglot logging) will impact performance"
:type 'boolean
:group 'terraform)
;; module facses
(defface terraform-resource-type-face
'((t :inherit font-lock-type-face))
"Face for resource names."
:group 'terraform-mode)
(defface terraform-resource-name-face
'((t :inherit font-lock-function-name-face))
"Face for resource names."
:group 'terraform-mode)
(defface terraform-builtin-face
'((t :inherit font-lock-builtin-face))
"Face for builtins."
:group 'terraform-mode)
(defface terraform-variable-name-face
'((t :inherit font-lock-variable-name-face))
"Face for varriables."
:group 'terraform-mode)
;; mode vars
(defvar terraform-ts--syntax-table
(let ((synTable (make-syntax-table)))
;; Word syntax
(modify-syntax-entry ?_ "w" synTable) ; underscore is always part of word (never punctiation) -> w
(modify-syntax-entry '(?0 . ?9) "w" synTable)
(modify-syntax-entry '(?a . ?z) "w" synTable)
(modify-syntax-entry '(?A . ?Z) "w" synTable)
;; Punctuation
(modify-syntax-entry ?- "_" synTable) ; - can be word and punctiation -> _ class
(modify-syntax-entry ?= "." synTable)
(modify-syntax-entry ?= "." synTable)
;; Whitespace
(modify-syntax-entry ?\s " " synTable)
(modify-syntax-entry ?\xa0 " " synTable) ; non-breaking space
(modify-syntax-entry ?\t " " synTable)
(modify-syntax-entry ?\f " " synTable)
;; Brackets
(modify-syntax-entry ?\( "()" synTable)
(modify-syntax-entry ?\) ")(" synTable)
(modify-syntax-entry ?\[ "(]" synTable)
(modify-syntax-entry ?\] ")[" synTable)
(modify-syntax-entry ?\{ "(}" synTable)
(modify-syntax-entry ?\} "){" synTable)
;; Comments
(modify-syntax-entry ?# "<" synTable) ; comment-start-class: single line comment
(modify-syntax-entry ?\n ">" synTable) ; comment-end-class: eol
(modify-syntax-entry ?/ ". 124" synTable) ; punctuation but also comment: can be // or (12) or /* (1) */ (4)
(modify-syntax-entry ?* ". 23b" synTable) ; punctuation but also comment: ca be /* (2) */ (3)
;; Others
(modify-syntax-entry ?\" "\"" synTable) ; string
(modify-syntax-entry ?\\ "\\" synTable) ; escape
synTable)
"Syntax table for `terraform-ts-mode'.")
;; Imenu
;; MODE VARS
(defvar terraform-ts--builtin-attributes
'("for_each" "count" "source" "type" "default" "providers" "provider")
"Terraform builtin attributes for tree-sitter font-locking.")
(defvar terraform-ts--builtin-expressions
'("local" "each" "count")
"Terraform builtin expressions for tree-sitter font-locking.")
(defvar terraform-ts--named-expressions
'("var" "module")
"Terraform named expressions for tree-sitter font-locking.")
(defvar terraform-ts--treesit-font-lock-rules
(treesit-font-lock-rules
:language 'terraform
:feature 'comments
'((comment) @font-lock-comment-face) ;; checkOK
:language 'terraform
:feature 'brackets
'(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face) ;; checkOK
:language 'terraform
:feature 'delimiters
'(["." ".*" "," "[*]" "=>"] @font-lock-delimiter-face) ;; checkOK
:language 'terraform
:feature 'operators
'(["!"] @font-lock-negation-char-face)
:language 'terraform
:feature 'operators
'(["\*" "/" "%" "\+" "-" ">" ">=" "<" "<=" "==" "!=" "&&" "||"] @font-lock-operator-face)
:language 'terraform
:feature 'builtin
'((function_call (identifier) @font-lock-builtin-face)) ;; checkOK
:language 'terraform
:feature 'objects
'((object_elem key: (expression (variable_expr (identifier) @font-lock-property-name-face))))
:language 'terraform
:feature 'expressions
`(
((expression (variable_expr (identifier) @terraform-builtin-face)
(get_attr (identifier) @font-lock-property-name-face))
(:match ,(rx-to-string `(seq bol (or ,@terraform-ts--builtin-expressions) eol)) @terraform-builtin-face)) ; local, each and count
((expression (variable_expr (identifier) @terraform-builtin-face)
:anchor (get_attr (identifier) @font-lock-function-call-face)
(get_attr (identifier) @font-lock-property-name-face) :* )
(:match ,(rx-to-string `(seq bol (or ,@terraform-ts--named-expressions) eol)) @terraform-builtin-face)) ; module and var
((expression (variable_expr (identifier) @terraform-builtin-face)
:anchor (get_attr (identifier) @terraform-resource-type-face)
(get_attr (identifier) @terraform-resource-name-face)
(get_attr (identifier) @font-lock-property-name-face) :* )
(:match "data" @terraform-builtin-face))
((expression (variable_expr (identifier) @terraform-resource-type-face)
:anchor (get_attr (identifier) @terraform-resource-name-face)
(get_attr (identifier) @font-lock-property-name-face) :* )) ; that should be a resource
)
:language 'terraform
:feature 'interpolation
'((interpolation "#{" @font-lock-misc-punctuation-face)
(interpolation "}" @font-lock-misc-punctuation-face))
:language 'terraform
:feature 'blocks
`(
((attribute (identifier) @terraform-builtin-face) (:match ,(rx-to-string `(seq bol (or ,@terraform-ts--builtin-attributes) eol)) @terraform-builtin-face))
((attribute (identifier) @terraform-variable-name-face))
)
:language 'terraform
:feature 'blocks
'(
((block (identifier) @terraform-builtin-face (string_lit (template_literal) @font-lock-type-face) (string_lit (template_literal) @font-lock-function-name-face)))
)
:language 'terraform
:feature 'blocks
'(
((block (identifier) @terraform-builtin-face (string_lit (template_literal) @font-lock-function-name-face) :?))
)
:language 'terraform
:feature 'conditionals
'(["if" "else" "endif"] @font-lock-keyword-face)
:language 'terraform
:feature 'constants
'((bool_lit) @font-lock-constant-face) ;; checkOK
:language 'terraform
:feature 'numbers
'((numeric_lit) @font-lock-number-face) ;; checkOK
:language 'terraform
:feature 'strings
'((string_lit (template_literal)) @font-lock-string-face)
)
"Tree-sitter font-lock settings.")
(defvar terraform-ts--indent-rules
`((terraform
((node-is "block_end") parent-bol 0)
((node-is "object_end") parent-bol 0)
((node-is ")") parent-bol 0)
((node-is "tuple_end") parent-bol 0)
((parent-is "function_call") parent-bol ,terraform-ts-indent-level)
((parent-is "object") parent-bol ,terraform-ts-indent-level)
((parent-is "tuple") parent-bol ,terraform-ts-indent-level)
((parent-is "block") parent-bol ,terraform-ts-indent-level))))
;; Major Mode def
(define-derived-mode terraform-ts-mode prog-mode "Terraform"
"Terraform Tresitter Mode"
:group 'terraform
:syntax-table terraform-ts--syntax-table
;; treesit - add terraform grammar
(add-to-list 'treesit-language-source-alist
'(terraform . ("https://github.com/MichaHoffmann/tree-sitter-hcl" "main" "dialects/terraform/src")))
;; treesit - check grammar is readdy if not most likly in need to be installed
(unless (treesit-ready-p 'terraform)
(treesit-install-language-grammar 'terraform))
;; treesit - init parser
(treesit-parser-create 'terraform)
;; eglot - integrate mode into terraform-ts-mode
(add-hook 'terraform-ts-mode-hook 'eglot-ensure)
(with-eval-after-load 'eglot
(put 'terraform-ts-mode 'eglot-language-id "terraform")
(add-to-list 'eglot-server-programs
'(terraform-ts-mode . ("terraform-ls" "serve"))))
;; eglot - use format on save
(if terraform-ts-format-on-save
(add-hook 'before-save-hook 'eglot-format)
(remove-hook 'before-save-hook 'eglot-format))
;; eglot - disable debugging eglot - increase performance
(unless terraform-ts-eglot-debug
(fset #'jsonrpc--log-event #'ignore) ; disable eglot event logging
(setq eglot-events-buffer-size 0) ; decrease event logging buffer (not needed see above)
(setq eglot-sync-connect nil) ; disabling waiting for eglot sync done / might mean that eglot is not avail at opening file
)
(setq-local comment-start "#")
(setq-local comment-use-syntax t)
(setq-local comment-start-skip "\\(//+\\|/\\*+\\)\\s *")
;; Electric
(setq-local electric-indent-chars (append "{}[]()" electric-indent-chars))
;; Indent.
(setq-local treesit-simple-indent-rules terraform-ts--indent-rules)
;; Navigation.
;; (setq-local treesit-defun-type-regexp (rx (or "pair" "object")))
;; (setq-local treesit-defun-name-function #'json-ts-mode--defun-name)
;; (setq-local treesit-sentence-type-regexp "pair")
;; Font-lock
(setq-local treesit-font-lock-feature-list '((comments)
(keywords attributes blocks strings numbers constants objects output modules workspaces vars)
(builtin brackets delimiters expressions operators interpolations conditionals)
()))
(setq-local treesit-font-lock-settings terraform-ts--treesit-font-lock-rules)
;; Imenu ... todo
;; (setq-local treesit-simple-imenu-settings
;; `((nil "block" nil nil)))
(treesit-major-mode-setup))
;;; autoload
(add-to-list 'auto-mode-alist '("\\.tf\\(vars\\)?\\'" . terraform-ts-mode))
(provide 'terraform-ts-mode)
;;; terraform-ts-mode.el ends here