javascript - Emacs 有问题的 JavaScript 缩进

标签 javascript emacs

我正在关注 Douglas Crockford's code convention , 但我无法在 Emacs 的 JS 模式下获得正确的标识。我尝试自定义模式的缩进选项,尝试了其他模式,如 js3,但似乎没有任何效果。

当我有括号并且必须中断表达式时,Emacs 缩进如下:

this.offices.each(this.addOfficesToMap,
                  this);

虽然我遵循的约定是,当表达式被分解时我应该只保留 4 个空格。所以缩进应该是这样的:

this.offices.each(this.addOfficesToMap,
    this);

知道如何更改分解表达式的缩进吗?

最佳答案

您要更改的行为被硬编码到名为 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))))  )

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

请注意,js--proper-indentation(和 js 库)需要 cl.el 库,但使用 eval-after-load 会搞砸。因此,您需要在 .emacs 中明确要求 cl 才能正常工作。

请注意,此“解决方案”仅针对您指定的情况对 4 个空格的缩进进行硬编码,并且根本不处理嵌套代码。但是了解处理您的情况的代码中的要点至少应该将您指向需要为更复杂的解决方案工作的部分。

关于javascript - Emacs 有问题的 JavaScript 缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10906228/

相关文章:

javascript - Viewmodel属性 'links'找不到模型类型

javascript在函数内存储设置变量

JavaScript:函数内的执行顺序

emacs - speedbar-如何修复基本目录(在切换缓冲区时不更改)

regex - 如何在Emacs中修复 "stack overflow in regexp matcher"

javascript - 通过express服务器流式文件上传到s3

javascript - 在 gmail 撰写邮件区域中获取选定/突出显示的文本 html

cocoa - 如何在 .emacs 文件中检测 Cocoa Emacs?

emacs - 防止 Emacs switch-to-buffer-other-window 调整其他窗口的大小

emacs - electric-indent-mode 破坏了我的 Python 代码