diff options
| author | Jackson Ray Hamilton | 2019-02-09 12:26:21 -0800 |
|---|---|---|
| committer | Dmitry Gutov | 2019-02-13 03:23:50 +0300 |
| commit | 21eef9fa7f5abc7587251a6671add780b7370bb7 (patch) | |
| tree | 22aca2523327daea120f1da4d518b72a5280a8c3 | |
| parent | dd319f2711f895eec87c1017b82cd9d88d9ecd0a (diff) | |
| download | emacs-21eef9fa7f5abc7587251a6671add780b7370bb7.tar.gz emacs-21eef9fa7f5abc7587251a6671add780b7370bb7.zip | |
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 <felipe@fov.space>
| -rw-r--r-- | lisp/progmodes/js.el | 23 | ||||
| -rw-r--r-- | 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 6e18e30517a..b0bb8213dcb 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el | |||
| @@ -1835,7 +1835,7 @@ This performs fontification according to `js--class-styles'." | |||
| 1835 | (skip-chars-backward " \t") | 1835 | (skip-chars-backward " \t") |
| 1836 | (or (bobp) (backward-char)) | 1836 | (or (bobp) (backward-char)) |
| 1837 | (and (> (point) (point-min)) | 1837 | (and (> (point) (point-min)) |
| 1838 | (save-excursion (backward-char) (not (looking-at "[/*]/"))) | 1838 | (save-excursion (backward-char) (not (looking-at "[/*]/\\|=>"))) |
| 1839 | (js--looking-at-operator-p) | 1839 | (js--looking-at-operator-p) |
| 1840 | (and (progn (backward-char) | 1840 | (and (progn (backward-char) |
| 1841 | (not (looking-at "+\\+\\|--\\|/[/*]")))))))))) | 1841 | (not (looking-at "+\\+\\|--\\|/[/*]")))))))))) |
| @@ -2064,6 +2064,24 @@ indentation is aligned to that column." | |||
| 2064 | (when comma-p | 2064 | (when comma-p |
| 2065 | (goto-char (1+ declaration-keyword-end)))))))) | 2065 | (goto-char (1+ declaration-keyword-end)))))))) |
| 2066 | 2066 | ||
| 2067 | (defconst js--line-terminating-arrow-re "\\s-*=>\\s-*\\(/[/*]\\|$\\)" | ||
| 2068 | "Regexp matching the last \"=>\" (arrow) token on a line. | ||
| 2069 | Whitespace and comments around the arrow are ignored.") | ||
| 2070 | |||
| 2071 | (defun js--looking-at-broken-arrow-function-p () | ||
| 2072 | "Helper function for `js--proper-indentation'. | ||
| 2073 | Return t if point is at the start of a (possibly async) arrow | ||
| 2074 | function and the last non-comment, non-whitespace token of the | ||
| 2075 | current line is the \"=>\" token." | ||
| 2076 | (when (looking-at "\\s-*async\\s-*") | ||
| 2077 | (goto-char (match-end 0))) | ||
| 2078 | (cond | ||
| 2079 | ((eq (char-after) ?\() | ||
| 2080 | (forward-list) | ||
| 2081 | (looking-at-p js--line-terminating-arrow-re)) | ||
| 2082 | (t (looking-at-p | ||
| 2083 | (concat js--name-re js--line-terminating-arrow-re))))) | ||
| 2084 | |||
| 2067 | (defun js--proper-indentation (parse-status) | 2085 | (defun js--proper-indentation (parse-status) |
| 2068 | "Return the proper indentation for the current line." | 2086 | "Return the proper indentation for the current line." |
| 2069 | (save-excursion | 2087 | (save-excursion |
| @@ -2094,7 +2112,8 @@ indentation is aligned to that column." | |||
| 2094 | (continued-expr-p (js--continued-expression-p))) | 2112 | (continued-expr-p (js--continued-expression-p))) |
| 2095 | (goto-char (nth 1 parse-status)) ; go to the opening char | 2113 | (goto-char (nth 1 parse-status)) ; go to the opening char |
| 2096 | (if (or (not js-indent-align-list-continuation) | 2114 | (if (or (not js-indent-align-list-continuation) |
| 2097 | (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)")) | 2115 | (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)") |
| 2116 | (save-excursion (forward-char) (js--looking-at-broken-arrow-function-p))) | ||
| 2098 | (progn ; nothing following the opening paren/bracket | 2117 | (progn ; nothing following the opening paren/bracket |
| 2099 | (skip-syntax-backward " ") | 2118 | (skip-syntax-backward " ") |
| 2100 | (when (eq (char-before) ?\)) (backward-list)) | 2119 | (when (eq (char-before) ?\)) (backward-list)) |
diff --git a/test/manual/indent/js.js b/test/manual/indent/js.js index df790986947..647d7438f45 100644 --- a/test/manual/indent/js.js +++ b/test/manual/indent/js.js | |||
| @@ -151,6 +151,15 @@ let b = { | |||
| 151 | ` | 151 | ` |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | // bug#25904 | ||
| 155 | foo.bar.baz(very => // A comment | ||
| 156 | very | ||
| 157 | ).biz(([baz={a: [123]}, boz]) => | ||
| 158 | baz | ||
| 159 | ).snarf((snorf) => /* Another comment */ | ||
| 160 | snorf | ||
| 161 | ); | ||
| 162 | |||
| 154 | // Local Variables: | 163 | // Local Variables: |
| 155 | // indent-tabs-mode: nil | 164 | // indent-tabs-mode: nil |
| 156 | // js-indent-level: 2 | 165 | // js-indent-level: 2 |