unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Noah Peart <noah.v.peart@gmail.com>
To: Dmitry Gutov <dmitry@gutov.dev>
Cc: Yuan Fu <casouri@gmail.com>, 68054@debbugs.gnu.org
Subject: bug#68054: [PATCH] Add tree-sitter indent rule for lexical decls in js/typescript
Date: Thu, 2 May 2024 06:26:00 -0700	[thread overview]
Message-ID: <CAPVBTScNQabge9RM9+RLFr6UmUsuh0pQpt7HL46W+i4MVFi+yg@mail.gmail.com> (raw)
In-Reply-To: <db3096de-803d-41ad-a86b-7c7eda3376b9@gutov.dev>


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

Ok, here is an updated patch for option 3.  The indentation looks like the
examples
you've provided - it's the same indentation as in js-mode for these cases.


On Thu, Apr 25, 2024 at 6:06 PM Dmitry Gutov <dmitry@gutov.dev> wrote:

> On 26/04/2024 01:48, Noah Peart wrote:
> > Ok, I can make a patch for option 3 (the same as js-mode) instead. It's
> > implementation is the simplest also.  Would that work?
>
> I'd be okay with it, yes, thank you. If only because it's good to have
> similar defaults in both modes, and evolve them together until one is
> deprecated.
>
> FTR, the option more in line with my previous explanation would be
> option 3, I think. Rhere's no real need to implement it right now, I
> guess, but for illustration:
>
> const a =
>          (x: string): string => {
>            return x + x;
>          },
>        bbb =
>          {
>            "x": 0
>          },
>        cccc =
>          1,
>        ddddd = 0;
>
> const bbb =
>        {
>          "x": 0
>        }, a = (x: string): string => {
>          return x + x;
>        },
>        bbb = {
>          "x": 0
>        },
>        cccc = 1,
>        ddddd = 0;
>
> js-mode also has by necessity the below exceptions: when the first
> variable's value starts on the same line, and it's multiline (usually
> that means that the same line ends with a {), then its indentation level
> goes back to the statement, not to the "const" keyword:
>
> const a = (x: string): string => {
>    return x + x;
> }
>
> var bbb = {
>    "x": 0
> }
>
> This is basically to support the non-multivar declarations better.
> js-ts-mode now has the exact same indentation; these cases might come up
> when you change how the first two cases indent, however.
>

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

[-- Attachment #2: 0001-Add-typescript-ts-mode-indentation-for-multi-assignm.patch --]
[-- Type: text/x-patch, Size: 3510 bytes --]

From 1e23b33a3b4b930386a932ee6177eaf1b6e6a96e Mon Sep 17 00:00:00 2001
From: Noah Peart <noah.v.peart@gmail.com>
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.
---
 lisp/progmodes/typescript-ts-mode.el          | 14 ++++++++-
 .../typescript-ts-mode-resources/indent.erts  | 31 +++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index ab1d76ab20e..ed60819388f 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,8 @@ 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 ,(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)
diff --git a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
index bec96ad82e0..877382953c1 100644
--- a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
@@ -62,6 +62,37 @@ 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 cccc = 1,
+        bbb = {
+          "x": 0
+        },
+        ddddd = 0;
+  // First decls with value starting on same line
+  const a = (x: string): string => {
+    return x + x;
+  };
+  var bbb = {
+    "x": 0
+  };
+};
+=-=-=
+
 Code:
   (lambda ()
     (setq indent-tabs-mode nil)
-- 
2.34.1


  reply	other threads:[~2024-05-02 13:26 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
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 [this message]
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=CAPVBTScNQabge9RM9+RLFr6UmUsuh0pQpt7HL46W+i4MVFi+yg@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).