mirror of
git://git.sv.gnu.org/emacs.git
synced 2025-12-06 06:20:55 -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:
parent
c3f4e6ca0e
commit
8d132359d1
2 changed files with 29 additions and 0 deletions
|
|
@ -229,6 +229,26 @@ Argument LANGUAGE is either `typescript' or `tsx'."
|
||||||
"&&" "||" "!" "?.")
|
"&&" "||" "!" "?.")
|
||||||
"TypeScript operators for tree-sitter font-locking.")
|
"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)
|
(defun tsx-ts-mode--font-lock-compatibility-bb1f97b (language)
|
||||||
"Font lock rules helper, to handle different releases of tree-sitter-tsx.
|
"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.
|
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.
|
;; Indent.
|
||||||
(setq-local treesit-simple-indent-rules
|
(setq-local treesit-simple-indent-rules
|
||||||
(typescript-ts-mode--indent-rules 'typescript))
|
(typescript-ts-mode--indent-rules 'typescript))
|
||||||
|
(setq-local treesit-simple-indent-standalone-predicate
|
||||||
|
#'typescript-ts--standalone-parent-p)
|
||||||
|
|
||||||
;; Font-lock.
|
;; Font-lock.
|
||||||
(setq-local treesit-font-lock-settings
|
(setq-local treesit-font-lock-settings
|
||||||
|
|
@ -728,6 +750,8 @@ at least 3 (which is the default value)."
|
||||||
;; Indent.
|
;; Indent.
|
||||||
(setq-local treesit-simple-indent-rules
|
(setq-local treesit-simple-indent-rules
|
||||||
(typescript-ts-mode--indent-rules 'tsx))
|
(typescript-ts-mode--indent-rules 'tsx))
|
||||||
|
(setq-local treesit-simple-indent-standalone-predicate
|
||||||
|
#'typescript-ts--standalone-parent-p)
|
||||||
|
|
||||||
(setq-local treesit-thing-settings
|
(setq-local treesit-thing-settings
|
||||||
`((tsx
|
`((tsx
|
||||||
|
|
|
||||||
|
|
@ -96,6 +96,11 @@ const foo = () => {
|
||||||
Name: Chained ternary expressions
|
Name: Chained ternary expressions
|
||||||
|
|
||||||
=-=
|
=-=
|
||||||
|
const a = cond1 ? 1 :
|
||||||
|
cond2 ? 2 :
|
||||||
|
cond3 ? 3 :
|
||||||
|
cond 4: 5;
|
||||||
|
|
||||||
const a = cond1 ? 1
|
const a = cond1 ? 1
|
||||||
: cond2 ? 2
|
: cond2 ? 2
|
||||||
: cond3 ? 3
|
: cond3 ? 3
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue