1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-05 22:20:24 -08:00

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.
This commit is contained in:
Yuan Fu 2025-06-01 16:14:12 -07:00
parent c3f4e6ca0e
commit 8d132359d1
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 29 additions and 0 deletions

View file

@ -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.
@ -679,6 +699,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
@ -728,6 +750,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

View file

@ -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