unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Noah Peart <noah.v.peart@gmail.com>
To: Yuan Fu <casouri@gmail.com>
Cc: Dmitry Gutov <dmitry@gutov.dev>, 68054@debbugs.gnu.org
Subject: bug#68054: [PATCH] Add tree-sitter indent rule for lexical decls in js/typescript
Date: Wed, 17 Apr 2024 13:21:10 -0700	[thread overview]
Message-ID: <CAPVBTSeLOYuEH6je5AJcXU0cxj5Qjx-FNZdo9Zt5UAngaWbtFQ@mail.gmail.com> (raw)
In-Reply-To: <D3C4EDA7-93A3-4698-8A1E-737BD98879AE@gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 1917 bytes --]

Sorry, I forgot about this. I've just added a rule to align the
variable_declarators
in let, var, and const declarations, but I need some feedback about the
indentation
for values in the variable_declarators following dangling '='.

For example, which of the following would be preferable?

1) indent the dangling values with respect to start of the declaration

    const a =
        (x: string): string => {
            return x + x;
        },
          bbb =
        {
            "x": 0
        },
          cccc =
        1,
          ddddd = 0;

2) indent them with respect to the start of the variable_declarator

    const a =
              (x: string): string => {
                  return x + x;
              },
          bbb =
              {
                  "x": 0
              },
          cccc =
              1,
          ddddd = 0;

3) align with the variable declarators (this is the same as js-mode)

    const a =
          (x: string): string => {
              return x + x;
          },
          bbb =
          {
              "x": 0
          },
          cccc =
          1,
          ddddd = 0;

I've attached a patch with with the rules for the 3 options here.

On Sun, Dec 31, 2023 at 8:56 PM Yuan Fu <casouri@gmail.com> wrote:

>
>
> > On Dec 31, 2023, at 5:41 AM, Dmitry Gutov <dmitry@gutov.dev> wrote:
> >
> > On 31/12/2023 07:35, Noah Peart wrote:
> >> Yea, I agree that would be better - would you align on start the
> variable names, or '=' like
> >> `c-lineup-assignments`?
> >
> > Like js-mode would be good.
> >
> > I'm not familiar with c-lineup-assignments, but we could add different
> variations later.
>
> Also, if you are feeling adventurous, I noticed that the second variable
> in a lexical_declaration isn’t fontified in variable-name-face. It’d be
> nice to fix that as well.
>
> Yuan

[-- Attachment #1.2: Type: text/html, Size: 2805 bytes --]

[-- Attachment #2: typescript-ts-decl-indent.patch --]
[-- Type: text/x-patch, Size: 2492 bytes --]

diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index ab1d76ab20e..9dbe5df8368 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -91,6 +91,16 @@ tsx-ts-mode--indent-compatibility-b893426
      `(((match "<" "jsx_text") parent 0)
        ((parent-is "jsx_text") parent typescript-ts-mode-indent-offset)))))
 
+(defun typescript-ts-mode--anchor-decl (_n parent &rest _)
+  "Return the position after the declaration keyword before PARENT.
+
+This anchor allows aligning variable_declarators in variable and lexical
+declarations, accounting for the length of keyword (var, let, or const)."
+  (let ((decl (treesit-parent-until
+               parent (rx (or "variable" "lexical") "_declaration") t)))
+    (+ (treesit-node-start decl)
+       (length (treesit-node-text (treesit-node-child decl 0))))))
+
 (defun typescript-ts-mode--indent-rules (language)
   "Rules used for indentation.
 Argument LANGUAGE is either `typescript' or `tsx'."
@@ -113,7 +123,24 @@ typescript-ts-mode--indent-rules
      ((parent-is "switch_case") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "switch_default") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "type_arguments") parent-bol typescript-ts-mode-indent-offset)
-     ((parent-is "variable_declarator") parent-bol typescript-ts-mode-indent-offset)
+
+     ;; 1. indent with respect to declaration start:
+     ((parent-is "variable_declarator") grand-parent typescript-ts-mode-indent-offset)
+     ((parent-is ,(rx (or "variable" "lexical") "_declaration"))
+      typescript-ts-mode--anchor-decl 1)
+
+     ;; 2. indent with respect to variable_declarator start:
+     ((match nil "variable_declarator" nil 0 1)
+      parent-bol typescript-ts-mode-indent-offset)
+     ((n-p-gp nil "variable_declarator" ,(rx (or "variable" "lexical") "_declaration"))
+      parent typescript-ts-mode-indent-offset)
+     ((parent-is ,(rx (or "variable" "lexical") "_declaration"))
+      typescript-ts-mode--anchor-decl 1)
+
+     ;; 3. align with variable declarator (like js-mode)
+     ((parent-is ,(rx (or "variable" "lexical") "_" (or "declaration" "declarator")))
+      typescript-ts-mode--anchor-decl 1)
+
      ((parent-is "arguments") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "array") parent-bol typescript-ts-mode-indent-offset)
      ((parent-is "formal_parameters") parent-bol typescript-ts-mode-indent-offset)

  reply	other threads:[~2024-04-17 20:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-27  6:21 bug#68054: [PATCH] Add tree-sitter indent rule for lexical decls in js/typescript Noah Peart
2023-12-30  4:24 ` Yuan Fu
2023-12-30 20:31   ` Yuan Fu
2023-12-31  0:31     ` Dmitry Gutov
2023-12-31  5:35       ` Noah Peart
2023-12-31 13:41         ` Dmitry Gutov
2024-01-01  4:56           ` Yuan Fu
2024-04-17 20:21             ` Noah Peart [this message]
2024-04-23  5:07               ` Yuan Fu
2024-04-24  0:15                 ` Dmitry Gutov
2024-04-24  2:36                   ` Noah Peart
2024-04-24 23:15                     ` Dmitry Gutov
2024-04-25 22:48                       ` Noah Peart
2024-04-26  1:06                         ` Dmitry Gutov
2024-05-02 13:26                           ` Noah Peart
2024-05-02 13:38                             ` Noah Peart
2024-05-18  8:29                               ` Eli Zaretskii

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=CAPVBTSeLOYuEH6je5AJcXU0cxj5Qjx-FNZdo9Zt5UAngaWbtFQ@mail.gmail.com \
    --to=noah.v.peart@gmail.com \
    --cc=68054@debbugs.gnu.org \
    --cc=casouri@gmail.com \
    --cc=dmitry@gutov.dev \
    /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).