Emacs有问题的JavaScript缩进 [英] Emacs problematic JavaScript indentation

查看:131
本文介绍了Emacs有问题的JavaScript缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循道格拉斯Crockford的代码约定,但是我无法在JS模式下获得正确的身份Emacs的。我试图自定义模式的缩进选项,尝试像js3这样的其他模式,但似乎没有任何效果。

I'm following the Douglas Crockford's code convention, but I can't get the correct identation in JS mode in Emacs. I tried to customize the indent options of the mode, tried another modes like js3, but nothing seems to work.

当我有括号,我必须打破表达式,Emacs缩进如下:

When I have parenthesis, and I have to break the expression, Emacs indent like this:

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. 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?

推荐答案

您想要更改的行为被硬编码为名为的函数js - 适当缩进。对你的问题的一个不恰当的修复将是替换.emacs中的函数:

The behaviour you want to change is hard-coded into a function called js--proper-indentation. An inelegant fix to your problem would be to replace the function in your .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)行。

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.

请注意, js - 适当缩进(和js库)需要cl.el库,但使用 eval-after-load 这样做。所以你需要明确地要求你的.emacs中的 cl 这个工作。

Note that js--proper-indentation (and the js library) requires the cl.el library, but that using eval-after-load mucks this up. So you need to explicitly require cl in your .emacs for this to work.

请注意,这个'硬编码一个4空格缩进,仅针对您指出的情况,并且根本不处理嵌套代码。但是,了解处理你的情况的代码中的一点应该至少指出你需要为更复杂的解决方案而工作的一点。

Note that this 'solution' hard codes a 4 space indentation only for the situation you indicate, and does not handle nested code at all. 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.

这篇关于Emacs有问题的JavaScript缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