The code that handles this is the function js--proper-indentation
, based on analysis from syntax-ppss
.
Easiest solution (if you can tolerate it) is to write your code like
console.log(
'Example',
var1,
var2);
Otherwise, you're going to have to modify the code. (I see no customization option in js.el
.)
Specifically, you need to do something different from this:
;; If there is something following the opening
;; paren/bracket, everything else should be indented at
;; the same level.
(unless same-indent-p
(forward-char)
(skip-chars-forward " \t"))
(current-column))))
If you feel adventuresome, you might try modifying that function. In very brief experimentation, this seemed to work:
Find the conditional that begins with this line
(if (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)")
If you remove the if
(that whole line above) and the s-expressions that are the else
part of the if
:
;; If there is something following the opening
;; paren/bracket, everything else should be indented at
;; the same level.
(unless same-indent-p
(forward-char)
(skip-chars-forward " \t"))
(current-column)) ;; <- that is the end of the "if"
)) ;; keep those last two close parens.
then, I think you will just get your normal indention level for lines after the start of a function. That worked for me.
Here is my modified defun
which you can use as :override
advice:
(defun my-js--proper-indentation (parse-status)
"Return the proper indentation for the current line."
(save-excursion
(back-to-indentation)
(cond ((nth 4 parse-status) ; inside comment
(js--get-c-offset 'c (nth 8 parse-status)))
((nth 3 parse-status) 0) ; inside string
((eq (char-after) ?#) 0)
((save-excursion (js--beginning-of-macro)) 4)
;; Indent array comprehension continuation lines specially.
((let ((bracket (nth 1 parse-status))
beg)
(and bracket
(not (js--same-line bracket))
(setq beg (js--indent-in-array-comp bracket))
;; At or after the first loop?
(>= (point) beg)
(js--array-comp-indentation bracket beg))))
((js--ctrl-statement-indentation))
((js--multi-line-declaration-indentation))
((nth 1 parse-status)
;; A single closing paren/bracket should be indented at the
;; same level as the opening statement. Same goes for
;; "case" and "default".
(let ((same-indent-p (looking-at "[]})]"))
(switch-keyword-p (looking-at "default\\_>\\|case\\_>[^:]"))
(continued-expr-p (js--continued-expression-p)))
(goto-char (nth 1 parse-status)) ; go to the opening char
(progn ; nothing following the opening paren/bracket
(skip-syntax-backward " ")
(when (eq (char-before) ?\)) (backward-list))
(back-to-indentation)
(js--maybe-goto-declaration-keyword-end parse-status)
(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 js-indent-level)
js-expr-indent-offset))
(t
(+ (current-column) js-indent-level
(pcase (char-after (nth 1 parse-status))
(?\( js-paren-indent-offset)
(?\[ js-square-indent-offset)
(?\{ js-curly-indent-offset)))))))
(if in-switch-p
(+ indent js-switch-indent-offset)
indent)))))
((js--continued-expression-p)
(+ js-indent-level js-expr-indent-offset))
(t 0))))
(advice-add 'js--proper-indentation :override 'my-js--proper-indentation)