unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Felix H. Dahlke" <fhd@ubercode.de>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: 8576@debbugs.gnu.org
Subject: bug#8576: 23.2; js-mode doesn't support multi-line variable declarations
Date: Tue, 17 Jul 2012 06:37:31 +0200	[thread overview]
Message-ID: <5004EC0B.5070707@ubercode.de> (raw)
In-Reply-To: <5004E847.2070000@yandex.ru>


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

On 07/17/2012 06:21 AM, Dmitry Gutov wrote:
> You shouldn't add the `js--indent-operator-re' constant, though, it's
> already present in js-mode.

Oh, you're right, thanks. I've updated the gists and attached a new patch.

>> The only solution I can think of would be to go back up after typing a
>> closing brace followed by a comma and indent the preceeding lines.
>> Some modes do that, but it would require drastic changes to js.el.
>> I'd rather get this patch in first and see if the issue annoys me (or
>> anyone else) enough to work further on this.
> 
> I don't think this will be much of a problem.

Probably not, but I fear it's going to be a lot of work - at least when
I do it.

> There is an alternative approach, though: one js2-mode user requested
> that when the first value in the declaration is a function/object/array,
> it should always be indented: https://github.com/mooz/js2-mode/issues/3
> 
> That option probably won't be very popular.

Yes, I thought about suggesting that. But considering that that makes
single variable declarations of functions/object literals look weird, I
figured that wouldn't be desirable. I saw that you can configure this in
j2-mode, perhaps that's an option. But I'd personally prefer the clever
solution.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: indent-multi-line-var-statements.patch --]
[-- Type: text/x-patch; name="indent-multi-line-var-statements.patch", Size: 2925 bytes --]

=== modified file 'lisp/progmodes/js.el'
--- lisp/progmodes/js.el	2012-07-11 23:13:41 +0000
+++ lisp/progmodes/js.el	2012-07-17 04:29:06 +0000
@@ -1675,12 +1675,15 @@
      "each"))
   "Regexp matching keywords optionally followed by an opening brace.")
 
+(defconst js--declaration-keyword-re
+  (regexp-opt '("var" "let" "const") 'words)
+  "Regular expression matching variable declaration keywords.")
+
 (defconst js--indent-operator-re
   (concat "[-+*/%<>=&^|?:.]\\([^-+*/]\\|$\\)\\|"
           (js--regexp-opt-symbol '("in" "instanceof")))
   "Regexp matching operators that affect indentation of continued expressions.")
 
-
 (defun js--looking-at-operator-p ()
   "Return non-nil if point is on a JavaScript operator, other than a comma."
   (save-match-data
@@ -1759,6 +1762,36 @@
          (list (cons 'c js-comment-lineup-func))))
     (c-get-syntactic-indentation (list (cons symbol anchor)))))
 
+(defun js--multi-line-declaration-indentation ()
+  "Helper function for `js--proper-indentation'.
+Return the proper indentation of the current line if it belongs to a declaration
+statement spanning multiple lines; otherwise, return nil."
+  (let (at-opening-bracket)
+    (save-excursion
+      (back-to-indentation)
+      (when (not (looking-at js--declaration-keyword-re))
+        (when (looking-at js--indent-operator-re)
+          (goto-char (match-end 0)))
+        (while (and (not at-opening-bracket)
+                    (not (bobp))
+                    (let ((pos (point)))
+                      (save-excursion
+                        (js--backward-syntactic-ws)
+                        (or (eq (char-before) ?,)
+                            (and (not (eq (char-before) ?\;))
+                                 (prog2
+                                     (skip-chars-backward "[[:punct:]]")
+                                     (looking-at js--indent-operator-re)
+                                   (js--backward-syntactic-ws))
+                                 (not (eq (char-before) ?\;)))
+                            (and (>= pos (point-at-bol))
+                                 (<= pos (point-at-eol)))))))
+          (condition-case err
+              (backward-sexp)
+            (scan-error (setq at-opening-bracket t))))
+        (when (looking-at js--declaration-keyword-re)
+          (+ (current-indentation) js-indent-level))))))
+
 (defun js--proper-indentation (parse-status)
   "Return the proper indentation for the current line."
   (save-excursion
@@ -1767,6 +1800,7 @@
            (js--get-c-offset 'c (nth 8 parse-status)))
           ((nth 8 parse-status) 0) ; inside string
           ((js--ctrl-statement-indentation))
+          ((js--multi-line-declaration-indentation))
           ((eq (char-after) ?#) 0)
           ((save-excursion (js--beginning-of-macro)) 4)
           ((nth 1 parse-status)


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

  reply	other threads:[~2012-07-17  4:37 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-28  7:22 bug#8576: 23.2; js-mode doesn't support multi-line variable declarations Felix H. Dahlke
2011-06-19 20:38 ` Daniel Colascione
2012-02-15 12:32   ` Felix H. Dahlke
2012-02-15 18:58     ` Stefan Monnier
2012-02-15 19:04       ` Glenn Morris
2012-02-15 19:25       ` Daniel Colascione
2012-02-15 20:41         ` Stefan Monnier
2012-04-04 11:26         ` Felix H. Dahlke
2012-06-01  7:30           ` Felix H. Dahlke
     [not found]           ` <mailman.2065.1338535901.855.bug-gnu-emacs@gnu.org>
2012-07-05 14:53             ` jaseemabid
2012-02-24  2:16 ` Dmitry Gutov
2012-03-03  1:46 ` bug#8576: " Zeth
2012-03-03  4:51   ` Daniel Colascione
2012-06-07 23:04 ` bug#8576: 23.2; " Dmitry Gutov
2012-06-08  3:13   ` Felix H. Dahlke
2012-07-17  3:16   ` Felix H. Dahlke
2012-07-05 23:02 ` bug#8576: 23.2; , " Dmitry Gutov
2012-07-06  0:52 ` Dmitry Gutov
     [not found] ` <mailman.4157.1341536766.855.bug-gnu-emacs@gnu.org>
2012-07-06  1:23   ` jaseemabid
2012-07-17  4:21 ` bug#8576: 23.2; " Dmitry Gutov
2012-07-17  4:37   ` Felix H. Dahlke [this message]
2012-07-17  5:00 ` Dmitry Gutov
2012-07-17  6:32   ` Stefan Monnier
2012-07-17  8:24     ` Felix H. Dahlke
2012-07-17  9:50       ` Stefan Monnier
2012-07-17  9:51         ` Felix H. Dahlke
2012-07-17 17:33         ` Felix H. Dahlke
2013-01-10  3:48           ` Felix H. Dahlke
2013-01-11 23:25             ` Stefan Monnier
2013-01-12  2:59               ` Felix H. Dahlke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5004EC0B.5070707@ubercode.de \
    --to=fhd@ubercode.de \
    --cc=8576@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).