Emacs有问题的JavaScript缩进

san*_*bor 11 javascript emacs

我遵循Douglas Crockford的代码约定,但我无法在Emacs中的JS模式中获得正确的标识.我试图自定义模式的缩进选项,尝试了另外的模式,如js3,但似乎没有任何工作.

当我有括号,我必须打破表达式,Emacs缩进像这样:

this.offices.each(this.addOfficesToMap,
                  this);
Run Code Online (Sandbox Code Playgroud)

虽然我正在遵循的惯例,但是当表达式被打破时我应该只留下4个空格.因此缩进应如下所示:

this.offices.each(this.addOfficesToMap,
    this);
Run Code Online (Sandbox Code Playgroud)

知道我怎么能改变分解表达式的缩进?

Tyl*_*ler 5

您想要更改的行为被硬编码到一个名为的函数中js--proper-indentation.解决问题的一个不优雅的方法是替换.emacs中的函数:

(require 'cl)

(eval-after-load "js" '(defun js--proper-indentation (parse-status)
 "Return the proper indentation for the current line."
 (save-excursion
   (back-to-indentation)
   (cond ((nth 4 parse-status)
          (js--get-c-offset 'c (nth 8 parse-status)))
         ((nth 8 parse-status) 0) ; inside string
         ((js--ctrl-statement-indentation))
         ((eq (char-after) ?#) 0)
         ((save-excursion (js--beginning-of-macro)) 4)
         ((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
                                "[]})]\\|\\_<case\\_>\\|\\_<default\\_>"))
                (continued-expr-p (js--continued-expression-p)))
            (goto-char (nth 1 parse-status)) ; go to the opening char
            (if (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)")
                (progn ; nothing following the opening paren/bracket
                  (skip-syntax-backward " ")
                  (when (eq (char-before) ?\)) (backward-list))
                  (back-to-indentation)
                  (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
                            (case (char-after (nth 1 parse-status))
                              (?\( js-paren-indent-offset)
                              (?\[ js-square-indent-offset)
                              (?\{ js-curly-indent-offset))))))
              ;; If there is something following the opening
              ;; paren/bracket, everything else should be indented at
              ;; the same level.

      ;; Modified code here:
              (unless same-indent-p
                (move-beginning-of-line 1)
                (forward-char 4))
      ;; End modified code
              (current-column))))

         ((js--continued-expression-p)
          (+ js-indent-level js-expr-indent-offset))
         (t 0))))  )
Run Code Online (Sandbox Code Playgroud)

我在函数的底部修改了三行代码.如果您希望缩进为8个字符而不是4个字符,请相应地更改该(forward-char 4)行.

请注意js--proper-indentation(和js库)需要cl.el库,但是使用了eval-after-load这个库.因此,您需要cl在.emacs中明确要求此操作.

请注意,此"解决方案"仅针对您指定的情况硬编码4空格缩进,并且根本不处理嵌套代码.但是要知道代码中处理您的情况的重点应该至少指向需要为更复杂的解决方案工作的位.