unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dmitry@gutov.dev>
To: Marius Kjeldahl <marius.kjeldahl@gmail.com>,
	Theodor Thornhill <theo@thornhill.no>
Cc: 74386@debbugs.gnu.org
Subject: bug#74386: Tree-sitter javascript indentation
Date: Mon, 18 Nov 2024 00:12:54 +0200	[thread overview]
Message-ID: <2ce8c98e-c399-46f1-a930-04f27a3d56dd@gutov.dev> (raw)
In-Reply-To: <CAHdMyCLdyVqiqDbnd=b51FHp8T3nDc++4QVq4ALbQCWUQuMHbQ@mail.gmail.com>

On 17/11/2024 21:21, Marius Kjeldahl wrote:
> Great, thanks.
> 
> Here are two examples (fingers crossed):
> 
> function a(b,
>    c) {
> ....d();
> 
> and
> 
> if (clause1
>    && clause2) {
> ....callSomeFunc();
> 
> Notice in both cases the four dots "...." representing spaces used for
> indentation. I would like only two dots (two spaces) for those
> specific examples.

Thank you, these two examples should be handled by the patch below.

It's a change which could affect a fair amount of other examples too - 
for example

Foobar
   .find()
   .catch((err) => {
     return 2;
   })
   .then((num) => {
     console.log(num);
   });

turns into

Foobar
   .find()
   .catch((err) => {
   return 2;
})
   .then((num) => {
   console.log(num);
});


I'm not 100% sure if that's good or bad (maybe worse compatibility with 
js-mode, that we could say), so it would be useful to go through a bunch 
of other examples and decide whether what we had is a bug, or these 
should be supported as different styles, or etc.

Cc'ing the original author of js-ts-mode, Theodor, for consultation.

BTW, to try out such a patch you need to apply it, evaluate the defvar 
form (C-M-x, for example), and then re-enable the major mode in a given 
buffer (M-x js-ts-mode).

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index f74b8ab1c46..17a9509dd45 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3453,7 +3453,7 @@ js--treesit-indent-rules
    (let ((switch-case (rx "switch_" (or "case" "default"))))
      `((javascript
         ((parent-is "program") parent-bol 0)
-       ((node-is "}") parent-bol 0)
+       ((node-is "}") standalone-parent 0)
         ((node-is ")") parent-bol 0)
         ((node-is "]") parent-bol 0)
         ((node-is ">") parent-bol 0)
@@ -3466,7 +3466,7 @@ js--treesit-indent-rules
         ;; "{" on the newline.
         ((node-is "statement_block") parent-bol js-indent-level)
         ((parent-is "named_imports") parent-bol js-indent-level)
-       ((parent-is "statement_block") parent-bol js-indent-level)
+       ((parent-is "statement_block") standalone-parent js-indent-level)
         ((parent-is "variable_declarator") parent-bol js-indent-level)
         ((parent-is "arguments") parent-bol js-indent-level)
         ((parent-is "array") parent-bol js-indent-level)
@@ -3481,7 +3481,6 @@ js--treesit-indent-rules
         ((parent-is "binary_expression") parent-bol js-indent-level)
         ((parent-is "class_body") parent-bol js-indent-level)
         ((parent-is ,switch-case) parent-bol js-indent-level)
-       ((parent-is "statement_block") parent-bol js-indent-level)
         ((match "while" "do_statement") parent-bol 0)
         ((match "else" "if_statement") parent-bol 0)
         ((parent-is ,(rx (or (seq (or "if" "for" "for_in" "while" "do") 
"_statement")






  reply	other threads:[~2024-11-17 22:12 UTC|newest]

Thread overview: 8+ 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 [this message]
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

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=2ce8c98e-c399-46f1-a930-04f27a3d56dd@gutov.dev \
    --to=dmitry@gutov.dev \
    --cc=74386@debbugs.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).