-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcombobulate-interface.el
327 lines (265 loc) · 12.1 KB
/
combobulate-interface.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
;;; combobulate-interface.el --- interface for treesitter -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Mickey Petersen
;; Author: Mickey Petersen <[email protected]>
;; Keywords:
;; 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 'treesit)
(eval-when-compile (require 'cl-lib))
(declare-function combobulate-filter-nodes "combobulate-navigation")
(declare-function combobulate-all-nodes-at-point "combobulate-navigation")
(declare-function combobulate-pretty-print-node "combobulate-navigation")
(declare-function combobulate-node-at-point "combobulate-navigation")
(declare-function combobulate--goto-node "combobulate-navigation")
(declare-function combobulate-get-registered-language "combobulate-setup")
(defsubst combobulate-language-available-p (language)
(treesit-language-available-p language))
(defsubst combobulate-create-language (language &optional buffer no-reuse)
(treesit-parser-create language buffer no-reuse))
(defsubst combobulate-node-p (node)
(treesit-node-p node))
(defsubst combobulate-buffer-root-node (&optional language)
(treesit-buffer-root-node (or language (combobulate-primary-language))))
(defsubst combobulate-node-on (beg end &optional parser-or-lang named)
(treesit-node-on beg end (or parser-or-lang (combobulate-primary-language)) named))
(defsubst combobulate-node-at (pos &optional parser-or-lang named)
(treesit-node-at pos (or parser-or-lang (combobulate-primary-language)) named))
(defsubst combobulate-induce-sparse-tree (root predicate &optional process-fn limit)
(treesit-induce-sparse-tree root predicate process-fn limit))
(defsubst combobulate-parser-list (&optional buffer)
(treesit-parser-list buffer))
(defsubst combobulate-parser-language (parser)
(treesit-parser-language parser))
(defsubst combobulate-parser-create (language &optional buffer no-reuse)
(treesit-parser-create language buffer no-reuse))
(defsubst combobulate-parser-delete (language)
(treesit-parser-delete language))
(defsubst combobulate-parser-node (node)
(treesit-node-parser node))
(defun combobulate-primary-language (&optional quiet)
(or
(treesit-language-at (point))
(car (combobulate-get-registered-language major-mode))
(when-let ((first-language (car (combobulate-parser-list))))
(combobulate-parser-language first-language))
(unless quiet
(error "No parsers available"))))
(defsubst combobulate-node-buffer (node)
(cl-assert (combobulate-node-p node) t "Must be a real tree-sitter node")
(treesit-node-buffer node))
(defsubst combobulate-query-validate (language query)
(treesit-query-validate language query))
(defsubst combobulate-query-capture (node query &optional beg end node-only)
(treesit-query-capture node query beg end node-only))
(defsubst combobulate-node-named-p (node)
(if (combobulate-node-p node)
(treesit-node-check node 'named)
(combobulate-proxy-node-named node)))
(defsubst combobulate-node-anonymous-p (node)
(not (combobulate-node-named-p node)))
(defsubst combobulate-node-start (node)
(if (combobulate-node-p node)
(treesit-node-start node)
(combobulate-proxy-node-start node)))
(defsubst combobulate-node-end (node)
(if (combobulate-node-p node)
(treesit-node-end node)
(combobulate-proxy-node-end node)))
(defsubst combobulate-node-field-name (node)
(if (combobulate-node-p node)
(treesit-node-field-name node)
(combobulate-proxy-node-field node)))
(defsubst combobulate-node-range (node)
(cons (combobulate-node-start node) (combobulate-node-end node)))
(defsubst combobulate-node-text (node &optional with-properties)
(if (combobulate-node-p node)
(or (treesit-node-text node (not with-properties)) "")
(or (and node (combobulate-proxy-node-text node)) "")))
(defsubst combobulate-node-next-sibling (node &optional anonymous)
(treesit-node-next-sibling node (not anonymous)))
(defsubst combobulate-node-prev-sibling (node &optional anonymous)
(treesit-node-prev-sibling node (not anonymous)))
(defsubst combobulate-node-child (node n &optional anonymous)
(treesit-node-child node n (not anonymous)))
(defsubst combobulate-node-children (node &optional anonymous)
(treesit-node-children node (not anonymous)))
(defsubst combobulate-node-parent (node)
(treesit-node-parent node))
(defsubst combobulate-parent-while (node pred)
(treesit-parent-while node pred))
(defsubst combobulate-parent-until (node pred)
(treesit-parent-until node pred))
(defsubst combobulate-node-child-by-field (node field)
(treesit-node-child-by-field-name node field))
(defsubst combobulate-node-eq (node1 node2)
(if (and (combobulate-node-p node1) (combobulate-node-p node2))
(treesit-node-eq node1 node2)
(eq node1 node2)))
(defsubst combobulate-root-node ()
(treesit-buffer-root-node
(combobulate-primary-language)))
(defsubst combobulate-node-descendant-for-range (node beg end &optional all)
(treesit-node-descendant-for-range node beg end (not all)))
(defsubst combobulate-node-type (node)
(and node
(if (combobulate-node-p node)
(treesit-node-type node)
(combobulate-proxy-node-type node))))
(defsubst combobulate-filter-child (node pred &optional anonymous)
(treesit-filter-child node pred (not anonymous)))
(cl-defstruct (combobulate-proxy-node
(:constructor combobulate-proxy-node-create)
(:copier nil))
"Proxy object for a some properties of a real tree-sitter node.
Only some fields are kept: relationships to other nodes are not
kept."
start end text type named field node pp extra)
(defun combobulate-proxy-node-make-from-nodes (nodes)
"Factory that creates a facsimile proxy node of NODES."
(let ((proxies
(mapcar
(pcase-lambda ((or (and (pred consp) `(,mark . ,node)) node))
(let ((tgt-node (if (combobulate-node-p node)
(combobulate-proxy-node-create
:start (set-marker (make-marker) (treesit-node-start node))
:end (set-marker (make-marker) (treesit-node-end node))
:text (treesit-node-text node t)
:type (treesit-node-type node)
:named (treesit-node-check node 'named)
:field (treesit-node-field-name node)
:node node
:pp (combobulate-pretty-print-node node)
:extra nil)
node)))
(if mark (cons mark tgt-node) tgt-node)))
(ensure-list nodes))))
(if (consp nodes)
proxies
(car-safe proxies))))
(defun combobulate-proxy-node-make-from-range (beg end &optional type pp)
"Factory that creates a facsimile proxy node of the region BEG END."
(combobulate-proxy-node-create
:start (set-marker (make-marker) beg)
:end (set-marker (make-marker) end)
:text (buffer-substring-no-properties beg end)
:type (or type "region")
:named t
:field nil
:node nil
:pp (or pp "Region")
:extra nil))
(defun combobulate-proxy-node-make-point-node (&optional pt)
"Create a proxy node at `point'."
(combobulate-proxy-node-create
:start (or pt (point))
:end (or pt (point))
:text ""
:type "point"
:named t
:node nil
:field nil
:pp "Point"))
(defun combobulate-proxy-node-to-real-node (proxy-node)
"Attempt to find the real tree-sitter node PROXY-NODE points to."
(when proxy-node
;; if we are holding on to a valid treesit node then just pass
;; that on provided it's still useful.
(cond
;; todo: disabled until it is possible to detect if a node belongs to a deleted parser
;; (or (treesit-node-check (combobulate-proxy-node-node proxy-node) 'outdated)
;; (treesit-node-check (combobulate-proxy-node-node proxy-node) 'missing))
;; check if proxy-node is even a proxy node
((not (combobulate-proxy-node-p proxy-node)) proxy-node)
(t (save-excursion
(combobulate--goto-node proxy-node)
(car-safe (seq-filter (lambda (pt-node)
(and
(equal (cons (marker-position (car (combobulate-node-range proxy-node)))
(marker-position (cdr (combobulate-node-range proxy-node))))
(combobulate-node-range pt-node))
(equal (combobulate-node-type pt-node)
(combobulate-node-type proxy-node))
(or (equal (combobulate-node-field-name pt-node)
(combobulate-node-field-name proxy-node))
t)))
(combobulate-filter-nodes
(combobulate-all-nodes-at-point)
:keep-types (list (combobulate-node-type proxy-node))))))))))
(defmacro combobulate-with-change-group (&rest body)
(declare (indent 0) (debug t))
(let ((handle (gensym "--change-group-handle--"))
(success (gensym "--change-group-success--"))
(explicitly-cancelled (gensym "--change-group-explicitly-cancelled--")))
`(let ((,handle (prepare-change-group))
(,explicitly-cancelled nil)
;; Don't truncate any undo data in the middle of this.
(undo-outer-limit nil)
(undo-limit most-positive-fixnum)
(undo-strong-limit most-positive-fixnum)
(,success nil))
(cl-flet
((accept-changes () (accept-change-group ,handle))
(cancel-changes ()
(cancel-change-group ,handle)
(setq ,explicitly-cancelled t)))
(unwind-protect
(progn
;; This is inside the unwind-protect because
;; it enables undo if that was disabled; we need
;; to make sure that it gets disabled again.
(activate-change-group ,handle)
(prog1 ,(macroexp-progn body)
(setq ,success t)))
;; Either of these functions will disable undo
;; if it was disabled before.
(cond
;; If we explicitly cancelled, do not cancel or accept the
;; changes.
(,explicitly-cancelled nil)
(,success (accept-changes))
(t (cancel-changes))))))))
(defmacro combobulate-atomic-change-group (&rest body)
"Re-entrant change group, like `atomic-change-group'.
This means that if BODY exits abnormally,
all of its changes to the current buffer are undone.
This works regardless of whether undo is enabled in the buffer.
This mechanism is transparent to ordinary use of undo;
if undo is enabled in the buffer and BODY succeeds, the
user can undo the change normally."
(declare (indent 0) (debug t))
`(combobulate-with-change-group
,@body))
(cl-defstruct (combobulate-proffer-action
(:constructor combobulate-proffer-action-create)
(:copier nil))
index
display-indicator
current-node
proxy-nodes
refactor-id
prompt-description
extra-map)
(defmacro lambda-slots (slots &rest body)
"Construct a macro that expands to a lambda with the given SLOTS and BODY.
The lambda takes a single argument, ACTION, which is an EIEIO object
or `cl-defstruct'.
The requested SLOTS are bound to the action object using `with-slots'."
(declare (indent defun)
(debug (&define [&or symbolp (symbolp &optional sexp &rest sexp)]
def-body)))
`(lambda (action)
(with-slots ,slots action
,@body)))
(provide 'combobulate-interface)
;;; combobulate-interface.el ends here