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

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

[nongnu] elpa/treesit-fold 3e1a96dad0 241/417: Remove redundant fold nod


From: ELPA Syncer
Subject: [nongnu] elpa/treesit-fold 3e1a96dad0 241/417: Remove redundant fold node alist (#36)
Date: Mon, 1 Jul 2024 10:02:30 -0400 (EDT)

branch: elpa/treesit-fold
commit 3e1a96dad0f87146befa224ba03278a1405d354b
Author: The noodles <samrjack@users.noreply.github.com>
Commit: GitHub <noreply@github.com>

    Remove redundant fold node alist (#36)
    
    When trying to add new language fold specifications to ts-fold, it was
    quite troublesome working around the coupled data structures
    `ts-fold-range-alist` and `ts-fold-foldable-node-alist`. This removes
    the `ts-fold-foldable-node-alist` variable and pulls all information
    from the `ts-fold-range-alist` var.
    
    Co-authored-by: Samuel Jackson <samueljackson@bymason.com>
---
 ts-fold-indicators.el |  4 ++--
 ts-fold-summary.el    |  2 +-
 ts-fold.el            | 66 +++++++++++++++++++++------------------------------
 3 files changed, 30 insertions(+), 42 deletions(-)

diff --git a/ts-fold-indicators.el b/ts-fold-indicators.el
index 36a72d4673..7a9185ab12 100644
--- a/ts-fold-indicators.el
+++ b/ts-fold-indicators.el
@@ -267,8 +267,8 @@ Argument FOLDED holds folding state; it's a boolean."
   (when ts-fold-indicators-mode
     (ts-fold--ensure-ts
       (when-let* ((node (tsc-root-node tree-sitter-tree))
-                  (patterns (seq-mapcat (lambda (type) `(,(list type) @name))
-                                        (alist-get major-mode 
ts-fold-foldable-node-alist)
+                  (patterns (seq-mapcat (lambda (fold-range) `((,(car 
fold-range)) @name))
+                                        (alist-get major-mode 
ts-fold-range-alist)
                                         'vector))
                   (query (ignore-errors
                            (tsc-make-query tree-sitter-language patterns)))
diff --git a/ts-fold-summary.el b/ts-fold-summary.el
index 66e49cdbff..a558ff0185 100644
--- a/ts-fold-summary.el
+++ b/ts-fold-summary.el
@@ -229,7 +229,7 @@ type of content by checking the word boundary's existence."
     (typescript-mode   . ts-fold-summary-javadoc)
     (nxml-mode         . ts-fold-summary-xml))
   "Alist mapping `major-mode' to doc parser function."
-  :type 'hook
+  :type '(alist :key-type symbol :value-type function)
   :group 'ts-fold)
 
 (provide 'ts-fold-summary)
diff --git a/ts-fold.el b/ts-fold.el
index 17cb0356cb..fc197a337c 100644
--- a/ts-fold.el
+++ b/ts-fold.el
@@ -56,11 +56,6 @@
   :group 'tree-sitter
   :prefix "ts-fold-")
 
-(defvar ts-fold-foldable-node-alist nil
-  "Collect a list of foldable node from variable `ts-fold-range-alist'.
-
-The alist is in form of (major-mode . (foldable-node-type)).")
-
 ;; TODO(everyone): This is a bit messy, but try to keep this alist
 ;; alphabetically sorted
 (defcustom ts-fold-range-alist
@@ -103,15 +98,6 @@ FOLDABLE-NODE-TYPE) and return the buffer positions of the 
beginning and end of
 the fold in a cons cell.  See `ts-fold-range-python' for an example."
   :type '(alist :key-type symbol
                 :value-type (alist :key-type symbol :value-type function))
-  :set (lambda (symbol value)
-         (set-default symbol value)
-         (setq ts-fold-foldable-node-alist
-               (let (alist)
-                 (dolist (item ts-fold-range-alist)
-                   (let ((mode (car item)) nodes)
-                     (dolist (rule (cdr item)) (push (car rule) nodes))
-                     (push (cons mode nodes) alist)))
-                 alist)))
   :group 'ts-fold)
 
 (defcustom ts-fold-mode-hook nil
@@ -186,28 +172,27 @@ the fold in a cons cell.  See `ts-fold-range-python' for 
an example."
 (defun ts-fold--foldable-node-at-pos (&optional pos)
   "Return the smallest foldable node at POS.  If POS is nil, use `point'.
 
-Raise `user-error' if no foldable node is found.
+Return nil if no valid node is found.
 
 This function is borrowed from `tree-sitter-node-at-point'."
   (let* ((pos (or pos (point)))
-         (foldable-types (alist-get major-mode ts-fold-foldable-node-alist))
+         (mode-ranges (alist-get major-mode ts-fold-range-alist))
          (root (tsc-root-node tree-sitter-tree))
-         (node (tsc-get-descendant-for-position-range root pos pos)))
-    (let ((current node) result)
-      (while current
-        (if (memq (tsc-node-type current) foldable-types)
-            (setq result current
-                  current nil)
-          (setq current (tsc-get-parent current))))
-      (or result (user-error "No foldable node found at POS")))))
+         (node (tsc-get-descendant-for-position-range root pos pos))
+         ;; Used for looping
+         (current node))
+    (while (and current (not (alist-get (tsc-node-type current) mode-ranges)))
+        (setq current (tsc-get-parent current)))
+    current))
 
 (defun ts-fold--get-fold-range (node)
-  "Return the beginning (as buffer position) of fold for NODE."
+  "Return the beginning (as buffer position) of fold for NODE.
+Return nil if there is no fold to be made."
   (when-let* ((fold-alist (alist-get major-mode ts-fold-range-alist))
-              (item (alist-get (tsc-node-type node) fold-alist)))
-    (cond ((functionp item) (funcall item node (cons 0 0)))
-          ((listp item) (funcall (nth 0 item) node (cons (nth 1 item) (nth 2 
item))))
-          (t (user-error "Current node is not found in `ts-fold-range-alist' 
in %s" major-mode)))))
+              (fold-func (alist-get (tsc-node-type node) fold-alist)))
+    (cond ((functionp fold-func) (funcall fold-func node (cons 0 0)))
+          ((listp fold-func) (funcall (nth 0 fold-func) node (cons (nth 1 
fold-func) (nth 2 fold-func))))
+          (t (user-error "Bad folding function for node")))))
 
 ;;
 ;; (@* "Overlays" )
@@ -216,7 +201,9 @@ This function is borrowed from `tree-sitter-node-at-point'."
 (defun ts-fold--create-overlay (range)
   "Create invisible overlay in RANGE."
   (when range
-    (let* ((beg (car range)) (end (cdr range)) (ov (make-overlay beg end)))
+    (let* ((beg (car range))
+           (end (cdr range))
+           (ov (make-overlay beg end)))
       (overlay-put ov 'creator 'ts-fold)
       (overlay-put ov 'invisible 'ts-fold)
       (overlay-put ov 'display (or (and ts-fold-summary-show
@@ -232,9 +219,7 @@ This function is borrowed from `tree-sitter-node-at-point'."
 (defun ts-fold-overlay-at (node)
   "Return the ts-fold overlay at NODE if NODE is foldable and folded.
 Return nil otherwise."
-  (when-let* ((foldable-types (alist-get major-mode 
ts-fold-foldable-node-alist))
-              ((memq (tsc-node-type node) foldable-types))
-              (range (ts-fold--get-fold-range node)))
+  (when-let* ((range (ts-fold--get-fold-range node)))
     (thread-last (overlays-in (car range) (cdr range))
                  (seq-filter (lambda (ov)
                                (and (eq (overlay-get ov 'invisible) 'ts-fold)
@@ -257,15 +242,18 @@ Return nil otherwise."
 (defun ts-fold-close (&optional node)
   "Fold the syntax node at `point` if it is foldable.
 
-Foldable nodes are defined in `ts-fold-foldable-node-alist' for the
-current `major-mode'.  If no foldable NODE is found in point, do nothing."
+Foldable nodes are defined in `ts-fold-range-alist' for the
+current `major-mode'.
+
+If no NODE is found in point, do nothing."
   (interactive)
   (ts-fold--ensure-ts
-    (let ((node (or node (ts-fold--foldable-node-at-pos))))
+    (when-let* ((node (or node (ts-fold--foldable-node-at-pos))))
       ;; make sure I do not create multiple overlays for the same fold
       (when-let* ((ov (ts-fold-overlay-at node)))
         (delete-overlay ov))
-      (ts-fold--create-overlay (ts-fold--get-fold-range node)))))
+      (when-let* ((range (ts-fold--get-fold-range node)))
+        (ts-fold--create-overlay range)))))
 
 ;;;###autoload
 (defun ts-fold-open ()
@@ -295,8 +283,8 @@ If the current node is not folded or not foldable, do 
nothing."
   (interactive)
   (ts-fold--ensure-ts
     (let* ((node (tsc-root-node tree-sitter-tree))
-           (patterns (seq-mapcat (lambda (type) `(,(list type) @name))
-                                 (alist-get major-mode 
ts-fold-foldable-node-alist)
+           (patterns (seq-mapcat (lambda (fold-range) `((,(car fold-range)) 
@name))
+                                 (alist-get major-mode ts-fold-range-alist)
                                  'vector))
            (query (tsc-make-query tree-sitter-language patterns))
            (nodes-to-fold (tsc-query-captures query node #'ignore)))



reply via email to

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