unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#58815: 29.0.50; Small treesitter fixes for js/ts-mode
@ 2022-10-27  9:58 Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; only message in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-10-27  9:58 UTC (permalink / raw)
  To: 58815; +Cc: Yuan Fu

[-- Attachment #1: Type: text/plain, Size: 156 bytes --]


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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Tweak-indentation-and-ensure-parser.patch --]
[-- Type: text/x-diff, Size: 3489 bytes --]

From c45f93e3677b3ef66730afd00024933568058755 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-27  9:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-27  9:58 bug#58815: 29.0.50; Small treesitter fixes for js/ts-mode Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors

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).