emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] master 21eef9f 2/2: Indent arrows’ expression bodies like


From: Dmitry Gutov
Subject: [Emacs-diffs] master 21eef9f 2/2: Indent arrows’ expression bodies like function bodies (Bug#25904)
Date: Tue, 12 Feb 2019 19:26:04 -0500 (EST)

branch: master
commit 21eef9fa7f5abc7587251a6671add780b7370bb7
Author: Jackson Ray Hamilton <address@hidden>
Commit: Dmitry Gutov <address@hidden>

    Indent arrows’ expression bodies like function bodies (Bug#25904)
    
    * lisp/progmodes/js.el (js--continued-expression-p): Don’t confuse
    ‘=>’ for a ‘>’ operator.
    (js--line-terminating-arrow-re): New variable.
    (js--looking-at-broken-arrow-function-p): New function.
    (js--proper-indentation): Don’t align arrow functions’ expression
    bodies starting on new lines like list continuations, instead align
    them like function bodies (js-indent-align-list-continuation need not
    be nil).
    
    * test/manual/indent/js.js: Add test for Bug#25904.
    
    Co-authored-by: Felipe Ochoa <address@hidden>
---
 lisp/progmodes/js.el     | 23 +++++++++++++++++++++--
 test/manual/indent/js.js |  9 +++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 6e18e30..b0bb821 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -1835,7 +1835,7 @@ This performs fontification according to 
`js--class-styles'."
              (skip-chars-backward " \t")
              (or (bobp) (backward-char))
              (and (> (point) (point-min))
-                  (save-excursion (backward-char) (not (looking-at "[/*]/")))
+                  (save-excursion (backward-char) (not (looking-at 
"[/*]/\\|=>")))
                   (js--looking-at-operator-p)
                   (and (progn (backward-char)
                               (not (looking-at "+\\+\\|--\\|/[/*]"))))))))))
@@ -2064,6 +2064,24 @@ indentation is aligned to that column."
         (when comma-p
           (goto-char (1+ declaration-keyword-end))))))))
 
+(defconst js--line-terminating-arrow-re "\\s-*=>\\s-*\\(/[/*]\\|$\\)"
+  "Regexp matching the last \"=>\" (arrow) token on a line.
+Whitespace and comments around the arrow are ignored.")
+
+(defun js--looking-at-broken-arrow-function-p ()
+  "Helper function for `js--proper-indentation'.
+Return t if point is at the start of a (possibly async) arrow
+function and the last non-comment, non-whitespace token of the
+current line is the \"=>\" token."
+  (when (looking-at "\\s-*async\\s-*")
+    (goto-char (match-end 0)))
+  (cond
+   ((eq (char-after) ?\()
+    (forward-list)
+    (looking-at-p js--line-terminating-arrow-re))
+   (t (looking-at-p
+       (concat js--name-re js--line-terminating-arrow-re)))))
+
 (defun js--proper-indentation (parse-status)
   "Return the proper indentation for the current line."
   (save-excursion
@@ -2094,7 +2112,8 @@ indentation is aligned to that column."
                  (continued-expr-p (js--continued-expression-p)))
              (goto-char (nth 1 parse-status)) ; go to the opening char
              (if (or (not js-indent-align-list-continuation)
-                     (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)"))
+                     (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)")
+                     (save-excursion (forward-char) 
(js--looking-at-broken-arrow-function-p)))
                  (progn ; nothing following the opening paren/bracket
                    (skip-syntax-backward " ")
                    (when (eq (char-before) ?\)) (backward-list))
diff --git a/test/manual/indent/js.js b/test/manual/indent/js.js
index df79098..647d743 100644
--- a/test/manual/indent/js.js
+++ b/test/manual/indent/js.js
@@ -151,6 +151,15 @@ let b = {
   `
 }
 
+// bug#25904
+foo.bar.baz(very => // A comment
+  very
+).biz(([baz={a: [123]}, boz]) =>
+  baz
+).snarf((snorf) => /* Another comment */
+  snorf
+);
+
 // Local Variables:
 // indent-tabs-mode: nil
 // js-indent-level: 2



reply via email to

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