unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 58873@debbugs.gnu.org
Cc: casouri@gmail.com
Subject: bug#58873: 29.0.50; Fix indent-line-function in tree-sitter
Date: Sat, 29 Oct 2022 21:11:27 +0200	[thread overview]
Message-ID: <87eduqsaw0.fsf@thornhill.no> (raw)

[-- Attachment #1: Type: text/plain, Size: 702 bytes --]


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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Fix indent-line-function in tree-sitter --]
[-- Type: text/x-patch, Size: 2151 bytes --]

From b8af08cbe03b66437901af2164e3b31c7df46f04 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
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


             reply	other threads:[~2022-10-29 19:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-29 19:11 Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2022-10-29 20:06 ` bug#58873: 29.0.50; Fix indent-line-function in tree-sitter Yuan Fu
2022-11-12 20:33   ` Stefan Kangas
2022-11-14  9:16     ` Yuan Fu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87eduqsaw0.fsf@thornhill.no \
    --to=bug-gnu-emacs@gnu.org \
    --cc=58873@debbugs.gnu.org \
    --cc=casouri@gmail.com \
    --cc=theo@thornhill.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).