Skip to content
Browse files

Indent JS code correctly.

  • Loading branch information...
1 parent 92ab867 commit 83d2e9b93050545def588eae0f55727dab28d7a5 @technomancy committed
Showing with 46 additions and 2 deletions.
  1. +46 −2 starter-kit-js.el
View
48 starter-kit-js.el
@@ -20,15 +20,59 @@
(defun js-lambda () (interactive) (insert "function () {\n}")
(backward-char 5))
+ (add-hook 'js2-mode-hook 'coding-hook)
+
(define-key js2-mode-map (kbd "C-c l") 'js-lambda)
(define-key js2-mode-map "\C-\M-h" 'backward-kill-word)
+
+ ;; Fix js2's crazy indentation
(define-key js2-mode-map (kbd "TAB") (lambda () (interactive)
(indent-for-tab-command)
(back-to-indentation)))
- (add-hook 'js2-mode-hook 'coding-hook)
(setq js2-bounce-indent-flag nil
- js2-indent-on-enter-key t)))
+ js2-indent-on-enter-key t)
+
+ (defun js-continued-var-decl-list-p ()
+ "Return non-nil if point is inside a continued variable declaration list."
+ (interactive)
+ (let ((start (save-excursion (js-re-search-backward "\\<var\\>" nil t))))
+ (and start
+ (save-excursion (re-search-backward "\n" start t))
+ (not (save-excursion
+ (js-re-search-backward
+ ";\\|[^, \t][ \t]*\\(/[/*]\\|$\\)" start t))))))
+
+ (defun js-proper-indentation (parse-status)
+ "Return the proper indentation for the current line."
+ (save-excursion
+ (back-to-indentation)
+ (let ((ctrl-stmt-indent (js-ctrl-statement-indentation))
+ (same-indent-p (looking-at "[]})]\\|\\<case\\>\\|\\<default\\>"))
+ (continued-expr-p (js-continued-expression-p)))
+ (cond (ctrl-stmt-indent)
+ ((js-continued-var-decl-list-p)
+ (js-re-search-backward "\\<var\\>" nil t)
+ (+ (current-indentation) js2-basic-offset))
+ ((nth 1 parse-status)
+ (goto-char (nth 1 parse-status))
+ (if (looking-at "[({[][ \t]*\\(/[/*]\\|$\\)")
+ (progn
+ (skip-syntax-backward " ")
+ (when (= (char-before) ?\)) (backward-list))
+ (back-to-indentation)
+ (cond (same-indent-p
+ (current-column))
+ (continued-expr-p
+ (+ (current-column) (* 2 js2-basic-offset)))
+ (t
+ (+ (current-column) js2-basic-offset))))
+ (unless same-indent-p
+ (forward-char)
+ (skip-chars-forward " \t"))
+ (current-column)))
+ (continued-expr-p js2-basic-offset)
+ (t 0)))))))
(defun esk-pp-json ()
"Pretty-print the json object following point."

0 comments on commit 83d2e9b

Please sign in to comment.
Something went wrong with that request. Please try again.