11

Douglas Crockford のコード規則に従っていますが、Emacs の JS モードで正しい ID を取得できません。モードのインデント オプションをカスタマイズしようとしたり、js3 などの別のモードを試したりしましたが、何も機能しないようです。

括弧があり、式を分割する必要がある場合、Emacs は次のようにインデントします。

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

私が従っている規則では、式が分割されている場合は 4 つのスペースだけを残すべきであると言われています。したがって、インデントは次のようになります。

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

壊れた式のインデントを変更する方法について何か考えはありますか?

4

4 に答える 4

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))))  )

関数の下部に向かって 3 行のコードを変更しました。インデントを 4 文字ではなく 8 文字にしたい場合は、(forward-char 4)それに応じて行を変更してください。

js--proper-indentation(およびjsライブラリには)cl.elライブラリが必要ですが、使用するとこれが台無しになることに注意してくださいeval-after-loadclしたがって、これを機能させるには、.emacs で明示的に require する必要があります。

この「ソリューション」は、指定した状況でのみ 4 スペースのインデントをハード コードし、ネストされたコードをまったく処理しないことに注意してください。しかし、自分の状況に対処するコードのポイントを知ることで、少なくとも、より洗練されたソリューションのために作業が必要な部分を知ることができます。

于 2012-06-12T14:41:59.243 に答える
1

https://github.com/mooz/js2-modeを試すことができます ...フォーク js2-mode ですが、適切なインデントなどの改善点があります...他の方法については、この記事をお読みください: http://mihai.bazon. net/projects/editing-javascript-with-emacs-js2-mode ..しかし、心から古いjs2-modeを置き換えることをお勧めします..いくつかの改善があります https://github.com/mooz/js2-mode/wiki/元のモードからの変更点 ...これがお役に立てば幸いです...

于 2012-06-26T02:18:47.773 に答える
1

https://github.com/thomblake/js3-mode/issuesで js3-mode の機能リクエストを提出できます。

スタイルガイドへのリンクはありますか?

于 2013-06-25T14:17:16.373 に答える
0

ところで、インデントの規則は言語ごとに異なり、好みもユーザーによって異なる場合があります (上記の例のように)。不一致。たとえば、上記のコードは次のように記述できます。

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

また

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

また、ほとんどのインデント スタイルは、インデントの方法についてほぼ一致しています。

于 2012-06-08T13:46:10.193 に答える