From: Theodor Thornhill Date: Fri, 28 Oct 2022 04:05:02 +0000 (-0700) Subject: Improve tree-sitter indent rules in ts-mode and js-mode X-Git-Tag: emacs-29.0.90~1767 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=75c8cc5eab51d1080424687167fec4feb34f68c1;p=emacs.git Improve tree-sitter indent rules in ts-mode and js-mode * lisp/progmodes/js.el (js--treesit-indent-rules) * lisp/progmodes/ts-mode.el (ts-mode--indent-rules): Add more rules. (ts-mode): Create parser. --- diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 583de0c5032..dae6de1052a 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3417,12 +3417,14 @@ This function is intended for use in `after-change-functions'." (defvar js--treesit-indent-rules (let ((switch-case (rx "switch_" (or "case" "default")))) `((javascript + ((parent-is "program") parent-bol 0) (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 "ternary_expression") parent-bol ,js-indent-level) + ((parent-is "member_expression") parent-bol ,js-indent-level) ((node-is ,switch-case) parent-bol 0) ;; "{" on the newline. ((node-is "statement_block") parent-bol ,js-indent-level) diff --git a/lisp/progmodes/ts-mode.el b/lisp/progmodes/ts-mode.el index 28800e378a8..15b8cbf7110 100644 --- a/lisp/progmodes/ts-mode.el +++ b/lisp/progmodes/ts-mode.el @@ -55,6 +55,7 @@ (defvar ts-mode--indent-rules `((tsx + ((parent-is "program") parent-bol 0) ((node-is "}") parent-bol 0) ((node-is ")") parent-bol 0) ((node-is "]") parent-bol 0) @@ -63,6 +64,8 @@ parent-bol ,ts-mode-indent-offset) ((parent-is "ternary_expression") parent-bol ,ts-mode-indent-offset) + ((parent-is "member_expression") + parent-bol ,ts-mode-indent-offset) ((parent-is "named_imports") parent-bol ,ts-mode-indent-offset) ((parent-is "statement_block") @@ -261,8 +264,11 @@ :syntax-table ts-mode--syntax-table (cond + ;; `ts-mode' requires tree-sitter to work, so we don't check if + ;; user enables tree-sitter for it. ((treesit-ready-p nil 'tsx) ;; Tree-sitter. + (treesit-parser-create 'tsx) ;; Comments. (setq-local comment-start "// ") (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")