aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2025-06-01 16:14:12 -0700
committerYuan Fu2025-06-01 16:14:12 -0700
commit8d132359d19b8efbdbd17786fa67c0c20efba35b (patch)
treeee3f00aae8a2d5567b5f29a8737fb595ea800488
parentc3f4e6ca0e379bf082b1262ff5d4c07a79a434f7 (diff)
downloademacs-8d132359d19b8efbdbd17786fa67c0c20efba35b.tar.gz
emacs-8d132359d19b8efbdbd17786fa67c0c20efba35b.zip
Fix typescript-ts-mode tenary indentation (bug#77901)
Fixes indentation for nested ternary expressions: const a = cond1 ? 1 : cond2 ? 2 : cond3 ? 3 : cond 4: 5; instead of const a = cond1 ? 1 : cond2 ? 2 : cond3 ? 3 : cond 4: 5; * lisp/progmodes/typescript-ts-mode.el: (typescript-ts--standalone-parent-p): New function. (typescript-ts-mode): (tsx-ts-mode): Use new function.
-rw-r--r--lisp/progmodes/typescript-ts-mode.el24
-rw-r--r--test/lisp/progmodes/typescript-ts-mode-resources/indent.erts5
2 files changed, 29 insertions, 0 deletions
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index 3c784d2b833..0bc629cc81e 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -229,6 +229,26 @@ Argument LANGUAGE is either `typescript' or `tsx'."
229 "&&" "||" "!" "?.") 229 "&&" "||" "!" "?.")
230 "TypeScript operators for tree-sitter font-locking.") 230 "TypeScript operators for tree-sitter font-locking.")
231 231
232(defun typescript-ts--standalone-parent-p (parent)
233 "Return t if PARENT can be considered standalone.
234This is used for `treesit-simple-indent-standalone-predicate'."
235 (save-excursion
236 (goto-char (treesit-node-start parent))
237 (cond
238 ;; Never allow nested ternary_expression node to be standalone
239 ;; parent, to avoid nested indentation.
240 ((equal (treesit-node-type (treesit-node-parent parent))
241 "ternary_expression")
242 nil)
243 ;; If there's only whitespace before node, consider
244 ;; this node standalone. To support function
245 ;; chaining, allow a dot to be before the node.
246 ((looking-back (rx bol (* whitespace) (? "."))
247 (line-beginning-position))
248 (if (looking-back "\\.")
249 (1- (point))
250 (point))))))
251
232(defun tsx-ts-mode--font-lock-compatibility-bb1f97b (language) 252(defun tsx-ts-mode--font-lock-compatibility-bb1f97b (language)
233 "Font lock rules helper, to handle different releases of tree-sitter-tsx. 253 "Font lock rules helper, to handle different releases of tree-sitter-tsx.
234Check if a node type is available, then return the right font lock rules. 254Check if a node type is available, then return the right font lock rules.
@@ -679,6 +699,8 @@ This mode is intended to be inherited by concrete major modes."
679 ;; Indent. 699 ;; Indent.
680 (setq-local treesit-simple-indent-rules 700 (setq-local treesit-simple-indent-rules
681 (typescript-ts-mode--indent-rules 'typescript)) 701 (typescript-ts-mode--indent-rules 'typescript))
702 (setq-local treesit-simple-indent-standalone-predicate
703 #'typescript-ts--standalone-parent-p)
682 704
683 ;; Font-lock. 705 ;; Font-lock.
684 (setq-local treesit-font-lock-settings 706 (setq-local treesit-font-lock-settings
@@ -728,6 +750,8 @@ at least 3 (which is the default value)."
728 ;; Indent. 750 ;; Indent.
729 (setq-local treesit-simple-indent-rules 751 (setq-local treesit-simple-indent-rules
730 (typescript-ts-mode--indent-rules 'tsx)) 752 (typescript-ts-mode--indent-rules 'tsx))
753 (setq-local treesit-simple-indent-standalone-predicate
754 #'typescript-ts--standalone-parent-p)
731 755
732 (setq-local treesit-thing-settings 756 (setq-local treesit-thing-settings
733 `((tsx 757 `((tsx
diff --git a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
index 210bfcabd41..ba41c10c08c 100644
--- a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts
@@ -96,6 +96,11 @@ const foo = () => {
96Name: Chained ternary expressions 96Name: Chained ternary expressions
97 97
98=-= 98=-=
99const a = cond1 ? 1 :
100 cond2 ? 2 :
101 cond3 ? 3 :
102 cond 4: 5;
103
99const a = cond1 ? 1 104const a = cond1 ? 1
100 : cond2 ? 2 105 : cond2 ? 2
101 : cond3 ? 3 106 : cond3 ? 3