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

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

[nongnu] elpa/treesit-fold c0e9bb7f3e 344/417: fix: Improve Julia rules


From: ELPA Syncer
Subject: [nongnu] elpa/treesit-fold c0e9bb7f3e 344/417: fix: Improve Julia rules
Date: Mon, 1 Jul 2024 10:03:02 -0400 (EDT)

branch: elpa/treesit-fold
commit c0e9bb7f3e76dab45a890cea756fb913076564cf
Author: JenChieh <jcs090218@gmail.com>
Commit: Jen-Chieh Shen <jcs090218@gmail.com>

    fix: Improve Julia rules
---
 ts-fold-parsers.el | 23 ++++++++++++++---------
 ts-fold-summary.el |  4 ++--
 ts-fold-util.el    |  6 ++----
 ts-fold.el         | 45 +++++++++++++++++++++++++++++++++++++++------
 4 files changed, 57 insertions(+), 21 deletions(-)

diff --git a/ts-fold-parsers.el b/ts-fold-parsers.el
index 87a57459f9..3705e3db03 100644
--- a/ts-fold-parsers.el
+++ b/ts-fold-parsers.el
@@ -55,7 +55,9 @@
 (declare-function ts-fold-range-fish-case "ts-fold.el")
 (declare-function ts-fold-range-haskell-function "ts-fold.el")
 (declare-function ts-fold-range-html "ts-fold.el")
-(declare-function ts-fold-range-julia "ts-fold.el")
+(declare-function ts-fold-range-julia-function "ts-fold.el")
+(declare-function ts-fold-range-julia-if "ts-fold.el")
+(declare-function ts-fold-range-julia-let "ts-fold.el")
 (declare-function ts-fold-range-kotlin-when "ts-fold.el")
 (declare-function ts-fold-range-lisp-function "ts-fold.el")
 (declare-function ts-fold-range-llvm-label "ts-fold.el")
@@ -320,16 +322,19 @@
   "Rule set for Julia."
   '((block_comment       . (ts-fold-range-seq 1 -1))
     (for_statement       . (ts-fold-range-seq 2 -2))
-    (function_definition . ts-fold-range-julia)
-    (if_statement        . (ts-fold-range-seq 1 -2))
-    (let_statement       . (ts-fold-range-seq 2 -2))
-    (macro_definition    . ts-fold-range-julia)
-    (module_definition   . ts-fold-range-julia)
-    (quote_statement     . ts-fold-range-julia)
-    (struct_definition   . ts-fold-range-julia)
+    (function_definition . ts-fold-range-julia-function)
+    (if_statement        . ts-fold-range-julia-if)
+    (let_statement       . ts-fold-range-julia-let)
+    (macro_definition    . ts-fold-range-julia-function)
+    (module_definition   . ts-fold-range-julia-function)
+    (quote_statement     . ts-fold-range-julia-function)
+    (struct_definition   . ts-fold-range-julia-function)
     (triple_string       . (ts-fold-range-seq 2 -2))
     (try_statement       . (ts-fold-range-seq 2 -2))
