forked from emacs-tree-sitter/ts-fold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts-fold.el
1264 lines (1071 loc) · 50 KB
/
ts-fold.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
;;; ts-fold.el --- Code folding using tree-sitter -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Junyi Hou
;; Copyright (C) 2021-2023 Shen, Jen-Chieh
;; Created date 2021-08-11 14:12:37
;; Author: Junyi Hou <[email protected]>
;; Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/emacs-tree-sitter/ts-fold
;; Version: 0.3.1
;; Package-Requires: ((emacs "26.1") (tree-sitter "0.15.1") (s "1.9.0") (fringe-helper "1.0.1"))
;; Keywords: convenience folding tree-sitter
;; 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:
;;
;; This package provides a code-folding mechanism based on tree-sitter
;; package. Turn on the minor-mode `ts-fold-mode' to enable
;; this mechanism. Note that all functionalities provided here based on the
;; `tree-sitter-mode', and thus it should be enabled before
;; `ts-fold-mode' can properly fold codes.
;;; Code:
(require 'seq)
(require 'subr-x)
(require 's)
(require 'tree-sitter)
(require 'ts-fold-util)
(require 'ts-fold-parsers)
(require 'ts-fold-summary)
;;
;; (@* "Customization" )
;;
(defgroup ts-fold nil
"Code folding using tree-sitter."
:group 'tree-sitter
:prefix "ts-fold-")
;; TODO(everyone): This is a bit messy, but try to keep this alist
;; alphabetically sorted
(defcustom ts-fold-range-alist
`((actionscript-mode . ,(ts-fold-parsers-actionscript))
(agda-mode . ,(ts-fold-parsers-agda))
(arduino-mode . ,(ts-fold-parsers-arduino))
(fasm-mode . ,(ts-fold-parsers-asm))
(masm-mode . ,(ts-fold-parsers-asm))
(nasm-mode . ,(ts-fold-parsers-asm))
(beancount-mode . ,(ts-fold-parsers-beancount))
(c-mode . ,(ts-fold-parsers-c))
(c++-mode . ,(ts-fold-parsers-c++))
(caml-mode . ,(ts-fold-parsers-ocaml))
(cmake-mode . ,(ts-fold-parsers-cmake))
(clojure-mode . ,(ts-fold-parsers-clojure))
(csharp-mode . ,(ts-fold-parsers-csharp))
(css-mode . ,(ts-fold-parsers-css))
(dart-mode . ,(ts-fold-parsers-dart))
(emacs-lisp-mode . ,(ts-fold-parsers-elisp))
(elixir-mode . ,(ts-fold-parsers-elixir))
(erlang-mode . ,(ts-fold-parsers-erlang))
(ess-r-mode . ,(ts-fold-parsers-r))
(fish-mode . ,(ts-fold-parsers-fish))
(gdscript-mode . ,(ts-fold-parsers-gdscript))
(glsl-mode . ,(ts-fold-parsers-glsl))
(go-mode . ,(ts-fold-parsers-go))
(groovy-mode . ,(ts-fold-parsers-groovy))
(jenkinsfile-mode . ,(ts-fold-parsers-groovy))
(haskell-mode . ,(ts-fold-parsers-haskell))
(hlsl-mode . ,(ts-fold-parsers-hlsl))
(html-mode . ,(ts-fold-parsers-html))
(jai-mode . ,(ts-fold-parsers-jai))
(java-mode . ,(ts-fold-parsers-java))
(javascript-mode . ,(ts-fold-parsers-javascript))
(js-mode . ,(ts-fold-parsers-javascript))
(js2-mode . ,(ts-fold-parsers-javascript))
(js3-mode . ,(ts-fold-parsers-javascript))
(json-mode . ,(ts-fold-parsers-json))
(jsonc-mode . ,(ts-fold-parsers-json))
(jsonnet-mode . ,(ts-fold-parsers-jsonnet))
(julia-mode . ,(ts-fold-parsers-julia))
(kotlin-mode . ,(ts-fold-parsers-kotlin))
(latex-mode . ,(ts-fold-parsers-latex))
(lisp-mode . ,(ts-fold-parsers-lisp))
(lisp-interaction-mode . ,(ts-fold-parsers-lisp))
(llvm-mode . ,(ts-fold-parsers-llvm))
(llvm-mir-mode . ,(ts-fold-parsers-llvm-mir))
(lua-mode . ,(ts-fold-parsers-lua))
(makefile-mode . ,(ts-fold-parsers-make))
(makefile-automake-mode . ,(ts-fold-parsers-make))
(makefile-gmake-mode . ,(ts-fold-parsers-make))
(makefile-makepp-mode . ,(ts-fold-parsers-make))
(makefile-bsdmake-mode . ,(ts-fold-parsers-make))
(makefile-imake-mode . ,(ts-fold-parsers-make))
(markdown-mode . ,(ts-fold-parsers-markdown))
(mermaid-mode . ,(ts-fold-parsers-mermaid))
(noir-mode . ,(ts-fold-parsers-noir))
(nix-mode . ,(ts-fold-parsers-nix))
(ocaml-mode . ,(ts-fold-parsers-ocaml))
(org-mode . ,(ts-fold-parsers-org))
(pascal-mode . ,(ts-fold-parsers-pascal))
(perl-mode . ,(ts-fold-parsers-perl))
(php-mode . ,(ts-fold-parsers-php))
(python-mode . ,(ts-fold-parsers-python))
(qss-mode . ,(ts-fold-parsers-qss))
(rjsx-mode . ,(ts-fold-parsers-javascript))
(rst-mode . ,(ts-fold-parsers-rst))
(ruby-mode . ,(ts-fold-parsers-ruby))
(rust-mode . ,(ts-fold-parsers-rust))
(rustic-mode . ,(ts-fold-parsers-rust))
(scheme-mode . ,(ts-fold-parsers-scheme))
(sh-mode . ,(ts-fold-parsers-bash))
(scala-mode . ,(ts-fold-parsers-scala))
(sql-mode . ,(ts-fold-parsers-sql))
(swift-mode . ,(ts-fold-parsers-swift))
(tablegen-mode . ,(ts-fold-parsers-tablegen))
(toml-mode . ,(ts-fold-parsers-toml))
(conf-toml-mode . ,(ts-fold-parsers-toml))
(tuareg-mode . ,(ts-fold-parsers-ocaml))
(typescript-mode . ,(ts-fold-parsers-typescript))
(verilog-mode . ,(ts-fold-parsers-verilog))
(vhdl-mode . ,(ts-fold-parsers-vhdl))
(nxml-mode . ,(ts-fold-parsers-xml))
(yaml-mode . ,(ts-fold-parsers-yaml))
(k8s-mode . ,(ts-fold-parsers-yaml))
(zig-mode . ,(ts-fold-parsers-zig)))
"An alist of (major-mode . (foldable-node-type . function)).
FUNCTION is used to determine where the beginning and end for FOLDABLE-NODE-TYPE
in MAJOR-MODE. It should take a single argument (the syntax node with type
FOLDABLE-NODE-TYPE) and return the buffer positions of the beginning and end of
the fold in a cons cell. See `ts-fold-range-python-def' for an example."
:type '(alist :key-type symbol
:value-type (alist :key-type symbol :value-type function))
:group 'ts-fold)
(defcustom ts-fold-mode-hook nil
"Hook to run when enabling `ts-fold-mode`."
:type 'hook
:group 'ts-fold)
(defcustom ts-fold-on-next-line t
"If non-nil, we leave ending keywords on the next line.
This is only used in languages that uses keyword to end the scope.
For example, Lua, Ruby, etc."
:type 'boolean
:group 'ts-fold)
(defcustom ts-fold-replacement "..."
"Show this string instead of the folded text."
:type 'string
:group 'ts-fold)
(defface ts-fold-replacement-face
'((t :foreground "#808080" :box (:line-width -1 :style pressed-button)))
"Face used to display the fold replacement text."
:group 'ts-fold)
(defface ts-fold-fringe-face
'((t ()))
"Face used to display fringe contents."
:group 'ts-fold)
;;
;; (@* "Externals" )
;;
(defvar ts-fold-indicators-mode)
(declare-function ts-fold-indicators-mode "ts-fold-indicators.el")
(declare-function ts-fold-indicators-refresh "ts-fold-indicators.el")
;;
;; (@* "Entry" )
;;
(defun ts-fold--enable ()
"Start folding minor mode."
(setq-local line-move-ignore-invisible t)
(add-to-invisibility-spec '(ts-fold . t))
;; evil integration
(when (bound-and-true-p evil-fold-list)
(add-to-list 'evil-fold-list
'((ts-fold-mode)
:toggle ts-fold-toggle
:open ts-fold-open
:close ts-fold-close
:open-rec ts-fold-open-recursively
:open-all ts-fold-open-all
:close-all ts-fold-close-all))))
(defun ts-fold--disable ()
"Stop folding minor mode."
(remove-from-invisibility-spec '(ts-fold . t))
(let ((tree-sitter-mode t))
(ts-fold-open-all)))
(defun ts-fold--tree-sitter-trigger ()
"Turn `ts-fold-mode' on and off alongside `tree-sitter-mode' when in a mode
ts-fold can act on."
(if (and tree-sitter-mode (ts-fold-usable-mode-p))
(ts-fold-mode 1)
(ts-fold-mode -1)))
;;;###autoload
(define-minor-mode ts-fold-mode
"Folding code using tree sitter."
:group 'ts-fold
:init-value nil
:lighter "TS-Fold"
(if ts-fold-mode (ts-fold--enable) (ts-fold--disable)))
;;;###autoload
(define-minor-mode global-ts-fold-mode
"Use `ts-fold-mode' wherever possible."
:group 'ts-fold
:init-value nil
:lighter nil
:global t
(if global-ts-fold-mode
(progn
(add-hook 'tree-sitter-mode-hook #'ts-fold--tree-sitter-trigger)
;; try to turn on in all buffers.
(dolist (buf (buffer-list))
(with-current-buffer buf
(ts-fold--tree-sitter-trigger))))
(remove-hook 'tree-sitter-mode-hook #'ts-fold--tree-sitter-trigger)))
(defun ts-fold-usable-mode-p (&optional mode)
"Return non-nil if `ts-fold' has defined folds for MODE."
(let ((mode (or mode major-mode)))
(alist-get mode ts-fold-range-alist)))
;;;###autoload
(define-minor-mode ts-fold-line-comment-mode
"Enable line comment folding."
:group 'ts-fold
:init-value nil
(when (bound-and-true-p ts-fold-indicators-mode)
(ts-fold-indicators-refresh)))
;;
;; (@* "Core" )
;;
(defun ts-fold--range-on-same-line (range)
"Return non-nil if RANGE is on the same line."
(let ((beg (car range))
(end (cdr range))
(lbp) (lep))
(save-excursion
(goto-char beg)
(setq lbp (line-beginning-position)
lep (line-end-position)))
(and (<= lbp beg) (<= beg lep)
(<= lbp end) (<= end lep))))
(defun ts-fold--get-fold-range (node)
"Return the beginning (as buffer position) of fold for NODE.
Return nil if there is no fold to be made."
(when-let* ((fold-alist (alist-get major-mode ts-fold-range-alist))
(fold-func (alist-get (tsc-node-type node) fold-alist)))
(cond ((functionp fold-func) (funcall fold-func node (cons 0 0)))
((listp fold-func) (funcall (nth 0 fold-func) node (cons (nth 1 fold-func) (nth 2 fold-func))))
(t (user-error "Bad folding function for node")))))
(defun ts-fold--non-foldable-node-p (node mode-ranges)
"Return non-nil if NODE is a non-foldable in MODE-RANGES."
(or (not (alist-get (tsc-node-type node) mode-ranges)) ; Not registered, continue.
(let ((range (ts-fold--get-fold-range node)))
(or (not range) ; Range not defined, continue.
(ts-fold--range-on-same-line range))))) ; On same line, continue.
(defun ts-fold--foldable-node-at-pos (&optional pos)
"Return the smallest foldable node at POS. If POS is nil, use `point'.
Return nil if no valid node is found.
This function is borrowed from `tree-sitter-node-at-point'."
(let* ((pos (or pos (point)))
(mode-ranges (alist-get major-mode ts-fold-range-alist))
(root (tsc-root-node tree-sitter-tree))
(node (tsc-get-descendant-for-position-range root pos pos))
;; Used for looping
(current node))
(while (and current
(ts-fold--non-foldable-node-p current mode-ranges))
(setq current (tsc-get-parent current)))
current))
;;
;; (@* "Overlays" )
;;
(defun ts-fold--create-overlay (range)
"Create invisible overlay in RANGE."
(when range
(let* ((beg (car range))
(end (cdr range))
(ov (make-overlay beg end)))
(overlay-put ov 'creator 'ts-fold)
(overlay-put ov 'invisible 'ts-fold)
(overlay-put ov 'display (or (and ts-fold-summary-show
(ts-fold-summary--get (buffer-substring beg end)))
ts-fold-replacement))
(overlay-put ov 'face 'ts-fold-replacement-face)
(overlay-put ov 'isearch-open-invisible #'ts-fold--isearch-open))))
(defun ts-fold--isearch-open (ov)
"Open overlay OV during `isearch' session."
(delete-overlay ov))
(defun ts-fold-overlay-at (node)
"Return the ts-fold overlay at NODE if NODE is foldable and folded.
Return nil otherwise."
(when-let* ((range (ts-fold--get-fold-range node)))
(thread-last (overlays-in (car range) (cdr range))
(seq-filter (lambda (ov)
(and (eq (overlay-get ov 'invisible) 'ts-fold)
(= (overlay-start ov) (car range))
(= (overlay-end ov) (cdr range)))))
car)))
;;
;; (@* "Commands" )
;;
(defmacro ts-fold--ensure-ts (&rest body)
"Run BODY only if `tree-sitter-mode` is enabled."
(declare (indent 0))
`(if (bound-and-true-p tree-sitter-mode)
(progn ,@body)
(user-error "Ignored, tree-sitter-mode is not enabled in the current buffer")))
;;;###autoload
(defun ts-fold-close (&optional node)
"Fold the syntax node at `point` if it is foldable.
Foldable nodes are defined in `ts-fold-range-alist' for the
current `major-mode'.
If no NODE is found in point, do nothing."
(interactive)
(ts-fold--ensure-ts
(when-let* ((node (or node (ts-fold--foldable-node-at-pos))))
;; make sure I do not create multiple overlays for the same fold
(when-let* ((ov (ts-fold-overlay-at node)))
(delete-overlay ov))
(when-let* ((range (ts-fold--get-fold-range node)))
(ts-fold--create-overlay range)))))
;;;###autoload
(defun ts-fold-open ()
"Open the fold of the syntax node in which `point' resides.
If the current node is not folded or not foldable, do nothing."
(interactive)
(ts-fold--ensure-ts
(when-let* ((node (ts-fold--foldable-node-at-pos))
(ov (ts-fold-overlay-at node)))
(delete-overlay ov)
t)))
;;;###autoload
(defun ts-fold-open-recursively ()
"Open recursively folded syntax NODE that are contained in the node at point."
(interactive)
(ts-fold--ensure-ts
(when-let* ((node (ts-fold--foldable-node-at-pos))
(beg (tsc-node-start-position node))
(end (tsc-node-end-position node)))
(thread-last (overlays-in beg end)
(seq-filter (lambda (ov) (eq (overlay-get ov 'invisible) 'ts-fold)))
(mapc #'delete-overlay)))))
;;;###autoload
(defun ts-fold-close-all ()
"Fold all foldable syntax nodes in the buffer."
(interactive)
(ts-fold--ensure-ts
(let* ((ts-fold-indicators-mode)
(node (tsc-root-node tree-sitter-tree))
(patterns (seq-mapcat (lambda (fold-range) `((,(car fold-range)) @name))
(alist-get major-mode ts-fold-range-alist)
'vector))
(query (tsc-make-query tree-sitter-language patterns))
(nodes-to-fold (tsc-query-captures query node #'ignore)))
(thread-last nodes-to-fold
(mapcar #'cdr)
(mapc #'ts-fold-close)))))
;;;###autoload
(defun ts-fold-open-all ()
"Unfold all syntax nodes in the buffer."
(interactive)
(ts-fold--ensure-ts
(thread-last (overlays-in (point-min) (point-max))
(seq-filter (lambda (ov) (eq (overlay-get ov 'invisible) 'ts-fold)))
(mapc #'delete-overlay))))
;;;###autoload
(defun ts-fold-toggle ()
"Toggle the syntax node at `point'.
If the current syntax node is not foldable, do nothing."
(interactive)
(ts-fold--ensure-ts
(if-let* ((node (ts-fold--foldable-node-at-pos (point)))
(ov (ts-fold-overlay-at node)))
(progn (delete-overlay ov) t)
(ts-fold-close))))
(defun ts-fold--after-command (&rest _)
"Function call after interactive commands."
(ts-fold-indicators-refresh))
(let ((commands '(ts-fold-close
ts-fold-open
ts-fold-open-recursively
ts-fold-close-all
ts-fold-open-all
ts-fold-toggle)))
(dolist (command commands)
(advice-add command :after #'ts-fold--after-command)))
;;
;; (@* "Rule Helpers" )
;;
(defun ts-fold--next-prev-node (node next)
"Return previous/next sibling node starting from NODE.
If NEXT is non-nil, return next sibling. Otherwirse, return previouse sibling."
(if next (tsc-get-next-sibling node) (tsc-get-prev-sibling node)))
(defun ts-fold--next-prev-node-skip-newline (node next)
"Like function `ts-fold--next-prev-node'.
For arguments NODE and NEXT, please see the function `ts-fold--next-prev-node'
for more information."
(let ((iter-node (ts-fold--next-prev-node node next)))
(while (and iter-node
(equal "\n" (tsc-node-text iter-node)))
(setq iter-node (ts-fold--next-prev-node iter-node next)))
iter-node))
(defun ts-fold--continuous-node-prefix (node prefix next)
"Iterate through node starting from NODE and compare node-text to PREFIX;
then return the last iterated node.
Argument NEXT is a boolean type. If non-nil iterate forward; otherwise iterate
in backward direction."
(let* ((iter-node node) (last-node node)
(last-line (car (tsc-node-start-point node))) line text break
(line-range 1) (last-line-range 1) max-line-range
(indentation (ts-fold--indentation (tsc-node-start-position iter-node)))
next-indentation)
(while (and iter-node (not break))
(setq text (string-trim (tsc-node-text iter-node))
line (car (tsc-node-start-point iter-node))
line-range (1+ (ts-fold--count-matches "\n" text))
max-line-range (max line-range last-line-range)
next-indentation (ts-fold--indentation (tsc-node-start-position iter-node)))
(if (and (ts-fold--in-range-p line (- last-line max-line-range) (+ last-line max-line-range))
(string-prefix-p prefix text)
(= indentation next-indentation))
(setq last-node iter-node last-line line
last-line-range (1+ (ts-fold--count-matches "\n" text)))
(setq break t))
(setq iter-node (ts-fold--next-prev-node-skip-newline iter-node next)))
last-node))
(defun ts-fold-range-seq (node offset)
"Return the fold range in sequence starting from NODE.
Argument OFFSET can be used to tweak the final beginning and end position."
(let ((beg (1+ (tsc-node-start-position node)))
(end (1- (tsc-node-end-position node))))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-markers (node offset start-seq &optional end-seq)
"Return the fold range for NODE with an OFFSET where the range starts at
the end of the first occurence of START-SEQ and ends at the end of the node
or the start of the last occurence of the optional parameter LAST-SEQ.
START-SEQ and LAST-SEQ can be named tree-sitter nodes or anonomous nodes.
If no occurence is found for START-SEQ or END-SEQ or the
occurences overlap, then the range returned is nil."
(when start-seq
(when-let ((beg-node (car (ts-fold-find-children node start-seq)))
(end-node (if end-seq
(car (last (ts-fold-find-children node end-seq)))
node))
(beg (tsc-node-end-position beg-node))
(end (if end-seq
(tsc-node-start-position end-node)
(1- (tsc-node-end-position node)))))
(unless (> beg end) (ts-fold--cons-add (cons beg end) offset)))))
(defun ts-fold-range-line-comment (node offset prefix)
"Define fold range for line comment.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information.
Argument PREFIX is the comment prefix in string."
(save-excursion
(when-let* ((ts-fold-line-comment-mode) ; XXX: Check enabled!?
(first-node (ts-fold--continuous-node-prefix node prefix nil))
(last-node (ts-fold--continuous-node-prefix node prefix t))
(prefix-len (length prefix))
(beg (+ (tsc-node-start-position first-node) prefix-len))
(end (tsc-node-end-position last-node)))
(ts-fold--cons-add (cons beg end) offset))))
(defun ts-fold-range-block-comment (node offset)
"Define fold range for block comment.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(ts-fold-range-seq node (ts-fold--cons-add '(1 . -1) offset)))
(defun ts-fold-range-c-like-comment (node offset)
"Define fold range for C-like comemnt.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let ((text (tsc-node-text node)))
(if (and (string-match-p "\n" text) (string-prefix-p "/*" text))
(ts-fold-range-block-comment node offset)
(if (string-prefix-p "///" text)
(ts-fold-range-line-comment node offset "///")
(ts-fold-range-line-comment node offset "//")))))
;;
;; (@* "Languages" )
;;
(defun ts-fold-range-asm--find-last-instruction (node)
"Find the last instruction node by starting NODE."
(let* ((iter-node (ts-fold--next-prev-node-skip-newline node t))
(last iter-node))
(while (and iter-node
(not (member (ts-fold-2str (tsc-node-type iter-node))
(ts-fold-listify "label"))))
(setq last iter-node
iter-node (ts-fold--next-prev-node-skip-newline iter-node t)))
last)) ; return last insturction node
(defun ts-fold-range-asm-label (node offset)
"Define fold range for `label' in Assembly.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((beg (tsc-node-end-position node))
(end (ts-fold-range-asm--find-last-instruction node))
(end (tsc-node-end-position end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-beancount-transaction (node offset)
"Define fold range for `transaction' in Beancount.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((beg (tsc-node-start-position node))
(beg (ts-fold--eol beg))
(end (1- (tsc-node-end-position node))))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-c-preproc-if (node offset)
"Define fold range for `if' preprocessor.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let* ((named-node (tsc-get-child-by-field node :condition))
(else (or (tsc-get-child-by-field node :alternative)
(car (ts-fold-find-children node "#endif"))))
(beg (tsc-node-end-position named-node))
(end (1- (tsc-node-start-position else))))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-c-preproc-ifdef (node offset)
"Define fold range for `ifdef' and `ifndef' preprocessor.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((named-node (tsc-get-child-by-field node :name))
(else (or (tsc-get-child-by-field node :alternative)
(car (ts-fold-find-children node "#endif"))))
(beg (tsc-node-end-position named-node))
(end (1- (tsc-node-start-position else))))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-c-preproc-elif (node offset)
"Define fold range for `elif' preprocessor.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((named-node (tsc-get-child-by-field node :condition))
(parent (or (ts-fold-find-parent node "preproc_if")
(ts-fold-find-parent node "preproc_ifdef")))
(next (or (tsc-get-child-by-field node :alternative)
(car (ts-fold-find-children parent "#endif"))))
(beg (tsc-node-end-position named-node))
(end (1- (tsc-node-start-position next))))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-c-preproc-else (node offset)
"Define fold range for `else' preprocessor.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((else-str (car (split-string (tsc-node-text node) "\n")))
(parent (or (ts-fold-find-parent node "preproc_if")
(ts-fold-find-parent node "preproc_ifdef")))
(next (car (ts-fold-find-children parent "#endif")))
(beg (+ (tsc-node-start-position node) (length else-str)))
(end (1- (tsc-node-start-position next))))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-clojure-function (node offset)
"Return the fold range for `list_lit' NODE in Clojure.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((param-node (car (ts-fold-find-children node "vec_lit")))
(next-node (tsc-get-next-sibling param-node))
(beg (tsc-node-start-position next-node))
(end (1- (tsc-node-end-position node))))
(unless ts-fold-on-next-line ; display nicely
(setq beg (ts-fold--last-eol beg)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-elisp-function (node offset)
"Return the fold range for `macro_definition' and `function_definition' NODE
in Elisp.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((param-node (tsc-get-nth-child node 4))
(beg (tsc-node-start-position param-node))
(end (1- (tsc-node-end-position node))))
(unless ts-fold-on-next-line ; display nicely
(setq beg (ts-fold--last-eol beg)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-elixir (node offset)
"Return the fold range for `function' `module' NODE in Elixir.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((end-child (ts-fold-last-child node))
(do-child (tsc-get-nth-child node 1))
(beg (tsc-node-start-position do-child))
(beg (ts-fold--last-eol beg))
(end (tsc-node-start-position end-child)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-erlang-signature (node offset start)
"Return the fold range for generic signature NODE in Erlang.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information.
Argument START is a string to target for the first node we use to find the
start of the position."
(when-let* ((start-node (car (ts-fold-find-children node start)))
(beg (tsc-node-end-position start-node))
(end (tsc-node-end-position node)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-erlang-clause-body (node offset)
"Return the fold range for `clause_body' NODE in Erlang.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(ts-fold-range-erlang-signature node offset "->"))
(defun ts-fold-range-erlang-type-guards (node offset)
"Return the fold range for `type_guards' NODE in Erlang.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(ts-fold-range-erlang-signature node offset "when"))
(defun ts-fold-range-fish-function (node offset)
"Define fold range for `function' in Fish.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((func-name (tsc-get-nth-child node 1))
(beg (tsc-node-end-position func-name))
(end (tsc-node-end-position node))
(end (- end 3)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-fish-if (node offset)
"Define fold range for `if_statement' in Fish.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((beg (tsc-node-start-position node))
(beg (ts-fold--eol beg))
(end (tsc-node-end-position node))
(end (- end 3)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-fish-case (node offset)
"Define fold range for `case_clause' in Fish.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((beg (tsc-node-start-position node))
(beg (ts-fold--eol beg))
(end (tsc-node-end-position node))
(end (1- end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-groovy-block (node offset)
"Define fold range for `block' in Groovy.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((open-bracket (car (ts-fold-find-children node "{")))
(beg (tsc-node-start-position open-bracket))
(beg (1+ beg))
(end (tsc-node-end-position node))
(end (1- end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-haskell-function (node offset)
"Define fold range for `function' in Haskell.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((beg (tsc-node-start-position node))
(beg (ts-fold--eol beg))
(end-node (ts-fold-last-child node))
(end (tsc-node-end-position end-node)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-html (node offset)
"Define fold range for tag in HTML.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let* ((beg (tsc-node-end-position (tsc-get-nth-child node 0)))
(end-node (ts-fold-last-child node))
(end (tsc-node-start-position end-node)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-julia-function (node offset)
"Return the fold range for a NODE in Julia.
It excludes the NODE's first child and the `end' keyword. For
argument OFFSET, see function `ts-fold-range-seq' for more
information."
(when-let* ((identifier (tsc-get-nth-named-child node 0))
(params (tsc-get-nth-named-child node 1))
(beg (tsc-node-end-position params))
(end (tsc-node-end-position node))
(end (- end 3)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-julia-if (node offset)
"Define fold range for if statement in Julia.
It excludes the NODE's first child and the `end' keyword. For
argument OFFSET, see function `ts-fold-range-seq' for more
information."
(when-let* ((params (car (ts-fold-find-children node "call_expression")))
(beg (tsc-node-end-position params))
(end (tsc-node-end-position node))
(end (- end 3)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-julia-let (node offset)
"Define fold range for let statement in Julia.
It excludes the NODE's first child and the `end' keyword. For
argument OFFSET, see function `ts-fold-range-seq' for more
information."
(when-let* ((vars (ts-fold-find-children node "variable_declaration"))
(last-var (last vars))
(last-var (car last-var))
(beg (tsc-node-end-position last-var))
(end (tsc-node-end-position node))
(end (- end 3)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-julia-if-for-while (node offset)
"Return the fold range for a NODE in Julia.
It excludes the NODE's first child and the `end' keyword. For
argument OFFSET, see function `ts-fold-range-seq' for more
information."
(when-let* ((condition (tsc-get-nth-named-child node 0))
(beg (tsc-node-end-position condition))
(end (tsc-node-end-position node))
(end (- end 3)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-kotlin-when (node offset)
"Return the fold range for `when' NODE in Kotlin.
It excludes the NODE's first child and the `end' keyword. For
argument OFFSET, see function `ts-fold-range-seq' for more
information."
(when-let* ((open-bracket (car (ts-fold-find-children node "{")))
(beg (tsc-node-end-position open-bracket))
(end (1- (tsc-node-end-position node))))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-lisp-function (node offset)
"Define fold range for function in Lisp .
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((header (car (ts-fold-find-children node "defun_header")))
(body (tsc-get-next-sibling header))
(beg (tsc-node-start-position body))
(end (1- (tsc-node-end-position node))))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-llvm--find-last-instruction (node)
"Find the last instruction node by starting NODE."
(let* ((iter-node (ts-fold--next-prev-node-skip-newline node t))
(last iter-node))
(while (and iter-node
(not (member (ts-fold-2str (tsc-node-type iter-node))
(ts-fold-listify '("label" "}")))))
(setq last iter-node
iter-node (ts-fold--next-prev-node-skip-newline iter-node t)))
last)) ; return last insturction node
(defun ts-fold-range-llvm-label (node offset)
"Define fold range for `label' in LLVM.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((beg (tsc-node-end-position node))
(end (ts-fold-range-llvm--find-last-instruction node))
(end (tsc-node-end-position end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-llvm-mir-label (node offset)
"Define fold range for `label' in LLVM MIR.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((label (car (ts-fold-find-children node "label")))
(colon (tsc-get-next-sibling label))
(beg (tsc-node-end-position colon))
(beg (ts-fold--eol beg))
(end (ts-fold-range-llvm--find-last-instruction label))
(end (tsc-node-end-position end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-lua-comment (node offset)
"Define fold range for Lua comemnt.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let ((text (tsc-node-text node)))
(if (and (string-match-p "\n" text) (string-prefix-p "--[[" text))
(ts-fold-range-block-comment node
;; XXX: Add 2 to for ]] at the end
(ts-fold--cons-add (cons 2 0) offset))
(ts-fold-range-line-comment node offset "--"))))
(defun ts-fold-range-lua-function (node offset)
"Define fold range for Lua `function' declaration.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let* ((params (tsc-get-child-by-field node :parameters))
(beg (tsc-node-end-position params))
(end (- (tsc-node-end-position node) 3))) ; fit identifier `end'
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-lua-if (node offset)
"Define fold range for Lua `if' statement.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let* ((then (car (ts-fold-find-children node "then")))
(beg (tsc-node-end-position then))
(next (or (ts-fold-find-children-traverse node "elseif_statement")
(ts-fold-find-children-traverse node "else_statement")))
(end (if next
(tsc-node-start-position (car next))
(- (tsc-node-end-position node) 3))))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-lua-elseif (node offset)
"Define fold range for Lua `elseif' statement.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let* ((then (car (ts-fold-find-children node "then")))
(beg (tsc-node-end-position then))
(next (tsc-get-next-sibling node))
(end (if next
(tsc-node-start-position next)
(tsc-node-end-position node))))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-lua-else (node offset)
"Define fold range for Lua `else' statement.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let* ((beg (+ (tsc-node-start-position node) 4)) ; fit `else', 4 letters
(next (tsc-get-next-sibling node)) ; the `end' node
(end (tsc-node-start-position next)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-lua-do-loop (node offset)
"Define fold range for Lua `while' and `for' statement.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let* ((do (car (ts-fold-find-children node "do")))
(beg (tsc-node-end-position do))
(end (- (tsc-node-end-position node) 3)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-lua-repeat (node offset)
"Define fold range for Lua `repeat' statement.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(let* ((beg (+ (tsc-node-start-position node) 6)) ; fit `repeat', 6 letters
(until (car (ts-fold-find-children node "until")))
(end (tsc-node-start-position until)))
(when ts-fold-on-next-line ; display nicely
(setq end (ts-fold--last-eol end)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-make-recipe (node offset)
"Define fold range for `recipe' in Make.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((last-child (ts-fold-last-child node))
(beg (tsc-node-start-position node))
(end (tsc-node-end-position last-child)))
(ts-fold--cons-add (cons beg end) offset)))
(defun ts-fold-range-mermaid-diagram (node offset)
"Define fold range for any diagram in Mermaid.
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(when-let* ((first-child (tsc-get-nth-child node 0))
(beg (tsc-node-end-position first-child))
(beg (ts-fold--eol beg))