unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: Dmitry Gutov <dmitry@gutov.dev>
Cc: 74386@debbugs.gnu.org, Eli Zaretskii <eliz@gnu.org>,
	Theodor Thornhill <theo@thornhill.no>,
	marius.kjeldahl@gmail.com
Subject: bug#74386: Tree-sitter javascript indentation
Date: Sun, 1 Dec 2024 18:31:02 -0800	[thread overview]
Message-ID: <8A7428E6-50EE-4783-82FF-3A62C4756C56@gmail.com> (raw)
In-Reply-To: <a82074c0-f8e7-4bb2-b2a6-20716b586a8f@gutov.dev>

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



> On Dec 1, 2024, at 2:33 PM, Dmitry Gutov <dmitry@gutov.dev> wrote:
> 
> On 01/12/2024 21:10, Yuan Fu wrote:
>> Ah, I see. That’s a good point, and I definitely prefer the indentation result of parent-bol here. The one produced by standalone-parent is just wrong. What we can do is make standalone-parent ignore “.” when checking for “standaloneness”. And perhaps make it configurable so it’s enabled only for modes that this waiver makes sense (C-like languages excluding C and C++).
> 
> Maybe not by hardcoding this in inside the 'standalone-parent' matcher, but writing this in the indentation rules? Different languages might have differing ASTs for such construct.
> 
> Or if you meant to do a text search, a period might start a method call, but it could also continue a "range" literal in some other language, or some struct initializer (I think?) in C/C++. Also, some languages allow (and style guides suggest) to have the previous at the end of the line, then followed by newline and then the method name.

Not hard-coded, but customizable, like this:

Yuan


[-- Attachment #2: standalone-predicate-poc.patch --]
[-- Type: application/octet-stream, Size: 2553 bytes --]

From 301e8a55a14f61258c505a48a973379ac3156079 Mon Sep 17 00:00:00 2001
From: Yuan Fu <casouri@gmail.com>
Date: Sun, 1 Dec 2024 18:26:40 -0800
Subject: [PATCH] Standalone predicate POC

---
 lisp/treesit.el | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 2acb46ab105..513f58eeffd 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1597,6 +1597,37 @@ treesit--indent-prev-line-node
       (back-to-indentation)
       (treesit--indent-largest-node-at (point)))))
 
+(defvar treesit-simple-indent-standalone-predicate nil
+  "Function used to determine if a node is \"standalone\".
+
+\"Standalone\" means the node starts on a new line.  For example, if we
+look at the opening bracket, then it's standalone in this case:
+
+    {            <-- Standalone.
+      return 1;
+    }
+
+but not in this case:
+
+    if (true) {  <-- Not standalone.
+      return 1;
+    }
+
+The value of this variable affects the `standalone-parent' indent preset
+for treesit-simple-indent.  If the value is nil, the standlone condition
+is as described.  Some major mode might want to relax the condition a
+little bit, so that it ignores some punctuation like \".\".  For
+example, a Javascript mode might want to consider the method call below
+to be standalone too:
+
+    obj
+    .method(() => {   <-- Consider \".method\" to be standalone,
+      return 1;       <-- so this line anchors on \".method\".
+    });
+
+The value should be a function that takes a node, and return t if it's
+standalone.")
+
 (defvar treesit-simple-indent-presets
   (list (cons 'match
               (lambda
@@ -1736,8 +1767,10 @@ treesit-simple-indent-presets
                   (catch 'term
                     (while parent
                       (goto-char (treesit-node-start parent))
-                      (when (looking-back (rx bol (* whitespace))
-                                          (line-beginning-position))
+                      (when (if (null treesit-simple-indent-standalone-predicate)
+                                (looking-back (rx bol (* whitespace))
+                                              (line-beginning-position))
+                              (funcall parent))
                         (throw 'term (point)))
                       (setq parent (treesit-node-parent parent)))))))
         (cons 'prev-sibling (lambda (node parent bol &rest _)
-- 
2.39.5 (Apple Git-151)


      reply	other threads:[~2024-12-02  2:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-16 23:26 bug#74386: Tree-sitter javascript indentation Marius Kjeldahl
2024-11-17 19:18 ` Dmitry Gutov
2024-11-17 19:21   ` Marius Kjeldahl
2024-11-17 22:12     ` Dmitry Gutov
2024-11-17 22:21       ` Marius Kjeldahl
2024-11-17 22:41         ` Dmitry Gutov
2024-11-18  8:35       ` Marius Kjeldahl
2024-11-18 15:29         ` Dmitry Gutov
2024-11-30 10:01           ` Eli Zaretskii
2024-12-01  5:23             ` Yuan Fu
2024-12-01 13:11               ` Dmitry Gutov
2024-12-01 19:10                 ` Yuan Fu
2024-12-01 22:33                   ` Dmitry Gutov
2024-12-02  2:31                     ` Yuan Fu [this message]

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=8A7428E6-50EE-4783-82FF-3A62C4756C56@gmail.com \
    --to=casouri@gmail.com \
    --cc=74386@debbugs.gnu.org \
    --cc=dmitry@gutov.dev \
    --cc=eliz@gnu.org \
    --cc=marius.kjeldahl@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).