Emacs 有问题的 JavaScript 缩进

Emacs problematic JavaScript indentation

本文关键字:缩进 JavaScript 有问题 Emacs      更新时间:2023-09-26

我遵循道格拉斯·克罗克福德的代码约定,但我无法在 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 空格缩进,并且根本不处理嵌套代码。但是,了解代码中处理您的情况的要点至少应该将您指向需要为更复杂的解决方案工作的地方。

你可以试试 https://github.com/mooz/js2-mode...这是一个分支 JS2 模式,但有一些改进,比如良好的缩进......另一种方法是阅读本文:http://mihai.bazon.net/projects/editing-javascript-with-emacs-js2-mode..但真诚地,最好更换旧的 JS2 模式..它有几个改进 https://github.com/mooz/js2-mode/wiki/Changes-from-the-original-mode...希望这能帮助你...

您可以在 js3-mode 上提交功能请求,网址为 https://github.com/thomblake/js3-mode/issues

你有风格指南的链接吗?

顺便说一句,虽然缩进约定因语言而异,甚至用户之间的偏好也会有所不同(例如在上述情况下),但存在相当多的重叠,并且通常有一些方法可以编写代码,以便几乎没有分歧。例如,可以编写上面的代码:

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

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

大多数缩进样式在很大程度上都同意如何缩进它。