@@ -90,6 +90,7 @@ The alist is in form of (major-mode . (foldable-node-type)).")
9090 (sh-mode . ,(ts-fold-parsers-bash))
9191 (scala-mode . ,(ts-fold-parsers-scala))
9292 (swift-mode . ,(ts-fold-parsers-swift))
93+ (tuareg-mode . ,(ts-fold-parsers-ocaml))
9394 (typescript-mode . ,(ts-fold-parsers-typescript)))
9495 " An alist of (major-mode . (foldable-node-type . function)).
9596
@@ -365,6 +366,10 @@ in backward direction."
365366 (setq iter-node (ts-fold--next-prev-node iter-node next)))
366367 last-node))
367368
369+ (defun ts-fold--one-liner-node (node )
370+ " Helper function to check if NODE is on one line only."
371+ (= (car (aref (tsc-node-range node) 2 )) (car (aref (tsc-node-range node) 3 ))))
372+
368373(defun ts-fold-range-seq (node offset )
369374 " Return the fold range in sequence starting from NODE.
370375
@@ -464,6 +469,71 @@ more information."
464469 (end (tsc-node-start-position end-node)))
465470 (ts-fold--cons-add (cons beg end) offset)))
466471
472+ ; ;+ OCaml
473+
474+ (defun ts-fold-range-ocaml-comment (node offset )
475+ " Define fold range for `comment' .
476+
477+ For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
478+ more information."
479+ (unless (ts-fold--one-liner-node node)
480+ (when-let*
481+ ((text (tsc-node-text node))
482+ (beg (if (string-prefix-p " (* " text)
483+ (+ 2 (tsc-node-start-position node))
484+ (+ 3 (tsc-node-start-position node))))
485+ (end (- (tsc-node-end-position node) 2 )))
486+ (ts-fold--cons-add (cons beg end) offset))))
487+
488+ (defun ts-fold-range-ocaml-module-definition (node offset )
489+ " Define fold range for `module_definition' .
490+
491+ For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
492+ more information."
493+ (unless (ts-fold--one-liner-node node)
494+ (when-let*
495+ ((module-binding (tsc-get-nth-named-child node 0 ))
496+ (body (tsc-get-child-by-field module-binding :body ))
497+ ; ; body is struct ... end
498+ (beg (+ 6 (tsc-node-start-position body)))
499+ (end (- (tsc-node-end-position node) 3 )))
500+ (ts-fold--cons-add (cons beg end) offset))))
501+
502+ (defun ts-fold-range-ocaml-type-definition (node offset )
503+ " Define fold range for `type_definition' .
504+
505+ For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
506+ more information."
507+ (unless (ts-fold--one-liner-node node)
508+ (when-let*
509+ ((type-definition (tsc-get-nth-named-child node 0 ))
510+ (body (tsc-get-child-by-field type-definition :body ))
511+ (text (tsc-node-text (tsc-get-nth-child body 0 )))
512+ (beg
513+ (if (string-equal " {" text)
514+ (1+ (tsc-node-start-position body))
515+ (tsc-node-end-position (tsc-get-prev-sibling body))))
516+ (end
517+ (if (string-equal " {" text)
518+ (1- (tsc-node-end-position node))
519+ (tsc-node-end-position node))))
520+ (ts-fold--cons-add (cons beg end) offset))))
521+
522+ (defun ts-fold-range-ocaml-value-definition (node offset )
523+ " Define fold range for `value_definition' .
524+
525+ For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
526+ more information."
527+ (unless (ts-fold--one-liner-node node)
528+ (when-let*
529+ ((let-binding (tsc-get-nth-named-child node 0 ))
530+ (body (tsc-get-child-by-field let-binding :body ))
531+ (beg (tsc-node-end-position (tsc-get-prev-sibling body)))
532+ (end (tsc-node-end-position node)))
533+ (ts-fold--cons-add (cons beg end) offset))))
534+
535+ ; ;- OCaml
536+
467537(defun ts-fold-range-python (node offset )
468538 " Define fold range for `function_definition' and `class_definition' .
469539
0 commit comments