Subject: [PATCH] Add tree-sitter indent rule for lexical decls in js/typescript
From: Noah Peart <[email-masked]>
To: [email-masked]
Date: Wed, 27 Dec 2023 01:21:05 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

* Bug: `js-ts-mode` and `typescript-ts-mode` are missing indentation
rules for lexical declarations that span multiple lines.

Recipe to reproduce:

Using the following function to configure js-ts-mode and indent the
buffer:

    (defun try-indent ()
      (interactive)
      (js-ts-mode)
      (setq-local indent-tabs-mode nil)
      (setq-local js-indent-level 4)
      (indent-region (point-min) (point-max)))

Add the following example to a buffer and call `try-indent`.

    let foo = 1,
    bar = 2; // no indent rule matches this line

No indentation is applied to the second line.

This patch adds a simple indentation rules for `js-ts-mode` and
`typescript-ts-mode` to handle the multi-line lexical declarations.

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.33, cairo version 1.16.0) of 2023-12-26 built on nverno-XPS-8940
Repository revision: d376462c7183752bf44b9bd20bf5020fe7eaf75a
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101004
System Description: Ubuntu 22.04.3 LTS

Configured using:
 'configure --prefix=/usr/local --with-modules --with-tree-sitter
--with-threads --with-x-toolkit=gtk3 --with-xwidgets --with-gnutls
--with-json --with-mailutils --with-jpeg --with-png --with-rsvg
--with-tiff --with-xml2 --with-xpm --with-imagemagick CC=gcc-12
CXX=gcc-12'
From 4c2793fcdb1199253396511a13c13519c76bb817 Mon Sep 17 00:00:00 2001
From: Noah Peart <[email-masked]>
Date: Wed, 27 Dec 2023 00:50:46 -0500
Subject: [PATCH] Add tree-sitter indent rule for lexical decls in
 js/typescript

* lisp/progmodes/typescript-ts-mode.el
(typescript-ts-mode--indent-rules): Add indent rule for
lexical_declarations.

* lisp/progmodes/js.el (js--treesit-indent-rules): Add indent rule
for lexical_declarations.
---
 lisp/progmodes/js.el                 | 1 +
 lisp/progmodes/typescript-ts-mode.el | 1 +
 2 files changed, 2 insertions(+)

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 61bd94222ac..935655f779f 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3449,6 +3449,7 @@ js--treesit-indent-rules
        ((parent-is "named_imports") parent-bol js-indent-level)
        ((parent-is "statement_block") parent-bol js-indent-level)
        ((parent-is "variable_declarator") parent-bol js-indent-level)
+       ((parent-is "lexical_declaration") parent-bol js-indent-level)
        ((parent-is "arguments") parent-bol js-indent-level)
        ((parent-is "array") parent-bol js-indent-level)
        ((parent-is "formal_parameters") parent-bol js-indent-level)
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index 7f0b7236301..a0f6e7c9501 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -114,6 +114,7 @@ typescript-ts-mode--indent-rules
      ((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 "lexical_declaration") parent-bol typescript-ts-mode-indent-offset)
      ((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)
-- 
2.34.1