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: Tue, 23 Apr 2024 19:36:45 -0700 [thread overview]
Message-ID: <CAPVBTSd+TcBx=Qvq0177syGnMKaRMtMd2sd1o83C=iNEWTUmkg@mail.gmail.com> (raw)
In-Reply-To: <661fc11d-6da8-4fe5-a900-80ed1b41823a@gutov.dev>
[-- Attachment #1.1: Type: text/plain, Size: 2644 bytes --]
> 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 <dmitry@gutov.dev> 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.
>
[-- Attachment #1.2: Type: text/html, Size: 3842 bytes --]
[-- Attachment #2: 0001-Add-typescript-ts-mode-indentation-for-multi-assignm.patch --]
[-- Type: text/x-patch, Size: 3856 bytes --]
From f202355048c8d915c6978f6e9300dd63104b8a52 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.
---
.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
next prev parent reply other threads:[~2024-04-24 2:36 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 [this message]
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='CAPVBTSd+TcBx=Qvq0177syGnMKaRMtMd2sd1o83C=iNEWTUmkg@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).