emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/typescript-mode 444aa36ec2 169/222: Fix the indentation of


From: ELPA Syncer
Subject: [nongnu] elpa/typescript-mode 444aa36ec2 169/222: Fix the indentation of member expressions and continued expressions.
Date: Sun, 6 Feb 2022 16:59:29 -0500 (EST)

branch: elpa/typescript-mode
commit 444aa36ec272d0fcb9c5066ce4513d377a78dda7
Author: Louis-Dominique Dubeau <ldd@lddubeau.com>
Commit: Louis-Dominique Dubeau <ldd@lddubeau.com>

    Fix the indentation of member expressions and continued expressions.
---
 typescript-mode.el | 143 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 88 insertions(+), 55 deletions(-)

diff --git a/typescript-mode.el b/typescript-mode.el
index 3ae8e16ae0..7806053db4 100644
--- a/typescript-mode.el
+++ b/typescript-mode.el
@@ -2258,6 +2258,23 @@ Searches specifically for any of \"=\", \"}\", and 
\"type\"."
                      (and (progn (backward-char)
                                  (not (looking-at "++\\|--\\|/[/*]")))))))))))
 
+(cl-defun typescript--compute-member-expression-indent ()
+  "Determine the indent of a member expression.
+
+This function must be called with point located at the dot that
+starts the member expression.
+"
+  ;; Find the line that has the object from which we are getting thismember.
+  ;; And set an indent relative to that.
+  (while (looking-at "\\.")
+    (typescript--backward-syntactic-ws)
+    (while (memq (char-before) '(?\] ?} ?\)))
+      (backward-list)
+      (typescript--backward-syntactic-ws))
+    (if (looking-back typescript--dotted-name-re nil)
+        (back-to-indentation)
+      (typescript--forward-syntactic-ws)))
+  (+ (current-column) typescript-indent-level))
 
 (defun typescript--end-of-do-while-loop-p ()
   "Return non-nil if point is on the \"while\" of a do-while statement.
@@ -2424,61 +2441,77 @@ moved on success."
   "Return the proper indentation for the current line."
   (save-excursion
     (back-to-indentation)
-    (cond ((nth 4 parse-status)
-           (typescript--get-c-offset 'c (nth 8 parse-status)))
-          ((nth 8 parse-status) 0) ; inside string
-          ((typescript--ctrl-statement-indentation))
-          ((eq (char-after) ?#) 0)
-          ((nth 1 parse-status)
-           (let ((same-indent-p (looking-at "[]})]"))
-                 (switch-keyword-p (looking-at 
"\\_<default\\_>\\|\\_<case\\_>[^:]"))
-                 (continued-expr-p (typescript--continued-expression-p))
-                 (list-start (nth 1 parse-status)))
-             (goto-char list-start)
-             (if (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)")
-                 (progn
-                   (skip-syntax-backward " ")
-                   (cond
-                    ((or (typescript--backward-to-parameter-list)
-                         (eq (char-before) ?\)))
-                     ;; Take the curly brace as marking off the body of a 
function.
-                     ;; In that case, we want the code that follows to see the 
indentation
-                     ;; that was in effect at the beginning of the function 
declaration, and thus
-                     ;; we want to move back over the list of function 
parameters.
-                     (backward-list))
-                    ((looking-back "," nil)
-                     ;; If we get here, we have a comma, spaces and an opening 
curly brace. (And
-                     ;; (point) is just after the comma.) We don't want to 
move from the current position
-                     ;; so that object literals in parameter lists are 
properly indented.
-                     nil)
-                    (t
-                     ;; In all other cases, we don't want to move from the 
curly brace.
-                     (goto-char list-start)))
-                   (back-to-indentation)
-                   (let* ((in-switch-p (unless same-indent-p
-                                         (looking-at "\\_<switch\\_>")))
-                          (same-indent-p (or same-indent-p
-                                             (and switch-keyword-p
-                                                  in-switch-p)))
-                          (indent
-                           (cond (same-indent-p
-                                  (current-column))
-                                 (continued-expr-p
-                                  (+ (current-column) (* 2 
typescript-indent-level)
-                                     typescript-expr-indent-offset))
-                                 (t
-                                  (+ (current-column) 
typescript-indent-level)))))
-                     (if (and in-switch-p typescript-indent-switch-clauses)
-                         (+ indent typescript-indent-level)
-                       indent)))
-               (unless same-indent-p
-                 (forward-char)
-                 (skip-chars-forward " \t"))
-               (current-column))))
-
-          ((typescript--continued-expression-p)
-           (+ typescript-indent-level typescript-expr-indent-offset))
-          (t 0))))
+    (let ((member-expr-p (looking-at "\\.")))
+      (cond ((nth 4 parse-status) ;; Inside a comment.
+             (typescript--get-c-offset 'c (nth 8 parse-status)))
+            ((nth 8 parse-status) 0) ;; Inside a string.
+            ((typescript--ctrl-statement-indentation)) ;; Control statements.
+            ((eq (char-after) ?#) 0) ;; Looking at a pragma.
+            ;; Inside a list of things. Note that in the TS contents, the 
curly braces
+            ;; marking code blocks are "list of things".
+            ((nth 1 parse-status)
+             (let ((indent-start (point))
+                   (same-indent-p (looking-at "[]})]"))
+                   (switch-keyword-p (looking-at 
"\\_<default\\_>\\|\\_<case\\_>[^:]"))
+                   (continued-expr-p (typescript--continued-expression-p))
+                   (list-start (nth 1 parse-status)))
+               (goto-char list-start)
+               (if (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)")
+                   (progn
+                     (skip-syntax-backward " ")
+                     (cond
+                      ((or (typescript--backward-to-parameter-list)
+                           (eq (char-before) ?\)))
+                       ;; Take the curly brace as marking off the body of a 
function.
+                       ;; In that case, we want the code that follows to see 
the indentation
+                       ;; that was in effect at the beginning of the function 
declaration, and thus
+                       ;; we want to move back over the list of function 
parameters.
+                       (backward-list))
+                      ((looking-back "," nil)
+                       ;; If we get here, we have a comma, spaces and an 
opening curly brace. (And
+                       ;; (point) is just after the comma.) We don't want to 
move from the current position
+                       ;; so that object literals in parameter lists are 
properly indented.
+                       nil)
+                      (t
+                       ;; In all other cases, we don't want to move from the 
curly brace.
+                       (goto-char list-start)))
+                     (back-to-indentation)
+                     (let* ((in-switch-p (unless same-indent-p
+                                           (looking-at "\\_<switch\\_>")))
+                            (same-indent-p (or same-indent-p
+                                               (and switch-keyword-p
+                                                    in-switch-p)))
+                            (indent
+                             (cond (same-indent-p
+                                    (current-column))
+                                   (continued-expr-p
+                                    (if (not member-expr-p)
+                                        (+ (current-column) (* 2 
typescript-indent-level)
+                                           typescript-expr-indent-offset)
+                                      (goto-char indent-start)
+                                      
(typescript--compute-member-expression-indent)))
+                                   (t
+                                    (+ (current-column) 
typescript-indent-level)))))
+                       (if (and in-switch-p typescript-indent-switch-clauses)
+                           (+ indent typescript-indent-level)
+                         indent)))
+                 (unless same-indent-p
+                   (forward-char)
+                   (skip-chars-forward " \t"))
+                 (if continued-expr-p
+                     (if (not member-expr-p)
+                         (progn (back-to-indentation)
+                                (+ (current-column) typescript-indent-level
+                                   typescript-expr-indent-offset))
+                       (goto-char indent-start)
+                       (typescript--compute-member-expression-indent))
+                   (current-column)))))
+
+            ((typescript--continued-expression-p) ;; Inside a continued 
expression.
+             (if member-expr-p
+                 (typescript--compute-member-expression-indent)
+               (+ typescript-indent-level typescript-expr-indent-offset)))
+            (t 0)))))
 
 (defun typescript-indent-line ()
   "Indent the current line as typescript."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]