-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgren-mode.el
231 lines (197 loc) · 8.86 KB
/
gren-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
;;; gren-mode.el --- a simple package -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Mae Brooks
;; Author: Mae Brooks <[email protected]>
;; Keywords: lisp
;; Version: 0.0.2
;; 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:
;; A Simple Gren highlighting and formatting package
;;; Code:
(require 'prog-mode)
(require 'treesit)
(defcustom gren-tree-sitter-grammar-url "https://github.com/MaeBrooks/tree-sitter-gren"
"The url to use to install the gren tree-sitter grammar the default is: https://github.com/MaeBrooks/tree-sitter-gren")
(defcustom gren-tree-sitter-grammar-revision nil
"The revision - the git tag or branch")
(defun gren-format ()
"Format gren with 'gren format <buffer-file-name>'"
(interactive)
(unless (not buffer-file-name)
(save-buffer)
;
(shell-command (format "gren format %s" buffer-file-name) "*Shell Command Output*" "*Shell Command Output*")
(revert-buffer :ignore-auto :noconfirm)))
(defun gren-mode-setup ())
(defun gren-ts-mode-prepare ()
;; Ensure that gren tree sitter is in the install list
(defun ensure-gren-ts-grammar-is-installable ()
(setq-local default-gren-language-source
`(gren
,gren-tree-sitter-grammar-url
,gren-tree-sitter-grammar-revision))
(if (not (boundp 'treesit-language-source-alist))
(setq treesit-language-source-alist (list default-gren-language-source))
(if (not (assoc 'gren treesit-language-source-alist))
(add-to-list treesit-language-source-alist default-gren-language-source)))))
(defun gren-ts-mode-setup ()
(defun setup-font-lock ()
;; What Decoration Levels are activated
;; (setq treesit-font-lock-level)
;; treesit-font-lock-level is set by the user / defaults to 3 - value of 1 - 4 1 being low highlighting 4 being higher (in general)
;; Decoration Levels / Precidence
(setq treesit-font-lock-feature-list
'((comment string keyword property number)
(type function bracket variable)
(control operator punctuation builtin)))
;; Map
(setq treesit-font-lock-settings
(treesit-font-lock-rules
:language 'gren
:feature 'builtin
:override 't
'((module_declaration (upper_case_qid (upper_case_identifier))) @font-lock-builtin-face
(import_clause (upper_case_qid (upper_case_identifier))) @font-lock-builtin-face
(value_qid (upper_case_identifier)) @font-lock-builtin-face
(value_qid
(upper_case_identifier)
(dot)
(lower_case_identifier))
@font-lock-builtin-face)
:language 'gren
:feature 'variable
:override t
'((lower_case_identifier) @font-lock-variable-name-face)
:language 'gren
:feature 'property
:override t
'((field) @font-lock-property-name-face)
:language 'gren
:feature 'type
:override t
'((type_ref (upper_case_qid (upper_case_identifier))) @font-lock-type-face
(exposed_type (upper_case_identifier)) @font-lock-type-face
(double_dot) @font-lock-type-face
(type_declaration (upper_case_identifier)) @font-lock-type-face
(union_variant (upper_case_identifier)) @font-lock-type-face
(union_pattern (upper_case_qid (upper_case_identifier))) @font-lock-type-face
(value_expr name: (upper_case_qid (upper_case_identifier)) @font-lock-type-face))
:language 'gren
:feature 'function
:override t
'((exposed_value (lower_case_identifier)) @font-lock-function-name-face)
:language 'gren
:feature 'variable
:override t
'((lower_pattern (lower_case_identifier)) @font-lock-variable-name-face
(type_variable (lower_case_identifier)) @font-lock-variable-name-face
(lower_type_name (lower_case_identifier)) @font-lock-variable-name-face
)
:language 'gren
:feature 'number
:override t
'((number_literal) @font-lock-number-face)
:language 'gren
:feature 'string
:override t
'((string_constant_expr) @font-lock-string-face)
:language 'gren
:feature 'keyword
:override t
'((type) @font-lock-keyword-face
(port) @font-lock-keyword-face
(module) @font-lock-keyword-face
(import) @font-lock-keyword-face
(exposing) @font-lock-keyword-face
(is) @font-lock-keyword-face
(when) @font-lock-keyword-face
(alias) @font-lock-keyword-face
(infix) @font-lock-keyword-face
"if" @font-lock-keyword-face
"else" @font-lock-keyword-face
"let" @font-lock-keyword-face
"in" @font-lock-keyword-face)
:language 'gren
:feature 'operator
:override t
'((operator_identifier) @font-lock-operator-face
(eq) @font-lock-operator-face
(arrow) @font-lock-operator-face
(colon) @font-lock-operator-face
"|" @font-lock-operator-face)
:language 'gren
:feature 'bracket
:override t
'("{" @font-lock-bracket-face
"}" @font-lock-bracket-face
"[" @font-lock-bracket-face
"]" @font-lock-bracket-face
"(" @font-lock-bracket-face
")" @font-lock-bracket-face
"," @font-lock-bracket-face)
:language 'gren
:feature 'punctuation
:override t
'(
"." @font-lock-punctuation-face)
:language 'gren
:feature 'comment
:override t
'((line_comment) @font-lock-comment-face)
)))
(defun setup-indentation ()
(setq treesit-simple-indent-rules
`((gren
;; Function Calls
((node-is "function_call_expr") parent 4)
((parent-is "function_call_expr") parent 2)
;; Records
((node-is "record_expr") parent 4)
((parent-is "record_expr") parent 0)
((parent-is "record_type") parent 0)
((parent-is "field") parent 0)
((parent-is "field_type") parent 0)
;; Type definition (type X)
((parent-is "type_expression") parent 4)
((node-is "union_variant") parent 4)
;; Function type def (foo : string)
((parent-is "type_declaration") parent 4)
((parent-is "type_alias_declaration") parent 4)
;; Function Definition
((parent-is "value_declaration") parent 4)
((parent-is "function_declaration_left") parent 4)
;; When ... is
((parent-is "when_is_expr") parent 4)
((parent-is "when_is_branch") parent 4)
((parent-is "pattern") parent 4)
;; (
((parent-is "parenthesized_expr") parent 0)
((parent-is "list_expr") parent 0)
;; let ... in
((parent-is "let_in_expr") parent 0)
;; Comments
((node-is "comment") parent 0)
;; Default
((no-node) parent 0)))))
(setup-font-lock)
(setup-indentation))
(define-derived-mode gren-ts-mode prog-mode "gren[ts]"
"Major mode for the gren programming language"
(when (treesit-ready-p 'gren)
(gren-mode-setup)
(gren-ts-mode-prepare)
(gren-ts-mode-setup)
;; Finally Set this up as a major tree sitter mode
(treesit-parser-create 'gren)
(treesit-major-mode-setup)))
(add-to-list 'auto-mode-alist '("\\.gren\\'" . gren-ts-mode))
(provide 'gren-ts-mode)
;;; gren-mode.el ends here