-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathts-movement.el
224 lines (193 loc) · 8.3 KB
/
ts-movement.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
;;; ts-movement.el --- Movement commands using treesit syntax tree -*- lexical-binding:t -*-
;; Copyright (C) 2022 Free Software Foundation, Inc.
;; Author: Harit Kapadia <[email protected]>
;; Version: 0.1
;; Package-Requires: ((treesit))
;; Keywords:
;; URL:
;;; Commentary:
;; Movement commands using treesit syntax tree.
;; Optionally depends on `hydra'.
;;; Code:
(require 'treesit)
(defvar-local tsm/-overlays (make-hash-table :test #'eq))
(defun tsm/-find-overlay-at-point (point)
"Find any overlay in tsm/-overlays containing POINT."
(seq-find (lambda (o) (gethash o tsm/-overlays)) (overlays-at point)))
(defun tsm/-overlay-at-node (node)
"Create overlay of NODE and add to `tsm/-overlays'"
(let ((overlay (make-overlay (treesit-node-start node) (treesit-node-end node))))
(overlay-put overlay 'face 'lazy-highlight)
(overlay-put overlay 'evaporate t)
(overlay-put overlay 'node node)
; Emacs documentation says integer must be nonnegative, but -1 seems to work...
; (i.e. puts it below multiple-cursors region overlay)
(overlay-put overlay 'priority '(nil . -1))
(puthash overlay overlay tsm/-overlays)
overlay))
(defun tsm/-overlay-at-point (point)
"Get overlay at POINT, or make one and add to `tsm/-overlays' if it does not exist."
(or (tsm/-find-overlay-at-point point)
(tsm/-overlay-at-node (treesit-node-on point point))))
(defun tsm/delete-overlay-at-point (point)
"Delete node indicator at POINT."
(interactive "d")
(let ((overlay (tsm/-find-overlay-at-point point)))
(when overlay
(delete-overlay overlay)
(remhash overlay tsm/-overlays))))
(defun tsm/-go-to-closest-overlay (point key)
(let* ((overlays (hash-table-keys tsm/-overlays))
(target-overlay
(seq-reduce (lambda (min overlay)
(if (< (funcall key overlay) (funcall key min)) overlay min))
(cdr overlays)
(car overlays))))
(goto-char (treesit-node-start (tsm/-get-node target-overlay)))))
(defun tsm/backward-overlay (point)
"Move POINT backward to the closest node indicator."
(interactive "d")
(cl-flet ((key (overlay)
(let ((value (- point (treesit-node-start (tsm/-get-node overlay)))))
(if (> value 0) value 1.0e+INF))))
(tsm/-go-to-closest-overlay point #'key)))
(defun tsm/forward-overlay (point)
"Move POINT forward to the closest node indicator."
(interactive "d")
(cl-flet ((key (overlay)
(let ((value (- (treesit-node-start (tsm/-get-node overlay)) point)))
(if (> value 0) value 1.0e+INF))))
(tsm/-go-to-closest-overlay point #'key)))
(defun tsm/node-prev (point)
"Go to previous sibling of node at POINT and go to start of node."
(interactive "d")
(let* ((overlay (tsm/-overlay-at-point point))
(node (overlay-get overlay 'node))
(next (treesit-node-prev-sibling node)))
(when next
(overlay-put overlay 'node next)
(move-overlay overlay (treesit-node-start next) (treesit-node-end next))
(goto-char (treesit-node-start next)))))
(defun tsm/node-next (point)
"Select next sibling of node at POINT and go to start of node."
(interactive "d")
(let* ((overlay (tsm/-overlay-at-point point))
(node (overlay-get overlay 'node))
(next (treesit-node-next-sibling node)))
(when next
(overlay-put overlay 'node next)
(move-overlay overlay (treesit-node-start next) (treesit-node-end next))
(goto-char (treesit-node-start next)))))
(defun tsm/node-parent (point)
"Select parent of indicated node at POINT."
(interactive "d")
(let* ((overlay (tsm/-overlay-at-point point))
(node (overlay-get overlay 'node))
(next (treesit-node-parent node)))
(when next
(overlay-put overlay 'node next)
(move-overlay overlay (treesit-node-start next) (treesit-node-end next)))))
(defun tsm/node-child (point)
"Select child containing POINT of indicated node."
(interactive "d")
(let* ((overlay (tsm/-overlay-at-point point))
(node (overlay-get overlay 'node))
(next (treesit-node-first-child-for-pos node point)))
(when next
(overlay-put overlay 'node next)
(move-overlay overlay (treesit-node-start next) (treesit-node-end next)))))
(defun tsm/node-start (point)
"Go to start of node at POINT."
(interactive "d")
(goto-char (treesit-node-start (overlay-get (tsm/-overlay-at-point point) 'node))))
(defun tsm/node-end (point)
"Go to end of node at POINT."
(interactive "d")
(goto-char (1- (treesit-node-end (overlay-get (tsm/-overlay-at-point point) 'node)))))
(defun tsm/node-mark (point)
"Mark node at POINT."
(interactive "d")
(let ((node (overlay-get (tsm/-overlay-at-point point) 'node)))
(push-mark (treesit-node-end node) nil t)
(goto-char (treesit-node-start node))))
(defun tsm/node-children (point)
"Select all immediate children of node at POINT that are TYPE."
(interactive "d")
(let* ((overlay (tsm/-overlay-at-point (point)))
(node (overlay-get overlay 'node)))
(cl-loop for i from 0 below (treesit-node-child-count node)
for child = (treesit-node-child node i)
do (tsm/-overlay-at-node child))
(delete-overlay overlay)
(remhash overlay tsm/-overlays)))
(defun tsm/-treesit-node-children (node)
"Get all children of NODE."
(cl-loop for i from 0 below (treesit-node-child-count node)
collect (treesit-node-child node i)))
(defun tsm/-collect-types (list-of-nodes)
(delete-dups (mapcar #'treesit-node-type list-of-nodes)))
(defun tsm/-get-node (overlay)
(overlay-get overlay 'node))
(defun tsm/node-children-of-type (type)
"Select all immediate children of node at POINT that are TYPE."
(interactive
(list (completing-read "Type: " (tsm/-collect-types (tsm/-treesit-node-children (tsm/-get-node (tsm/-overlay-at-point (point))))))))
(let* ((overlay (tsm/-overlay-at-point (point)))
(node (tsm/-get-node overlay)))
(cl-loop for i from 0 below (treesit-node-child-count node)
for child = (treesit-node-child node i)
if (string= type (treesit-node-type child))
do (tsm/-overlay-at-node child))
(delete-overlay overlay)
(remhash overlay tsm/-overlays)))
(defun tsm/clear-overlays-of-type (type)
"Remove all overlays of TYPE."
(interactive
(list (completing-read "Type: " (tsm/-collect-types (mapcar #'tsm/-get-node (hash-table-keys tsm/-overlays))))))
(dolist (overlay (hash-table-keys tsm/-overlays))
(when (string= type (treesit-node-type (tsm/-get-node overlay)))
(delete-overlay overlay)
(remhash overlay tsm/-overlays))))
;;;###autoload
(defun tsm/clear-overlays (&optional beg end)
"Remove all overlays. BEG and END are unused."
(interactive)
(ignore beg end)
(dolist (overlay (hash-table-keys tsm/-overlays))
(delete-overlay overlay))
(clrhash tsm/-overlays))
(defun tsm/mc/mark-all-overlays (point)
"Add a multiple-cursor cursor at the start of each node overlay"
(interactive "d")
(cl-flet ((key (overlay)
(abs (- point (treesit-node-start (tsm/-get-node overlay))))))
(let* ((overlays (hash-table-keys tsm/-overlays))
(current-overlay (tsm/-overlay-at-point point))
(target-overlay
(or current-overlay
(seq-reduce (lambda (min overlay)
(if (< (key overlay) (key min)) overlay min))
(cdr overlays)
(car overlays)))))
(maphash
(lambda (overlay _)
(ignore _)
(unless (eql overlay target-overlay)
(goto-char (treesit-node-start (tsm/-get-node overlay)))
(mc/create-fake-cursor-at-point)))
tsm/-overlays)
(goto-char (treesit-node-start (tsm/-get-node target-overlay)))
(if (> (mc/num-cursors) 1)
(multiple-cursors-mode 1)
(mc/disable-multiple-cursors-mode)))))
(when (require 'multiple-cursors nil 'noerror)
(push 'mc/mark-all-overlays mc--default-cmds-to-run-once))
;;;###autoload
(defvar ts-movement-map (make-sparse-keymap "Tree Sitter Movement"))
;;;###autoload
(define-minor-mode ts-movement-mode
"Movement and editing commands using treesit syntax tree."
:keymap ts-movement-map
(setq-local before-change-functions (cons #'tsm/clear-overlays before-change-functions)))
(provide 'ts-movement)
;;; ts-movement.el ends here