Subject: Re: bug#68054: [PATCH] Add tree-sitter indent rule for lexical decls in js/typescript

From: Noah Peart <[email-masked]>
Date: Tue, 23 Apr 2024 19:36:45 -0700

> What js-mode does (looks like 3) shouldn't be too bad, but I wouldn't
mind an extra indentation level for such cases, too (example 2).

The problem I found with option 2/3 was cases like the following (which
also seem like the most common) where I expect the start of
the function be indented according to `typescript-ts-mode-indent-offset`,
not with the declaration keyword "const" as in `js-mode` (and option 3).

    const someFuncWithReallyLongName =
      async (x: number, y: number, z: number): Promise<void> => {
        // ...
      };

> This seems like a rare enough case, so it probably doesn't matter too
much, so I'd suggest picking one style and implementing it, and then
adjusting based on user feedback later.

I've attached a patch implementing option 1 for now (with a test), but
I'm happy to change the style whenever.

Thanks!

On Tue, Apr 23, 2024 at 5:15 PM Dmitry Gutov <[email-masked]> wrote:

> On 23/04/2024 08:07, Yuan Fu wrote:
> >> 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.
> > I don’t really know what’s the convention, if there is one. Maybe Dmitry
> has better idea. Personally I like option 1.
>
> What js-mode does (looks like 3) shouldn't be too bad, but I wouldn't
> mind an extra indentation level for such cases, too (example 2).
>
> This seems like a rare enough case, so it probably doesn't matter too
> much, so I'd suggest picking one style and implementing it, and then
> adjusting based on user feedback later.
>
From f202355048c8d915c6978f6e9300dd63104b8a52 Mon Sep 17 00:00:00 2001
From: Noah Peart <[email-masked]>
Date: Fri, 19 Apr 2024 01:46:50 -0700
Subject: [PATCH] Add typescript-ts-mode indentation for multi-assignment decls

* lisp/progmodes/typescript-ts-mode.el
(typescript-ts-mode--indent-rules): Add indentation rules for
lexical and variable declarations with multiple assignments.
* test/lisp/progmodes/typescript-ts-mode-resources/indent.erts:
Add indent test for variable declarations.
---
 .last-build-hash                              |  1 +
 lisp/progmodes/typescript-ts-mode.el          | 15 +++++++++-
 .../typescript-ts-mode-resources/indent.erts  | 28 +++++++++++++++++++
 3 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 .last-build-hash

diff --git a/.last-build-hash b/.last-build-hash
new file mode 100644
index 00000000000..77fe4446df5
--- /dev/null
+++ b/.last-build-hash
@@ -0,0 +1 @@
+a2e327cbca1e756373109d4788ea635250d23224
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index ab1d76ab20e..3c10a19e712 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -91,6 +91,17 @@ 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* ((declaration (treesit-parent-until
+                       parent (rx (or "variable" "lexical") "_declaration") t))
+         (decl (treesit-node-child declaration 0)))
+    (+ (treesit-node-start declaration)
+       (- (treesit-node-end decl) (treesit-node-start decl)))))
+
 (defun typescript-ts-mode--indent-rules (language)
   "Rules used for indentation.
 Argument LANGUAGE is either `typescript' or `tsx'."
@@ -113,7 +124,9 @@ 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)
+     ((parent-is "variable_declarator") grand-parent typescript-ts-mode-indent-offset)
+     ((parent-is ,(rx (or "variable" "lexical") "_declaration"))
+      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)
diff --git a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
index bec96ad82e0..908dd8103b5 100644
--- a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
@@ -62,6 +62,34 @@ const foo = (x: string) => {
 };
 =-=-=
 
+Name: Lexical and variable declarations
+
+=-=
+const foo = () => {
+  let x = 1,
+      yyyy: {
+        [k: string | number]: string,
+      } = {
+        "foo": "foo",
+        "bar": "bar",
+      };
+  var obar = 1,
+      fo: { [x: any]: any } = {
+        "a": 1,
+        "b": 2,
+      };
+  const someFuncWithReallyLongName =
+    async (x: number, y: number, z: number): Promise<void> => {
+      return new Promise();
+    };
+  const cccc = 1,
+        bbb = {
+          "x": 0
+        },
+        ddddd = 0;
+};
+=-=-=
+
 Code:
   (lambda ()
     (setq indent-tabs-mode nil)
-- 
2.34.1