From: Theodor Thornhill Date: Sat, 18 Feb 2023 22:10:13 +0000 (+0100) Subject: Adjust jsx indentation X-Git-Tag: emacs-29.0.90~377 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=2e6093b425e;p=emacs.git Adjust jsx indentation We can use the fact that 'treesit-indent-1' uses 'treesit-node-on' when on a whitespace to set the actual current node as parent. Now we can correctly indent the 'jsx_text' nodes. We also add some more electric-indent-chars so that auto-indenting of jsx behaves a little more fluently. * lisp/progmodes/js.el (js--treesit-indent-rules): Add new rules. (js-ts-mode): Add more indent-chars. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--indent-rules): Add new rules. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-base-mode): Add more indent-chars and layout rules. --- diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 1f08f09935b..027d6053f6e 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3458,12 +3458,14 @@ This function is intended for use in `after-change-functions'." ((match "<" "jsx_fragment") parent 0) ((parent-is "jsx_fragment") parent js-indent-level) ((node-is "jsx_closing_element") parent 0) - ((node-is "jsx_element") parent js-indent-level) + ((match "jsx_element" "statement") parent js-indent-level) ((parent-is "jsx_element") parent js-indent-level) + ((parent-is "jsx_text") parent-bol js-indent-level) ((parent-is "jsx_opening_element") parent js-indent-level) ((parent-is "jsx_expression") parent-bol js-indent-level) ((match "/" "jsx_self_closing_element") parent 0) ((parent-is "jsx_self_closing_element") parent js-indent-level) + ;; FIXME(Theo): This no-node catch-all should be removed. When is it needed? (no-node parent-bol 0))))) (defvar js--treesit-keywords @@ -3820,7 +3822,7 @@ Currently there are `js-mode' and `js-ts-mode'." (setq-local comment-multi-line t) ;; Electric-indent. (setq-local electric-indent-chars - (append "{}():;," electric-indent-chars)) ;FIXME: js2-mode adds "[]*". + (append "{}():;,<>/" electric-indent-chars)) ;FIXME: js2-mode adds "[]*". (setq-local electric-layout-rules '((?\; . after) (?\{ . after) (?\} . before))) diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 88a1ff3e202..de9b2f6845c 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -101,12 +101,14 @@ Argument LANGUAGE is either `typescript' or `tsx'." `(((match "<" "jsx_fragment") parent 0) ((parent-is "jsx_fragment") parent typescript-ts-mode-indent-offset) ((node-is "jsx_closing_element") parent 0) - ((node-is "jsx_element") parent typescript-ts-mode-indent-offset) + ((match "jsx_element" "statement") parent typescript-ts-mode-indent-offset) ((parent-is "jsx_element") parent typescript-ts-mode-indent-offset) + ((parent-is "jsx_text") parent-bol typescript-ts-mode-indent-offset) ((parent-is "jsx_opening_element") parent typescript-ts-mode-indent-offset) ((parent-is "jsx_expression") parent-bol typescript-ts-mode-indent-offset) ((match "/" "jsx_self_closing_element") parent 0) ((parent-is "jsx_self_closing_element") parent typescript-ts-mode-indent-offset))) + ;; FIXME(Theo): This no-node catch-all should be removed. When is it needed? (no-node parent-bol 0)))) (defvar typescript-ts-mode--keywords @@ -326,8 +328,9 @@ Argument LANGUAGE is either `typescript' or `tsx'." ;; Electric (setq-local electric-indent-chars - (append "{}():;," electric-indent-chars)) - + (append "{}():;,<>/" electric-indent-chars)) + (setq-local electric-layout-rules + '((?\; . after) (?\{ . after) (?\} . before))) ;; Navigation. (setq-local treesit-defun-type-regexp (regexp-opt '("class_declaration"