-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
ts-fold-util.el
229 lines (184 loc) · 6.39 KB
/
ts-fold-util.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
;;; ts-fold-util.el --- Utility module -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2024 Shen, Jen-Chieh
;; Created date 2021-10-04 20:19:42
;; This file is NOT part of GNU 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Utility module.
;;
;;; Code:
(require 'tsc)
;;
;; (@* "Redisplay" )
;;
(defmacro ts-fold--with-no-redisplay (&rest body)
"Execute BODY without any redisplay execution."
(declare (indent 0) (debug t))
`(let ((inhibit-redisplay t)
(inhibit-modification-hooks t)
after-focus-change-function
buffer-list-update-hook
display-buffer-alist
window-configuration-change-hook
window-scroll-functions
window-size-change-functions
window-state-change-hook
jit-lock-mode)
,@body))
;;
;; (@* "String" )
;;
(defun ts-fold-2str (obj)
"Convert OBJ to string."
(format "%s" obj))
(defun ts-fold--count-matches (pattern str)
"Count occurrences of PATTERN in STR.
Like function `s-count-matches' but faster."
(max 0 (1- (length (split-string str pattern)))))
;;
;; (@* "Cons" )
;;
(defun ts-fold--cons-add (&rest args)
"Addition for list of cons ARGS."
(let ((v1 0) (v2 0))
(dolist (c args)
(setq v1 (+ v1 (car c))
v2 (+ v2 (cdr c))))
(cons v1 v2)))
;;
;; (@* "Overlay" )
;;
(defun ts-fold--overlays-in (prop name &optional beg end)
"Return overlays with PROP of NAME, from region BEG to END."
(unless beg (setq beg (point-min))) (unless end (setq end (point-max)))
(let ((lst '()) (ovs (overlays-in beg end)))
(dolist (ov ovs)
(when (eq name (overlay-get ov prop))
(push ov lst)))
lst))
;;
;; (@* "Face" )
;;
(defvar ts-fold--doc-faces
'( font-lock-doc-face
font-lock-comment-face
font-lock-comment-delimiter-face
tree-sitter-hl-face:comment
tree-sitter-hl-face:doc
hl-todo
rst-comment)
"List of face that apply for document string.")
(defun ts-fold--get-face (obj trim)
"Return face name from OBJ.
If argument TRIM is non-nil, trim the OBJ."
(let* ((obj (if trim (string-trim obj) obj))
(len (length obj)))
(or (get-text-property 0 'face obj)
(and (<= 1 len)
(get-text-property 1 'face obj)))))
(defun ts-fold--is-face (obj lst-face &optional trim)
"Return non-nil if OBJ's face is define inside list LST-FACE.
Optional argument TRIM, see function `ts-fold--get-face'."
(unless (listp lst-face) (setq lst-face (list lst-face)))
(let ((faces (ts-fold--get-face obj trim)))
(cond ((listp faces)
(cl-some (lambda (face) (memq face lst-face)) faces))
(t (memq faces lst-face)))))
(defun ts-fold--doc-faces-p (obj &optional trim)
"Return non-nil if face at OBJ is within `ts-fold--doc-faces' list.
Optional argument TRIM, see function `ts-fold--get-face'."
(ts-fold--is-face obj ts-fold--doc-faces trim))
;;
;; (@* "Positions" )
;;
(defun ts-fold--last-eol (pos)
"Go to POS then find previous line break, and return its position."
(save-excursion
(goto-char pos)
(max 1 (1- (line-beginning-position)))))
(defun ts-fold--bol (point)
"Return line beginning position at POINT."
(save-excursion (goto-char point) (line-beginning-position)))
(defun ts-fold--eol (point)
"Return line end position at POINT."
(save-excursion (goto-char point) (line-end-position)))
(defun ts-fold--indentation (pos)
"Return current indentation by POS."
(goto-char pos)
(current-indentation))
;;
;; (@* "Math" )
;;
(defun ts-fold--in-range-p (in-val in-min in-max)
"Check to see if IN-VAL is between IN-MIN and IN-MAX."
(and (<= in-min in-val) (<= in-val in-max)))
;;
;; (@* "List" )
;;
(defun ts-fold-listify (obj)
"Ensure OBJ is a list."
(if (listp obj) obj (list obj)))
;;
;; (@* "Window" )
;;
(defmacro ts-fold--with-selected-window (window &rest body)
"Same with `with-selected-window' but safe.
See macro `with-selected-window' description for arguments WINDOW and BODY."
(declare (indent 1) (debug t))
`(when (window-live-p ,window) (with-selected-window ,window ,@body)))
;;
;; (@* "TS node" )
;;
(defun ts-fold--compare-type (node type)
"Compare NODE's type to TYPE."
;; tsc-node-type returns a symbol or a string and `string=' automatically
;; converts symbols to strings
(string= (tsc-node-type node) type))
(defun ts-fold-get-children (node)
"Get list of direct children of NODE."
(let (children)
(dotimes (index (tsc-count-children node))
(push (tsc-get-nth-child node index) children))
(reverse children)))
(defun ts-fold-get-children-traverse (node)
"Return children from NODE but traverse it."
(let (nodes)
(tsc-traverse-mapc (lambda (child) (push child nodes)) node)
(reverse nodes)))
(defun ts-fold-find-children (node type)
"Search through the children of NODE to find all with type equal to TYPE;
then return that list."
(cl-remove-if-not (lambda (child) (ts-fold--compare-type child type))
(ts-fold-get-children node)))
(defun ts-fold-find-children-traverse (node type)
"Like function `ts-fold-find-children' but traverse it.
For arguments NODE and TYPE, see function `ts-fold-find-children' for more
information."
(cl-remove-if-not (lambda (child) (ts-fold--compare-type child type))
(ts-fold-get-children-traverse node)))
(defun ts-fold-find-parent (node type)
"Find the TYPE of parent from NODE."
(let ((parent (tsc-get-parent node))
(break))
(while (and parent (not break))
(setq break (ts-fold--compare-type parent type))
(unless break
(setq parent (tsc-get-parent parent))))
parent))
(defun ts-fold-last-child (node)
"Return last child node from parent NODE."
(when-let* ((count (tsc-count-children node))
((not (= count 0))))
(tsc-get-nth-child node (1- count))))
(provide 'ts-fold-util)
;;; ts-fold-util.el ends here