unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Requesting patch review
@ 2015-03-08  2:09 Jackson Hamilton
  2015-03-08  8:16 ` Thien-Thi Nguyen
  2015-03-08 17:55 ` Dmitry Gutov
  0 siblings, 2 replies; 10+ messages in thread
From: Jackson Hamilton @ 2015-03-08  2:09 UTC (permalink / raw)
  To: emacs-devel


[-- Attachment #1.1: Type: text/plain, Size: 123 bytes --]

Hi guys,

New patch for js-mode. Adds a new indentation option. I would appreciate
review before merging.

Thanks,
Jackson

[-- Attachment #1.2: Type: text/html, Size: 202 bytes --]

[-- Attachment #2: 0001-New-indentation-option-for-js-mode.patch --]
[-- Type: text/x-patch, Size: 4144 bytes --]

From 4915e411a22eeefc9561639f5cde2bbdf1731c00 Mon Sep 17 00:00:00 2001
From: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
Date: Sat, 7 Mar 2015 18:01:05 -0800
Subject: [PATCH] New indentation option for js-mode

* lisp/progmodes/js.el (js--proper-indentation): Add and use custom
option `js-dynamic-multi-line-declaration-indentation'.
---
 lisp/ChangeLog       |  5 +++++
 lisp/progmodes/js.el | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 0b277c7..2609693 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,8 @@
+2015-03-07  Jackson Ray Hamilton  <jackson@jacksonrayhamilton.com>
+
+	* lisp/progmodes/js.el (js--proper-indentation): Add and use new
+	custom option `js-dynamic-multi-line-declaration-indentation'.
+
 2015-03-07  Stefan Monnier  <monnier@iro.umontreal.ca>
 
 	* battery.el (battery-echo-area-format): Simplify default.
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index d7712e4..2f00da0 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -509,6 +509,37 @@ getting timeout messages."
   :type 'integer
   :group 'js)
 
+(defcustom js-dynamic-multi-line-declaration-indentation nil
+  "Non-nil to indent the first function, array or object literal
+in a multi-line variable declaration specially.
+
+Normally, the first item is unindented, and subsequent items have
+their identifiers lined up against the first:
+
+  var o = {
+      foo: 3
+  };
+
+  var o = {
+      foo: 3
+  },
+      bar = 2;
+
+With this option enabled, the first declaration will be indented
+if there are more declarations, such that the first function,
+array or object lines up nicely with the remaining declarations:
+
+  var o = {
+      foo: 3
+  };
+
+  var o = {
+          foo: 3
+      },
+      bar = 2;"
+  :type 'boolean
+  :group 'js)
+
 ;;; KeyMap
 
 (defvar js-mode-map
@@ -1884,13 +1915,34 @@ In particular, return the buffer position of the first `for' kwd."
 	   ;; "case" and "default".
            (let ((same-indent-p (looking-at "[]})]"))
                  (switch-keyword-p (looking-at "default\\_>\\|case\\_>[^:]"))
-                 (continued-expr-p (js--continued-expression-p)))
+                 (continued-expr-p (js--continued-expression-p))
+                 declaration-keyword-end)
              (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)
+                   (when (and js-dynamic-multi-line-declaration-indentation
+                              ;; Check the condition and
+                              ;; simultaneously preserve the match
+                              ;; data.
+                              (when (looking-at js--declaration-keyword-re)
+                                (setq declaration-keyword-end (match-end 0)))
+                              ;; Determine if the declaration
+                              ;; statement has multiple declarations
+                              ;; (implied if at least one is separated
+                              ;; by a ",").
+                              (save-excursion
+                                (goto-char (nth 1 parse-status))
+                                (when (condition-case nil
+                                          (progn
+                                            (forward-sexp)
+                                            t)
+                                        (error nil))
+                                  (while (forward-comment 1))
+                                  (looking-at ","))))
+                     (goto-char (1+ declaration-keyword-end)))
                    (let* ((in-switch-p (unless same-indent-p
                                          (looking-at "\\_<switch\\_>")))
                           (same-indent-p (or same-indent-p
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-03-10 23:39 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-08  2:09 Requesting patch review Jackson Hamilton
2015-03-08  8:16 ` Thien-Thi Nguyen
2015-03-08 17:55 ` Dmitry Gutov
2015-03-08 22:01   ` Jackson Hamilton
2015-03-09 23:36     ` Dmitry Gutov
2015-03-10  1:34       ` Jackson Hamilton
2015-03-10  3:22         ` Stefan Monnier
2015-03-10 16:04         ` Dmitry Gutov
2015-03-10 23:39         ` Paul Eggert
2015-03-10  0:40     ` Running test/indent examples, Was: " Dmitry Gutov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).