From 6fcab5a203fadefc21efc11e806f71fda96e5a89 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Wed, 14 May 2025 09:57:40 +0300 Subject: [PATCH] Use JSX comments on jsx treesit nodes in js-ts-mode and tsx-ts-mode * 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 | 13 ++++++++++++- lisp/progmodes/typescript-ts-mode.el | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 20e31391740..312310bd915 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -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. diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 55b2421a20e..6ae3ddd646a 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -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 -- 2.39.5