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.
This commit is contained in:
Theodor Thornhill 2023-02-18 23:10:13 +01:00
parent c544df4fa3
commit 2e6093b425
2 changed files with 10 additions and 5 deletions

View file

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

View file

@ -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"