Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I tried to customize the indent options of the mode, tried another modes like js3, but nothing seems to work.

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?

like image 664
sanbor Avatar asked Jun 05 '12 23:06

sanbor


2 Answers

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

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.

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.

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.

like image 184
Tyler Avatar answered Oct 01 '22 00:10

Tyler


you can try https://github.com/mooz/js2-mode ...it's a fork js2-mode but with some impovements like good indentation...other way is read this article: http://mihai.bazon.net/projects/editing-javascript-with-emacs-js2-mode .. but sincerely it's better idea replace the old js2-mode ..it has several improvements https://github.com/mooz/js2-mode/wiki/Changes-from-the-original-mode ...hope this can help you...

like image 33
clagccs Avatar answered Sep 30 '22 23:09

clagccs