簡體   English   中英

Emacs有問題的JavaScript縮進

[英]Emacs problematic JavaScript indentation

我遵循Douglas Crockford的代碼約定 ,但我無法在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 ...它是一個fork js2-mode但是有一些像壓痕這樣的改進...其他方式是閱讀這篇文章: http://mihai.bazon。 net / projects / editing-javascript-with-emacs-js2-mode ..但真誠的是,更好的想法更換舊的js2-mode ..有幾個改進https://github.com/mooz/js2-mode/wiki/從原始模式改變 ...希望這可以幫助你......

您可以在https://github.com/thomblake/js3-mode/issues上以js3模式提交功能請求

你有風格指南的鏈接嗎?

順便說一句,雖然縮進慣例因語言而異,並且用戶之間的偏好甚至可能不同(例如在上述情況下),但是存在相當多的重疊,並且通常有方法編寫代碼使得幾乎沒有分歧。 例如,您的上述代碼可以寫成:

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

要么

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

並且大多數縮進樣式在很大程度上都同意如何縮進它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM