Subject: 29.0.50; Fix indent-line-function in tree-sitter
From: Theodor Thornhill <[email-masked]>
To: [email-masked]
Cc: [email-masked]
Date: Sat, 29 Oct 2022 21:11:27 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi Yuan!

There were some regressions with the new indent-line-function - see
attached patch.  We need the point, not the indentation, and the
save-excursion is actually needed.

consider:

```
foo({
  thing: 1,
                tho|ng: 2, // <--- point is |
})
```

if you indent now you want to end up like this:

```
foo({
  thing: 1,
  tho|ng: 2, // <--- point is |
})
```

or if 

```
foo({
  thing: 1,
|thong: 2, // <--- point is |
})
```
You want to end up like this:

```
foo({
  thing: 1,
  |thong: 2, // <--- point is |
})
```

This patch addresses this :-)

In addition there was a bug where the parent-bol didn't indent
correctly, this should also be fixed now.

Thanks again, Yuan!

Theo

From b8af08cbe03b66437901af2164e3b31c7df46f04 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <[email-masked]>
Date: Sat, 29 Oct 2022 21:03:23 +0200
Subject: [PATCH] Fix indent-line-function

* lisp/treesit.el (treesit-simple-indent-presets): We need the actual
position, not the indentation offset from column 0.  Revert earlier change.

(treesit-indent): Revert earlier change to preserve behavior with
similar functions for other modes.  Specifically.  We want to preserve
position in text if we are "in front of" the indentation goal.
Otherwise just jump to the correct indentation.
---
 lisp/treesit.el | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index dd0aca5049..4032af5f14 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -783,7 +783,8 @@ treesit-simple-indent-presets
                     (lambda (_n parent &rest _)
                       (save-excursion
                         (goto-char (treesit-node-start parent))
-                        (current-indentation)))))
+                        (back-to-indentation)
+                        (point)))))
     (prev-sibling . ,(byte-compile
                       (lambda (node &rest _)
                         (treesit-node-start
@@ -973,13 +974,19 @@ treesit--indent-1
 (defun treesit-indent ()
   "Indent according to the result of `treesit-indent-function'."
   (treesit-update-ranges)
-  (pcase-let* ((`(,anchor . ,offset) (treesit--indent-1)))
+  (pcase-let* ((orig-pos (point))
+               (bol (save-excursion
+                      (back-to-indentation)
+                      (point)))
+               (`(,anchor . ,offset) (treesit--indent-1)))
     (when (and anchor offset)
       (let ((col (+ (save-excursion
                       (goto-char anchor)
                       (current-column))
                     offset)))
-        (indent-line-to col)))))
+        (if (< bol orig-pos)
+            (save-excursion (indent-line-to col))
+          (indent-line-to col))))))
 
 (defvar treesit--indent-region-batch-size 400
   "How many lines of indent value do we precompute.
-- 
2.34.1