emacs-diffs
[Top][All Lists]
Advanced

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

master 1d425125694 02/11: Refactor treesit--indent-1


From: Yuan Fu
Subject: master 1d425125694 02/11: Refactor treesit--indent-1
Date: Sun, 1 Dec 2024 21:10:22 -0500 (EST)

branch: master
commit 1d425125694b61e2b07a7b429a6475dfe59afe37
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    Refactor treesit--indent-1
    
    * lisp/treesit.el (treesit--indent-largest-node-at): New
    function.
    (treesit--indent-1): Extract code out into the new function.
---
 lisp/treesit.el | 57 ++++++++++++++++++++++++++++++---------------------------
 1 file changed, 30 insertions(+), 27 deletions(-)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 6f5d065cc24..5d57b9e8e3e 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1943,41 +1943,44 @@ PARENT is its parent; ANCHOR is a point (not a node), 
and OFFSET
 is a number.  Emacs finds the column of ANCHOR and adds OFFSET to
 it as the final indentation of the current line.")
 
-(defun treesit--indent-1 ()
-  "Indent the current line.
-Return (ANCHOR . OFFSET).  This function is used by
-`treesit-indent' and `treesit-indent-region'."
-  ;; Basically holds the common part between the two indent function.
-  (let* ((bol (save-excursion
-                (forward-line 0)
-                (skip-chars-forward " \t")
-                (point)))
-         (local-parsers (treesit-local-parsers-at bol nil t))
+(defun treesit--indent-largest-node-at (pos)
+  "Get largest node that still starts at POS."
+  (let* ((local-parsers (treesit-local-parsers-at pos nil t))
          (smallest-node
           (cond ((car local-parsers)
                  (let ((local-parser (caar local-parsers))
                        (host-parser (cdar local-parsers)))
                    (if (eq (treesit-node-start
                             (treesit-parser-root-node local-parser))
-                           bol)
-                       (treesit-node-at bol host-parser)
-                     (treesit-node-at bol local-parser))))
+                           pos)
+                       (treesit-node-at pos host-parser)
+                     (treesit-node-at pos local-parser))))
                 ((null (treesit-parser-list)) nil)
                 ((eq 1 (length (treesit-parser-list nil nil t)))
-                 (treesit-node-at bol))
-                ((treesit-language-at bol)
-                 (treesit-node-at bol (treesit-language-at bol)))
-                (t (treesit-node-at bol))))
+                 (treesit-node-at pos))
+                ((treesit-language-at pos)
+                 (treesit-node-at pos (treesit-language-at pos)))
+                (t (treesit-node-at pos))))
          (root (treesit-parser-root-node
-                (treesit-node-parser smallest-node)))
-         (node (treesit-parent-while
-                smallest-node
-                (lambda (node)
-                  (and (eq bol (treesit-node-start node))
-                       (not (treesit-node-eq node root)))))))
-    (let*
-        ((parser (if smallest-node
-                     (treesit-node-parser smallest-node)
+                (treesit-node-parser smallest-node))))
+    (treesit-parent-while
+     smallest-node
+     (lambda (node)
+       (and (eq pos (treesit-node-start node))
+            (not (treesit-node-eq node root)))))))
+
+(defun treesit--indent-1 ()
+  "Indent the current line.
+Return (ANCHOR . OFFSET).  This function is used by
+`treesit-indent' and `treesit-indent-region'."
+  ;; Basically holds the common part between the two indent function.
+  (let* ((bol (save-excursion
+                (forward-line 0)
+                (skip-chars-forward " \t")
+                (point)))
+         (node (treesit--indent-largest-node-at bol))
+         (parser (if node
+                     (treesit-node-parser node)
                    nil))
          ;; NODE would be nil if BOL is on a whitespace.  In that case
          ;; we set PARENT to the "node at point", which would
@@ -1985,7 +1988,7 @@ Return (ANCHOR . OFFSET).  This function is used by
          (parent (cond ((and node parser)
                         (treesit-node-parent node))
                        (t (treesit-node-on bol bol)))))
-      (funcall treesit-indent-function node parent bol))))
+    (funcall treesit-indent-function node parent bol)))
 
 (defun treesit-indent ()
   "Indent according to the result of `treesit-indent-function'."



reply via email to

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