From 75c8cc5eab51d1080424687167fec4feb34f68c1 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Thu, 27 Oct 2022 21:05:02 -0700 Subject: [PATCH] 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. --- lisp/progmodes/js.el | 4 +++- lisp/progmodes/ts-mode.el | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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 *") -- 2.39.2