Subject: 29.0.50; Small treesitter fixes for js/ts-mode

From: Theodor Thornhill <[email-masked]>
Date: Thu, 27 Oct 2022 11:58:06 +0200

Hello Yuan!

See the attached patch.  There's a small fix to ensure the parser, as
well as adding in two small, but nice indentation fixes.

Thanks,
Theo

>From c45f93e3677b3ef66730afd00024933568058755 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <[email-masked]>
Date: Thu, 27 Oct 2022 11:53:07 +0200
Subject: [PATCH] Tweak indentation and ensure parser

* lisp/progmodes/js.el (js--treesit-indent-rules)
(js--treesit-cc-indent-rules): Don't use '.' as indent anchor and add
in 'program' as root anchor.

* lisp/progmodes/ts-mode.el (ts-mode--indent-rules): Don't use '.' as
indent anchor and add in 'program' as root anchor.

(ts-mode): Create parser so that mode can initialize.
---
 lisp/progmodes/js.el      | 12 +++++++++++-
 lisp/progmodes/ts-mode.el |  9 +++++----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 89bd04b231..b940a3a2a4 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3416,12 +3416,16 @@ js--treesit-backward-up-list
 
 (defvar js--treesit-indent-rules
   `((javascript
+     ((parent-is "program") parent-bol 0)
      (no-node (js--treesit-backward-up-list) ,js-indent-level)
      ((node-is "}") parent-bol 0)
      ((node-is ")") parent-bol 0)
      ((node-is "]") parent-bol 0)
      ((node-is ">") parent-bol 0)
-     ((node-is ".") parent-bol ,js-indent-level)
+     ((parent-is "ternary_expression")
+      parent-bol ,js-indent-level)
+     ((parent-is "member_expression")
+      parent-bol ,js-indent-level)
      ((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)
@@ -3452,6 +3456,7 @@ js--treesit-cc-indent-rules
                               "_clause"))
         (switch-case-re (rx "switch_" (or "case" "default"))))
     `((javascript
+       ((parent-is "program") parent-bol 0)
        ;; Function declaration.
        ;; "{"
        ((match "statement_block" ,function-re) parent defun-open)
@@ -3529,6 +3534,11 @@ js--treesit-cc-indent-rules
        ((node-is "formal_parameters") parent statement-cont)
 
        ;; Misc.
+       ((parent-is "ternary_expression")
+        parent-bol ,js-indent-level)
+       ((parent-is "member_expression")
+        parent-bol ,js-indent-level)
+
        ((parent-is "function_declaration") parent func-decl-cont)
        ((parent-is "string-fragment") grand-parent string)
        ((parent-is "comment") grand-parent c)
diff --git a/lisp/progmodes/ts-mode.el b/lisp/progmodes/ts-mode.el
index 28800e378a..88c93a6e93 100644
--- a/lisp/progmodes/ts-mode.el
+++ b/lisp/progmodes/ts-mode.el
@@ -55,14 +55,15 @@ ts-mode--syntax-table
 
 (defvar ts-mode--indent-rules
   `((tsx
+     ((parent-is "program") parent-bol 0)
      ((node-is "}") parent-bol 0)
      ((node-is ")") parent-bol 0)
      ((node-is "]") parent-bol 0)
      ((node-is ">") parent-bol 0)
-     ((node-is ".")
-      parent-bol ,ts-mode-indent-offset)
      ((parent-is "ternary_expression")
       parent-bol ,ts-mode-indent-offset)
+     ((parent-is "member_expression")
+      parent-bol ,ts-mode-indent-offset)
      ((parent-is "named_imports")
       parent-bol ,ts-mode-indent-offset)
      ((parent-is "statement_block")
@@ -261,8 +262,8 @@ ts-mode
   :syntax-table ts-mode--syntax-table
 
   (cond
-   ((treesit-ready-p nil 'tsx)
-    ;; Tree-sitter.
+   ((treesit-ready-p 'ts-mode 'tsx)
+    (treesit-parser-create 'tsx)
     ;; Comments.
     (setq-local comment-start "// ")
     (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
-- 
2.34.1