From 474abbd65c9d66410976e9f816d37ffa545d0aa4 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Sun, 1 Jun 2025 16:14:12 -0700 Subject: [PATCH] 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. (cherry picked from commit 8d132359d19b8efbdbd17786fa67c0c20efba35b) --- lisp/progmodes/typescript-ts-mode.el | 24 +++++++++++++++++++ .../typescript-ts-mode-resources/indent.erts | 5 ++++ 2 files changed, 29 insertions(+) diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index eafaad876bd..c8efb4f771a 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'." "&&" "||" "!" "?.") "TypeScript operators for tree-sitter font-locking.") +(defun typescript-ts--standalone-parent-p (parent) + "Return t if PARENT can be considered standalone. +This is used for `treesit-simple-indent-standalone-predicate'." + (save-excursion + (goto-char (treesit-node-start parent)) + (cond + ;; Never allow nested ternary_expression node to be standalone + ;; parent, to avoid nested indentation. + ((equal (treesit-node-type (treesit-node-parent parent)) + "ternary_expression") + nil) + ;; If there's only whitespace before node, consider + ;; this node standalone. To support function + ;; chaining, allow a dot to be before the node. + ((looking-back (rx bol (* whitespace) (? ".")) + (line-beginning-position)) + (if (looking-back "\\.") + (1- (point)) + (point)))))) + (defun tsx-ts-mode--font-lock-compatibility-bb1f97b (language) "Font lock rules helper, to handle different releases of tree-sitter-tsx. Check if a node type is available, then return the right font lock rules. @@ -677,6 +697,8 @@ This mode is intended to be inherited by concrete major modes." ;; Indent. (setq-local treesit-simple-indent-rules (typescript-ts-mode--indent-rules 'typescript)) + (setq-local treesit-simple-indent-standalone-predicate + #'typescript-ts--standalone-parent-p) ;; Font-lock. (setq-local treesit-font-lock-settings @@ -726,6 +748,8 @@ at least 3 (which is the default value)." ;; Indent. (setq-local treesit-simple-indent-rules (typescript-ts-mode--indent-rules 'tsx)) + (setq-local treesit-simple-indent-standalone-predicate + #'typescript-ts--standalone-parent-p) (setq-local treesit-thing-settings `((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 = () => { Name: Chained ternary expressions =-= +const a = cond1 ? 1 : + cond2 ? 2 : + cond3 ? 3 : + cond 4: 5; + const a = cond1 ? 1 : cond2 ? 2 : cond3 ? 3 -- 2.39.5