Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCaml parser and first functions added #21

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ These languages are in development:
* Agda
* Elm
* Emacs Lisp
* OCaml
* XML (upstream)

## ⚖️ Indicators Mode
Expand Down
8 changes: 8 additions & 0 deletions ts-fold-parsers.el
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
(declare-function ts-fold-range-c-preproc-elif "ts-fold.el")
(declare-function ts-fold-range-c-preproc-else "ts-fold.el")
(declare-function ts-fold-range-html "ts-fold.el")
(declare-function ts-fold-range-ocaml "ts-fold.el")
(declare-function ts-fold-range-python "ts-fold.el")
(declare-function ts-fold-range-ruby "ts-fold.el")
(declare-function ts-fold-range-rust-macro "ts-fold.el")
Expand Down Expand Up @@ -171,6 +172,13 @@
(ts-fold-range-line-comment node offset "#")
(ts-fold-range-c-like-comment node offset))))))

(defun ts-fold-parsers-ocaml ()
"Rule sets for OCaml."
'((comment . ts-fold-range-ocaml-comment)
(module_definition . ts-fold-range-ocaml-module-definition)
(type_definition . ts-fold-range-ocaml-type-definition)
(value_definition . ts-fold-range-ocaml-value-definition)))

(defun ts-fold-parsers-python ()
"Rule sets for Python."
'((function_definition . ts-fold-range-python)
Expand Down
70 changes: 70 additions & 0 deletions ts-fold.el
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ The alist is in form of (major-mode . (foldable-node-type)).")
(sh-mode . ,(ts-fold-parsers-bash))
(scala-mode . ,(ts-fold-parsers-scala))
(swift-mode . ,(ts-fold-parsers-swift))
(tuareg-mode . ,(ts-fold-parsers-ocaml))
(typescript-mode . ,(ts-fold-parsers-typescript)))
"An alist of (major-mode . (foldable-node-type . function)).

Expand Down Expand Up @@ -365,6 +366,10 @@ in backward direction."
(setq iter-node (ts-fold--next-prev-node iter-node next)))
last-node))

(defun ts-fold--one-liner-node (node)
"Helper function to check if NODE is on one line only."
(= (car (aref (tsc-node-range node) 2)) (car (aref (tsc-node-range node) 3))))

(defun ts-fold-range-seq (node offset)
"Return the fold range in sequence starting from NODE.

Expand Down Expand Up @@ -446,6 +451,71 @@ more information."
(end (tsc-node-start-position end-node)))
(ts-fold--cons-add (cons beg end) offset)))

;;+ OCaml

(defun ts-fold-range-ocaml-comment (node offset)
"Define fold range for `comment'.

For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(unless (ts-fold--one-liner-node node)
(when-let*
((text (tsc-node-text node))
(beg (if (string-prefix-p "(* " text)
(+ 2 (tsc-node-start-position node))
(+ 3 (tsc-node-start-position node))))
(end (- (tsc-node-end-position node) 2)))
(ts-fold--cons-add (cons beg end) offset))))

(defun ts-fold-range-ocaml-module-definition (node offset)
"Define fold range for `module_definition'.

For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(unless (ts-fold--one-liner-node node)
(when-let*
((module-binding (tsc-get-nth-named-child node 0))
(body (tsc-get-child-by-field module-binding :body))
;; body is struct ... end
(beg (+ 6 (tsc-node-start-position body)))
(end (- (tsc-node-end-position node) 3)))
(ts-fold--cons-add (cons beg end) offset))))

(defun ts-fold-range-ocaml-type-definition (node offset)
"Define fold range for `type_definition'.

For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(unless (ts-fold--one-liner-node node)
(when-let*
((type-definition (tsc-get-nth-named-child node 0))
(body (tsc-get-child-by-field type-definition :body))
(text (tsc-node-text (tsc-get-nth-child body 0)))
(beg
(if (string-equal "{" text)
(1+ (tsc-node-start-position body))
(tsc-node-end-position (tsc-get-prev-sibling body))))
(end
(if (string-equal "{" text)
(1- (tsc-node-end-position node))
(tsc-node-end-position node))))
(ts-fold--cons-add (cons beg end) offset))))

(defun ts-fold-range-ocaml-value-definition (node offset)
"Define fold range for `value_definition'.

For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
more information."
(unless (ts-fold--one-liner-node node)
(when-let*
((let-binding (tsc-get-nth-named-child node 0))
(body (tsc-get-child-by-field let-binding :body))
(beg (tsc-node-end-position (tsc-get-prev-sibling body)))
(end (tsc-node-end-position node)))
(ts-fold--cons-add (cons beg end) offset))))

;;- OCaml

(defun ts-fold-range-python (node offset)
"Define fold range for `function_definition' and `class_definition'.

Expand Down