-
Notifications
You must be signed in to change notification settings - Fork 1
/
zig-ts-mode.el
735 lines (624 loc) · 23.2 KB
/
zig-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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
;;; zig-ts-mode.el --- major mode for zig -*- lexical-binding: t; -*-
;; Author: 2023 Rylee Lyman <[email protected]>
;;
;; URL: https://github.com/ryleelyman/zig-ts-mode
;; Version: 0.0.1
;; Package-Requires: ((emacs "29.0") (reformatter "0.6"))
;;
;; This file is NOT part of Emacs.
;;
;; 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 <https://www.gnu.org/licenses/>.
;; Keywords: languages, processes, tools
;;; Commentary:
;;; Code:
(require 'treesit)
(require 'reformatter)
(defgroup zig-ts-mode nil
"Tree-sitter powered support for Zig code."
:link '(url-link "https://ziglang.org/")
:group 'languages)
(defcustom zig-indent-offset 4
"Indent Zig code by this number of spaces."
:type 'integer
:group 'zig-ts-mode
:safe #'integerp)
(defcustom zig-format-on-save t
"Format buffers before saving using zig fmt."
:type 'boolean
:safe #'booleanp
:group 'zig-ts-mode)
(defcustom zig-format-show-buffer t
"Show a *zig-fmt* buffer after zig fmt completes with errors."
:type 'boolean
:safe #'booleanp
:group 'zig-ts-mode)
(defcustom zig-zig-bin "zig"
"Path to zig executable."
:type 'file
:safe #'stringp
:group 'zig-ts-mode)
(defcustom zig-run-optimization-mode "Debug"
"Optimization mode to run code with."
:type 'string
:safe #'stringp
:group 'zig-ts-mode)
(defcustom zig-test-optimization-mode "Debug"
"Optimization mode to run tests with."
:type 'string
:safe #'stringp
:group 'zig-ts-mode)
;; zig CLI commands
(defun zig--run-cmd (cmd &optional source &rest args)
"Use compile command to execute a zig CMD with ARGS if given.
If given a SOURCE, execute the CMD on it."
(let ((cmd-args
(if source
(mapconcat 'shell-quote-argument (cons source args) " ")
args)))
(compilation-start (concat zig-zig-bin " " cmd " " cmd-args))))
;;;###autoload
(defun zig-compile ()
"Compile using `zig build`."
(interactive)
(zig--run-cmd "build"))
;;;###autoload
(defun zig-build-exe ()
"Create executable from source or object file."
(interactive)
(zig--run-cmd "build-exe" (file-local-name (buffer-file-name))))
;;;###autoload
(defun zig-build-lib ()
"Create library from source or assembly."
(interactive)
(zig--run-cmd "build-lib" (file-local-name (buffer-file-name))))
;;;###autoload
(defun zig-build-obj ()
"Create object from source or assembly."
(interactive)
(zig--run-cmd "build-obj" (file-local-name (buffer-file-name))))
;;;###autoload
(defun zig-test-buffer ()
"Test buffer using `zig test`."
(interactive)
(zig--run-cmd "test" (file-local-name (buffer-file-name)) "-O" zig-test-optimization-mode))
;;;###autoload
(defun zig-run ()
"Create an executable from the current buffer and run it immediately."
(interactive)
(zig--run-cmd "run" (file-local-name (buffer-file-name)) "-O" zig-run-optimization-mode))
;; zig fmt
(reformatter-define zig-format
:program zig-zig-bin
:args '("fmt" "--stdin")
:group 'zig-ts-mode
:lighter " ZigFmt")
;;;###autoload (autoload 'zig-format-buffer "current-file" nil t)
;;;###autoload (autoload 'zig-format-region "current-file" nil t)
;;;###autoload (autoload 'zig-format-on-save-mode "current-file" nil t)
(defun zig-re-word (inner)
"Construct a regular expression for the word INNER."
(concat "\\<" inner "\\>"))
(defun zig-re-grab (inner)
"Construct a group regular expression for INNER."
(concat "\\(" inner "\\)"))
(defconst zig-re-optional "\\(?:[[:space:]]*\\?[[:space:]]*\\)")
(defconst zig-re-pointer "\\(?:[[:space:]]*\\*\\(?:const[[:space:]]*\\)?[[:space:]]*\\)")
(defconst zig-re-array "\\(?:[[:space:]]*\\[[^]]*\\]\\(?:const[[:space:]]*\\)?[[:space:]]*\\)")
(defconst zig-re-optionals-pointers-arrays
(concat "\\(?:" zig-re-optional "\\|" zig-re-pointer "\\|" zig-re-array "\\)*"))
(defconst zig-re-identifier "[[:word:]_][[:word:]_[:digit:]]*")
(defconst zig-re-type-annotation
(concat (zig-re-grab zig-re-identifier)
"[[:space:]]*:[[:space:]]*"
zig-re-optionals-pointers-arrays
(zig-re-grab zig-re-identifier)))
(defun zig-re-definition (dtype)
"Construct a regular expression for definitions of type DTYPE."
(concat (zig-re-word dtype) "[[:space:]]+" (zig-re-grab zig-re-identifier)))
(defconst zig-mode-syntax-table
(let ((table (make-syntax-table)))
;; Operators
(dolist (i '(?+ ?- ?* ?/ ?% ?& ?| ?= ?! ?< ?>))
(modify-syntax-entry i "." table))
;; Strings
(modify-syntax-entry ?\' "\"" table)
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?\\ "\\" table)
;; Comments
(modify-syntax-entry ?/ ". 12" table)
(modify-syntax-entry ?\n ">" table)
table))
(defconst zig-keywords
'(
;; Storage
"const" "var" "extern" "packed" "export" "pub" "noalias" "inline"
"noinline" "comptime" "callconv" "volatile" "allowzero"
"align" "linksection" "threadlocal" "addrspace"
;; Structure
"struct" "enum" "union" "error" "opaque"
;; Statement
"break" "return" "continue" "asm" "defer" "errdefer" "unreachable"
"try" "catch" "async" "nosuspend" "await" "suspend" "resume"
;; Conditional
"if" "else" "switch" "and" "or" "orelse"
;; Repeat
"while" "for"
;; Other keywords
"fn" "usingnamespace" "test"))
(defconst zig-types
'(
;; Integer types
"i2" "u2" "i3" "u3" "i4" "u4" "i5" "u5" "i6" "u6" "i7" "u7" "i8" "u8"
"i16" "u16" "i29" "u29" "i32" "u32" "i64" "u64" "i128" "u128"
"isize" "usize"
;; Floating types
"f16" "f32" "f64" "f80" "f128"
;; C types
"c_char" "c_short" "c_ushort" "c_int" "c_uint" "c_long" "c_ulong"
"c_longlong" "c_ulonglong" "c_longdouble"
;; Comptime types
"comptime_int" "comptime_float"
;; Other types
"bool" "void" "noreturn" "type" "error" "anyerror" "anyframe" "anytype"
"anyopaque"))
(defconst zig-constants
'(
;; Boolean
"true" "false"
;; Other constants
"null" "undefined"))
(defconst zig-electric-indent-chars
'(?\; ?, ?\) ?\] ?}))
(defface zig-multiline-string-face
'((t :inherit font-lock-string-face))
"Face for multiline string literals."
:group 'zig-mode)
(defvar zig-font-lock-keywords
(append
`(
;; Builtins (prefixed with @)
(,(concat "@" zig-re-identifier) . font-lock-builtin-face)
;; Keywords, constants and types
(,(regexp-opt zig-keywords 'symbols) . font-lock-keyword-face)
(,(regexp-opt zig-constants 'symbols) . font-lock-constant-face)
(,(regexp-opt zig-types 'symbols) . font-lock-type-face)
;; Type annotations (both variable and type)
(,zig-re-type-annotation 1 font-lock-variable-name-face)
(,zig-re-type-annotation 2 font-lock-type-face)
)
;; Definitions
(mapcar (lambda (x)
(list (zig-re-definition (car x))
1 (cdr x)))
'(("const" . font-lock-variable-name-face)
("var" . font-lock-variable-name-face)
("fn" . font-lock-function-name-face)))))
(defun zig-paren-nesting-level () (nth 0 (syntax-ppss)))
(defun zig-currently-in-str () (nth 3 (syntax-ppss)))
(defun zig-start-of-current-str-or-comment () (nth 8 (syntax-ppss)))
(defun zig-skip-backwards-past-whitespace-and-comments ()
(while (or
;; If inside a comment, jump to start of comment.
(let ((start (zig-start-of-current-str-or-comment)))
(and start
(not (zig-currently-in-str))
(goto-char start)))
;; Skip backwards past whitespace and comment end delimiters.
(/= 0 (skip-syntax-backward " >")))))
(defun zig-in-str-or-cmnt () (nth 8 (syntax-ppss)))
(defconst zig-top-item-beg-re
(concat "^ *"
(regexp-opt
'("pub" "extern" "export" ""))
"[[:space:]]*"
(regexp-opt
'("fn" "test"))
"[[:space:]]+")
"Start of a Zig item.")
(defun zig-beginning-of-defun (&optional arg)
"Move backward to the beginning of the current defun.
With ARG, move backward multiple defuns. Negative ARG means
move forward.
This is written mainly to be used as `beginning-of-defun-function' for Zig."
(interactive "p")
(let* ((arg (or arg 1))
(magnitude (abs arg))
(sign (if (< arg 0) -1 1)))
;; If moving forward, don't find the defun we might currently be
;; on.
(when (< sign 0)
(end-of-line))
(catch 'done
(dotimes (_ magnitude)
;; Search until we find a match that is not in a string or comment.
(while (if (re-search-backward (concat "^[[:space:]]*\\(" zig-top-item-beg-re "\\)")
nil 'move sign)
(zig-in-str-or-cmnt)
;; Did not find it.
(throw 'done nil)))))
t))
(defun zig-end-of-defun ()
"Move forward to the next end of defun.
With argument, do it that many times.
Negative argument -N means move back to Nth preceding end of defun.
Assume that this is called after `beginning-of-defun'. So point is
at the beginning of the defun body.
This is written mainly to be used as `end-of-defun-function' for Zig."
(interactive)
;; Jump over the function parameters and paren-wrapped return, if they exist.
(while (re-search-forward "(" (line-end-position) t)
(progn
(backward-char)
(forward-sexp)))
;; Find the opening brace
(if (re-search-forward "[{]" nil t)
(progn
(goto-char (match-beginning 0))
;; Go to the closing brace
(condition-case nil
(forward-sexp)
(scan-error
(goto-char (point-max))))
(end-of-line))
;; There is no opening brace, so consider the whole buffer to be one "defun"
(goto-char (point-max))))
(defun zig-mode-indent-line ()
(interactive)
;; First, calculate the column that this line should be indented to.
(let ((indent-col
(save-excursion
(back-to-indentation)
(let* (;; paren-level: How many sets of parens (or other delimiters)
;; we're within, except that if this line closes the
;; innermost set(s) (e.g. the line is just "}"), then we
;; don't count those set(s).
(paren-level
(save-excursion
(while (looking-at "[]})]") (forward-char))
(zig-paren-nesting-level)))
;; prev-block-indent-col: If we're within delimiters, this is
;; the column to which the start of that block is indented
;; (if we're not, this is just zero).
(prev-block-indent-col
(if (<= paren-level 0) 0
(save-excursion
(while (>= (zig-paren-nesting-level) paren-level)
(backward-up-list)
(back-to-indentation))
(current-column))))
;; base-indent-col: The column to which a complete expression
;; on this line should be indented.
(base-indent-col
(if (<= paren-level 0)
prev-block-indent-col
(or (save-excursion
(backward-up-list)
(forward-char)
(and (not (looking-at " *\\(//[^\n]*\\)?\n"))
(current-column)))
(+ prev-block-indent-col zig-indent-offset))))
;; is-expr-continutation: True if this line continues an
;; expression from the previous line, false otherwise.
(is-expr-continutation
(and
(not (looking-at "[]});]\\|else"))
(save-excursion
(zig-skip-backwards-past-whitespace-and-comments)
(when (> (point) 1)
(backward-char)
(not (looking-at "[,;([{}]")))))))
;; Now we can calculate indent-col:
(if is-expr-continutation
(+ base-indent-col zig-indent-offset)
base-indent-col)))))
;; If point is within the indentation whitespace, move it to the end of the
;; new indentation whitespace (which is what the indent-line-to function
;; always does). Otherwise, we don't want point to move, so we use a
;; save-excursion.
(if (<= (current-column) (current-indentation))
(indent-line-to indent-col)
(save-excursion (indent-line-to indent-col)))))
(defun zig-syntax-propertize-to-newline-if-in-multiline-str (end)
;; First, we need to check if we're in a multiline string literal; if we're
;; not, do nothing.
(when (zig-currently-in-str)
(let ((start (zig-start-of-current-str-or-comment)))
(when (save-excursion
(goto-char start)
(looking-at "\\\\\\\\"))
;; At this point, we've determined that we're within a multiline string
;; literal. Let `stop' be the position of the closing newline, or
;; `end', whichever comes first.
(let ((stop (if (save-excursion
(goto-char start)
(re-search-forward "\n" end t))
(prog1 (match-end 0)
;; We found the closing newline, so mark it as the
;; end of this string literal.
(put-text-property (match-beginning 0)
(match-end 0)
'syntax-table
(string-to-syntax "|")))
end)))
;; Zig multiline string literals don't support escapes, so mark all
;; backslashes (up to `stop') as punctation instead of escapes.
(save-excursion
(goto-char (1+ start))
(while (re-search-forward "\\\\" stop t)
(put-text-property (match-beginning 0) (match-end 0)
'syntax-table (string-to-syntax "."))
(goto-char (match-end 0))))
;; Move to the end of the string (or `end'), so that
;; zig-syntax-propertize can pick up from there.
(goto-char stop))))))
(defun zig-syntax-propertize (start end)
(goto-char start)
(zig-syntax-propertize-to-newline-if-in-multiline-str end)
(funcall
(syntax-propertize-rules
;; Multiline strings
;; Do not match backslashes that are preceded by single or
;; double-quotes.
("[^\\'\"]c?\\(\\\\\\)\\\\"
(1 (prog1 "|"
(goto-char (match-end 0))
(zig-syntax-propertize-to-newline-if-in-multiline-str end)))))
(point) end))
(defun zig-mode-syntactic-face-function (state)
(if (nth 3 state)
(save-excursion
(goto-char (nth 8 state))
(if (looking-at "\\\\\\\\")
'zig-multiline-string-face
'font-lock-string-face))
(save-excursion
(goto-char (nth 8 state))
(if (looking-at "//[/|!][^/]")
'font-lock-doc-face
'font-lock-comment-face))))
;;; Imenu support
(defun zig-re-structure-def-imenu (stype)
"Construct a regular expression for strucutres definitions of type STYPE."
(concat (zig-re-word "const") "[[:space:]]+"
(zig-re-grab zig-re-identifier)
".*"
(zig-re-word stype)))
(defvar zig-imenu-generic-expression
(append (mapcar (lambda (x)
(list (capitalize x) (zig-re-structure-def-imenu x) 1))
'("enum" "struct" "union"))
`(("Fn" ,(zig-re-definition "fn") 1))))
;;; Guarantee filesystem unix line endings
(defun zig-file-coding-system ()
(with-current-buffer (current-buffer)
(if (buffer-file-name)
(if (string-match "\\.d?zig\\'" buffer-file-name)
(setq buffer-file-coding-system 'utf-8-unix)
nil))
))
(add-hook 'zig-mode-hook 'zig-file-coding-system)
(defvar zig-ts-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-b") 'zig-compile)
(define-key map (kbd "C-c C-f") 'zig-format-buffer)
(define-key map (kbd "C-c C-r") 'zig-run)
(define-key map (kbd "C-c C-t") 'zig-test-buffer)
map)
"Keymap for Zig major mode.")
(defvar zig--treesit-font-lock-setting
(treesit-font-lock-rules
:feature 'comment
:language 'zig
'([
(container_doc_comment)
(doc_comment)
(line_comment)
] @font-lock-comment-face)
:feature 'brackets
:language 'zig
'([
"[" "]" "(" ")" "{" "}"
(Payload "|")
(PtrPayload "|")
(PtrIndexPayload "|")
] @font-lock-bracket-face)
:feature 'misc-punctuation
:language 'zig
'([ ".." "..." ] @font-lock-misc-punctuation-face)
:feature 'delimiters
:language 'zig
'([ ";" "." "," ":" ] @font-lock-delimiter-face)
:feature 'operators
:language 'zig
'([
(CompareOp)
(BitwiseOp)
(BitShiftOp)
(AdditionOp)
(AssignOp)
(MultiplyOp)
(PrefixOp)
"*"
"**"
"=>"
".?"
".*"
"?"] @font-lock-operator-face)
:feature 'builtin-constants
:language 'zig
'(["null" "unreachable" "undefined"] @font-lock-builtin-face)
:feature 'attributes
:language 'zig
'([
"comptime"
"export"
"extern"
"inline"
"noinline"
"packed"
"pub"
"threadlocal"] @font-lock-keyword-face)
:feature 'storageclass
:language 'zig
'([
"addrspace"
"align"
"callconv"
"linksection"
] @font-lock-type-face)
:feature 'type-qualifier
:language 'zig
'([
"const"
"var"
"volatile"
"allowzero"
"noalias"
] @font-lock-keyword-face)
:feature 'builtin-types
:language 'zig
'([
"anytype"
(BuildinTypeExpr)
] @font-lock-type-face)
:feature 'try-catch
:language 'zig
:override t
'(["try" "catch"] @font-lock-keyword-face)
:feature 'include
:language 'zig
'(["usingnamespace"] @font-lock-keyword-face)
:feature 'keywords
:language 'zig
'(["asm" "defer" "errdefer" "test" "struct" "union" "enum" "opaque" "error"]
@font-lock-keyword-face)
:feature 'logic
:language 'zig
'(["and" "or" "orelse"] @font-lock-operator-face)
:feature 'async
:language 'zig
'(["async" "await" "suspend" "nosuspend" "resume"] @font-lock-keyword-face)
:feature 'fn
:language 'zig
'(["fn"] @font-lock-keyword-face)
:feature 'conditional
:language 'zig
'(["if" "else" "switch"] @font-lock-keyword-face)
:feature 'repeat
:language 'zig
'(["for" "while" "break" "continue"] @font-lock-keyword-face)
:feature 'labels
:language 'zig
'(((BreakLabel (IDENTIFIER) @font-lock-keyword-face))
((BlockLabel (IDENTIFIER) @font-lock-keyword-face)))
:feature 'string-special
:language 'zig
'(((CHAR_LITERAL) @font-lock-escape-face)
((EscapeSequence) @font-lock-string-face)
((FormatSequence) @font-lock-string-face))
:feature 'string
:language 'zig
'([
(LINESTRING)
(STRINGLITERALSINGLE)
] @font-lock-string-face)
:feature 'boolean
:language 'zig
'([ "true" "false" ] @font-lock-builtin-face)
:feature 'numbers
:language 'zig
'(((INTEGER) @font-lock-number-face)
((FLOAT) @font-lock-number-face))
:feature 'builtin-calls
:language 'zig
'((BUILTINIDENTIFIER) @font-lock-builtin-face)
:feature 'enum-constants
:language 'zig
'(
("." field_constant: (IDENTIFIER) @font-lock-constant-face)
((ErrorSetDecl field_constant: (IDENTIFIER) @font-lock-constant-face)))
:feature 'misc-builtins
:language 'zig
'((
((IDENTIFIER) @font-lock-builtin-face)
(:equal @font-lock-builtin-face "_")
)
((FnProto exception: "!" @font-lock-builtin-face))
((PtrTypeStart "c" @font-lock-builtin-face)))
:feature 'function
:language 'zig
'(((FnProto function: (IDENTIFIER) @font-lock-function-name-face))
((SuffixExpr variable_type_function: (IDENTIFIER) @font-lock-function-call-face (FnCallArguments)))
((FieldOrFnCall function_call: (IDENTIFIER) @font-lock-function-call-face)))
:feature 'variables
:language 'zig
'(((SuffixExpr variable_type_function: (IDENTIFIER) @font-lock-variable-use-face))
((VarDecl variable_type_function: (IDENTIFIER) @font-lock-variable-name-face))
((ParamDecl parameter: (IDENTIFIER) @font-lock-variable-name-face))
((FieldOrFnCall field_access: (IDENTIFIER) @font-lock-variable-use-face))
((ContainerField field_member: (IDENTIFIER) @font-lock-variable-name-face)))
:feature 'types
:override t
:language 'zig
'(([
(VarDecl variable_type_function: (IDENTIFIER) @font-lock-type-face)
(SuffixExpr variable_type_function: (IDENTIFIER) @font-lock-type-face)
(ParamDecl parameter: (IDENTIFIER) @font-lock-type-face)
(FieldOrFnCall field_access: (IDENTIFIER) @font-lock-type-face)
]
(:match "^[A-Z]\\([a-z]+[A-Za-z_0-9]*\\)*$" @font-lock-type-face)))
:feature 'constants
:override t
:language 'zig
'(([
(VarDecl variable_type_function: (IDENTIFIER) @font-lock-constant-face)
(SuffixExpr variable_type_function: (IDENTIFIER) @font-lock-constant-face)
(FieldOrFnCall field_access: (IDENTIFIER) @font-lock-constant-face)
]
(:match "^[A-Z][A-Z_0-9]+$" @font-lock-constant-face)))
)
"Tree-sitter font-lock settings.")
;;;###autoload
(define-derived-mode zig-ts-mode prog-mode "Zig-ts"
"A tree-sitter-powered major mode for the Zig programming language.
\\{zig-ts-mode-map}"
:group 'zig-ts-mode
(setq-local comment-start "// ")
(setq-local comment-start-skip "//+ *")
(setq-local comment-end "")
(setq-local electric-indent-chars
(append zig-electric-indent-chars
(and (boundp 'electric-indent-chars)
electric-indent-chars)))
(setq-local beginning-of-defun-function 'zig-beginning-of-defun)
(setq-local end-of-defun-function 'zig-end-of-defun)
(setq-local indent-line-function 'zig-mode-indent-line)
(setq-local indent-tabs-mode nil) ; Zig forbids tab characters.
(setq-local syntax-propertize-function 'zig-syntax-propertize)
(setq-local imenu-generic-expression zig-imenu-generic-expression)
(when zig-format-on-save
(zig-format-on-save-mode 1))
(when (treesit-ready-p 'zig)
(treesit-parser-create 'zig)
(setq-local treesit-font-lock-feature-list
'((comment string numbers)
(builtin-constants attributes keywords builtin-types type-qualifier function try-catch)
(operators repeat conditional fn async logic include storageclass enum-constants builtin-calls boolean types)
(brackets delimiters misc-punctuation labels misc-builtins string-special constants variables)))
(setq-local treesit-font-lock-settings zig--treesit-font-lock-setting)
(treesit-major-mode-setup)))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.\\(zig\\|zon\\)\\'" . zig-ts-mode))
(provide 'zig-ts-mode)
;;; zig-ts-mode.el ends here