From 9c4dddad456e23892a80303da26774a13250b647 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Thu, 27 Oct 2022 17:07:12 -0700 Subject: [PATCH] Add some js-mode tree-sitter indent rules * lisp/progmodes/js.el (js--treesit-indent-rules): Add rules and a let form. --- lisp/progmodes/js.el | 59 +++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index f258b093f4a..583de0c5032 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3415,32 +3415,39 @@ This function is intended for use in `after-change-functions'." (point)))) (defvar js--treesit-indent-rules - `((javascript - (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 "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) - ((parent-is "arguments") parent-bol ,js-indent-level) - ((parent-is "array") parent-bol ,js-indent-level) - ((parent-is "formal_parameters") parent-bol ,js-indent-level) - ((parent-is "template_substitution") parent-bol ,js-indent-level) - ((parent-is "object_pattern") parent-bol ,js-indent-level) - ((parent-is "object") parent-bol ,js-indent-level) - ((parent-is "arrow_function") parent-bol ,js-indent-level) - ((parent-is "parenthesized_expression") parent-bol ,js-indent-level) - - ;; JSX - ((parent-is "jsx_opening_element") parent ,js-indent-level) - ((node-is "jsx_closing_element") parent 0) - ((node-is "jsx_text") parent ,js-indent-level) - ((parent-is "jsx_element") parent ,js-indent-level) - ((node-is "/") parent 0) - ((parent-is "jsx_self_closing_element") parent ,js-indent-level)))) + (let ((switch-case (rx "switch_" (or "case" "default")))) + `((javascript + (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) + ((node-is ,switch-case) parent-bol 0) + ;; "{" on the newline. + ((node-is "statement_block") 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) + ((parent-is "arguments") parent-bol ,js-indent-level) + ((parent-is "array") parent-bol ,js-indent-level) + ((parent-is "formal_parameters") parent-bol ,js-indent-level) + ((parent-is "template_substitution") parent-bol ,js-indent-level) + ((parent-is "object_pattern") parent-bol ,js-indent-level) + ((parent-is "object") parent-bol ,js-indent-level) + ((parent-is "pair") parent-bol ,js-indent-level) + ((parent-is "arrow_function") parent-bol ,js-indent-level) + ((parent-is "parenthesized_expression") parent-bol ,js-indent-level) + ((parent-is "class_body") parent-bol ,js-indent-level) + ((parent-is ,switch-case) parent-bol ,js-indent-level) + + ;; JSX + ((parent-is "jsx_opening_element") parent ,js-indent-level) + ((node-is "jsx_closing_element") parent 0) + ((node-is "jsx_text") parent ,js-indent-level) + ((parent-is "jsx_element") parent ,js-indent-level) + ((node-is "/") parent 0) + ((parent-is "jsx_self_closing_element") parent ,js-indent-level))))) (defvar js--treesit-keywords '("as" "async" "await" "break" "case" "catch" "class" "const" "continue" -- 2.39.2