forked from emacs-tree-sitter/treesit-fold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treesit-fold.el
1554 lines (1329 loc) · 65.4 KB
/
treesit-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
;;; treesit-fold.el --- Code folding using treesit -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Junyi Hou
;; Copyright (C) 2021-2024 Shen, Jen-Chieh
;; Created date 2021-08-11 14:12:37
;; Author: Junyi Hou <junyi.yi.hou@gmail.com>
;; Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/emacs-tree-sitter/treesit-fold
;; Version: 0.1.0
;; Package-Requires: ((emacs "29.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 `treesit-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
;; `treesit-fold-mode' can properly fold codes.
;;; Code:
(require 'seq)
(require 'subr-x)
(require 'treesit)
(require 'treesit-fold-util)
(require 'treesit-fold-parsers)
(require 'treesit-fold-summary)
;;
;; (@* "Customization" )
;;
(defgroup treesit-fold nil
"Code folding using tree-sitter."
:group 'tree-sitter
:prefix "treesit-fold-")
;; TODO(everyone): This is a bit messy, but try to keep this alist
;; alphabetically sorted
(defcustom treesit-fold-range-alist
`((actionscript-mode . ,(treesit-fold-parsers-actionscript))
(agda-mode . ,(treesit-fold-parsers-agda))
(arduino-mode . ,(treesit-fold-parsers-arduino))
(asm-mode . ,(treesit-fold-parsers-asm))
(awk-mode . ,(treesit-fold-parsers-awk))
(awk-ts-mode . ,(treesit-fold-parsers-awk))
(editorconfig-conf-mode . ,(treesit-fold-parsers-editorconfig))
(fasm-mode . ,(treesit-fold-parsers-asm))
(masm-mode . ,(treesit-fold-parsers-asm))
(nasm-mode . ,(treesit-fold-parsers-asm))
(gas-mode . ,(treesit-fold-parsers-asm))
(bash-ts-mode . ,(treesit-fold-parsers-bash))
(beancount-mode . ,(treesit-fold-parsers-beancount))
(beancount-ts-mode . ,(treesit-fold-parsers-beancount))
(c-mode . ,(treesit-fold-parsers-c))
(c-ts-mode . ,(treesit-fold-parsers-c))
(c++-mode . ,(treesit-fold-parsers-c++))
(c++-ts-mode . ,(treesit-fold-parsers-c++))
(caml-mode . ,(treesit-fold-parsers-ocaml))
(caml-ts-mode . ,(treesit-fold-parsers-ocaml))
(cmake-mode . ,(treesit-fold-parsers-cmake))
(cmake-ts-mode . ,(treesit-fold-parsers-cmake))
(clojure-mode . ,(treesit-fold-parsers-clojure))
(clojure-ts-mode . ,(treesit-fold-parsers-clojure))
(csharp-mode . ,(treesit-fold-parsers-csharp))
(csharp-ts-mode . ,(treesit-fold-parsers-csharp))
(css-mode . ,(treesit-fold-parsers-css))
(css-ts-mode . ,(treesit-fold-parsers-css))
(dart-mode . ,(treesit-fold-parsers-dart))
(dart-ts-mode . ,(treesit-fold-parsers-dart))
(emacs-lisp-mode . ,(treesit-fold-parsers-elisp))
(elixir-mode . ,(treesit-fold-parsers-elixir))
(elixir-ts-mode . ,(treesit-fold-parsers-elixir))
(erlang-mode . ,(treesit-fold-parsers-erlang))
(erlang-ts-mode . ,(treesit-fold-parsers-erlang))
(ess-r-mode . ,(treesit-fold-parsers-r))
(fish-mode . ,(treesit-fold-parsers-fish))
(gdscript-mode . ,(treesit-fold-parsers-gdscript))
(gdscript-ts-mode . ,(treesit-fold-parsers-gdscript))
(gleam-ts-mode . ,(treesit-fold-parsers-gleam))
(glsl-mode . ,(treesit-fold-parsers-glsl))
(go-mode . ,(treesit-fold-parsers-go))
(go-ts-mode . ,(treesit-fold-parsers-go))
(go-mod-ts-mode . ,(treesit-fold-parsers-go))
(groovy-mode . ,(treesit-fold-parsers-groovy))
(jenkinsfile-mode . ,(treesit-fold-parsers-groovy))
(haskell-mode . ,(treesit-fold-parsers-haskell))
(haskell-ts-mode . ,(treesit-fold-parsers-haskell))
(haxe-mode . ,(treesit-fold-parsers-haxe))
(heex-mode . ,(treesit-fold-parsers-heex))
(heex-ts-mode . ,(treesit-fold-parsers-heex))
(hlsl-mode . ,(treesit-fold-parsers-hlsl))
(hlsl-ts-mode . ,(treesit-fold-parsers-hlsl))
(html-mode . ,(treesit-fold-parsers-html))
(html-ts-mode . ,(treesit-fold-parsers-html))
(jai-mode . ,(treesit-fold-parsers-jai))
(janet-mode . ,(treesit-fold-parsers-janet))
(java-mode . ,(treesit-fold-parsers-java))
(java-ts-mode . ,(treesit-fold-parsers-java))
(javascript-mode . ,(treesit-fold-parsers-javascript))
(js-mode . ,(treesit-fold-parsers-javascript))
(js-ts-mode . ,(treesit-fold-parsers-javascript))
(js2-mode . ,(treesit-fold-parsers-javascript))
(js3-mode . ,(treesit-fold-parsers-javascript))
(json-mode . ,(treesit-fold-parsers-json))
(json-ts-mode . ,(treesit-fold-parsers-json))
(jsonc-mode . ,(treesit-fold-parsers-json))
(jsonnet-mode . ,(treesit-fold-parsers-jsonnet))
(julia-mode . ,(treesit-fold-parsers-julia))
(julia-ts-mode . ,(treesit-fold-parsers-julia))
(kotlin-mode . ,(treesit-fold-parsers-kotlin))
(kotlin-ts-mode . ,(treesit-fold-parsers-kotlin))
(latex-mode . ,(treesit-fold-parsers-latex))
(latex-ts-mode . ,(treesit-fold-parsers-latex))
(LaTeX-mode . ,(treesit-fold-parsers-latex))
(lisp-mode . ,(treesit-fold-parsers-lisp))
(lisp-interaction-mode . ,(treesit-fold-parsers-lisp))
(llvm-mode . ,(treesit-fold-parsers-llvm))
(llvm-mir-mode . ,(treesit-fold-parsers-llvm-mir))
(lua-mode . ,(treesit-fold-parsers-lua))
(lua-ts-mode . ,(treesit-fold-parsers-lua))
(makefile-mode . ,(treesit-fold-parsers-make))
(makefile-ts-mode . ,(treesit-fold-parsers-make))
(makefile-automake-mode . ,(treesit-fold-parsers-make))
(makefile-gmake-mode . ,(treesit-fold-parsers-make))
(makefile-makepp-mode . ,(treesit-fold-parsers-make))
(makefile-bsdmake-mode . ,(treesit-fold-parsers-make))
(makefile-imake-mode . ,(treesit-fold-parsers-make))
(markdown-mode . ,(treesit-fold-parsers-markdown))
(markdown-ts-mode . ,(treesit-fold-parsers-markdown))
(matlab-mode . ,(treesit-fold-parsers-matlab))
(mermaid-mode . ,(treesit-fold-parsers-mermaid))
(mermaid-ts-mode . ,(treesit-fold-parsers-mermaid))
(ninja-mode . ,(treesit-fold-parsers-ninja))
(noir-mode . ,(treesit-fold-parsers-noir))
(noir-ts-mode . ,(treesit-fold-parsers-noir))
(nix-mode . ,(treesit-fold-parsers-nix))
(nix-ts-mode . ,(treesit-fold-parsers-nix))
(ocaml-mode . ,(treesit-fold-parsers-ocaml))
(ocaml-ts-mode . ,(treesit-fold-parsers-ocaml))
(org-mode . ,(treesit-fold-parsers-org))
(pascal-mode . ,(treesit-fold-parsers-pascal))
(perl-mode . ,(treesit-fold-parsers-perl))
(php-mode . ,(treesit-fold-parsers-php))
(php-ts-mode . ,(treesit-fold-parsers-php))
(python-mode . ,(treesit-fold-parsers-python))
(python-ts-mode . ,(treesit-fold-parsers-python))
(qss-mode . ,(treesit-fold-parsers-qss))
(rjsx-mode . ,(treesit-fold-parsers-javascript))
(rst-mode . ,(treesit-fold-parsers-rst))
(ruby-mode . ,(treesit-fold-parsers-ruby))
(ruby-ts-mode . ,(treesit-fold-parsers-ruby))
(rust-mode . ,(treesit-fold-parsers-rust))
(rust-ts-mode . ,(treesit-fold-parsers-rust))
(rustic-mode . ,(treesit-fold-parsers-rust))
(scheme-mode . ,(treesit-fold-parsers-scheme))
(sh-mode . ,(treesit-fold-parsers-bash))
(scala-mode . ,(treesit-fold-parsers-scala))
(scala-ts-mode . ,(treesit-fold-parsers-scala))
(sql-mode . ,(treesit-fold-parsers-sql))
(svelte-mode . ,(treesit-fold-parsers-svelte))
(swift-mode . ,(treesit-fold-parsers-swift))
(tablegen-mode . ,(treesit-fold-parsers-tablegen))
(toml-mode . ,(treesit-fold-parsers-toml))
(toml-ts-mode . ,(treesit-fold-parsers-toml))
(conf-toml-mode . ,(treesit-fold-parsers-toml))
(tuareg-mode . ,(treesit-fold-parsers-ocaml))
(typescript-mode . ,(treesit-fold-parsers-typescript))
(typescript-ts-mode . ,(treesit-fold-parsers-typescript))
(tsx-ts-mode . ,(treesit-fold-parsers-typescript))
(verilog-mode . ,(treesit-fold-parsers-verilog))
(verilog-ts-mode . ,(treesit-fold-parsers-verilog))
(vhdl-mode . ,(treesit-fold-parsers-vhdl))
(vhdl-ts-mode . ,(treesit-fold-parsers-vhdl))
(vimscript-ts-mode . ,(treesit-fold-parsers-vimscript))
(nxml-mode . ,(treesit-fold-parsers-xml))
(xml-ts-mode . ,(treesit-fold-parsers-xml))
(yaml-mode . ,(treesit-fold-parsers-yaml))
(yaml-ts-mode . ,(treesit-fold-parsers-yaml))
(k8s-mode . ,(treesit-fold-parsers-yaml))
(zig-mode . ,(treesit-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 `treesit-fold-range-python-def' for an example."
:type '(alist :key-type symbol
:value-type (alist :key-type symbol :value-type function))
:group 'treesit-fold)
(defcustom treesit-fold-mode-hook nil
"Hook to run when enabling `treesit-fold-mode`."
:type 'hook
:group 'treesit-fold)
(defcustom treesit-fold-on-fold-hook nil
"Hook runs on folding."
:type 'hook
:group 'treesit-fold)
(defcustom treesit-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 'treesit-fold)
(defcustom treesit-fold-replacement "..."
"Show this string instead of the folded text."
:type 'string
:group 'treesit-fold)
(defcustom treesit-fold-priority 30
"Fold range overlay's priority."
:type 'integer
:group 'treesit-fold)
(defface treesit-fold-replacement-face
'((t :foreground "#808080" :box (:line-width -1 :style pressed-button)))
"Face used to display the fold replacement text."
:group 'treesit-fold)
(defface treesit-fold-replacement-mouse-face
'((t :foreground "#808080" :box (:line-width -1 :style released-button)))
"Face used to when mouse hovering replacement text."
:group 'treesit-fold)
(defface treesit-fold-fringe-face
'((t ()))
"Face used to display fringe contents."
:group 'treesit-fold)
(defcustom treesit-fold-modes '(prog-mode conf-mode python-ts-mode)
"Modes in which `treesit-fold-mode' gets enabled."
:type '(repeat symbol))
(defvar-keymap treesit-fold-mode-map
:doc "Keymap used when `treesit-fold-mode' is active.")
;;
;; (@* "Externals" )
;;
(defvar treesit-fold-indicators-mode)
(declare-function treesit-fold-indicators-mode "treesit-fold-indicators.el")
(declare-function treesit-fold-indicators-refresh "treesit-fold-indicators.el")
;;
;; (@* "Entry" )
;;
(defun treesit-fold--enable ()
"Start folding minor mode."
(setq-local line-move-ignore-invisible t)
(add-to-invisibility-spec '(treesit-fold . t))
;; evil integration
(when (bound-and-true-p evil-fold-list)
(add-to-list 'evil-fold-list
'((treesit-fold-mode)
:toggle treesit-fold-toggle
:open treesit-fold-open
:close treesit-fold-close
:open-rec treesit-fold-open-recursively
:open-all treesit-fold-open-all
:close-all treesit-fold-close-all))))
(defun treesit-fold--disable ()
"Stop folding minor mode."
(remove-from-invisibility-spec '(treesit-fold . t))
(treesit-fold-open-all))
(defun treesit-fold-ready-p ()
"Return non-nil if the current buffer has a tree-sitter parser."
(treesit-parser-list))
(defun treesit-fold--trigger ()
"Enable `treesit-fold-mode' when the current mode is treesit-fold compatible."
(when (and (treesit-fold-ready-p)
(treesit-fold-usable-mode-p))
(treesit-fold-mode 1)))
;;;###autoload
(define-minor-mode treesit-fold-mode
"Folding code using tree sitter."
:group 'treesit-fold
:init-value nil
:lighter "Treesit-Fold"
:keymap treesit-fold-mode-map
(cond
((not (and (treesit-available-p)
(treesit-parser-list)
(treesit-fold-usable-mode-p)))
(when treesit-fold-mode
(treesit-fold-mode -1)))
(treesit-fold-mode
(treesit-fold--enable) t)
(t
(treesit-fold--disable))))
;;;###autoload
(define-globalized-minor-mode global-treesit-fold-mode
treesit-fold-mode treesit-fold--trigger
:group 'treesit-fold)
(defun treesit-fold-usable-mode-p (&optional mode)
"Return non-nil if `treesit-fold' has defined folds for MODE."
(let ((mode (or mode major-mode)))
(alist-get mode treesit-fold-range-alist)))
;;;###autoload
(define-minor-mode treesit-fold-line-comment-mode
"Enable line comment folding."
:group 'treesit-fold
:init-value nil
(when (bound-and-true-p treesit-fold-indicators-mode)
(treesit-fold-indicators-refresh)))
;;
;; (@* "Core" )
;;
(defun treesit-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 treesit-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 treesit-fold-range-alist))
(fold-func (alist-get (intern (treesit-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 treesit-fold--node-range-on-same-line (node)
"Return non-nil when NODE range is on the same line."
(let ((range (treesit-fold--get-fold-range node)))
(or (not range) ; Range not defined, continue.
(treesit-fold--range-on-same-line range)))) ; On same line, continue.
(defun treesit-fold--non-foldable-node-p (node mode-ranges)
"Return non-nil if NODE is a non-foldable in MODE-RANGES."
(or (not (alist-get (intern (treesit-node-type node)) mode-ranges)) ; Not registered, continue.
(treesit-fold--node-range-on-same-line node))) ; On same line, continue.
(defun treesit-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 treesit-fold-range-alist))
(root (treesit-buffer-root-node))
(node (treesit-node-descendant-for-range root pos pos))
;; Used for looping
(current node))
(while (and current
(treesit-fold--non-foldable-node-p current mode-ranges))
(setq current (treesit-node-parent current)))
current))
;;
;; (@* "Overlays" )
;;
(defun treesit-fold--create-overlay (range)
"Create invisible overlay in RANGE."
(when range
(let* ((beg (car range))
(end (cdr range))
(ov (make-overlay beg end))
(map (make-sparse-keymap)))
(define-key map (kbd "<mouse-1>") #'treesit-fold-open)
(overlay-put ov 'creator 'treesit-fold)
(overlay-put ov 'priority treesit-fold-priority)
(overlay-put ov 'invisible 'treesit-fold)
(overlay-put ov 'display
(propertize (or (and treesit-fold-summary-show
(treesit-fold-summary--get (buffer-substring beg end)))
treesit-fold-replacement)
'mouse-face 'treesit-fold-replacement-mouse-face
'help-echo "mouse-1: unfold this node"
'keymap map))
(overlay-put ov 'face 'treesit-fold-replacement-face)
(overlay-put ov 'modification-hooks '(treesit-fold--on-change))
(overlay-put ov 'insert-in-front-hooks '(treesit-fold--on-change))
(overlay-put ov 'isearch-open-invisible #'treesit-fold--on-change)
(overlay-put ov 'isearch-open-invisible-temporary #'treesit-fold--open-invisible-temporary)
ov)))
(defun treesit-fold--open-invisible-temporary (ov hide-p)
"Temporary show/hide OV depends on HIDE-P flag."
(if hide-p (treesit-fold--hide-ov ov)
(treesit-fold--show-ov ov)))
(defun treesit-fold--on-change (ov &rest _)
"Open overlay OV during content is changed."
(delete-overlay ov))
(defun treesit-fold--show-ov (ov &rest _)
"Show the OV."
(overlay-put ov 'invisible nil)
(overlay-put ov 'display nil)
(overlay-put ov 'face nil)
(treesit-fold-indicators-refresh))
(defun treesit-fold--hide-ov (ov &rest _)
"Hide the OV."
(let ((beg (overlay-start ov))
(end (overlay-end ov)))
(overlay-put ov 'invisible 'treesit-fold)
(overlay-put ov 'display (or (and treesit-fold-summary-show
(treesit-fold-summary--get (buffer-substring beg end)))
treesit-fold-replacement))
(overlay-put ov 'face 'treesit-fold-replacement-face))
(treesit-fold-indicators-refresh))
(defun treesit-fold-overlay-at (node)
"Return the treesit-fold overlay at NODE if NODE is foldable and folded.
Return nil otherwise."
(when-let* ((range (treesit-fold--get-fold-range node)))
(thread-last (overlays-in (car range) (cdr range))
(seq-filter (lambda (ov)
(and (eq (overlay-get ov 'invisible) 'treesit-fold)
(= (overlay-start ov) (car range))
(= (overlay-end ov) (cdr range)))))
car)))
;;
;; (@* "Commands" )
;;
(defmacro treesit-fold--ensure-ts (&rest body)
"Run BODY only if `tree-sitter-mode` is enabled."
(declare (indent 0))
`(if (treesit-fold-ready-p)
(progn ,@body)
(user-error "Ignored, no tree-sitter parser in current buffer")))
;;;###autoload
(defun treesit-fold-close (&optional node)
"Fold the syntax node at `point` if it is foldable.
Foldable nodes are defined in `treesit-fold-range-alist' for the
current `major-mode'.
If no NODE is found in point, do nothing."
(interactive)
(treesit-fold--ensure-ts
(when-let* ((node (or node (treesit-fold--foldable-node-at-pos))))
;; make sure I do not create multiple overlays for the same fold
(when-let* ((ov (treesit-fold-overlay-at node)))
(delete-overlay ov))
(when-let* ((range (treesit-fold--get-fold-range node))
(ov (treesit-fold--create-overlay range)))
(run-hooks 'treesit-fold-on-fold-hook)
ov))))
;;;###autoload
(defun treesit-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)
(treesit-fold--ensure-ts
(when-let* ((node (treesit-fold--foldable-node-at-pos))
(ov (treesit-fold-overlay-at node)))
(delete-overlay ov)
(run-hooks 'treesit-fold-on-fold-hook)
t)))
;;;###autoload
(defun treesit-fold-open-recursively ()
"Open recursively folded syntax NODE that are contained in the node at point."
(interactive)
(treesit-fold--ensure-ts
(when-let* ((node (treesit-fold--foldable-node-at-pos))
(beg (treesit-node-start node))
(end (treesit-node-end node))
(nodes (treesit-fold--overlays-in 'invisible 'treesit-fold beg end)))
(mapc #'delete-overlay nodes)
(run-hooks 'treesit-fold-on-fold-hook)
t)))
;;;###autoload
(defun treesit-fold-close-all ()
"Fold all foldable syntax nodes in the buffer."
(interactive)
(treesit-fold--ensure-ts
(let (nodes)
(let* ((treesit-fold-indicators-mode)
(treesit-fold-on-fold-hook)
(node (treesit-buffer-root-node))
(patterns (seq-mapcat (lambda (fold-range) `((,(car fold-range)) @name))
(alist-get major-mode treesit-fold-range-alist)))
(query (treesit-query-compile (treesit-node-language node) patterns)))
(setq nodes (treesit-query-capture node query)
nodes (cl-remove-if (lambda (node)
;; Removed if on same line
(treesit-fold--node-range-on-same-line (cdr node)))
nodes))
(thread-last nodes
(mapcar #'cdr)
(mapc #'treesit-fold-close)))
(when nodes
(run-hooks 'treesit-fold-on-fold-hook)
t))))
;;;###autoload
(defun treesit-fold-open-all ()
"Unfold all syntax nodes in the buffer."
(interactive)
(treesit-fold--ensure-ts
(when-let* ((nodes (treesit-fold--overlays-in 'invisible 'treesit-fold)))
(mapc #'delete-overlay nodes)
(run-hooks 'treesit-fold-on-fold-hook)
t)))
;;;###autoload
(defun treesit-fold-toggle ()
"Toggle the syntax node at `point'.
If the current syntax node is not foldable, do nothing."
(interactive)
(treesit-fold--ensure-ts
(if-let* ((node (treesit-fold--foldable-node-at-pos (point)))
(ov (treesit-fold-overlay-at node)))
(progn
(delete-overlay ov)
(run-hooks 'treesit-fold-on-fold-hook)
t)
(treesit-fold-close))))
(defun treesit-fold--after-command (&rest _)
"Function call after interactive commands."
(treesit-fold-indicators-refresh))
(let ((commands '(treesit-fold-close
treesit-fold-open
treesit-fold-open-recursively
treesit-fold-close-all
treesit-fold-open-all
treesit-fold-toggle)))
(dolist (command commands)
(advice-add command :after #'treesit-fold--after-command)))
;;
;; (@* "Rule Helpers" )
;;
(defun treesit-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 (treesit-node-next-sibling node) (treesit-node-prev-sibling node)))
(defun treesit-fold--next-prev-node-skip-newline (node next)
"Like function `treesit-fold--next-prev-node'.
For arguments NODE and NEXT, please see the function
`treesit-fold--next-prev-node' for more information."
(let ((iter-node (treesit-fold--next-prev-node node next)))
(while (and iter-node
(equal "\n" (ignore-errors (treesit-node-text iter-node))))
(setq iter-node (treesit-fold--next-prev-node iter-node next)))
iter-node))
(defun treesit-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 (treesit-fold--node-start-position node)) line text break
(line-range 1) (last-line-range 1) max-line-range
(indentation (treesit-fold--indentation (treesit-node-start iter-node)))
next-indentation)
(while (and iter-node (not break))
(setq text (string-trim (treesit-node-text iter-node))
line (treesit-fold--node-start-position iter-node)
line-range (1+ (treesit-fold--count-matches "\n" text))
max-line-range (max line-range last-line-range)
next-indentation (treesit-fold--indentation (treesit-node-start iter-node)))
(if (and (treesit-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+ (treesit-fold--count-matches "\n" text)))
(setq break t))
(setq iter-node (treesit-fold--next-prev-node-skip-newline iter-node next)))
last-node))
(defun treesit-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+ (treesit-node-start node)))
(end (1- (treesit-node-end node))))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-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 (treesit-fold-find-children node start-seq)))
(end-node (if end-seq
(car (last (treesit-fold-find-children node end-seq)))
node))
(beg (treesit-node-end beg-node))
(end (if end-seq
(treesit-node-start end-node)
(1- (treesit-node-end node)))))
(unless (> beg end) (treesit-fold--cons-add (cons beg end) offset)))))
(defun treesit-fold-range-line-comment (node offset prefix)
"Define fold range for line comment.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information.
Argument PREFIX is the comment prefix in string."
(save-excursion
(when-let* ((treesit-fold-line-comment-mode) ; XXX: Check enabled!?
(first-node (ignore-errors
(treesit-fold--continuous-node-prefix node prefix nil)))
(last-node (ignore-errors
(treesit-fold--continuous-node-prefix node prefix t)))
(prefix-len (length prefix))
(beg (+ (treesit-node-start first-node) prefix-len))
(end (treesit-node-end last-node)))
(treesit-fold--cons-add (cons beg end) offset))))
(defun treesit-fold-range-block-comment (node offset)
"Define fold range for block comment.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(treesit-fold-range-seq node (treesit-fold--cons-add '(1 . -1) offset)))
(defun treesit-fold-range-c-like-comment (node offset)
"Define fold range for C-like comemnt.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(let ((text (treesit-node-text node)))
(if (and (string-match-p "\n" text) (string-prefix-p "/*" text))
(treesit-fold-range-block-comment node offset)
(if (string-prefix-p "///" text)
(treesit-fold-range-line-comment node offset "///")
(treesit-fold-range-line-comment node offset "//")))))
;;
;; (@* "Languages" )
;;
(defun treesit-fold-range-asm--find-last-instruction (node)
"Find the last instruction node by starting NODE."
(let* ((iter-node (treesit-fold--next-prev-node-skip-newline node t))
(last iter-node))
(while (and iter-node
(not (member (treesit-node-type iter-node)
(treesit-fold-listify "label"))))
(setq last iter-node
iter-node (treesit-fold--next-prev-node-skip-newline iter-node t)))
last)) ; return last insturction node
(defun treesit-fold-range-asm-label (node offset)
"Define fold range for `label' in Assembly.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((beg (treesit-node-end node))
(end (treesit-fold-range-asm--find-last-instruction node))
(end (treesit-node-end end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-beancount-transaction (node offset)
"Define fold range for `transaction' in Beancount.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((beg (treesit-node-start node))
(beg (treesit-fold--eol beg))
(end (1- (treesit-node-end node))))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-c-preproc-if (node offset)
"Define fold range for `if' preprocessor.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(let* ((named-node (treesit-node-child-by-field-name node "condition"))
(else (or (treesit-node-child-by-field-name node "alternative")
(car (treesit-fold-find-children node "#endif"))))
(beg (treesit-node-end named-node))
(end (1- (treesit-node-start else))))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-c-preproc-ifdef (node offset)
"Define fold range for `ifdef' and `ifndef' preprocessor.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((named-node (treesit-node-child-by-field-name node "name"))
(else (or (treesit-node-child-by-field-name node "alternative")
(car (treesit-fold-find-children node "#endif"))))
(beg (treesit-node-end named-node))
(end (1- (treesit-node-start else))))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-c-preproc-elif (node offset)
"Define fold range for `elif' preprocessor.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((named-node (treesit-node-child-by-field-name node "condition"))
(parent (or (treesit-fold-find-parent node "preproc_if")
(treesit-fold-find-parent node "preproc_ifdef")))
(next (or (treesit-node-child-by-field-name node "alternative")
(car (treesit-fold-find-children parent "#endif"))))
(beg (treesit-node-end named-node))
(end (1- (treesit-node-start next))))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-c-preproc-else (node offset)
"Define fold range for `else' preprocessor.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((else-str (car (split-string (treesit-node-text node) "\n")))
(parent (or (treesit-fold-find-parent node "preproc_if")
(treesit-fold-find-parent node "preproc_ifdef")))
(next (car (treesit-fold-find-children parent "#endif")))
(beg (+ (treesit-node-start node) (length else-str)))
(end (1- (treesit-node-start next))))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-clojure-function (node offset)
"Return the fold range for `list_lit' NODE in Clojure.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((name-node (nth 1 (treesit-fold-find-children node "sym_lit")))
(next-node (treesit-node-next-sibling name-node))
(beg (treesit-node-start next-node))
(end (1- (treesit-node-end node))))
(unless treesit-fold-on-next-line ; display nicely
(setq beg (treesit-fold--last-eol beg)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-cmake-body (node offset)
"Return the fold range for `body' NODE in CMake.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((beg (treesit-node-start node))
(end (treesit-node-end node)))
(when treesit-fold-on-next-line ; display nicely
(setq end (treesit-fold--last-eol end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-editorconfig-end-section (node)
"Return the section NODE's end point."
(let ((pt (treesit-node-end node))
(children (reverse (treesit-node-children node))))
(cl-some (lambda (child)
(when (equal 'pair (treesit-node-type child))
(setq pt (treesit-node-end child))))
children)
pt))
(defun treesit-fold-range-editorconfig-section (node offset)
"Return the fold range for `section' NODE in EditorConfig.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((end-bracket (car (treesit-fold-find-children node "]")))
(beg (treesit-node-end end-bracket))
(end (1- (treesit-fold-range-editorconfig-end-section node))))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-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 `treesit-fold-range-seq' for
more information."
(when-let* ((param-node (treesit-node-child node 4))
(beg (treesit-node-start param-node))
(end (1- (treesit-node-end node))))
(unless treesit-fold-on-next-line ; display nicely
(setq beg (treesit-fold--last-eol beg)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-elixir (node offset)
"Return the fold range for `function' `module' NODE in Elixir.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((end-child (treesit-fold-last-child node))
(do-child (treesit-node-child node 1))
(beg (treesit-node-start do-child))
(beg (treesit-fold--last-eol beg))
(end (treesit-node-start end-child)))
(when treesit-fold-on-next-line ; display nicely
(setq end (treesit-fold--last-eol end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-erlang-signature (node offset start)
"Return the fold range for generic signature NODE in Erlang.
For arguments NODE and OFFSET, see function `treesit-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 (treesit-fold-find-children node start)))
(beg (treesit-node-end start-node))
(end (treesit-node-end node)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-erlang-clause-body (node offset)
"Return the fold range for `clause_body' NODE in Erlang.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(treesit-fold-range-erlang-signature node offset "->"))
(defun treesit-fold-range-erlang-type-guards (node offset)
"Return the fold range for `type_guards' NODE in Erlang.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(treesit-fold-range-erlang-signature node offset "when"))
(defun treesit-fold-range-fish-function (node offset)
"Define fold range for `function' in Fish.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((func-name (treesit-node-child node 1))
(beg (treesit-node-end func-name))
(end (treesit-node-end node))
(end (- end 3)))
(when treesit-fold-on-next-line ; display nicely
(setq end (treesit-fold--last-eol end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-fish-if (node offset)
"Define fold range for `if_statement' in Fish.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((beg (treesit-node-start node))
(beg (treesit-fold--eol beg))
(end (treesit-node-end node))
(end (- end 3)))
(when treesit-fold-on-next-line ; display nicely
(setq end (treesit-fold--last-eol end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-fish-case (node offset)
"Define fold range for `case_clause' in Fish.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((beg (treesit-node-start node))
(beg (treesit-fold--eol beg))
(end (treesit-node-end node))
(end (1- end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-gleam (node offset)
"Return the fold range for `function' `type_definition' NODE in Gleam.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((open-bracket (car (treesit-fold-find-children node "{")))
(beg (treesit-node-start open-bracket))
(beg (1+ beg))
(end (treesit-node-end node))
(end (1- end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-groovy-block (node offset)
"Define fold range for `block' in Groovy.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((open-bracket (car (treesit-fold-find-children node "{")))
(beg (treesit-node-start open-bracket))
(beg (1+ beg))
(end (treesit-node-end node))
(end (1- end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-haskell-function (node offset)
"Define fold range for `function' in Haskell.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(when-let* ((beg (treesit-node-start node))
(beg (treesit-fold--eol beg))
(end-node (treesit-fold-last-child node))
(end (treesit-node-end end-node)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-html (node offset)
"Define fold range for tag in HTML.
For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for
more information."
(let* ((beg (treesit-node-end (treesit-node-child node 0)))
(end-node (treesit-fold-last-child node))
(end (treesit-node-start end-node)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-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 `treesit-fold-range-seq' for more
information."
(when-let* ((identifier (treesit-node-child node 0 t))
(params (treesit-node-child node 1 t))
(beg (treesit-node-end params))
(end (treesit-node-end node))
(end (- end 3)))
(when treesit-fold-on-next-line ; display nicely
(setq end (treesit-fold--last-eol end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-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 `treesit-fold-range-seq' for more
information."
(when-let* ((params (treesit-node-child node 0 t))
(beg (treesit-node-end params))
(end (treesit-node-end node))
(end (- end 3)))
(when treesit-fold-on-next-line ; display nicely
(setq end (treesit-fold--last-eol end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-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 `treesit-fold-range-seq' for more
information."
(when-let* ((vars (treesit-fold-find-children node "variable_declaration"))
(last-var (last vars))
(last-var (car last-var))
(beg (treesit-node-end last-var))
(end (treesit-node-end node))
(end (- end 3)))
(when treesit-fold-on-next-line ; display nicely
(setq end (treesit-fold--last-eol end)))
(treesit-fold--cons-add (cons beg end) offset)))
(defun treesit-fold-range-kotlin-when (node offset)