-
Notifications
You must be signed in to change notification settings - Fork 0
/
typst-ts-editing.el
477 lines (427 loc) · 18.1 KB
/
typst-ts-editing.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
;;; typst-ts-editing.el --- Helper functions for editing Typst documents -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2024 The typst-ts-mode Project Contributors
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; For item node, it's recommended to use `+' rather than `<num>.'. Operations
;; for `<num>.' may not be implemented comprehensively.
;;; Code:
(require 'outline)
(require 'typst-ts-core)
(defun typst-ts-mode-heading-up ()
"Switch the current heading with the heading above."
(interactive)
(call-interactively #'outline-move-subtree-up))
(defun typst-ts-mode-heading-down ()
"Switch the current heading with the heading below."
(interactive)
(call-interactively #'outline-move-subtree-down))
(defun typst-ts-mode-heading-left ()
"Increase the heading level."
(interactive)
(call-interactively #'outline-promote))
(defun typst-ts-mode-heading-right ()
"Decrease heading level."
(interactive)
(call-interactively #'outline-demote))
(defun typst-ts-mode-heading--at-point-p ()
"Whether the current line is a heading.
Return the heading node when yes otherwise nil."
(let ((node (treesit-node-parent
(treesit-node-at
(save-excursion
(beginning-of-line-text)
(point))))))
(if (string= (treesit-node-type node) "heading")
node
nil)))
(defun typst-ts-mode-item--at-point-p ()
"Return item node when point is on item.
Otherwise nil."
(treesit-parent-until (treesit-node-at (point))
(lambda (x) (string= (treesit-node-type x)
"item"))))
(defun typst-ts-mode-item--with-siblings ()
"Return (prev current next numbered-p) items.
The last item in the last tells you if the list is numbered (t) or not (nil).
When current does not have a previous or next sibling,
the index for it will be nil.
Being a different item type does not count as sibling, ex:
1. foo
- bar
When point is not on an item node return nil."
(when-let* ((node (typst-ts-mode-item--at-point-p))
(get-item-type (lambda (x)
(treesit-node-text (treesit-node-child x 0))))
(item-type (funcall get-item-type node))
(node-numbered-p t)
(same-item-type (lambda (x)
(let ((type (funcall get-item-type x)))
(or (string= type item-type)
;; are they numbers?
(and node-numbered-p
(not (= (string-to-number type)
0)))))))
(only-if (lambda (x) (and (string= (treesit-node-type x)
"item")
(funcall same-item-type x)
x))))
(setq node-numbered-p (not (= (string-to-number item-type) 0)))
(cond
((not node) node)
(node (list (funcall only-if (treesit-node-prev-sibling node))
node
(funcall only-if (treesit-node-next-sibling node))
node-numbered-p)))))
(defun typst-ts-mode--swap-regions (start1 end1 start2 end2)
"Swap region between START1 and END1 with region between START2 and END2."
(let ((text1 (buffer-substring start1 end1))
(text2 (buffer-substring start2 end2))
(marker1-start (make-marker))
(marker1-end (make-marker))
(marker2-start (make-marker))
(marker2-end (make-marker)))
(set-marker marker1-start start1)
(set-marker marker1-end end1)
(set-marker marker2-start start2)
(set-marker marker2-end end2)
(delete-region marker1-start marker1-end)
(delete-region marker2-start marker2-end)
(goto-char marker1-start)
(insert text2)
(goto-char marker2-start)
(insert text1)
(set-marker marker1-start nil)
(set-marker marker1-end nil)
(set-marker marker2-start nil)
(set-marker marker2-end nil)))
(defun typst-ts-mode-item--move (direction)
"Moves item node up or down (swap).
DIRECTION should be `up' or `down'."
(let* ( previous current next swap-with numbered-p
(bind (lambda ()
(pcase direction
('up
(setq swap-with previous))
('down
(setq swap-with next))
(_ (error "%s is not one of: `up' `down'" direction))))))
(seq-setq (previous current next numbered-p)
(typst-ts-mode-item--with-siblings))
(unless current
(error "Point is not on an item"))
(funcall bind)
(unless swap-with
(user-error "There is no %s item to swap with"
(if (eq direction 'up) "previous" "next")))
;; numbers may need to be swapped
(when numbered-p
(let* ((number1 (treesit-node-child current 0))
(number2 (treesit-node-child swap-with 0))
(current-begin (treesit-node-start number1))
(current-end (treesit-node-end number1))
(other-begin (treesit-node-start number2))
(other-end (treesit-node-end number2)))
(save-excursion
(typst-ts-mode--swap-regions current-begin current-end
other-begin other-end))))
;; the nodes must be reinitialized
(seq-setq (previous current next numbered-p)
(typst-ts-mode-item--with-siblings))
(funcall bind)
(let ((current-begin (treesit-node-start current))
(current-end (treesit-node-end current))
(other-begin (treesit-node-start swap-with))
(other-end (treesit-node-end swap-with))
(column (current-column)))
(typst-ts-mode--swap-regions current-begin current-end
other-begin other-end)
(move-to-column column))))
(defun typst-ts-mode-item-up ()
"Move the item at point up."
(interactive)
(typst-ts-mode-item--move 'up))
(defun typst-ts-mode-item-down ()
"Move the item at point down."
(interactive)
(typst-ts-mode-item--move 'down))
(defun typst-ts-mode-meta-up ()
"See `typst-ts-mode-meta--dwim'."
(interactive)
(call-interactively (typst-ts-mode-meta--dwim 'up)))
(defun typst-ts-mode-meta-down ()
"See `typst-ts-mode-meta--dwim'."
(interactive)
(call-interactively (typst-ts-mode-meta--dwim 'down)))
(defun typst-ts-mode-meta-left ()
"See `typst-ts-mode-meta--dwim'."
(interactive)
(call-interactively (typst-ts-mode-meta--dwim 'left)))
(defun typst-ts-mode-meta-right ()
"See `typst-ts-mode-meta--dwim'."
(interactive)
(call-interactively (typst-ts-mode-meta--dwim 'right)))
(defun typst-ts-mode-meta--dwim (direction)
"Return function depending on the context with meta key + DIRECTION.
When point is at heading:
`left': `typst-ts-mode-heading-decrease',
`right': `typst-ts-mode-heading-increase',
`up': `typst-ts-mode-heading-up',
`down': `typst-ts-mode-heading-down'.
When point is at item list:
`up': `typst-ts-mode-item-up'
`down': `typst-ts-mode-item-down'
When there is no relevant action to do it will return the relevant function in
the `GLOBAL-MAP' (example: `right-word')."
(let* ((prefix "typst-ts-mode-")
(mid (cond
((typst-ts-mode-heading--at-point-p) "heading")
((and (typst-ts-mode-item--at-point-p)
;; does not exist, maybe will exist at some point
(not (or (eq 'left direction)
(eq 'right direction))))
"item")
(t nil)))
(end
(pcase direction
('left
"-left")
('right
"-right")
('up
"-up")
('down
"-down")
(_ (error "DIRECTION: %s is not one of: `right' `left', `up', `down'"
direction)))))
(if (not mid)
(keymap-lookup global-map (substitute-command-keys
(concat "\\[" prefix "meta" end "]")))
(intern-soft (concat prefix mid end)))))
(defun typst-ts-mode-meta-return (&optional arg)
"Depending on context, insert a heading or insert an item.
The new heading is created after the ending of current heading.
Using ARG argument will ignore the context and it will insert a heading instead."
(interactive "P")
(let ((item-node (treesit-parent-until
(treesit-node-at (line-beginning-position))
(lambda (node)
(string= "item" (treesit-node-type node)))))
(node (typst-ts-core-get-parent-of-node-at-bol-nonwhite)))
(cond
(arg (typst-ts-mode-insert--heading nil))
(item-node
(typst-ts-mode-insert--item item-node))
(t
(typst-ts-mode-insert--heading node)))))
(defun typst-ts-mode-return (&optional arg)
"Handle RET depends on condition.
When prefix ARG is non-nil, call global return function."
(interactive "P")
(let (execute-result)
(unless current-prefix-arg
(setq
execute-result
(catch 'execute-result
(let* ((cur-pos (point))
(cur-node (treesit-node-at cur-pos))
(cur-node-type (treesit-node-type cur-node))
(parent-node (treesit-node-parent cur-node)) ; could be nil
(parent-node-type (treesit-node-type parent-node))
node)
;; (message "%s %s" cur-node parent-node)
(cond
;; on item node end
((and (eolp)
(setq node (typst-ts-core-parent-util-type
(typst-ts-core-get-parent-of-node-at-bol-nonwhite)
"item" t t)))
(let* ((item-node node)
(has-children (treesit-node-child item-node 1))
(next-line-pos
(save-excursion
(forward-line 1)
(point)))
(next-line-node
(typst-ts-core-get-parent-of-node-at-bol-nonwhite
next-line-pos))
(next-line-top-node ; get container type or `item' type node
(typst-ts-core-parent-util-type
next-line-node
(regexp-opt '("code" "item"))
t)))
(if has-children
;; example:
;; - #[| <- return
;; ]
(if (and next-line-top-node
;; end of buffer situation (or next line is the end
;; line (and no newline character))
(not (equal
(line-number-at-pos next-line-pos)
(line-number-at-pos (point-max)))))
(call-interactively #'newline)
(typst-ts-mode-insert--item item-node))
;; no text means delete the item on current line: (item -)
(delete-region (line-beginning-position) (line-end-position))
;; whether the previous line is in an item
(let* ((prev-line-item-node
(typst-ts-core-parent-util-type
(typst-ts-core-get-parent-of-node-at-bol-nonwhite
(save-excursion
(forward-line -1)
(point)))
"item" t t)))
(if prev-line-item-node
(progn
(delete-line)
(forward-line -1)
(end-of-line)
(call-interactively #'newline))
(indent-according-to-mode)))))
(throw 'execute-result 'success))
)))))
;; execute default action if not successful
(unless (eq execute-result 'success)
;; we only need to look for global keybinding, see `(elisp) Active Keymaps'
(let ((global-ret-function (global-key-binding (kbd "RET"))))
(if (not current-prefix-arg)
(call-interactively global-ret-function)
(if (yes-or-no-p
(format
"Execute function `%s' without/with the given prefix argument?"
global-ret-function))
(let ((current-prefix-arg nil))
(call-interactively global-ret-function))
(call-interactively global-ret-function)))))))
(defun typst-ts-mode-insert--item (node)
"Insert an item after NODE.
NODE must be an item node!
This function respects indentation."
(let* (;; +, -, or <num>.
(item-type (treesit-node-text
(treesit-node-child node 0)))
(item-number (string-to-number item-type))
(item-end (treesit-node-end node))
(node-bol-column (typst-ts-core-column-at-pos
(typst-ts-core-get-node-bol node))))
(goto-char item-end)
(newline)
(indent-line-to node-bol-column)
(insert (if (= item-number 0) ; not a number type
item-type
(concat (number-to-string (1+ item-number)) "."))
" ")))
(defun typst-ts-mode-insert--heading (node)
"Insert a heading after the section that NODE is part of.
When there is no section it will insert a heading below point."
(let* ((section
(treesit-parent-until
node
(lambda (node)
(string= (treesit-node-type node) "section"))
t))
;; first child is heading
(heading (treesit-node-child section 0))
(heading-level (treesit-node-type (treesit-node-child heading 0))))
(if section
(goto-char (treesit-node-end section))
;; no headings so far
(setq heading-level "=")
(forward-line 1))
;; something can be in the next line/section, the heading needs be on its own line
;; this has to be done after `goto-char' because it will invalidate the node
(newline)
(forward-line -1)
;; insert the heading and indent
(insert heading-level " ")
(indent-according-to-mode)))
(defun typst-ts-editing--indent-item-node-lines (node offset)
(let ((item-node-min-column
(typst-ts-core-column-at-pos
(typst-ts-core-line-bol-nonwhite-pos
(treesit-node-start node)))))
(if (< (+ item-node-min-column offset) 0)
(setq offset (- item-node-min-column)))
(typst-ts-core-for-lines-covered-by-node
node
(lambda ()
(indent-line-to
(+ (typst-ts-core-column-at-pos
(typst-ts-core-line-bol-nonwhite-pos))
offset))))))
(defun typst-ts-mode-cycle (&optional _arg)
"Cycle."
(interactive "P")
(let (execute-result node)
(setq
execute-result
;; plz manually throw `\'success' to `execute-result'
(catch 'execute-result
(when-let* ((cur-pos (point))
(cur-node (treesit-node-at cur-pos))
(cur-node-type (treesit-node-type cur-node))
(cur-line-nonwhite-bol-node
(typst-ts-core-get-node-at-bol-nonwhite))
(cur-line-nonwhite-bol-node-type
(treesit-node-type cur-line-nonwhite-bol-node))
(parent-node (treesit-node-parent cur-node)) ; could be nil
(parent-node-type (treesit-node-type parent-node)))
(cond
((equal parent-node-type "raw_blck")
(insert-tab)
(throw 'execute-result 'success))
((setq node
(typst-ts-core-parent-util-type
cur-line-nonwhite-bol-node "item" t t))
(let* ((cur-item-node node)
(prev-significant-node
(typst-ts-core-prev-sibling-ignore-types
cur-item-node
"parbreak"))
(prev-significant-node-type
(treesit-node-type prev-significant-node))
prev-item-node)
(if (equal prev-significant-node-type "item")
(setq prev-item-node prev-significant-node)
(if (equal
"item"
(treesit-node-type
(treesit-node-parent prev-significant-node)))
(setq prev-item-node (treesit-node-parent
prev-significant-node))))
;; (message "%s, %s" cur-item-node prev-item-node)
(unless prev-item-node
(throw 'execute-result 'default))
(let* ((cur-item-node-start-column
(typst-ts-core-column-at-pos
(treesit-node-start cur-item-node)))
(prev-item-node-start-column
(typst-ts-core-column-at-pos
(treesit-node-start prev-item-node)))
(offset
(- cur-item-node-start-column
prev-item-node-start-column)))
(if (>= offset typst-ts-mode-indent-offset)
(typst-ts-editing--indent-item-node-lines
cur-item-node
(- (+ offset typst-ts-mode-indent-offset)))
(typst-ts-editing--indent-item-node-lines
cur-item-node
(- typst-ts-mode-indent-offset (abs offset)))))
(throw 'execute-result 'success)))
(t nil)))))
;; execute default action if not successful
(unless (eq execute-result 'success)
(call-interactively (global-key-binding (kbd "TAB"))))))
(provide 'typst-ts-editing)
;;; typst-ts-editing.el ends here