-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheex-ts-mode.el
208 lines (176 loc) · 7.11 KB
/
heex-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
;;; heex-ts-mode.el --- Major mode for Heex with tree-sitter support -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2024 Wilhelm H Kirschbaum
;; Author : Wilhelm H Kirschbaum
;; Version : 1.3
;; URL : https://github.com/wkirschbaum/elixir-ts-mode
;; Package-Requires : ((emacs "29.1"))
;; Created : November 2022
;; Keywords : heex languages tree-sitter
;; 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/>.
;;; Commentary:
;; This package defines heex-ts-mode which is a major mode for editing
;; Elixir and Heex files.
;;; Code:
(require 'treesit)
(eval-when-compile (require 'rx))
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-node-child "treesit.c")
(declare-function treesit-node-type "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-install-language-grammar "treesit.el")
(defgroup heex-ts nil
"Major mode for editing HEEx code."
:prefix "heex-ts-"
:group 'langauges)
(defcustom heex-ts-indent-offset 2
"Indentation of HEEx statements."
:version "30.1"
:type 'integer
:safe 'integerp
:group 'heex-ts)
(defconst heex-ts--sexp-regexp
(rx bol
(or "directive" "tag" "component" "slot"
"attribute" "attribute_value" "quoted_attribute_value")
eol))
;; There seems to be no parent directive block for tree-sitter-heex,
;; so we ignore them for now until we learn how to query them.
;; https://github.com/phoenixframework/tree-sitter-heex/issues/28
(defvar heex-ts--indent-rules
(let ((offset heex-ts-indent-offset))
`((heex
((parent-is "fragment")
(lambda (node parent &rest _)
;; If HEEx is embedded indent to parent
;; otherwise indent to the bol.
(if (eq (treesit-language-at (point-min)) 'heex)
(point-min)
(save-excursion
(goto-char (treesit-node-start parent))
(back-to-indentation)
(point))
)) 0)
((node-is "end_tag") parent-bol 0)
((node-is "end_component") parent-bol 0)
((node-is "end_slot") parent-bol 0)
((node-is "/>") parent-bol 0)
((node-is ">") parent-bol 0)
((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "component") parent-bol ,offset)
((parent-is "tag") parent-bol ,offset)
((parent-is "start_tag") parent-bol ,offset)
((parent-is "component") parent-bol ,offset)
((parent-is "start_component") parent-bol ,offset)
((parent-is "slot") parent-bol ,offset)
((parent-is "start_slot") parent-bol ,offset)
((parent-is "self_closing_tag") parent-bol ,offset)
(no-node parent-bol ,offset)))))
(defvar heex-ts--font-lock-settings
(when (treesit-available-p)
(treesit-font-lock-rules
:language 'heex
:feature 'heex-comment
'((comment) @font-lock-comment-face)
:language 'heex
:feature 'heex-doctype
'((doctype) @font-lock-doc-face)
:language 'heex
:feature 'heex-tag
`([(tag_name) (slot_name)] @font-lock-function-name-face)
:language 'heex
:feature 'heex-attribute
`((attribute_name) @font-lock-variable-name-face)
:language 'heex
:feature 'heex-keyword
`((special_attribute_name) @font-lock-keyword-face)
:language 'heex
:feature 'heex-string
`([(attribute_value) (quoted_attribute_value)] @font-lock-constant-face)
:language 'heex
:feature 'heex-component
`([
(component_name) @font-lock-function-name-face
(module) @font-lock-keyword-face
(function) @font-lock-keyword-face
"." @font-lock-keyword-face
])))
"Tree-sitter font-lock settings.")
(defun heex-ts--defun-name (node)
"Return the name of the defun NODE.
Return nil if NODE is not a defun node or doesn't have a name."
(pcase (treesit-node-type node)
((or "component" "slot" "tag")
(string-trim
(treesit-node-text
(treesit-node-child (treesit-node-child node 0) 1) nil)))
(_ nil)))
(defun heex-ts--forward-sexp (&optional arg)
"Move forward across one balanced expression (sexp).
With ARG, do it many times. Negative ARG means move backward."
(or arg (setq arg 1))
(funcall
(if (> arg 0) #'treesit-end-of-thing #'treesit-beginning-of-thing)
heex-ts--sexp-regexp
(abs arg)))
(defvar heex-ts-mode-default-grammar-sources
'((heex . ("https://github.com/phoenixframework/tree-sitter-heex.git"))))
(defun heex-ts-install-grammar ()
"Experimental function to install the tree-sitter-heex grammar."
(interactive)
(if (and (treesit-available-p) (boundp 'treesit-language-source-alist))
(let ((treesit-language-source-alist
(append
treesit-language-source-alist
heex-ts-mode-default-grammar-sources)))
(if (y-or-n-p
(format
(concat "The following language grammar repository which will be "
"downloaded and installed "
"%s, proceed?")
(cadr (assoc 'heex treesit-language-source-alist))))
(treesit-install-language-grammar 'heex)))
(display-warning
'treesit
(concat "Cannot install grammar because"
" "
"tree-sitter library is not compiled with Emacs"))))
;;;###autoload
(define-derived-mode heex-ts-mode html-mode "HEEx"
"Major mode for editing HEEx, powered by tree-sitter."
:group 'heex-ts
(when (treesit-ready-p 'heex)
(treesit-parser-create 'heex)
;; Comments
(setq-local treesit-thing-settings
`((heex
(text ,(regexp-opt '("comment" "text"))))))
(setq-local forward-sexp-function #'heex-ts--forward-sexp)
;; Navigation.
(setq-local treesit-defun-type-regexp
(rx bol (or "component" "tag" "slot") eol))
(setq-local treesit-defun-name-function #'heex-ts--defun-name)
;; Imenu
(setq-local treesit-simple-imenu-settings
'(("Component" "\\`component\\'" nil nil)
("Slot" "\\`slot\\'" nil nil)
("Tag" "\\`tag\\'" nil nil)))
(setq-local treesit-font-lock-settings heex-ts--font-lock-settings)
(setq-local treesit-simple-indent-rules heex-ts--indent-rules)
(setq-local treesit-font-lock-feature-list
'(( heex-comment heex-keyword heex-doctype )
( heex-component heex-tag heex-attribute heex-string )
() ()))
(treesit-major-mode-setup)))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.[hl]?eex\\'" . heex-ts-mode))
(provide 'heex-ts-mode)
;;; heex-ts-mode.el ends here