-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
origami-parsers.el
1162 lines (1040 loc) · 48.9 KB
/
origami-parsers.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
;;; origami-parsers.el --- Collection of parsers -*- lexical-binding: t -*-
;; The MIT License (MIT)
;; Copyright (c) 2014 Greg Sexton
;; Copyright (c) 2019-2023 Jen-Chieh Shen
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
;;; Commentary:
;;
;; Collection of parsers.
;;
;;; Code:
(require 'cl-lib)
(require 'rx)
(require 'seq)
(require 'subr-x)
(require 'dash)
(require 's)
(require 'origami-util)
;;
;; (@* "Exterals" )
;;
(defvar origami-parser-summary-alist)
(declare-function origami-fold-root-node "origami.el")
(declare-function origami-fold-children "origami.el")
(declare-function origami-fold-children-set "origami.el")
(declare-function origami-fold-shallow-merge "origami.el")
;;
;; (@* "Utility" )
;;
(defun origami-get-positions (_content regex &optional predicate fnc-pos)
"Return a list of positions where REGEX matche in CONTENT.
A position is a cons cell of the character and the numerical position
in the CONTENT.
Optional argument PREDICATE is for filtering.
Optional argument FNC-POS, is function that returns the mark position
from the matching string. If omitted, the mark will be the first
non-whitespace character of the match."
(save-excursion
(goto-char (point-min))
(let (acc open)
(while (re-search-forward regex nil t)
(let ((match (match-string 0)))
(when (or (null predicate) (funcall predicate (cons match (point))))
;; NOTE: Variable `open' is only accurate when `regex' is exactly
;; the same. This is only used for symmetric token.
(setq open (not open))
(push (cons match
(or (ignore-errors (funcall fnc-pos match open))
(- (point) (length (string-trim-left match)))))
acc))))
(reverse acc))))
(defun origami-indent-parser (create)
"Not documented, CREATE."
(cl-labels
((lines (string) (origami-get-positions
string ".*?\r?\n" nil
(lambda (match &rest _) (- (point) (length match)))))
(annotate-levels (lines)
(-map (lambda (line)
;; TODO: support tabs
(let ((indent (length (car (s-match "^ *" (car line)))))
(beg (cdr line))
(end (+ (cdr line) (length (car line)) -1)))
(if (s-blank? (s-trim (car line)))
'newline ; sentinel representing line break
(vector indent beg end (- end beg)))))
lines))
(indent (line) (if (eq line 'newline) -1 (aref line 0)))
(beg (line) (aref line 1))
(end (line) (aref line 2))
(offset (line) (aref line 3))
(collapse-same-level (lines)
(->>
(cdr lines)
(-reduce-from (lambda (acc line)
(cond ((and (eq line 'newline) (eq (car acc) 'newline)) acc)
((= (indent line) (indent (car acc)))
(cons (vector (indent (car acc))
(beg (car acc))
(end line)
(offset (car acc)))
(cdr acc)))
(t (cons line acc))))
(list (car lines)))
(remove 'newline)
reverse))
(create-tree (levels)
(if (null levels)
levels
(let ((curr-indent (indent (car levels))))
(->> levels
(-partition-by (lambda (l) (= (indent l) curr-indent)))
(-partition-all 2)
(-mapcat (lambda (x)
;takes care of multiple identical levels, introduced when there are newlines
(-concat
(-map 'list (butlast (car x)))
(list (cons (-last-item (car x)) (create-tree (cadr x)))))))))))
(build-nodes (tree)
(if (null tree) (cons 0 nil)
;; complexity here is due to having to find the end of the children so that the
;; parent encompasses them
(-reduce-r-from
(lambda (nodes acc)
(cl-destructuring-bind (children-end . children) (build-nodes (cdr nodes))
(let ((this-end (max children-end (end (car nodes)))))
(cons (max this-end (car acc))
(cons (funcall create
(beg (car nodes))
this-end
(offset (car nodes))
children)
(cdr acc))))))
'(0 . nil)
tree))))
(lambda (content)
(-> content
lines
annotate-levels
collapse-same-level
create-tree
build-nodes
cdr))))
(defun origami-build-pair-tree (create open close else positions &optional fnc-offset)
"Build the node tree from POSITIONS.
Argument CREATE is the parser function to create the nodes.
Arguments OPEN, CLOSE and ELSE are regular expressions used to determine the
type of a position. An OPEN match will start a new pending node, a CLOSE match
will finish a started pending node, an ELSE match works like CLOSE + OPEN - it
will finish a started pending node, then immediately start a new one.
Argument POSITIONS is a list of cons cell form by (match . point).
Optional argument FNC-OFFSET is a function that return's the position of the
node offset. This point is the actual position that folds (hide) the code
in region; the beginning position will be the header that could triggers the
fold/unfold action."
(cl-labels
((build (positions)
;; recursive function to build nodes from positions, returns cons cell with
;; (remaining-positions . created-nodes)
(let (;; nil indicates the current tree level was finished by
;; a closing match, and we need to ascend back to upper
;; level from recursion
(tree-level-unfinished t)
acc ; accumulates created nodes
beg-pos ; beginning pos of started, but unfinished (not created) node
beg-match ; beginning match of started, but unfinished node
cur-pos ; pos of current position
cur-match)
(while (and tree-level-unfinished positions)
(setq cur-match (caar positions)
cur-pos (cdar positions))
(origami-log "\f")
(origami-log "current match: %s" cur-match)
(origami-log "beg (pos, cur-match): %s, %s" beg-pos beg-match)
(cond
;;; --- ELSE --------------------------------------------
((and (stringp else) (string-match-p else cur-match))
(origami-log ">> else: " cur-match)
;; Stay on same level - finish beg node & start new one. As we are starting a
;; new node at cur-pos, make the beg node end at one char before that.
(push (funcall create beg-pos (1- cur-pos)
(or (origami-util-function-offset fnc-offset beg-pos beg-match)
(length beg-match))
nil)
acc)
;; begin a new pair
(setq beg-pos cur-pos
beg-match cur-match
positions (cdr positions)))
;;; --- OPEN --------------------------------------------
((string-match-p open cur-match)
(origami-log ">> open: " cur-match)
(if beg-pos ; go down a level
(progn
(origami-log "dig in...")
(let* ((res (build positions)) ; recurse
(new-pos (car res))
(children (cdr res))
;; auto-close unclosed folds at point-max
(close-pos (or (cdar new-pos) (point-max)))
(node (funcall create beg-pos close-pos
(or (origami-util-function-offset fnc-offset beg-pos beg-match)
(length beg-match))
children)))
;; close with children
(when node (push node acc))
(setq beg-pos nil
beg-match nil
positions (cdr new-pos))))
;; begin a new pair
(setq beg-pos cur-pos
beg-match cur-match
positions (cdr positions))))
;;; --- CLOSE --------------------------------------------
((string-match-p close cur-match)
(origami-log ">> close: " cur-match)
(if beg-pos ; close with no children
(progn
(let ((node (funcall create beg-pos cur-pos
(or (origami-util-function-offset fnc-offset beg-pos beg-match)
(length beg-match))
nil)))
(when node (push node acc)))
(setq beg-pos nil
beg-match nil
positions (cdr positions)))
;; stop loop to reascend to upper tree level
(setq tree-level-unfinished nil)))))
(origami-log "dig out...")
;; Pass remaining positions and accumulated nodes up the recursion stack
(cons positions (reverse acc)))))
(cdr (build positions))))
(defun origami--force-pair-positions (positions)
"Force POSITIONS pair into a length of even number."
(let ((last-pos-symbol "") result)
(-filter (lambda (position)
(setq result (not (string= last-pos-symbol (car position)))
last-pos-symbol (car position))
result)
positions)))
(defvar origami--strict-pair nil
"Strictly check the pair tree must have length of even number.
This flag is use to debug function `origami-build-pair-tree-2'.")
(defun origami-build-pair-tree-2 (create positions &optional fnc-offset)
"Build pair list tree.
POSITIONS is from by a list of cons cell form by (syntax . point).
Notice POSITIONS' length must be an even number due to the list must be
pair up. For instance,
<0> (syntax . point),
<1> (syntax . point),
<2> (syntax . point),
<3> (syntax . point), ...
Item N and the next item (N + 1) should be a pair; hence, N should always
be even number (if count starting from 0 and not 1).
Optional argument FNC-OFFSET is a function that return's the position of
the offset."
(let ((index 0) (len (length positions))
beg end offset pos-beg pos-end match
ov ovs)
(when (origami-util-is-odd len)
(if origami--strict-pair
(error "Pair tree 2 should not have length of odd number: %s" len)
(setq positions (origami--force-pair-positions positions))))
(while (< index len)
(setq pos-beg (nth index positions)
pos-end (nth (1+ index) positions)
beg (cdr pos-beg) end (cdr pos-end)
offset (length (string-trim (car pos-beg)))
match (car pos-beg)
ov (ignore-errors (funcall create beg end
(or (origami-util-function-offset fnc-offset beg match)
offset)
nil)))
(when ov (push ov ovs))
(cl-incf index 2))
(reverse ovs)))
(defun origami-build-pair-tree-single (create syntax &optional predicate fnc-pos fnc-offset)
"Build pair tree for single line SYNTAX.
This is use for syntax continuous appears repeatedly on each line.
For instance,
1 | # This is comment line.
2 | #
3 | # This is also a comment line.
4 |
5 | # Another comment line.
In the above case, L1 - L3 will be mark; but L5 will be ignored. This
function can be use for any kind of syntax like `//`, `;`, `#`."
(lambda (content)
(let* ((positions (origami-get-positions content syntax predicate fnc-pos))
valid-positions)
(let ((index 0) (len (length positions))
last-position position
line pos last-line start-p
on-next-line-p)
(while (< index len)
(setq position (nth index positions)
pos (cdr position)
line (line-number-at-pos pos t)
on-next-line-p (when last-line (= 1 (- line last-line))))
(if (= index 0)
(setq last-position position
last-line line)
(if start-p
;; Collect ending point; then wrap up.
(unless on-next-line-p
(setcdr last-position (origami-util-pos-line-end (cdr last-position)))
(push last-position valid-positions)
(setq start-p nil))
;; Collect starting point.
(when on-next-line-p
(push last-position valid-positions)
(setq start-p t)))
(setq last-position position
last-line line))
(cl-incf index))
;; NOTE: Add the last from list `positions' in order to pair up!
(when start-p
(setcdr last-position (origami-util-pos-line-end (cdr last-position)))
(push last-position valid-positions)))
(setq valid-positions (reverse valid-positions))
(origami-build-pair-tree-2 create valid-positions fnc-offset))))
;;
;; (@* "Token Identification" )
;;
(defvar origami-doc-faces
'(font-lock-doc-face
font-lock-comment-face
font-lock-comment-delimiter-face
tree-sitter-hl-face:comment
tree-sitter-hl-face:doc
hl-todo)
"List of face that apply for document string.")
(defun origami-doc-faces-p (obj &optional trim)
"Return non-nil if face at OBJ is within `origami-doc-faces' list.
Optional argument TRIM, see function `origami-util-get-face'."
(origami-util-is-face obj origami-doc-faces trim))
(defun origami-filter-doc-face (position)
"Predicate we use to filter out for non-comment.
Argument POSITION can either be cons (match . position); or a integer value."
(if (consp position)
(or (origami-doc-faces-p (car position) t)
(origami-util-comment-block-p (cdr position)))
(not (origami-filter-code-face position))))
(defun origami-filter-code-face (position)
"Predicate we use to filter out non-code.
Argument POSITION can either be cons (match . position); or a integer value."
(when (consp position) (setq position (cdr position)))
(not (origami-util-comment-or-string-p position)))
(defun origami-search-forward (sym predicate &optional start bound)
"Find SYM and return it's position.
Argument PREDICATE is use to test for valid condition. Optional argument
START is the starting point to scan the symbol. Optional argument BOUND
is the ending point to stop the scanning processs."
(unless bound (setq bound (line-end-position)))
(save-excursion
(when start (goto-char start))
(let ((pt (point)))
(while (and (re-search-forward sym bound t)
(ignore-errors (funcall predicate (point))))
(setq pt (point)))
pt)))
;;
;; (@* "Parsers" )
;;
(defun origami-parsers--prefix (create prefix)
"Internal single line syntax parser with PREFIX."
(origami-build-pair-tree-single
create prefix 'origami-filter-doc-face
(lambda (&rest _) (line-beginning-position))
(lambda (&rest _) (origami-search-forward prefix 'origami-filter-doc-face))))
(defun origami-parser-triple-slash (create)
"Parser for single line syntax triple slash."
(origami-parsers--prefix create "^[ \t\r\n]*///"))
(defun origami-parser-double-slash (create)
"Parser for single line syntax double slash."
(origami-parsers--prefix create "^[ \t]*//"))
(defun origami-parser-single-sharp (create)
"Parser for single line syntax single sharp."
(origami-parsers--prefix create "^[ \t]*#"))
(defun origami-parser-double-semi-colon (create)
"Parser for single line syntax double semi-colon."
(origami-parsers--prefix create "^[ \t]*;;"))
(defun origami-parser-double-dash (create)
"Parser for single line syntax double dash."
(origami-parsers--prefix create "^[ \t]*--"))
(defun origami-parser-double-colon (create)
"Parser for single line syntax double colon."
(origami-parsers--prefix create "^[ \t]*::"))
(defun origami-parser-rem (create)
"Parser for single line syntax REM."
(origami-parsers--prefix create "^[ \t]*[Rr][Ee][Mm]"))
;; TODO: tag these nodes? have ability to manipulate nodes that are tagged?
;; in a scoped fashion?
(defun origami-javadoc-parser (create)
"Parser for Javadoc."
(lambda (content)
(let* ((beg "/[*]")
(positions
(origami-get-positions
content "/\\*\\|\\*/"
(lambda (pos &rest _) (origami-filter-doc-face pos))
(lambda (match &rest _)
(when (string-match-p beg match) (line-beginning-position))))))
(origami-build-pair-tree-2
create positions
(lambda (&rest _) (origami-search-forward beg #'origami-filter-doc-face))))))
(defun origami-python-doc-parser (create)
"Parser for Python document string."
(lambda (content)
(let* ((sec "\"\"\"")
(positions
(origami-get-positions
content sec
(lambda (pos &rest _) (origami-filter-doc-face pos))
(lambda (_match open &rest _) (when open (line-beginning-position))))))
(origami-build-pair-tree-2
create positions
(lambda (&rest _)
(origami-search-forward sec #'origami-filter-doc-face))))))
(defun origami-lua-doc-parser (create)
"Parser for Lua document string."
(lambda (content)
(let* ((beg '("--[[][[]")) (end '("]]"))
(_beg-regex (origami-util-comment-regex beg))
(_end-regex (origami-util-comment-regex end))
(all-regex (origami-util-comment-regex (append beg end)))
(positions
(origami-get-positions
content all-regex
(lambda (pos &rest _) (origami-filter-doc-face pos))
(lambda (match &rest _)
(when (string-match-p beg match) (line-beginning-position))))))
(origami-build-pair-tree-2
create positions
(lambda (&rest _)
(origami-search-forward beg #'origami-filter-doc-face))))))
(defun origami-batch-parser (create)
"Parser for Batch."
(let ((p-rem (origami-parser-rem create))
(p-dc (origami-parser-double-colon create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall p-rem content))
(origami-fold-root-node (funcall p-dc content)))))))
(defun origami-c-style-parser (create)
"Parser for C style programming language."
(lambda (content)
(let ((positions
(origami-get-positions
content "[{}]"
(lambda (pos &rest _) (origami-filter-code-face pos))
(lambda (match &rest _)
(when (string= match "{")
(origami-search-forward "}" #'origami-filter-code-face
(line-beginning-position) (point)))))))
(origami-build-pair-tree
create "{" "}" nil positions
(lambda (&rest _)
(origami-search-forward "{" #'origami-filter-code-face))))))
(defun origami--macro-regex (direc)
"Return a regular expression to search for DIREC."
(format "[ \t]*#[ \t]*%s" direc))
(defun origami-c-macro-parser (create)
"Parser for C style macro."
(lambda (content)
(let* ((beg `(,(origami--macro-regex "if[n]*[d]*[e]*[f]*")))
(end `(,(origami--macro-regex "endif")))
(else `(,(origami--macro-regex "else") ,(origami--macro-regex "elif")))
(beg-regex (origami-util-keywords-regex beg))
(end-regex (origami-util-keywords-regex end))
(else-regex (origami-util-keywords-regex else))
(all-regex (origami-util-keywords-regex (append beg end else)))
(positions
(origami-get-positions
content all-regex
(lambda (pos &rest _) (origami-filter-code-face pos))
(lambda (match &rest _)
(if (origami-util-contain-list-type-str end match 'regex)
;; keep end pos on separate line on folding
(1- (line-beginning-position))
(line-beginning-position))))))
(origami-build-pair-tree create beg-regex end-regex else-regex
positions
(lambda (&rest _) (line-end-position))))))
(defun origami-csharp-macro-parser (create)
"Parser for C# style macro."
(lambda (content)
(let* ((beg `(,(origami--macro-regex "if[n]*[d]*[e]*[f]*")
,(origami--macro-regex "region")))
(end `(,(origami--macro-regex "endif") ,(origami--macro-regex "endregion")))
(else `(,(origami--macro-regex "else") ,(origami--macro-regex "elif")))
(beg-regex (origami-util-keywords-regex beg))
(end-regex (origami-util-keywords-regex end))
(else-regex (origami-util-keywords-regex else))
(all-regex (origami-util-keywords-regex (append beg end else)))
(positions
(origami-get-positions
content all-regex
(lambda (pos &rest _) (origami-filter-code-face pos))
(lambda (match &rest _)
(if (origami-util-contain-list-type-str end match 'regex)
;; keep end pos on separate line on folding
(1- (line-beginning-position))
(line-beginning-position))))))
(origami-build-pair-tree create beg-regex end-regex else-regex
positions
(lambda (&rest _) (line-end-position))))))
(defun origami-c-parser (create)
"Parser for C."
(let ((c-style (origami-c-style-parser create))
(macros (origami-c-macro-parser create))
(javadoc (origami-javadoc-parser create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall c-style content))
(origami-fold-root-node (funcall macros content))
(origami-fold-root-node (funcall javadoc content)))))))
(defun origami-c++-parser (create)
"Parser for C++."
(origami-c-parser create))
(defun origami-objc-parser (create)
"Parser for Objective-C."
(origami-c-parser create))
(defun origami-java-parser (create)
"Parser for Java."
(let ((c-style (origami-c-style-parser create))
(javadoc (origami-javadoc-parser create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall c-style content))
(origami-fold-root-node (funcall javadoc content)))))))
(defun origami-js-parser (create)
"Parser for JavaScript."
(origami-java-parser create))
(defun origami-csharp-parser (create)
"Parser for C#."
(let ((c-style (origami-c-style-parser create))
(macros (origami-csharp-macro-parser create))
(javadoc (origami-javadoc-parser create))
(p-ts (origami-parser-triple-slash create))
(p-ds (origami-parser-double-slash create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall c-style content))
(origami-fold-root-node (funcall macros content))
(origami-fold-root-node (funcall javadoc content))
(origami-fold-root-node (funcall p-ts content))
(origami-fold-root-node (funcall p-ds content)))))))
(defun origami-python-subparser (create beg end)
"Find all fold block between BEG and END.
See function `origami-python-parser' description for argument CREATE."
(goto-char beg)
(let (acc)
;; iterate all same level children.
(while (and (beginning-of-defun -1) (<= (point) end)) ; have children between beg and end?
(let* ((new-beg (point))
(new-offset (progn (search-forward-regexp ":" nil t) (- (point) new-beg)))
(new-end (progn (end-of-defun) (point))))
(setq acc (cons (funcall create new-beg new-end new-offset
(origami-python-subparser create new-beg new-end))
acc))
(goto-char new-end)))
acc))
(defun origam-python-parser-indent (create)
"Indent Python core parser."
(lambda (content)
(with-temp-buffer
(insert content)
;; Prevent infinite recursive
(let (prog-mode-hook after-change-major-mode-hook) (python-mode))
(origami-python-subparser create (point-min) (point-max)))))
(defun origami-python-parser (create)
"Parser for Python."
(let ((py-indent (origam-python-parser-indent create))
(python-doc (origami-python-doc-parser create))
(p-ss (origami-parser-single-sharp create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall py-indent content))
(origami-fold-root-node (funcall python-doc content))
(origami-fold-root-node (funcall p-ss content)))))))
(defun origami-lisp-parser (create regex)
"Parser for Lisp."
(lambda (content)
(with-temp-buffer
(insert content)
(goto-char (point-min))
(beginning-of-defun -1)
(let (beg end offset acc)
(while (< (point) (point-max))
(setq beg (point))
(search-forward-regexp regex nil t)
(setq offset (- (point) beg))
(end-of-defun)
(backward-char) ; move point to one after the last paren
(setq end (1- (point))) ; don't include the last paren in the fold
(when (> offset 0)
(push (funcall create beg end offset nil) acc))
(beginning-of-defun -1))
(reverse acc)))))
(defun origami-elisp-parser (create)
"Parser for Emacs Lisp."
(origami-lisp-parser create "(def\\w*\\s-*\\(\\s_\\|\\w\\|[:?!]\\)*\\([ \\t]*(.*?)\\)?"))
(defun origami-go-parser (create)
"Parser for Go."
(let ((c-style (origami-c-style-parser create))
(p-ds (origami-parser-double-slash create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall c-style content))
(origami-fold-root-node (funcall p-ds content)))))))
(defun origami-lua-core-parser (create)
"Core parser for Lua."
(lambda (content)
(let* ((beg '("function" "then" "do"))
(end '("end" "elseif"))
(else '("else"))
(beg-regex (origami-util-keywords-regex beg))
(end-regex (origami-util-keywords-regex end))
(else-regex (origami-util-keywords-regex else))
(all-regex (origami-util-keywords-regex (append beg end else)))
(last-beg-pt -1) (current-beg-pt -1)
overlaps-pts
(positions
(origami-get-positions
content all-regex
(lambda (pos &rest _) (origami-filter-code-face pos))
(lambda (match &rest _)
(setq current-beg-pt (line-beginning-position))
(if (origami-util-contain-list-type-str end match 'strict)
(if (= last-beg-pt current-beg-pt)
(progn
(push current-beg-pt overlaps-pts)
(- (point) (length match)))
;; keep end pos on separate line on folding
(1- current-beg-pt))
(setq last-beg-pt current-beg-pt)
current-beg-pt)))))
(origami-build-pair-tree create beg-regex end-regex else-regex
positions
(lambda (match &rest _)
(if (memq (point) overlaps-pts)
(progn
;; default to after point match
(search-forward match nil t)
(point))
(if (string= "function" match)
(origami-search-forward ")" #'origami-filter-code-face)
(line-end-position))))))))
(defun origami-lua-parser (create)
"Parser for Lua."
(let ((p-lua (origami-lua-core-parser create))
(p-lua-doc (origami-lua-doc-parser create))
(p-dd (origami-parser-double-dash create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall p-lua content))
(origami-fold-root-node (funcall p-lua-doc content))
(origami-fold-root-node (funcall p-dd content)))))))
(defun origami-ruby-core-parser (create)
"Core parser for Ruby."
(lambda (content)
(let* ((beg '("def" "class" "module" "if" "unless" "while" "until" "case" "for" "begin"))
(end '("end"))
(else '("else" "when" "elsif"))
(beg-regex (origami-util-keywords-regex beg))
(end-regex (origami-util-keywords-regex end))
(else-regex (origami-util-keywords-regex else))
(all-regex (origami-util-keywords-regex (append beg end else)))
(last-beg-pt -1) (current-beg-pt -1)
overlaps-pts
(positions
(origami-get-positions
content all-regex
(lambda (pos &rest _) (origami-filter-code-face pos))
(lambda (match &rest _)
(setq current-beg-pt (line-beginning-position))
(if (origami-util-contain-list-type-str end match 'strict)
(if (= last-beg-pt current-beg-pt)
(progn
(push current-beg-pt overlaps-pts)
(- (point) (length match)))
;; keep end pos on separate line on folding
(1- current-beg-pt))
(setq last-beg-pt current-beg-pt)
current-beg-pt)))))
(origami-build-pair-tree create beg-regex end-regex else-regex
positions
(lambda (match &rest _)
(if (memq (point) overlaps-pts)
(progn
;; default to after point match
(search-forward match nil t)
(point))
(line-end-position)))))))
(defun origami-ruby-parser (create)
"Parser for Ruby."
(let ((p-ruby (origami-ruby-core-parser create))
(p-ss (origami-parser-single-sharp create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall p-ruby content))
(origami-fold-root-node (funcall p-ss content)))))))
(defun origami-rust-parser (create)
"Parser for Rust."
(origami-csharp-parser create))
(defun origami-clj-parser (create)
"Parser for Clojure."
(origami-lisp-parser create "(def\\(\\w\\|-\\)*\\s-*\\(\\s_\\|\\w\\|[?!]\\)*\\([ \\t]*\\[.*?\\]\\)?"))
(defun origami-scala-parser (create)
"Parser for Scala."
(let ((c-style (origami-c-style-parser create))
(javadoc (origami-javadoc-parser create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall c-style content))
(origami-fold-root-node (funcall javadoc content)))))))
(defun origami-sh-parser (create)
"Parser for Shell script."
(origami-parser-single-sharp create))
(defun origami-swift-parser (create)
"Parser for Swift."
(origami-c-parser create))
(defun origami-markers-parser (start-marker end-marker &optional is-regex)
"Create a parser for simple start and end markers.
If IS-REGEX is t, the markers will be interpreted as regular
expressions."
(let ((rx-beg (if is-regex start-marker
(regexp-quote start-marker)))
(rx-end (if is-regex end-marker
(regexp-quote end-marker)))
(rx-all (rx-to-string
(if is-regex
`(or (regexp ,start-marker) (regexp ,end-marker))
`(or ,start-marker ,end-marker)))))
(lambda (create)
(lambda (content)
(let ((positions (origami-get-positions content rx-all)))
(origami-build-pair-tree create rx-beg rx-end nil positions))))))
(defun origami-markdown-parser (create)
"Parser for Markdown."
(lambda (content)
(let* ((sec "```")
(positions (origami-get-positions
content sec nil
(lambda (&rest _)
(1- (line-beginning-position))))))
(origami-build-pair-tree-2
create positions
(lambda (match &rest _) (+ (point) (length match) 1))))))
(defun origami-org-parser (create)
"Parser for Org."
(lambda (content)
(let* ((beg '("#[+]BEGIN_SRC" "#[+]BEGIN_EXAMPLE"))
(end '("#[+]END_SRC" "#[+]END_EXAMPLE"))
(beg-regex (origami-util-keywords-regex beg))
(end-regex (origami-util-keywords-regex end))
(all-regex (origami-util-keywords-regex (append beg end)))
(positions (origami-get-positions
content all-regex nil
(lambda (&rest _) (1- (line-beginning-position))))))
(origami-build-pair-tree create beg-regex end-regex nil
positions
(lambda (match &rest _)
(+ (point) (length match) 1))))))
(defun origami-xml-base-parser (create &optional remove-leaves ignored-tags-regex)
"Base parser for xml style markup."
(cl-labels
((valid-pos-p (pos)
(and (origami-filter-code-face pos)
(or (null ignored-tags-regex)
(not (string-match-p ignored-tags-regex (car pos))))))
(build-nodes (content)
(rx-let
((beg-tag
(seq "<"
;; elements start with letter or _, don't match preamble
(any word "_")
(zero-or-more
(or
;; anything but closing tag or attribute
(not (any ">"
;; ignore self-closing tags
"/"
;; attribute values require their own matching
"\""))
;; attribute value
(seq "\""
(zero-or-more (not "\""))
"\"")))
">"))
(end-tag
(seq "</" (any word "_") (zero-or-more (not ">")) ">")))
;; no need to care for comments/CDATA, these pos are filtered by face
;; in valid-pos-p
(let* ((rx-beg-tag (rx beg-tag))
(rx-end-tag (rx end-tag))
(rx-all (rx (or beg-tag end-tag)))
(positions (origami-get-positions content rx-all #'valid-pos-p)))
(origami-build-pair-tree create rx-beg-tag rx-end-tag nil positions))))
(trim (nodes)
;; trims leaves, if required
;; no artificial root node yet, so base level is a list of nodes
(if (not remove-leaves)
nodes
(let ((trimmed-nodes
;; remove base level leaves
(seq-remove (lambda (node) (null (origami-fold-children node)))
nodes)))
(if (seq-empty-p trimmed-nodes)
trimmed-nodes
;; recurse to remove deeper level leaves
(-map #'trim-recurse trimmed-nodes)))))
(trim-recurse (tree)
(if (origami-fold-children tree)
;; has children - recurse for children, and don't remove this node
(let ((trimmed-childs
(-map #'trim-recurse (origami-fold-children tree))))
;; return copy of node with filtered childs
(origami-fold-children-set tree
(seq-remove #'null trimmed-childs)))
;; no childrens, remove this node
nil)))
(lambda (content)
(-some-> content
build-nodes
trim))))
(defcustom origami-xml-skip-leaf-nodes t
"In xml files, only elements with child elements will be
foldable, not if they contain text only."
:type 'boolean
:group 'origami)
(defun origami-xml-parser (create)
"Parser for xml."
(origami-xml-base-parser create origami-xml-skip-leaf-nodes))
(defun origami-html-parser (create)
"Parser for html."
(rx-let ((ignore-tags (&rest tags) (seq "<" (or tags) word-end)))
;; Self-closing tags (void elements) without closing slash would throw off parser, ignore
(let ((ignore-tags-rx
(rx (ignore-tags "area" "base" "br" "col" "command" "embed" "hr" "img" "input"
"keygen" "link" "menuitem" "meta" "param" "source" "track" "wbr"))))
(origami-xml-base-parser create nil ignore-tags-rx))))
(defun origami-json-parser (create)
"Parser for JSON."
(lambda (content)
(let* ((rx-beg "[[{]")
(rx-end "[]}]")
(rx-all "[][{}]")
(positions
(origami-get-positions content rx-all
(lambda (pos &rest _) (origami-filter-code-face pos)))))
(origami-build-pair-tree
create rx-beg rx-end nil positions
(lambda (&rest _)
(origami-search-forward rx-beg #'origami-filter-code-face))))))
(defcustom origami-parser-alist
`((actionscript-mode . origami-java-parser)
(bat-mode . origami-batch-parser)
(c-mode . origami-c-parser)
(c++-mode . origami-c++-parser)
(clojure-mode . origami-clj-parser)
(cperl-mode . origami-c-style-parser)
(csharp-mode . origami-csharp-parser)
(dart-mode . origami-c-style-parser)
(emacs-lisp-mode . origami-elisp-parser)
(go-mode . origami-go-parser)
(html-mode . origami-html-parser)
(java-mode . origami-java-parser)
(javascript-mode . origami-js-parser)
(js-mode . origami-js-parser)
(js2-mode . origami-js-parser)
(js3-mode . origami-js-parser)
(json-mode . origami-json-parser)
(kotlin-mode . origami-java-parser)
(lisp-mode . origami-elisp-parser)
(lisp-interaction-mode . origami-elisp-parser)
(lua-mode . origami-lua-parser)
(markdown-mode . origami-markdown-parser)
(nxml-mode . origami-xml-parser)
(objc-mode . origami-objc-parser)
(org-mode . origami-org-parser)
(perl-mode . origami-c-style-parser)
(php-mode . origami-java-parser)
(python-mode . origami-python-parser)
(rjsx-mode . origami-js-parser)
(ruby-mode . origami-ruby-parser)
(rust-mode . origami-rust-parser)
(scala-mode . origami-scala-parser)
(sh-mode . origami-sh-parser)
(swift-mode . origami-swift-parser)
(triple-braces . ,(origami-markers-parser "{{{" "}}}"))
(typescript-mode . origami-js-parser)
(web-mode . origami-html-parser))
"alist mapping major-mode to parser function."
:type 'hook
:group 'origami)
;;
;; (@* "Summary" )
;;
(defcustom origami-show-summary t
"Flag to show summary if available."
:type 'boolean
:group 'origami)
(defcustom origami-max-summary-length 60
"Maximum length for summary to display."
:type '(choice (const :tag "nil" nil)
(integer :tag "positive integer number"))
:group 'origami)
(defcustom origami-summary-exceeded-string "..."
"String that added after display summary.