-    (while_statement     . ts-fold-range-julia)))
+    (while_statement     . ts-fold-range-julia-function)
+    (comment
+     . (lambda (node offset)
+         (ts-fold-range-line-comment node offset "#")))))
 
 (defun ts-fold-parsers-kotlin ()
   "Rule set for Kotlin."
diff --git a/ts-fold-summary.el b/ts-fold-summary.el
index be9bf42443..14c4726439 100644
--- a/ts-fold-summary.el
+++ b/ts-fold-summary.el
@@ -183,8 +183,8 @@ type of content by checking the word boundary's existence."
   (ts-fold-summary--generic doc-str "-"))
 
 (defun ts-fold-summary-julia-doc (doc-str)
-  "Extract summary from DOC-STR in julia."
-  (ts-fold-summary--generic doc-str "\"\"\""))
+  "Extract summary from DOC-STR in Julia."
+  (ts-fold-summary--generic doc-str '("#" "\"\"\"")))
 
 ;;
 ;; (@* "Core" )
diff --git a/ts-fold-util.el b/ts-fold-util.el
index 0f8cd4aed8..d360369028 100644
--- a/ts-fold-util.el
+++ b/ts-fold-util.el
@@ -96,10 +96,8 @@ Like function `s-count-matches' but faster."
 (defun ts-fold--get-face (obj trim)
   "Return face name from OBJ.
 If argument TRIM is non-nil, trim the OBJ."
-  (let ((obj (if trim (string-trim obj) obj))
-        (len (length obj)))
-    (or (get-text-property 0 'face obj)
-        (get-text-property (max 0 (1- len)) 'face obj))))
+  (let ((obj (if trim (string-trim obj) obj)))
+    (get-text-property 0 'face obj)))
 
 (defun ts-fold--is-face (obj lst-face &optional trim)
   "Return non-nil if OBJ's face is define inside list LST-FACE.
diff --git a/ts-fold.el b/ts-fold.el
index 5434a16157..8beffb9b20 100644
--- a/ts-fold.el
+++ b/ts-fold.el
@@ -770,17 +770,50 @@ more information."
          (end (tsc-node-start-position end-node)))
     (ts-fold--cons-add (cons beg end) offset)))
 
-(defun ts-fold-range-julia (node offset)
+(defun ts-fold-range-julia-function (node offset)
   "Return the fold range for a NODE in Julia.
 
 It excludes the NODE's first child and the `end' keyword.  For
 argument OFFSET, see function `ts-fold-range-seq' for more
 information."
-  (let* ((identifier (tsc-get-nth-named-child node 0))
-         (end-position (byte-to-position (aref (tsc-node-range identifier) 1)))
-         (start-position (byte-to-position (aref (tsc-node-range node) 0)))
-         (fold-begin (1- (- end-position start-position))))
-    (ts-fold-range-seq node (ts-fold--cons-add (cons fold-begin -2) offset))))
+  (when-let* ((identifier (tsc-get-nth-named-child node 0))
+              (params (tsc-get-nth-named-child node 1))
+              (beg (tsc-node-end-position params))
+              (end (tsc-node-end-position node))
+              (end (- end 3)))
+    (when ts-fold-on-next-line  ; display nicely
+      (setq end (ts-fold--last-eol end)))
+    (ts-fold--cons-add (cons beg end) offset)))
+
+(defun ts-fold-range-julia-if (node offset)
+  "Define fold range for if statement in Julia.
+
+It excludes the NODE's first child and the `end' keyword.  For
+argument OFFSET, see function `ts-fold-range-seq' for more
+information."
+  (when-let* ((params (car (ts-fold-find-children node "call_expression")))
+              (beg (tsc-node-end-position params))
+              (end (tsc-node-end-position node))
+              (end (- end 3)))
+    (when ts-fold-on-next-line  ; display nicely
+      (setq end (ts-fold--last-eol end)))
+    (ts-fold--cons-add (cons beg end) offset)))
+
+(defun ts-fold-range-julia-let (node offset)
+  "Define fold range for let statement in Julia.
+
+It excludes the NODE's first child and the `end' keyword.  For
+argument OFFSET, see function `ts-fold-range-seq' for more
+information."
+  (when-let* ((vars (ts-fold-find-children node "variable_declaration"))
+              (last-var (last vars))
+              (last-var (car last-var))
+              (beg (tsc-node-end-position last-var))
+              (end (tsc-node-end-position node))
+              (end (- end 3)))
+    (when ts-fold-on-next-line  ; display nicely
+      (setq end (ts-fold--last-eol end)))
+    (ts-fold--cons-add (cons beg end) offset)))
 
 (defun ts-fold-range-kotlin-when (node offset)
   "Return the fold range for `when' NODE in Kotlin.



reply via email to

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