]> git.eshelyaron.com Git - emacs.git/commitdiff
Use JSX comments on jsx treesit nodes in js-ts-mode and tsx-ts-mode
authorJuri Linkov <juri@linkov.net>
Wed, 14 May 2025 06:57:40 +0000 (09:57 +0300)
committerEshel Yaron <me@eshelyaron.com>
Wed, 14 May 2025 12:35:26 +0000 (14:35 +0200)
* lisp/progmodes/js.el (js--treesit-comment-jsx): New internal variable.
(js--treesit-comment-setup): New function.
(js-ts-mode): Set buffer-local 'comment-setup-function' to
'js--treesit-comment-setup'.

* lisp/progmodes/typescript-ts-mode.el (typescript-ts-base-mode):
Set buffer-local 'comment-setup-function' to 'js--treesit-comment-setup'.

(cherry picked from commit 8bccccedb65a6d3991c41398effa9317adaec873)

lisp/progmodes/js.el
lisp/progmodes/typescript-ts-mode.el

index 20e313917400226ac0d4805e5f0443a5ea06ea15..312310bd91569dca49d4a2584a2f746647087350 100644 (file)
@@ -4035,6 +4035,17 @@ See `treesit-thing-settings' for more information.")
   (rx bos (or "comment" "line_comment" "block_comment" "description") eos)
   "Regexp for `c-ts-common--comment-regexp'.")
 
+(defvar-local js--treesit-comment-jsx 'undefined)
+
+(defun js--treesit-comment-setup ()
+  (let ((jsx (not (null (treesit-parent-until
+                         (treesit-node-at (point)) "jsx")))))
+    (unless (eq js--treesit-comment-jsx jsx)
+      (setq js--treesit-comment-jsx jsx)
+      (cond (jsx (setq-local comment-start "{/* ")
+                 (setq-local comment-end " */}"))
+            (t (c-ts-common-comment-setup))))))
+
 ;;;###autoload
 (define-derived-mode js-ts-mode js-base-mode "JavaScript"
   "Major mode for editing JavaScript.
@@ -4049,7 +4060,7 @@ See `treesit-thing-settings' for more information.")
     ;; Which-func.
     (setq-local which-func-imenu-joiner-function #'js--which-func-joiner)
     ;; Comment.
-    (c-ts-common-comment-setup)
+    (setq-local comment-setup-function #'js--treesit-comment-setup)
     (setq-local comment-multi-line t)
 
     ;; Electric-indent.
index 55b2421a20efb1a95bfb9ab33d84f42c44748714..6ae3ddd646aa2b19ed3cad732fd13823f431587f 100644 (file)
@@ -610,7 +610,7 @@ This mode is intended to be inherited by concrete major modes."
   :syntax-table typescript-ts-mode--syntax-table
 
   ;; Comments.
-  (c-ts-common-comment-setup)
+  (setq-local comment-setup-function #'js--treesit-comment-setup)
 
   ;; Electric
   (setq-local electric-indent-chars