-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathearthfile-mode.el
114 lines (99 loc) · 2.97 KB
/
earthfile-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
;;; earthfile-mode.el --- Major mode for editing Earthly file -*- lexical-binding: t -*-
;; Author: Thanabodee Charoenpiriyakij <wingyminus@gmail.com>
;; URL: https://github.com/earthly/earthly-mode
;; Version: 0.1.0
;; Package-Requires: ((emacs "26"))
;; SPDX-License-Identifier: MPL-2.0
;; This file is not part of GNU Emacs.
;;; Commentary:
;; This package provides syntax highlight and toggle comment support
;; for Earthly files.
;;; Code:
(defconst earthfile-keywords-regexp
(rx line-start
(* space)
(or "FROM"
"RUN"
"COPY"
"ARG"
"SAVE ARTIFACT"
"SAVE IMAGE"
"BUILD"
"VERSION"
"GIT CLONE"
"CMD"
"LABEL"
"EXPOSE"
"ENV"
"ENTRYPOINT"
"VOLUME"
"USER"
"WORKDIR"
"HEALTHCHECK NONE"
"HEALTHCHECK CMD"
"FROM DOCKERFILE"
"WITH DOCKER"
"IF"
"ELSE"
"ELSE IF"
"FOR"
"END"
"LOCALLY"
"COMMAND"
"DO"
"IMPORT"
"PROJECT"
"PIPELINE"
"TRIGGER"
"CACHE"
"WAIT"
"TRY"
"FINALLY"
"DOCKER LOAD"
"DOCKER PULL"
"HOST"
"SET"
"LET"
"ADD"
"STOP SIGNAL"
"ONBUILD"
"SHELL")
(or (+ space) line-end))
"All Earthfile keywords.")
(defconst earthfile-variable-regexp
(rx (sequence "$" (? "{") (+ (in (?A . ?Z) (?a . ?z) (?0 . ?9) ?- ?_)) (? "}")))
"Regular Expression for Earthfile variable.")
(defconst earthfile-for-keyword-regexp
(rx line-start (* space) (group "FOR") word-boundary (*? (regex ".")) word-boundary (group "IN") word-boundary)
"Regular Expression for Earthfile FOR .. IN syntax.")
(defconst earthfile-save-artifact-keyword-regexp
(rx line-start (* space) (group "SAVE ARTIFACT") word-boundary (*? (regex ".")) word-boundary (group "AS LOCAL") word-boundary)
"Regular Expression for Earthfile SAVE ARTIFACT .. AS LOCAL syntax.")
(defun earthfile-build-font-lock-keywords ()
"Build font lock for Earthfile syntax."
(list
`(,earthfile-keywords-regexp . font-lock-keyword-face)
`(,earthfile-variable-regexp . font-lock-variable-name-face)
`(,earthfile-for-keyword-regexp (1 font-lock-keyword-face) (2 font-lock-keyword-face))
`(,earthfile-save-artifact-keyword-regexp (1 font-lock-keyword-face) (2 font-lock-keyword-face))))
(defvar earthfile-syntax-table
(let ((syntax-table (make-syntax-table)))
(modify-syntax-entry ?\# "<" syntax-table)
(modify-syntax-entry ?\n ">" syntax-table)
(modify-syntax-entry ?\" "\"" syntax-table)
(modify-syntax-entry ?\' "\"" syntax-table)
syntax-table)
"Syntax table for `earthfile-mode'.")
;;;###autoload
(define-derived-mode earthfile-mode prog-mode "Earthfile"
"A major mode for editing Earthfile file."
:syntax-table earthfile-syntax-table
(setq-local comment-start "#")
(setq-local comment-end "")
(setq-local font-lock-defaults '(earthfile-build-font-lock-keywords)))
;;;###autoload
(progn
(add-to-list 'auto-mode-alist '("Earthfile\\'" . earthfile-mode))
(add-to-list 'auto-mode-alist '("\\.earth\\'" . earthfile-mode)))
(provide 'earthfile-mode)
;;; earthfile-mode.el ends here