简体   繁体   English

Emacs有问题的JavaScript缩进

[英]Emacs problematic JavaScript indentation

I'm following the Douglas Crockford's code convention , but I can't get the correct identation in JS mode in Emacs. 我遵循Douglas Crockford的代码约定 ,但我无法在Emacs中的JS模式中获得正确的标识。 I tried to customize the indent options of the mode, tried another modes like js3, but nothing seems to work. 我试图自定义模式的缩进选项,尝试了另外的模式,如js3,但似乎没有任何工作。

When I have parenthesis, and I have to break the expression, Emacs indent like this: 当我有括号,我必须打破表达式,Emacs缩进像这样:

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

While the convention that I'm following, says that I should leave just 4 spaces when an expression is broken up. 虽然我正在遵循的惯例,但是当表达式被打破时我应该只留下4个空格。 So the indentation should look like: 因此缩进应如下所示:

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

Any idea of how I can change the indentation on broken up expressions? 知道我怎么能改变分解表达式的缩进?

The behaviour you want to change is hard-coded into a function called js--proper-indentation . 您想要更改的行为被硬编码到一个名为js--proper-indentation的函数中。 An inelegant fix to your problem would be to replace the function in your .emacs: 解决问题的一个不优雅的方法是替换.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))))  )

I have modified three lines of code towards the bottom of the function. 我在函数的底部修改了三行代码。 If you want your indentation to be 8 chars instead of 4, change the (forward-char 4) line accordingly. 如果您希望缩进为8个字符而不是4个字符,请相应地更改(forward-char 4)行。

Note that js--proper-indentation (and the js library) requires the cl.el library, but that using eval-after-load mucks this up. 请注意, js--proper-indentation (和js库)需要cl.el库,但使用eval-after-load会将其搞砸。 So you need to explicitly require cl in your .emacs for this to work. 因此,您需要在.emacs中明确要求cl才能使其正常工作。

Note that this 'solution' hard codes a 4 space indentation only for the situation you indicate, and does not handle nested code at all. 请注意,此“解决方案”仅针对您指定的情况硬编码4空格缩进,并且根本不处理嵌套代码。 But knowing the point in the code that deals with your situation should at least point you towards the bit that needs work for a more sophisticated solution. 但是要知道代码中处理您的情况的重点应该至少指向需要为更复杂的解决方案工作的位。

你可以尝试https://github.com/mooz/js2-mode ...它是一个fork js2-mode但是有一些像压痕这样的改进...其他方式是阅读这篇文章: http://mihai.bazon。 net / projects / editing-javascript-with-emacs-js2-mode ..但真诚的是,更好的想法更换旧的js2-mode ..有几个改进https://github.com/mooz/js2-mode/wiki/从原始模式改变 ...希望这可以帮助你......

You can file a feature request on js3-mode at https://github.com/thomblake/js3-mode/issues 您可以在https://github.com/thomblake/js3-mode/issues上以js3模式提交功能请求

Do you have a link to a style guide? 你有风格指南的链接吗?

BTW, while the indentation conventions vary from language to language, and the preferences can even vary between users (such as in the above case), there is a fair bit of overlap and there are often ways to write your code such that there is little disagreement. 顺便说一句,虽然缩进惯例因语言而异,并且用户之间的偏好甚至可能不同(例如在上述情况下),但是存在相当多的重叠,并且通常有方法编写代码使得几乎没有分歧。 Eg your above code could be written: 例如,您的上述代码可以写成:

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

or 要么

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

and most indentation styles would largely agree on how to indent it. 并且大多数缩进样式在很大程度上都同意如何缩进它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM