-
Notifications
You must be signed in to change notification settings - Fork 53
/
combobulate-html.el
240 lines (219 loc) · 10.6 KB
/
combobulate-html.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
;;; combobulate-html.el --- HTML and SGML-alike structured editing for combobulate -*- lexical-binding: t; -*-
;; Copyright (C) 2021-23 Mickey Petersen
;; Author: Mickey Petersen <mickey at masteringemacs.org>
;; Package-Requires: ((emacs "29"))
;; Version: 0.1
;; Homepage: https://www.github.com/mickeynp/combobulate
;; Keywords: convenience, tools, languages
;; 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:
;;
;;; Code:
(require 'combobulate-settings)
(require 'combobulate-navigation)
(require 'combobulate-setup)
(require 'combobulate-rules)
(defvar-local combobulate-sgml-open-tag nil)
(defvar-local combobulate-sgml-close-tag nil)
(defvar-local combobulate-sgml-whole-tag nil)
(defvar-local combobulate-sgml-self-closing-tag nil)
(defvar-local combobulate-sgml-exempted-tags '("jsx_expression"))
(declare-function combobulate-execute-envelope "combobulate-manipulation")
(defun combobulate-maybe-auto-close-tag ()
"Insert `>' or maybe insert the closing tag."
(interactive)
(self-insert-command 1 ?>)
(save-excursion
(forward-char -1)
(when (not (eq ?/ (char-before)))
(let ((open-node (combobulate-node-at-point (list combobulate-sgml-open-tag))))
;; only attempt an expansion if we are not inside one of the exempted tags.
(when (and open-node
(= (combobulate-node-end open-node) (1+ (point)))
(not (combobulate-node-contains-node-p
(combobulate-node-at-point combobulate-sgml-exempted-tags)
open-node)))
(forward-char 1)
(insert (format "</%s>" (combobulate-node-text
(or
;; for tsx/jsx
(combobulate-node-child-by-field
open-node "name")
;; for `html'
(combobulate-node-child
open-node 0))))))))))
;; Note: brittle.
;; (defun combobulate-maybe-close-tag-or-self-insert ()
;; "Insert `/' or maybe close the nearest unopened tag."
;; (interactive)
;; (cl-flet ((get-text (node closing)
;; (if (equal (combobulate-parser-language (combobulate-parser-node node)) 'html)
;; (thread-first node
;; (combobulate-node-child
;; (if (equal field combobulate-sgml-close-tag)
;; -1
;; 0))
;; (combobulate-node-child 0)
;; (combobulate-node-text))
;; (thread-first node
;; (combobulate-node-child-by-field
;; (if closing "close_tag" "open_tag"))
;; (combobulate-node-child-by-field "name")
;; (combobulate-node-text)))))
;; (if (looking-back "<" 1)
;; (progn
;; (let* ((element (combobulate-node-at-point (list combobulate-sgml-whole-tag))))
;; (if (equal (get-text element nil)
;; (get-text element t))
;; (self-insert-command 1 ?/)
;; (combobulate-message "Closing node" element)
;; (combobulate-pulse-node (combobulate-node-child element 0))
;; (insert (format "/%s>" (get-text element combobulate-sgml-open-tag))))))
;; (self-insert-command 1 ?/))))
(defun combobulate-maybe-insert-attribute ()
"Insert `=' or maybe a an attribute."
(interactive)
(if-let ((node (combobulate-node-at-point (list combobulate-sgml-open-tag
combobulate-sgml-self-closing-tag))))
;; Expanding an attribute is only desirable if we are inside an
;; open (or self-closing) node, but *also not* inside a node
;; type in `combobulate-sgml-exempted-tags' which happens to be
;; inside one of those node types.
(if (combobulate-node-contains-node-p
(combobulate-node-at-point combobulate-sgml-exempted-tags)
node)
(self-insert-command 1 ?=)
(combobulate-atomic-change-group
(catch 'done
(unless (equal (combobulate-parser-language (combobulate-parser-node node)) 'html)
(pcase-dolist (`(,attribute . ,envelope) (combobulate-read attribute-envelope-alist))
(when (looking-back (concat "\\<" attribute "\\>") (length attribute))
(combobulate-execute-envelope envelope node)
(throw 'done t))))
;; catch flagrant, out-of-place uses of `='.
(if (looking-back "[[:alpha:]]" 1)
(cond
;; html can only do strings, so just use that.
((equal (combobulate-parser-language (combobulate-parser-node node)) 'html)
(combobulate-execute-envelope "attr-string" node))
((combobulate-read attribute-envelope-default)
(combobulate-execute-envelope (combobulate-read attribute-envelope-default) node))
(t (self-insert-command 1 ?=)))
(self-insert-command 1 ?=)))))
(self-insert-command 1 ?=)))
(defun combobulate-html-pretty-print (node default-name)
(cond
((and node (member (combobulate-node-type node) '("element" "script_element" "style_element")))
(format "<%s>" (thread-first node
(combobulate-node-child 0)
(combobulate-node-child 0)
(combobulate-node-text))))
((and node (equal (combobulate-node-type node) "text"))
(format "`%s'" (combobulate-node-text node)))
((and node (equal (combobulate-node-type node) "comment"))
(combobulate-node-text node))
(t default-name)))
(eval-and-compile
(defvar combobulate-html-definitions
'((procedures-sexp '((:activation-nodes ((:nodes ("element" "attribute" "text" "script_element" "style_element"))))))
(context-nodes '("attribute_name" "attribute_value" "tag_name" "text"))
(envelope-list
'((:description
"<...> ... </...>"
:name "tag"
:mark-node t
:nodes ("element" "self_closing_tag" "jsx_fragment" "script_element" "style_element" "text")
:key "t"
:template ("<" (p tag "Tag Name: ") ">" n>
r>
n> "</" (field tag) ">"))
(:description
"...={ ... }"
:key "=e"
:mark-node nil
:point-placement 'stay
:nodes ("start_tag" "self_closing_tag" "script_element" "style_element")
:name "attr-expression"
:template ("=" "{" @ "}"))
(:description
"...={{ ... }}"
:key "=E"
:mark-node nil
:point-placement 'stay
:nodes ("start_tag" "self_closing_tag")
:name "attr-expression-object"
:template ("=" "{{" @ "}}"))
(:description
"...=\" ... \""
:key "=s"
:mark-node nil
:point-placement 'stay
:nodes ("start_tag" "self_closing_tag")
:name "attr-string"
:template ("=" "\"" @ "\""))))
(procedures-sequence
'((:activation-nodes
((:nodes ("tag_name") :position any :has-ancestor ("element")))
:selector (:choose parent :match-query
(:query
(_ (start_tag (tag_name) @match)
(end_tag (tag_name) @match))
:engine combobulate)))))
(procedures-hierarchy
'(;; seamless navigation between elements and their children.
(:activation-nodes
((:nodes ("element" "script_element" "style_element") :position at))
;; do not discard "end_tag" as it lets us navigate into a
;; tag without any children. Bit of a hack...
:selector (:choose node :match-children (:discard-rules ("start_tag" "tag_name"))))
;; go into attribute if point is inside the start tag
(:activation-nodes
((:nodes ("start_tag" "self_closing_tag") :position in))
:selector (:choose node
:match-children
(:match-rules ("attribute"))))
;; if we're inside an attribute, go to its value
(:activation-nodes
((:nodes ("attribute") :position in))
:selector (:choose node :match-children t))))
;; Ordinarily, we discard comments as they tend to be line
;; comments. This is not a problem in HTML where they have
;; beginnings and ends.
(procedure-discard-rules nil)
(display-ignored-node-types '("start_tag" "self_closing_tag" "end_tag" "tag_name"))
(procedures-sibling
'((:activation-nodes
((:nodes
("attribute")))
:selector (:choose node :match-siblings (:match-rules ("attribute"))))
(:activation-nodes
((:nodes
((rule "document") "comment")
:has-parent ((rule "document") "document" "start_tag" "self_closing_tag")))
:selector (:match-children (:match-rules (exclude (all) "start_tag" "end_tag" "self_closing_tag"))))))
(pretty-print-node-name-function #'combobulate-html-pretty-print))))
(define-combobulate-language
:name html
:language html
:major-modes (html-mode html-ts-mode mhtml-mode sgml-mode)
:custom combobulate-html-definitions
:setup-fn combobulate-html-setup)
(defun combobulate-html-setup (_)
(define-key (combobulate-read map) (kbd "=") #'combobulate-maybe-insert-attribute)
;; (local-set-key (kbd "/") #'combobulate-maybe-close-tag-or-self-insert)
(define-key (combobulate-read map) (kbd ">") #'combobulate-maybe-auto-close-tag)
(setq combobulate-sgml-open-tag "start_tag")
(setq combobulate-sgml-close-tag "end_tag")
(setq combobulate-sgml-whole-tag "element")
(setq combobulate-sgml-self-closing-tag "self_closing_tag"))
(provide 'combobulate-html)
;;; combobulate-html.el ends here