-
Notifications
You must be signed in to change notification settings - Fork 0
/
typst-ts-core.el
160 lines (137 loc) · 5.74 KB
/
typst-ts-core.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
;;; typst-ts-core.el --- core functions for typst-ts-mode -*- 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:
;; Utility functions for typst-ts-mode
;;; Code:
(require 'treesit)
;; don't use 'treesit.c' since package Emacs distribution may separate the source
;; code from Emacs binary
(declare-function treesit-parser-list "treesit" t t)
(defcustom typst-ts-mode-indent-offset 4
"Number of spaces for each indentation step in `typst-ts-mode'."
:type 'natnum
:group 'typst-ts)
(defconst typst-ts-core--container-node-types
;; '_math_group' here is because `treesit-parent-until' doesn't hanlde node type alias well
;; TODO file a bug
'("block" "content" "group" "math" "_math_group"))
(defconst typst-ts-mode--container-node-types-regexp
(regexp-opt typst-ts-core--container-node-types)
"Container node types regexp.")
(defun typst-ts-core-column-at-pos (point)
"Get the column at position POINT."
(save-excursion
(goto-char point)
(current-column)))
(defun typst-ts-core-get-node-bol (node)
"Get the NODE's indentation offset (at node beginning)."
(save-excursion
(goto-char (treesit-node-start node))
(back-to-indentation)
(point)))
(defun typst-ts-core-line-bol-nonwhite-pos (&optional pos)
"POS."
(save-excursion
(when pos
(goto-char pos))
(back-to-indentation)
(point)))
(defun typst-ts-core-get-node-at-bol-nonwhite (&optional pos)
"Get node at the first non-whitespace character at line beginning.
If POS is given, operate on the line that POS locates at."
(save-excursion
(when pos
(goto-char pos))
(back-to-indentation)
(treesit-node-at (point))))
(defun typst-ts-core-get-parent-of-node-at-bol-nonwhite (&optional pos)
"See `typst-ts-core-get-node-at-bol-nonwhite'.
POS. May return nil."
(treesit-node-parent
(typst-ts-core-get-node-at-bol-nonwhite pos)))
(defun typst-ts-core-for-lines-covered-by-node (node fn)
"Execute FN on all lines covered by NODE.
Currently the effect of FN shouldn't change line number."
(let ((ns (treesit-node-start node))
;; use line number not position since when editing, position of node end
;; changes, but the information is not updated
(ne-line-num (line-number-at-pos (treesit-node-end node))))
(save-excursion
(goto-char ns)
(while (< (line-number-at-pos) ne-line-num)
(funcall fn)
(forward-line 1))
;; in case the last line is the last line of buffer, we separate this
;; operation from while loop
(funcall fn))))
;; Emacs 29 doesn't support string type PRED, so this function is created for
;; convenience
(defun typst-ts-core-parent-util-type (node type &optional include-node same-context)
"See `treesit-parent-until'.
TYPE is an regexp expression for matching types.
SAME-CONTEXT: whether the parent should be in the current context with NODE.
The following example means parent item node is in a different context with
`hi' text node
- #[
hi
]
NODE TYPE INCLUDE-NODE see `treesit-parent-until'."
(let ((matched-node
(treesit-parent-until
node
(lambda (node)
(let ((node-type (treesit-node-type node)))
(or (and same-context
(string-match-p
typst-ts-mode--container-node-types-regexp node-type))
(string-match-p type node-type))))
include-node)))
(when (and matched-node
(string-match-p type (treesit-node-type matched-node)))
matched-node)))
(defun typst-ts-core-prev-sibling-ignore-types (node types)
"Find previous sibling node ignoring nodes whose type matches TYPES.
NODE: current node.
TYPES is an regexp expression."
(let* ((prev-node (treesit-node-prev-sibling node))
(prev-node-type (treesit-node-type prev-node)))
(while (and prev-node-type
(string-match-p types prev-node-type))
(setq
prev-node (treesit-node-prev-sibling prev-node)
prev-node-type (treesit-node-type prev-node)))
prev-node))
(defun typst-ts-core-node-get (node instructions)
"Get things from NODE by INSTRUCTIONS.
It's a copy of Emacs 30's `treesit-node-get' function."
(declare (indent 1))
(if (fboundp 'treesit-node-get)
(treesit-node-get node instructions)
(while (and node instructions)
(pcase (pop instructions)
('(field-name) (setq node (treesit-node-field-name node)))
('(type) (setq node (treesit-node-type node)))
(`(child ,idx ,named) (setq node (treesit-node-child node idx named)))
(`(parent ,n) (dotimes (_ n)
(setq node (treesit-node-parent node))))
(`(text ,no-property) (setq node (treesit-node-text node no-property)))
(`(children ,named) (setq node (treesit-node-children node named)))
(`(sibling ,step ,named)
(dotimes (_ (abs step))
(setq node (if (> step 0)
(treesit-node-next-sibling node named)
(treesit-node-prev-sibling node named)))))))
node))
(provide 'typst-ts-core)
;;; typst-ts-core.el ends here