Skip to content

Commit a7c2994

Browse files
authored
OCaml parser and first functions added (#21)
This update allows to fold comments, value, type and module definitions if they're not on one line only I'm not sure about the one line rule but it seems strange to fold a value that fits on one line
1 parent 0d884a4 commit a7c2994

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ These languages are in development:
100100
* Agda
101101
* Elm
102102
* Emacs Lisp
103+
* OCaml
103104
* XML (upstream)
104105

105106
## ⚖️ Indicators Mode

ts-fold-parsers.el

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
(declare-function ts-fold-range-c-preproc-elif "ts-fold.el")
4646
(declare-function ts-fold-range-c-preproc-else "ts-fold.el")
4747
(declare-function ts-fold-range-html "ts-fold.el")
48+
(declare-function ts-fold-range-ocaml "ts-fold.el")
4849
(declare-function ts-fold-range-python "ts-fold.el")
4950
(declare-function ts-fold-range-ruby "ts-fold.el")
5051
(declare-function ts-fold-range-rust-macro "ts-fold.el")
@@ -171,6 +172,13 @@
171172
(ts-fold-range-line-comment node offset "#")
172173
(ts-fold-range-c-like-comment node offset))))))
173174

175+
(defun ts-fold-parsers-ocaml ()
176+
"Rule sets for OCaml."
177+
'((comment . ts-fold-range-ocaml-comment)
178+
(module_definition . ts-fold-range-ocaml-module-definition)
179+
(type_definition . ts-fold-range-ocaml-type-definition)
180+
(value_definition . ts-fold-range-ocaml-value-definition)))
181+
174182
(defun ts-fold-parsers-python ()
175183
"Rule set for Python."
176184
'((function_definition . ts-fold-range-python)

ts-fold.el

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)