-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcombobulate-navigation.el
1831 lines (1618 loc) · 77.2 KB
/
combobulate-navigation.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; combobulate-navigation.el --- navigational aids for combobulate -*- lexical-binding: t; -*-
;; Copyright (C) 2021-23 Mickey Petersen
;; Author: Mickey Petersen <mickey at masteringemacs.org>
;; Package-Requires: ((emacs "29"))
;; Version: 0.1
;; Homepage: https://www.github.com/mickeynp/combobulate
;; Keywords: convenience, tools, languages
;; 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 'generator)
(require 'treesit)
(require 'combobulate-settings)
(require 'combobulate-misc)
(require 'combobulate-interface)
(require 'combobulate-setup)
(require 'map)
(declare-function combobulate-production-rules-get-rules "combobulate-procedure")
(declare-function combobulate-production-rules-get-inverted "combobulate-procedure")
(declare-function combobulate-procedure-start-matches "combobulate-procedure")
(declare-function combobulate-procedure-collect-activation-nodes "combobulate-procedure")
(defvar combobulate-skip-prefix-regexp " \t\n"
"Skip prefix regexp used to skip past whitespace characters.")
(defvar combobulate-skip-prefix-regexp-no-newline " \t"
"Skip prefix regexp used to skip past whitespace characters.")
(defsubst combobulate-group-nodes (labelled-nodes &optional group-fn)
"Group LABELLED-NODES from a query by their label.
If GROUP-FN is nil the default is to use the `car' of each cons
in LABELLED-NODES and group by the label.
If GROUP-FN is non-nil it must be a function that takes a single
cons cell as an argument."
(seq-sort
(lambda (a b) (> (length a) (length b)))
(seq-group-by (or group-fn #'car) labelled-nodes)))
(defun combobulate-pretty-print-node-type (node)
(or (and node (concat (mapconcat 'capitalize (split-string
(combobulate-node-type node)
"[_-]")
" ")))
""))
(defun combobulate-pretty-print-node-name (node default-name)
"Pretty prints the name of NODE"
(if node
(concat (mapconcat 'capitalize (split-string
(combobulate-node-type node)
"[_-]")
" "))
default-name))
(defun combobulate-pretty-print-node (node &optional highlighted)
"Pretty prints NODE and optionally HIGHLIGHTED"
(combobulate--pretty-print-node node highlighted))
(defun combobulate--pretty-print-node (node &optional highlighted)
"Internal method that pretty prints NODE and returns a string of text.
If HIGHLIGHTED then the node is highlighted with
`combobulate-tree-highlighted-node-face'. "
(if (combobulate-node-p node)
(let ((s (funcall (combobulate-read pretty-print-node-name-function) node
(combobulate-pretty-print-node-name node ""))))
(format "%s%s"
(propertize s 'face
(cond
(highlighted 'combobulate-tree-highlighted-node-face)
(t 'combobulate-tree-normal-node-face)))
(if combobulate-debug (format " %s" (combobulate-node-range node)) "")))
;; fallback in case we've got a proxy node
(if (combobulate-proxy-node-p node)
(combobulate-proxy-node-pp node)
nil)))
;;; Node comparison functions
(defun combobulate--on-node-p (node)
"Return t if the current node at point is equal to NODE"
(or (= (combobulate-node-start node) (point))))
(defun combobulate-point-in-node-range-p (node)
"Return t if point is contained between NODE's start and end positions"
(and (>= (point) (combobulate-node-start node))
(< (point) (or (combobulate-node-end node) (point-max)))))
(defun combobulate-point-in-node-overlaps-p (node)
"Return t if point overlaps NODE's start position"
(>= (point) (combobulate-node-start node)))
(defun combobulate-node-in-region-p (node-a)
"Return t if NODE-A is wholly contained in a region.
Uses `point' and `mark' to infer the boundaries."
(and
(use-region-p)
(>= (combobulate-node-start node-a)
(funcall #'min (point) (mark)))
(<= (combobulate-node-end node-a)
(funcall #'max (point) (mark)))))
(defun combobulate-before-point-blank-p (pt)
"Return t if there is nothing but blank text before PT."
(save-excursion
(goto-char pt)
(string-blank-p
(buffer-substring-no-properties
(save-excursion
(beginning-of-line)
(point))
(point)))))
(defun combobulate-before-point-anonymous-node-p (pt)
"Return the node if there is an anonymous node before point."
(save-excursion
(goto-char pt)
(skip-chars-backward combobulate-skip-prefix-regexp)
(seq-find #'combobulate-node-anonymous-p (combobulate-all-nodes-at-point t t))))
(defun combobulate-after-point-blank-p (pt)
"Return t if there is nothing but blank text or a newline after PT."
(save-excursion
(goto-char pt)
(looking-at "[ ]*\n")))
(defun combobulate-node-overlaps-node-p (node-a node-b)
"Return t if NODE-A overlaps NODE-B"
(and node-a node-b
(>= (combobulate-node-start node-a)
(combobulate-node-start node-b))
(<= (combobulate-node-start node-a)
(combobulate-node-end node-b))))
(defun combobulate-node-contains-node-p (node-a node-b)
"Return t if NODE-A is wholly contained inside NODE-B"
(and node-a node-b
(>= (combobulate-node-start node-a)
(combobulate-node-start node-b))
(<= (combobulate-node-end node-a)
(combobulate-node-end node-b))))
(defun combobulate-node-before-node-p (node-a node-b)
"Return t if NODE-A is positioned before NODE-B"
(and node-a node-b
(< (combobulate-node-start node-a)
(combobulate-node-start node-b))))
(defun combobulate-node-after-node-p (node-a node-b)
"Return t if NODE-A is positioned after NODE-B"
(and node-a node-b
(> (combobulate-node-start node-a)
(combobulate-node-start node-b))))
(defun combobulate-node-larger-than-node-p (node-a node-b)
"Return t if NODE-A is larger than NODE-B"
(and node-a node-b
(> (- (combobulate-node-end node-a) (combobulate-node-start node-a))
(- (combobulate-node-end node-b) (combobulate-node-start node-b)))))
(defun combobulate-node-smaller-than-node-p (node-a node-b)
"Return t if NODE-A is larger than NODE-B"
(and node-a node-b
(< (- (combobulate-node-end node-a) (combobulate-node-start node-a))
(- (combobulate-node-end node-b) (combobulate-node-start node-b)))))
(defun combobulate-node-ends-before-node-p (node-a node-b)
"Return t if NODE-A ends before NODE-B "
(and node-a node-b
(< (combobulate-node-end node-a)
(combobulate-node-end node-b))))
(defun combobulate--query-from-node (query node &optional beg end node-only)
"Executes QUERY against NODE and returns the results"
(when combobulate-debug
(treesit-query-validate (treesit-node-language node) query))
(treesit-query-capture node query beg end node-only))
(defun combobulate-query-node-text (query node node-only)
"Executes QUERY against NODE and retrieve their node text
If NODE-ONLY is non-nil then only the node texts are returned"
(mapcar (lambda (node) (if node-only (combobulate-node-text node)
(combobulate-node-text (cdr node))))
(combobulate-query-search node query t t)))
(defun combobulate-linear-siblings (node &optional anonymous)
"Return all linear siblings of NODE.
Linear siblings are nodes that are at the same level in the
syntax tree."
(let ((siblings '()))
(cl-do ((prev (combobulate-node-prev-sibling node anonymous)
(combobulate-node-prev-sibling prev anonymous)))
((null prev))
(push prev siblings))
(setq siblings (nreverse siblings))
(push node siblings)
(cl-do ((next (combobulate-node-next-sibling node anonymous)
(combobulate-node-next-sibling next anonymous)))
((null next))
(push next siblings))
(nreverse siblings)))
(defun combobulate--get-nearest-navigable-node ()
"Returns the nearest navigable node to point"
(combobulate-node-at-point combobulate-navigable-nodes))
(defun combobulate-get-parents (node)
"Get all parent nodes of NODE"
(reverse
(let ((parents '()))
(while (setq node (combobulate-node-parent node))
(push node parents))
parents)))
(defun combobulate-get-specific-parent-type (node specific-types &optional skip-self-similar)
"Get the first parent node of NODE that matches one of the SPECIFIC-TYPES
If SKIP-SELF-SIMILAR is non-nil then the first parent node that is
self-similar to NODE is skipped"
(let* ((parents (combobulate-get-parents node))
(self-similar-node nil)
(match (seq-find (lambda (match-node)
(and (member (combobulate-node-type match-node) specific-types)
;; NOTE there's a special case here. If we
;; are at the beginning of a node, like a
;; JSX element, then that is also the
;; self-same type of node we're often
;; looking for as a parent. Only we're
;; going to match against ourselves and so
;; we're not getting the 'actual' parent
;; of `node'. This only happens when the
;; noce you're looking at happens to be
;; self-similarly the same as the parent.
(if (and skip-self-similar (combobulate-point-at-beginning-of-node-p match-node))
;; This is the equivalent of skipping a parent, so we should not increment.
(progn (setq self-similar-node match-node) nil)
t)))
parents)))
(cons self-similar-node match)))
(defun combobulate-find-similar-ancestors (node parents)
"Find PARENTS of NODE that share common production rules."
(let ((node-types (combobulate-production-rules-get-inverted (combobulate-node-type node))))
(seq-filter
(lambda (parent-node)
(seq-intersection
(combobulate-production-rules-get-inverted (combobulate-node-type parent-node))
node-types))
parents)))
(defun combobulate-node-visible-window-p (node &optional exclude-start exclude-end)
"Return t if NODE is visible in the current window"
(and node
;; This appears to be faster than `pos-visible-in-window-p'. More research needed.
(or exclude-start
(>= (combobulate-node-start node)
(save-excursion (goto-char (point-min))
(forward-line (1- (line-number-at-pos (window-start))))
(point))))
(or exclude-end (<= (combobulate-node-start node)
(save-excursion (goto-char (point-min))
(forward-line (1- (line-number-at-pos (window-end))))
(point))))
;; Too slow?
;; (pos-visible-in-window-p (combobulate-node-start node) (selected-window))
))
(defun combobulate-node-on-or-after-node-p (node-a node-b)
"Return t if NODE-A is positioned on or after NODE-B"
(and node-a node-b
(>= (combobulate-node-start node-a)
(combobulate-node-start node-b))))
(defun combobulate-point-at-node-p (node &optional end)
"Return t if point is at the beginning (or maybe END) of NODE"
(and (= (if end (combobulate-node-end node)
(combobulate-node-start node))
(point))))
(defun combobulate-node-before-point-p (node)
"Return t if NODE's start position is < point"
(< (combobulate-node-start node) (point)))
(defun combobulate-node-on-or-before-point-p (node)
"Return t if NODE's start position is <= point"
(<= (combobulate-node-start node) (point)))
(defun combobulate-node-after-point-p (node)
"Return t if NODE's start position is > point"
(> (combobulate-node-start node) (point)))
(defun combobulate-node-ends-after-point-p (node)
"Return t if NODE's end position is > point"
(> (combobulate-node-end node) (point)))
(defun combobulate-node-on-or-after-point-p (node)
"Return t if NODE's start position is >= point"
(>= (combobulate-node-start node) (point)))
(defun combobulate-point-near-node (node)
"Return t if point is inside or at NODE."
(or (combobulate-point-at-node-p node)
(combobulate-point-in-node-range-p node)))
(defun combobulate-node-start-line (node)
"Return the line number of NODE's start position."
(line-number-at-pos (combobulate-node-start node)))
(defun combobulate-node-end-line (node)
"Return the line number of NODE's end position."
(line-number-at-pos (combobulate-node-end node)))
(defun combobulate-node-occupies-single-line-p (node)
"Return t if NODE occupies a single line of text in its buffer."
(and node
(= (combobulate-node-start-line node)
(combobulate-node-end-line node))))
(defun combobulate-move-to-node (node &optional end)
"Moves the point to NODE and if END is set to the end of the node."
(unless node
(error "Cannot move to node as it does not exist."))
(goto-char (if end (combobulate-node-end node)
(combobulate-node-start node)))
node)
(defun combobulate-visual-move-to-node (node &optional end auto)
"Move point to node, maybe the END, and then visually indicate it.
If AUTO is non-nil, then move to the end if point is at NODE's
start."
(when node
(combobulate-move-to-node
node (or end (and auto (= (combobulate-node-start node) (point)))))
(unless (combobulate-proxy-node-p node)
(combobulate--flash-node node))
node))
(defun combobulate--make-navigation-query ()
"Generates a query that matches all default node types"
`([,@(mapcar (lambda (node) (list (make-symbol node))) combobulate-navigable-nodes)] @node))
(defun combobulate--query-tree (query filter-fn)
"Given QUERY build a query and filter elements with FILTER-FN"
(when (combobulate-node-p (combobulate-buffer-root-node))
(seq-filter filter-fn (mapcar 'cdr (combobulate--query-from-node query (combobulate-buffer-root-node))))))
(defun combobulate-get-parents-until (point-node stop-parent)
"Collect parents of POINT-NODE until STOP-PARENT is found."
(let ((collected)
(all-parents (combobulate-get-parents point-node)))
(when (member stop-parent all-parents)
(while all-parents
(let ((parent (pop all-parents)))
(if (equal parent stop-parent)
(setq all-parents nil)
(push parent collected))))
(reverse (cons stop-parent collected)))))
(defun combobulate-get-parent-nodes (point-node possible-parents)
"Find POSSIBLE-PARENTS on or near POINT-NODE.
POSSIBLE-PARENTS must be a list of strings or forms. If a form,
it must be a valid combobulate query that contains one or more
`@parent' labels.
Returns a list of parents ordered closest to farthest."
(let* ((actual-parents (combobulate-get-parents point-node))
(matched-parents))
(reverse (seq-uniq
(seq-sort #'combobulate-node-after-node-p
(dolist (possible-parent possible-parents matched-parents)
(cond
((stringp possible-parent)
(when-let (m (seq-find (lambda (p) (equal (combobulate-node-type p) possible-parent))
actual-parents))
(push m matched-parents)))
((consp possible-parent)
(dolist (actual-parent actual-parents)
(pcase-dolist (`(,label . ,match) (combobulate-query-search actual-parent possible-parent t))
(when (eq label '@parent)
(push match matched-parents))))))))))))
(defun combobulate--split-node-types (node-types)
"Split NODE-TYPES into two lists: one of consp nodes and one without."
(let ((hierarchical-node-types)
(flat-node-types))
(dolist (node-type node-types)
(if (consp node-type)
(push node-type hierarchical-node-types)
(push node-type flat-node-types)))
(list hierarchical-node-types flat-node-types)))
(defun combobulate-node-at-point (&optional node-types named-only)
"Return the smallest syntax node at point whose type is one of NODE-TYPES "
(let* ((p (point))
(node (combobulate-node-on p p nil named-only)))
(if node-types
(let ((this node))
(catch 'done
(while this
(let ((smallest-node (combobulate-node-descendant-for-range
this
(combobulate-node-start this)
(combobulate-node-start this))))
(cond
((member (combobulate-node-type this) node-types)
(throw 'done this))
((member (combobulate-node-type smallest-node) node-types)
(throw 'done smallest-node))
(t (setq this (combobulate-node-parent this))))
))))
node)))
(defun combobulate--get-all-navigable-nodes-at-point ()
"Returns all navigable nodes that start at `point'.
The returned list is ordered smallest-to-largest by the node's
extent."
(seq-filter #'combobulate-navigable-node-p (combobulate-all-nodes-at-point)))
(defun combobulate-all-nodes-at-point (&optional backward anonymous)
"Returns all nodes that start at `point'.
The returned list is ordered smallest-to-largest by the node's
extent."
(let ((nodes)
(sub-node (combobulate-node-descendant-for-range
(combobulate-root-node) (if backward (1- (point)) (point)) (point) anonymous)))
(while (and sub-node (= (if backward (combobulate-node-end sub-node) (combobulate-node-start sub-node)) (point)))
(push sub-node nodes)
(setq sub-node (combobulate-node-parent sub-node)))
(reverse nodes)))
(defun combobulate-node-unique (nodes)
"Removes duplicate NODES and keeps only uniques"
(seq-uniq nodes (lambda (a b) (and (combobulate-node-eq a b)))))
(defun combobulate-node-range-extent (nodes)
"Returns the extent -- the smallest node position and the largest -- in NODES"
(let ((smallest most-positive-fixnum)
(largest most-negative-fixnum))
(mapc (lambda (c)
(and c (pcase-let ((`(,start . ,end) (combobulate-node-range c)))
(when (< start smallest)
(setq smallest start))
(when (> end largest)
(setq largest end)))))
nodes)
(cons smallest largest)))
(defun combobulate-get-error-nodes (&optional node)
"Return all nodes in NODES that are of type ERROR."
(combobulate-query-capture (or node (combobulate-root-node)) '((ERROR) @node) nil nil t))
(defun combobulate-filter-nodes-by-type (nodes unwanted-node-types)
"Filter NODES of UNWANTED-NODE-TYPES."
(while (and unwanted-node-types nodes)
(let ((unwanted-node-type (pop unwanted-node-types)))
(setq nodes (seq-filter (lambda (node) (not (equal (combobulate-node-type node) unwanted-node-type))) nodes))))
nodes)
(cl-defun combobulate-filter-nodes (nodes &key keep-types remove-types (get-fn nil))
(when (and keep-types remove-types)
(error "Cannot specify both `:keep-types' and `:remove-types'."))
(if (and (not keep-types) (not remove-types))
nodes
(seq-filter (lambda (elem)
(let ((node-type (combobulate-node-type (if get-fn
(funcall get-fn elem)
elem))))
(if keep-types
(and keep-types (member node-type keep-types))
(if (and remove-types (member node-type remove-types))
nil
t))))
nodes)))
(defun combobulate-node-looking-at (node-types)
"Returns the node point is looking at if it is one of NODE-TYPES."
(when-let (node (combobulate-node-at-point node-types))
(when (combobulate-point-in-node-range-p node)
node)))
(defun combobulate-node-point (node &optional end)
"Returns the `point' of NODE at its beginning or END
If NODE is nil, then nil is returned."
(when node
(if end (combobulate-node-end node)
(combobulate-node-start node))))
(defun combobulate-skip-whitespace-forward (&optional skip-newline)
"Skip whitespace forward, including newlines if SKIP-NEWLINE is non-nil."
(skip-chars-forward
(if skip-newline combobulate-skip-prefix-regexp
combobulate-skip-prefix-regexp-no-newline)))
(defun combobulate-skip-whitespace-backward (&optional skip-newline)
"Skip whitespace backward, including newlines if SKIP-NEWLINE is non-nil."
(skip-chars-backward
(if skip-newline combobulate-skip-prefix-regexp
combobulate-skip-prefix-regexp-no-newline)))
(cl-defmacro with-navigation-nodes ((&key (nodes nil) skip-prefix backward (skip-newline t) (procedures nil)) &rest body)
"Invoke BODY with a list of specific navigational nodes, and maybe advance point.
If `:nodes' is non-nil, it must be a list of legitimate tree
sitter node types to `let'-bind to
`combobulate-navigable-nodes'. If nil, or not specified,
use the default nodes.
If `:skip-prefix' is non-nil, then skip forward (unless
`:backward' is set) in the direction, skipping past
`combobulate-skip-prefix-regexp' characters.
If an error is raised during BODY, then reset the point to its
original position."
(declare (indent 1) (debug (sexp body)))
(let ((--old-pos (gensym))
(--err (gensym)))
`(let* ((procedure-value
(and
(not (equal ',procedures nil))
,procedures))
(combobulate-navigable-nodes
(or ,nodes
(when (and procedure-value (not ,nodes))
(combobulate-procedure-collect-activation-nodes
procedure-value))
(combobulate-read default-nodes)
(combobulate-procedure-collect-activation-nodes
(combobulate-read procedures-default))))
(combobulate-default-procedures procedure-value))
;; keep the old position around: if we skip chars around but
;; `body' fails with an error we want to snap back.
(let ((,--old-pos (point))
(,--err t))
(when ,skip-prefix
(if ,backward
(skip-chars-backward
(if ,skip-newline combobulate-skip-prefix-regexp
combobulate-skip-prefix-regexp-no-newline))
(combobulate-skip-whitespace-forward ,skip-newline)))
;; preserves call stack in case of an error
(unwind-protect
(prog1 (progn ,@body)
(setq ,--err nil))
(when ,--err (goto-char ,--old-pos))
nil)))))
(defmacro with-argument-repetition (arg &rest body)
"Repeats BODY an ARG number of times
This is designed to handle the prefix argument and negative
modifier you can pass to many interactive movement commands."
(declare (indent 1) (debug (atom body)))
(let ((--arg (gensym))
(--inc (gensym))
(--result (gensym)))
`(let* ((,--arg (or ,arg 1))
(,--inc (if (> ,--arg 0) 1 -1))
(,--result))
(while (/= ,--arg 0)
(setq ,--result
(prog1 (progn ,@body)
(setq ,--arg (- ,--arg ,--inc)))))
,--result)))
(defun combobulate-navigable-node-p (node)
"Returns non-nil if NODE is a navigable node"
(when node
(member (combobulate-node-type node) combobulate-navigable-nodes)))
(defun combobulate-point-at-beginning-of-node-p (node)
"Returns non-nil if the beginning position of NODE is equal to `point'"
(= (combobulate-node-point node) (point)))
(defun combobulate-node-blank-p (node)
"Returns t if NODE consists of blank characters.
The function `string-blank-p' is used to determine this."
(string-blank-p (combobulate-node-text node)))
(defun combobulate-point-at-end-of-node-p (node &optional error-margin)
"Returns non-nil if the end position of NODE is equal to `point'
If ERROR-MARGIN is given an integer an allowance of up to
ERRROR-MARGIN in the end point position is used to determine if
`point' is considered at the end of a node."
(or (= (combobulate-node-point node t) (point))
(<= (abs (- (point) (combobulate-node-point node t)))
(or error-margin 0))))
(defun combobulate--goto-node (node &optional end)
"Moce point to the beginning position of NODE"
(and node (goto-char (if end (combobulate-node-end node) (combobulate-node-start node)))))
(defun combobulate-nav-get-parent (node)
"Finds a navigable parent of NODE."
(and node
(catch 'done
(while (setq node (combobulate-node-parent node))
(if (and (combobulate-navigable-node-p node)
(not (combobulate-point-at-beginning-of-node-p node)))
(throw 'done node))))))
(defun combobulate-nav-get-parents (node &optional skip-current)
"Finds all navigable parents of NODE.
SKIP-CURRENT removes all nodes where the point at the beginning
of the node."
(seq-filter (lambda (node)
(and (combobulate-navigable-node-p node)
(if skip-current
(not (combobulate-point-at-beginning-of-node-p node))
t)))
(combobulate-get-parents node)))
(defun combobulate-nav-get-smallest-node-at-point (&optional end)
"Returns the smallest navigable node at point, possibly from the END"
(seq-filter (lambda (node) (and (combobulate-navigable-node-p node)
(funcall (if end #'combobulate-point-at-end-of-node-p
#'combobulate-point-at-beginning-of-node-p)
node)))
(combobulate-all-nodes-at-point)))
(defun combobulate-nav-logical-next ()
"Navigate to the next logical node."
(treesit-search-forward
(combobulate-node-at-point)
#'combobulate-node-after-point-p))
(defun combobulate-nav-logical-previous ()
"Navigate to the previous logical node."
(treesit-search-forward
(combobulate-node-at-point)
#'combobulate-node-before-point-p t))
(defun combobulate-nodes-share-parent-p (node-a node-b)
"Return t if NODE-A and NODE-B have a common navigable ancestor."
(let ((parent-a (combobulate-nav-get-parent node-a))
(parent-b (combobulate-nav-get-parent node-b)))
(and parent-a parent-b (combobulate-node-eq parent-a parent-b))))
(defun combobulate-nav-get-siblings (node)
"Return all navigable siblings of NODE."
(combobulate-procedure-start-matches node))
(defun combobulate--get-sibling (node direction)
"Returns the sibling node of NODE in the specified DIRECTION.
The sibling node is determined by the value of DIRECTION, which
can be either `backward' or `forward'. If DIRECTION is
`backward', the previous sibling of NODE is returned.
If DIRECTION is `forward', the next sibling of NODE is
returned.
If DIRECTION is `self', then NODE is resolved to the closes
self-like sibling node.
The function will aggressively try to search through the parents
of the current node if the direction if `forward'. This is done
to try and prevent point from getting stuck at the end of a node
that technically has another immediate parent."
(when node
(save-excursion
(combobulate--goto-node node)
(let* ((siblings (combobulate-nav-get-siblings node)))
(cond
((eq direction 'forward)
(car (seq-filter #'combobulate-node-after-point-p siblings)))
((eq direction 'backward)
(car (last (seq-filter #'combobulate-node-before-point-p siblings))))
((eq direction 'self)
(or (car (seq-filter #'combobulate-point-at-beginning-of-node-p siblings))
(when (combobulate-point-at-beginning-of-node-p node) node))))))))
(defun combobulate-nav-get-next-sibling (node)
"Get the next sibling of NODE"
(combobulate--get-sibling node 'forward))
(defun combobulate-nav-get-prev-sibling (node)
"Get the previous sibling of NODE"
(combobulate--get-sibling node 'backward))
(defun combobulate-nav-get-self-sibling (node)
"Get the current (self) sibling of NODE."
(combobulate--get-sibling node 'self))
(defun combobulate-nav-get-child (node)
"Finds the first navigable child of NODE"
(car-safe (seq-filter #'combobulate-node-p
(flatten-tree
(combobulate-build-sparse-tree
'forward combobulate-navigable-nodes
nil node)))))
(defun combobulate-forward-sexp-function-1 (backward)
(car (seq-filter
(lambda (node) (and (combobulate-navigable-node-p node)
(funcall (if backward
#'combobulate-point-at-end-of-node-p
#'combobulate-point-at-beginning-of-node-p)
node)))
(combobulate-all-nodes-at-point backward))))
(defun combobulate-forward-sexp-function (arg)
"Combobulate-aware function capable of navigating by sexp.
This function must be installed in `forward-sexp-function' to
work properly."
(with-navigation-nodes (:procedures
(combobulate-read procedures-sexp)
:skip-prefix t :backward (< arg 0))
(let ((node)
(inc (if (> arg 0) 1 -1))
(backward (< arg 0)))
(while (/= arg 0)
(unless (setq node (combobulate-forward-sexp-function-1 backward))
;; no node found? try harder..
(save-excursion
(if backward
(progn (skip-chars-forward combobulate-skip-prefix-regexp)
(skip-syntax-forward ". "))
(skip-syntax-forward ". ")
(skip-chars-forward combobulate-skip-prefix-regexp)))
(setq node (combobulate-forward-sexp-function-1 backward)))
(goto-char (if node
(if backward
(combobulate-node-start node)
(combobulate-node-end node))
(or (scan-sexps (point) inc) (buffer-end inc))))
(when backward (save-excursion (backward-prefix-chars) (point)))
(setq arg (- arg inc))))))
(defun combobulate-walk-tree (tree node-fn &optional result-fn)
"Walk TREE applying NODE-FN to each node.
RESULT-FN is called post-order with the left and right-hand side
of the result from the recursive call. By default it calls `cons'
on the result.
NODE-FN can throw `stop' to stop walking; or `skip' to skip
that part of the branch."
(catch 'stop
(combobulate-walk-tree-1 tree node-fn
nil
(or result-fn #'cons)
0)))
(defun combobulate-walk-tree-1 (tree node-fn leaf-fn result-fn depth)
(pcase tree
;; ((guard (and (consp tree)
;; (atom (car tree))
;; (null (cdr tree))))
;; (funcall leaf-fn tree depth))
((pred combobulate-node-p)
(funcall node-fn tree depth))
(`(,left . ,right)
(catch 'skip
(funcall result-fn
(combobulate-walk-tree-1 left node-fn leaf-fn result-fn (1+ depth))
(combobulate-walk-tree-1 right node-fn leaf-fn result-fn depth))))))
(defun combobulate-build-sparse-tree (direction match-nodes &optional match-fn start-node limit)
"Build a sparse tree of MATCH-NODES in DIRECTION.
Optionally use MATCH-FN instead of the builtin
search (`combboulate--node-after-point-p' or
`combobulate-node-before-point-p' depending on direction).
If START-NODE is set, use it in lieu of `combobulate-root-node'."
(with-navigation-nodes (:nodes match-nodes :backward (eq direction 'backward))
(combobulate-induce-sparse-tree
(or start-node (combobulate-root-node))
(lambda (node)
(and (combobulate-navigable-node-p node)
(if match-fn
(funcall match-fn node)
(cond
((eq direction 'forward)
(combobulate-node-after-point-p node))
((eq direction 'backward)
(combobulate-node-before-point-p node))
(t (error "Unknown direction `%s'" direction))))))
nil
limit)))
(defun combobulate-get-nodes-at-depth (tree depth)
"Walk TREE and find all nodes at DEPTH."
(if (zerop depth)
(if (consp tree)
(list (car tree))
nil)
(if (consp tree)
(apply 'append (mapcar (lambda (node) (combobulate-get-nodes-at-depth node (1- depth)))
(cdr tree)))
nil)))
(defun combobulate-find-node-in-subtree (tree node &optional starting-offset)
"Find the subtree containing NODE in TREE.
TREE is a tree data structure in the form of a nested conses.
NODE is an element that is expected to be found in TREE.
STARTING-OFFSET is the depth at which the search for NODE should
start. If not provided, the search starts at the root
level (depth 0).
The function returns the subtree containing NODE, or nil if NODE
is not found.
This function uses `combobulate-get-nodes-at-depth' to retrieve
the nodes at each depth and `combobulate-node-eq' to compare NODE
with the nodes at each depth. The search terminates as soon as
NODE is found or all depths have been searched."
(let ((subtree)
(offset (or starting-offset 0)))
(catch 'done
(while (setq subtree (combobulate-get-nodes-at-depth tree offset))
(when (seq-position subtree node #'combobulate-node-eq)
(throw 'done subtree))
(cl-incf offset)))))
(defun combobulate-nav-to-defun (direction &optional node)
"Navigate to a defun in DIRECTION, possibly from NODE.
DIRECTION must be `forward' or `backward'."
(when-let* ((current-node (or node
(combobulate--get-nearest-navigable-node)
(combobulate-node-at-point)))
(min-depth most-positive-fixnum)
(tree (save-excursion
(combobulate-move-to-node current-node (eq direction 'backward))
(combobulate-build-sparse-tree direction combobulate-navigable-nodes
(if (eq direction 'backward)
#'combobulate-node-before-point-p
#'combobulate-node-on-or-after-point-p)))))
(let ((valid-nodes)
(current-node-depth
;; determine the smallest depth in the tree and also the
;; depth of `current-node'.
(combobulate-walk-tree tree
(lambda (leaf depth)
(setq min-depth (min depth min-depth))
(when (combobulate-node-eq current-node leaf)
;; once we've found
;; `current-node' in the tree,
;; we unwind the walk; no need
;; to continue.
(throw 'stop depth))
(cons depth leaf)))))
;; if we get anything but a number back, default to node depth
;; 0
(when (consp current-node-depth)
(setq current-node-depth 0))
;; now walk the tree again. This time it's to find matches
;; that satisfy `combobulate-beginning-of-defun-behavior'
(combobulate-walk-tree
tree
(lambda (leaf depth)
(when (and (if (eq 'forward direction)
(or (combobulate-point-near-node leaf)
(combobulate-node-on-or-after-point-p leaf))
(combobulate-node-before-point-p leaf))
(or
;; we can always match against nodes at the
;; root depth. we need this in case current
;; node depth is root-level.
(= depth min-depth)
(when (eq 'backward direction)
(cond
;;; methods of movement of use to combobulate only (probably.)
((eq 'sibling-only combobulate-beginning-of-defun-behavior)
(= depth current-node-depth))
;;; methods of movement of interest to users.
;; `self-and-sibling-first' matches the defun
;; we're in and any other defun at the same or
;; lower depth than we're currently at
((eq 'self-and-sibling-first combobulate-beginning-of-defun-behavior)
(<= depth current-node-depth))
;; `parent' matches only nodes at a depth less
;; than our current node depth, including self
((eq 'parent combobulate-beginning-of-defun-behavior)
(or (< depth current-node-depth)
(combobulate-node-eq leaf current-node)))
;; `root' means we only match the minimum depth.
((eq 'root combobulate-beginning-of-defun-behavior)
(= depth min-depth))
;; `linear' matches any defun at any depth
((eq 'linear combobulate-beginning-of-defun-behavior) t)))))
(push leaf valid-nodes))
(cons depth leaf)))
(if (eq 'forward direction)
(car (last valid-nodes))
(car valid-nodes)))))
(defun combobulate-nav-beginning-of-defun ()
"Navigate backward to the beginning of defun."
(combobulate-nav-to-defun 'backward))
(defun combobulate-nav-end-of-defun ()
"Navigate forward to the end of the defun."
(combobulate-nav-to-defun 'forward))
(defun combobulate--navigate-scan (text direction)
"Scan for TEXT in DIRECTION."
(save-excursion
(let ((text (rx symbol-start (literal text) symbol-end)))
(if (funcall
(if (eq direction 'next) #'re-search-forward #'re-search-backward)
text
nil
t
;; Skip ahead of the text at point if it matches TEXT. We want
;; the match before/after that.
(if (and (eq direction 'next) (looking-at-p text)) 2 1))
(combobulate-proxy-node-make-from-range (match-beginning 0) (match-end 0) "Scan" text)
(error "No more matches for `%s'" text)))))
(defvar combobulate--navigate-term nil
"The search term used by `combobulate--navigate-seqeunce'.")
(defun combobulate--navigate-sequence (direction)
"Navigate to the next node in the sequence in DIRECTION.
If there is no node in the sequence in DIRECTION, raise an error.
If the command is re-issued after an error is raised, the search will
continue by scanning in DIRECTION for any other contextual node (as per
`context-nodes') that has the same text."
(let* ((thing (thing-at-point 'symbol))
(node (combobulate--get-nearest-navigable-node))
(node-text (or (with-navigation-nodes (:procedures (combobulate-read context-nodes))
;; we must explicitly get the nearest
;; navigable node again as we have changed
;; the context.
(combobulate-node-text (combobulate--get-nearest-navigable-node)))
;; fall back to the thing at point if this
;; function is triggered in a comment, string,
;; or other ex-node context.
thing))
(seq-nodes (ignore-errors (combobulate-procedure-start-matches node))))
(cond
;; This is the 'fallback scan' in case there are no sequences
;; available at/near point. It can only trigger if:
;;
;; 1. There are no sequences available at/near point. 2. The
;; last command was a sequence scan, indicating continuity (i.e.,
;; the user has requested multiple scans in a row and we should
;; just proceed accordingly.)
((or (null seq-nodes)
(eq last-command 'combobulate-navigation-sequence-scan))
(unless thing (error "No valid symbol at point"))
(setq this-command 'combobulate-navigation-sequence-scan)
(combobulate--navigate-scan
;; Use the last term if we have one, otherwise use the symbol
;; at point.
(if (not (eq last-command 'combobulate-navigation-sequence-scan))
(progn (push-mark)
(setq combobulate--navigate-term thing))
combobulate--navigate-term)
direction))