]> git.eshelyaron.com Git - emacs.git/commitdiff
Allow treesit-simple-indent-standalone-predicate to return anchor
authorYuan Fu <casouri@gmail.com>
Fri, 14 Feb 2025 04:47:23 +0000 (20:47 -0800)
committerEshel Yaron <me@eshelyaron.com>
Fri, 14 Feb 2025 11:44:26 +0000 (12:44 +0100)
* lisp/treesit.el:
(treesit-simple-indent-standalone-predicate): Allow it to return
an anchor instead of t.
(treesit-simple-indent-presets): Supports number.
* lisp/progmodes/c-ts-common.el:
(c-ts-common--standalone-parent):
(c-ts-common--prev-standalone-sibling): Supports number.

(cherry picked from commit 34f90bf2cab7c1711136dc055b665e46f714c3e1)

lisp/progmodes/c-ts-common.el
lisp/treesit.el

index 90eb7aa64c83c47afe434a450acc0635b90e646d..e23ae786ab57039ff08cc7045fa946e047cf06b8 100644 (file)
@@ -569,20 +569,22 @@ chaining like
 
 But ff `treesit-simple-indent-standalone-predicate' is non-nil, use that
 for determining standlone line."
-  (save-excursion
-    (catch 'term
-      (while parent
-        (goto-char (treesit-node-start parent))
-        (when (if treesit-simple-indent-standalone-predicate
-                  (funcall treesit-simple-indent-standalone-predicate
-                           parent)
-                (looking-back (rx bol (* whitespace) (? "."))
-                              (line-beginning-position)))
-          (throw 'term (point)))
-        (setq parent (treesit-node-parent parent))))))
+  (let (anchor)
+    (save-excursion
+      (catch 'term
+        (while parent
+          (goto-char (treesit-node-start parent))
+          (when (if treesit-simple-indent-standalone-predicate
+                    (setq anchor
+                          (funcall treesit-simple-indent-standalone-predicate
+                                   parent))
+                  (looking-back (rx bol (* whitespace) (? "."))
+                                (line-beginning-position)))
+            (throw 'term (if (numberp anchor) anchor (point))))
+          (setq parent (treesit-node-parent parent)))))))
 
 (defun c-ts-common--prev-standalone-sibling (node)
-  "Return the previous sibling of NODE that starts on a new line.
+  "Return the start of the previous sibling of NODE that starts on a new line.
 Return nil if no sibling satisfies the condition.
 
 Unlike simple-indent's standalone preset, this function handles method
@@ -597,15 +599,18 @@ for determining standlone line."
   (save-excursion
     (setq node (treesit-node-prev-sibling node 'named))
     (goto-char (treesit-node-start node))
-    (while (and node
-                (goto-char (treesit-node-start node))
-                (not (if treesit-simple-indent-standalone-predicate
-                         (funcall treesit-simple-indent-standalone-predicate
-                                  node)
-                       (looking-back (rx bol (* whitespace) (? "."))
-                                     (pos-bol)))))
-      (setq node (treesit-node-prev-sibling node 'named)))
-    node))
+    (let (anchor)
+      (while (and node
+                  (goto-char (treesit-node-start node))
+                  (not (if treesit-simple-indent-standalone-predicate
+                           (setq anchor
+                                 (funcall
+                                  treesit-simple-indent-standalone-predicate
+                                  node))
+                         (looking-back (rx bol (* whitespace) (? "."))
+                                       (pos-bol)))))
+        (setq node (treesit-node-prev-sibling node 'named))))
+    (if (numberp anchor) anchor (treesit-node-start node))))
 
 (defun c-ts-common-parent-ignore-preproc (node)
   "Return the parent of NODE, skipping preproc nodes."
@@ -696,9 +701,8 @@ The rule also handles method chaining like
             (cons (c-ts-common--standalone-parent parent)
                   offset)))
          ;; Not first sibling
-         (t (cons (treesit-node-start
-                   (or (c-ts-common--prev-standalone-sibling node)
-                       first-sibling))
+         (t (cons (or (c-ts-common--prev-standalone-sibling node)
+                      (treesit-node-start first-sibling))
                   0)))))
      ;; Condition 2 for initializer list, only apply to
      ;; second line. Eg,
index dc9248acb26e74883a62d225d4b049b434fa2d78..5fabdc323142815595b1c67a0f643e3974deb04f 100644 (file)
@@ -1817,7 +1817,8 @@ to be standalone too:
     });
 
 The value should be a function that takes a node, and return t if it's
-standalone.")
+standalone.  If the function returns a position, that position is used
+as the anchor.")
 
 (defvar treesit-simple-indent-presets
   (list (cons 'match
@@ -1952,20 +1953,23 @@ standalone.")
                               (goto-char (treesit-node-start parent))
                               (back-to-indentation)
                               (point))))
-        (cons 'standalone-parent
-              (lambda (_n parent &rest _)
-                (save-excursion
-                  (catch 'term
-                    (while parent
-                      (goto-char (treesit-node-start parent))
-                      (when
-                          (if (null treesit-simple-indent-standalone-predicate)
-                              (looking-back (rx bol (* whitespace))
-                                            (line-beginning-position))
-                            (funcall treesit-simple-indent-standalone-predicate
-                                     parent))
-                        (throw 'term (point)))
-                      (setq parent (treesit-node-parent parent)))))))
+        (cons
+         'standalone-parent
+         (lambda (_n parent &rest _)
+           (save-excursion
+             (let (anchor)
+               (catch 'term
+                 (while parent
+                   (goto-char (treesit-node-start parent))
+                   (when (if (null treesit-simple-indent-standalone-predicate)
+                             (looking-back (rx bol (* whitespace))
+                                           (line-beginning-position))
+                           (setq anchor
+                                 (funcall
+                                  treesit-simple-indent-standalone-predicate
+                                  parent)))
+                     (throw 'term (if (numberp anchor) anchor (point))))
+                   (setq parent (treesit-node-parent parent))))))))
         (cons 'prev-sibling (lambda (node parent bol &rest _)
                               (treesit-node-start
                                (or (treesit-node-prev-sibling node t)