[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/treesit-fold a7c29941c2 225/417: OCaml parser and first fu
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/treesit-fold a7c29941c2 225/417: OCaml parser and first functions added (#21) |
Date: |
Mon, 1 Jul 2024 10:02:28 -0400 (EDT) |
branch: elpa/treesit-fold
commit a7c29941c2424e6cee1ecf2e5c149de14366ab75
Author: Mattias <5543639+mattiasdrp@users.noreply.github.com>
Commit: GitHub <noreply@github.com>
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
---
README.md | 1 +
ts-fold-parsers.el | 8 +++++++
ts-fold.el | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 79 insertions(+)
diff --git a/README.md b/README.md
index 31acec2ba0..a07cdf35bd 100644
--- a/README.md
+++ b/README.md
@@ -100,6 +100,7 @@ These languages are in development:
* Agda
* Elm
* Emacs Lisp
+* OCaml
* XML (upstream)
## ⚖️ Indicators Mode
diff --git a/ts-fold-parsers.el b/ts-fold-parsers.el
index d8f837528f..4e9102b2f8 100644
--- a/ts-fold-parsers.el
+++ b/ts-fold-parsers.el
@@ -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")
@@ -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 set for Python."
'((function_definition . ts-fold-range-python)
diff --git a/ts-fold.el b/ts-fold.el
index edab17dac7..bf2af89741 100644
--- a/ts-fold.el
+++ b/ts-fold.el
@@ -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)).
@@ -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.
@@ -464,6 +469,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'.
- [nongnu] elpa/treesit-fold 44714c252a 126/417: Quote description, (continued)
- [nongnu] elpa/treesit-fold 44714c252a 126/417: Quote description, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold a1b5e97fa0 132/417: Improve doc, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 49aff53680 136/417: Add comment, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 5e956f74dc 148/417: Support python comment and document string, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 68c7954b3a 155/417: Apply more accurate c-like comment, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold b2222f0f7f 159/417: Update list, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 68d16a9bc9 166/417: Rename to ts-fold, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold d556beb204 172/417: Merge pull request #4 from rynffoll/feature/add-support-evil-toggle-fold, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 52d36972ef 218/417: chore(list): sort language alphabetically (#12), ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold ea554f10e7 220/417: Fix a minor typo in ts-fold-parsers.el (#14), ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold a7c29941c2 225/417: OCaml parser and first functions added (#21),
ELPA Syncer <=
- [nongnu] elpa/treesit-fold 1d690b8d06 267/417: feat(python): Support assignment string, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 78f0b0437f 278/417: docs(README.md): Mention Noir in TODO, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold c36f3ad85c 279/417: feat: Add Perl support (#66), ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold be2b3dd211 275/417: feat: Add Elisp support (#63), ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 323e252cf5 284/417: feat: Add Kotlin support (#70), ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 8f1f182b40 307/417: perf: Speed up count matches (#88), ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold c4aaf00303 326/417: fix: Render it's own window, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 0d2b177527 381/417: fix: Update rust line comment rule, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold cc31fb573b 399/417: docs: Update line-reminder config, ELPA Syncer, 2024/07/01
- [nongnu] elpa/treesit-fold 6bb13a51ce 401/417: docs: Changelog, ELPA Syncer, 2024/07/01