emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/treesit-fold 9d18dee909 354/417: Added a matlab parser for


From: ELPA Syncer
Subject: [nongnu] elpa/treesit-fold 9d18dee909 354/417: Added a matlab parser for folding.
Date: Mon, 1 Jul 2024 10:03:04 -0400 (EDT)

branch: elpa/treesit-fold
commit 9d18dee90973a5405e6a5045ea1ee14e9fd872fe
Author: Nidish Narayanaa Balaji <nidbid@gmail.com>
Commit: Jen-Chieh Shen <jcs090218@gmail.com>

    Added a matlab parser for folding.
---
 ts-fold-parsers.el | 13 +++++++++++++
 ts-fold-summary.el |  1 +
 ts-fold.el         | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+)

diff --git a/ts-fold-parsers.el b/ts-fold-parsers.el
index f2d673aace..2519cf92fe 100644
--- a/ts-fold-parsers.el
+++ b/ts-fold-parsers.el
@@ -501,6 +501,19 @@
      . (lambda (node offset)
          (ts-fold-range-line-comment node offset "#")))))
 
+(defun ts-fold-parsers-matlab ()
+  "Rule set for MATLAB."
+  '((expression_list     . ts-fold-range-seq)
+    (function_definition . ts-fold-range-matlab-function)
+    (class_definition   . ts-fold-range-matlab-function)
+    (if_statement       . ts-fold-range-matlab-statements)
+    (for_statement      . ts-fold-range-matlab-statements)
+    (while_statement    . ts-fold-range-matlab-statements)
+    (switch_statement   . ts-fold-range-matlab-statements)
+    (try_statement      . ts-fold-range-matlab-statements)
+    (comment            . ts-fold-range-matlab-blocks)
+    ))
+
 (defun ts-fold-parsers-qss ()
   "Rule set for QSS."
   (append (ts-fold-parsers-css)))
diff --git a/ts-fold-summary.el b/ts-fold-summary.el
index 636deedbc7..1e9e9e2f75 100644
--- a/ts-fold-summary.el
+++ b/ts-fold-summary.el
@@ -283,6 +283,7 @@ type of content by checking the word boundary's existence."
     (php-mode               . ts-fold-summary-javadoc)
     (pascal-mode            . ts-fold-summary-pascal-doc)
     (python-mode            . ts-fold-summary-python-doc)
+    (matlab-mode            . ts-fold-summary-matlab-doc)
     (qss-mode               . ts-fold-summary-css)
     (rjsx-mode              . ts-fold-summary-javadoc)
     (rst-mode               . ts-fold-summary-rst-doc)
diff --git a/ts-fold.el b/ts-fold.el
index 1210659697..77595a841e 100644
--- a/ts-fold.el
+++ b/ts-fold.el
@@ -110,6 +110,7 @@
     (makefile-makepp-mode   . ,(ts-fold-parsers-make))
     (makefile-bsdmake-mode  . ,(ts-fold-parsers-make))
     (makefile-imake-mode    . ,(ts-fold-parsers-make))
+    (matlab-mode            . ,(ts-fold-parsers-matlab))
     (markdown-mode          . ,(ts-fold-parsers-markdown))
     (matlab-mode            . ,(ts-fold-parsers-matlab))
     (mermaid-mode           . ,(ts-fold-parsers-mermaid))
@@ -1154,6 +1155,55 @@ more information."
               (end (tsc-node-end-position node)))
     (ts-fold--cons-add (cons (+ beg 3) (- end 3)) offset)))
 
+(defun ts-fold-range-matlab-function (node offset)
+  "Define fold range for MATLAB function definitions.
+
+For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
+more information."
+  (when-let* ((named-node (or (tsc-get-child-by-field node :superclass)
+                              (tsc-get-child-by-field node :properties)
+                             (tsc-get-child-by-field node :methods)            
             
+                              (tsc-get-child-by-field node :function_arguments)
+                              (tsc-get-child-by-field node :function_output)
+                             (tsc-get-child-by-field node :name)))
+             (beg (tsc-node-end-position (tsc-get-next-sibling named-node)))
+              (end (tsc-node-end-position node)))
+    (ts-fold--cons-add (cons beg end) offset)))
+
+(defun ts-fold-range-matlab-statements (node offset)
+  "Define fold range for MATLAB statements.
+
+For arguments NODE and OFFSET, see function `ts-fold-range-line-comment' for
+more information."
+  (when-let* ((named-node (car (ts-fold-find-children node "\n")))
+             (beg (tsc-node-start-position named-node))
+             (ins (append
+                   (ts-fold-find-children node "catch_clause")
+                   (ts-fold-find-children node "case_clause")
+                   (ts-fold-find-children node "otherwise_clause")
+                   (ts-fold-find-children node "elseif_clause")
+                   (ts-fold-find-children node "else_clause")
+                   (ts-fold-find-children node "end")))  ;; can include parts 
maybe
+              (end (tsc-node-start-position (car (ts-fold-find-children node 
"end")))))
+    (ts-fold--cons-add (cons beg end) offset)))
+
+(defun ts-fold-range-matlab-blocks (node offset)
+  "Define fold range for MATLAB blocks.
+
+Each block is delimited by a line starting with '%%'.
+For arguments NODE and OFFSET, see function `ts-fold-range-line-comment' for
+more information."
+  (when (string-prefix-p "%%" (ts-node-text node))
+    (let* ((beg (ts-node-end-position node))
+          (end (or (save-excursion
+                     (progn (goto-char beg)
+                            (when (re-search-forward "^\s*\^L%%" nil t)
+                              (forward-line -1) (end-of-line)
+                              (point))))
+                   (ts-node-end-position (ts-get-parent node)))))
+      (message (format "%d %d" beg end))
+      (ts-fold--cons-add (cons beg end) offset))))
+
 (defun ts-fold-range-rst-body (node offset)
   "Define fold range for `body' in reStructuredText.
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]