From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tom Tromey Newsgroups: gmane.emacs.bugs Subject: bug#20896: patch to add chained indentation Date: Mon, 09 Jan 2017 23:29:20 -0700 Message-ID: <87tw97zav3.fsf@tromey.com> References: <87a8vnlrxs.fsf@gmx.us> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1484029898 26090 195.159.176.226 (10 Jan 2017 06:31:38 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 10 Jan 2017 06:31:38 +0000 (UTC) Cc: Daniel Colascione To: 20896@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Jan 10 07:31:29 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQpxq-0005GX-PI for geb-bug-gnu-emacs@m.gmane.org; Tue, 10 Jan 2017 07:31:22 +0100 Original-Received: from localhost ([::1]:45137 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cQpxu-0004hO-V2 for geb-bug-gnu-emacs@m.gmane.org; Tue, 10 Jan 2017 01:31:26 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:44054) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cQpwd-0003te-8Q for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2017 01:30:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cQpwa-00030x-3h for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2017 01:30:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:32945) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cQpwZ-000302-Q0 for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2017 01:30:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cQpwZ-0005zt-DY for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2017 01:30:03 -0500 X-Loop: help-debbugs@gnu.org In-Reply-To: <87a8vnlrxs.fsf@gmx.us> Resent-From: Tom Tromey Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 10 Jan 2017 06:30:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 20896 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 20896-submit@debbugs.gnu.org id=B20896.148402977922985 (code B ref 20896); Tue, 10 Jan 2017 06:30:03 +0000 Original-Received: (at 20896) by debbugs.gnu.org; 10 Jan 2017 06:29:39 +0000 Original-Received: from localhost ([127.0.0.1]:48344 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQpwB-0005yf-1b for submit@debbugs.gnu.org; Tue, 10 Jan 2017 01:29:39 -0500 Original-Received: from gproxy5-pub.mail.unifiedlayer.com ([67.222.38.55]:36133) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1cQpw8-0005yP-HC for 20896@debbugs.gnu.org; Tue, 10 Jan 2017 01:29:37 -0500 Original-Received: (qmail 13688 invoked by uid 0); 10 Jan 2017 06:29:27 -0000 Original-Received: from unknown (HELO cmgw4) (10.0.90.85) by gproxy5.mail.unifiedlayer.com with SMTP; 10 Jan 2017 06:29:27 -0000 Original-Received: from box522.bluehost.com ([74.220.219.122]) by cmgw4 with id WWVQ1u0042f2jeq01WVT2H; Mon, 09 Jan 2017 23:29:27 -0700 X-Authority-Analysis: v=2.1 cv=SY5kKJhu c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IgFoBzBjUZAA:10 a=2JzSmF5pz1Lc-0lVTIoA:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Type:MIME-Version:Message-ID:Date:CC:Subject:To:From; bh=LGti+ZQvxbD38hjwV0APJkcm3Ba9ch6dO4bZ1TQYlX8=; b=Nq4XaN4P0FnXlvZmcA1gOYwzo vpzEj/T+mQsasIhbsmyPLt/qveXR8fdrDRYRmJpijk3qomIlM80JRgmHt1W3zlIvqbdqsFnmrMKgt zP5ln+0T+I2WyLmSGzB2G7kx07; Original-Received: from 174-16-146-181.hlrn.qwest.net ([174.16.146.181]:55852 helo=bapiya) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.86_1) (envelope-from ) id 1cQpvw-0001ix-3I; Mon, 09 Jan 2017 23:29:24 -0700 X-Attribution: Tom X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box522.bluehost.com X-AntiAbuse: Original Domain - debbugs.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 174.16.146.181 X-Exim-ID: 1cQpvw-0001ix-3I X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 174-16-146-181.hlrn.qwest.net (bapiya) [174.16.146.181]:55852 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:127950 Archived-At: This patch adds chained indentation, as requested in the bug. It comes with some tests (added to a file that first appears in patch in another bug -- I can commit these when the time comes, mostly I'm interested in review). Tom diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 0551f2a..1211631 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -552,6 +552,20 @@ js-indent-first-init :safe 'symbolp :group 'js) +(defcustom js-chain-indent nil + "Use \"chained\" indentation. +Chained indentation applies when the current line starts with \".\". +If the previous expression also contains a \".\" at the same level, +then the \".\"s will be lined up: + + let x = svg.mumble() + .chained; +" + :version "26.1" + :type 'boolean + :safe 'booleanp + :group 'js) + ;;; KeyMap (defvar js-mode-map @@ -1808,6 +1822,62 @@ js--continued-expression-p (and (progn (backward-char) (not (looking-at "+\\+\\|--\\|/[/*]")))))))))) +(defun js--skip-term-backward () + "Skip a term before point; return t if a term was skipped." + (let ((term-skipped nil)) + ;; Skip backward over balanced parens. + (let ((progress t)) + (while progress + (setq progress nil) + ;; First skip whitespace. + (skip-syntax-backward " ") + ;; Now if we're looking at closing paren, skip to the opener. + ;; This doesn't strictly follow JS syntax, in that we might + ;; skip something nonsensical like "()[]{}", but it is enough + ;; if it works ok for valid input. + (when (memq (char-before) '(?\] ?\) ?\})) + (setq progress t term-skipped t) + (backward-list)))) + ;; Maybe skip over a symbol. + (let ((save-point (point))) + (if (and (< (skip-syntax-backward "w_") 0) + (looking-at js--name-re)) + ;; Skipped. + (progn + (setq term-skipped t) + (skip-syntax-backward " ")) + ;; Did not skip, so restore point. + (goto-char save-point))) + (when (and term-skipped (> (point) (point-min))) + (backward-char) + (eq (char-after) ?.)))) + +(defun js--skip-terms-backward () + "Skip any number of terms backward. +Move point to the earliest \".\" without changing paren levels. +Returns t if successful, nil if no term was found." + (when (js--skip-term-backward) + ;; Found at least one. + (let ((last-point (point))) + (while (js--skip-term-backward) + (setq last-point (point))) + (goto-char last-point) + t))) + +(defun js--chained-expression-p () + "A helper for js--proper-indentation that handles chained expressions. +A chained expression is when the current line starts with '.' and the +previous line also has a '.' expression. +This function returns the indentation for the current line if it is +a chained expression line; otherwise nil. +This should only be called while point is at the start of the line." + (when js-chain-indent + (save-excursion + (when (and (eq (char-after) ?.) + (js--continued-expression-p) + (js--find-newline-backward) + (js--skip-terms-backward)) + (current-column))))) (defun js--end-of-do-while-loop-p () "Return non-nil if point is on the \"while\" of a do-while statement. @@ -1984,6 +2054,7 @@ js--proper-indentation ;; At or after the first loop? (>= (point) beg) (js--array-comp-indentation bracket beg)))) + ((js--chained-expression-p)) ((js--ctrl-statement-indentation)) ((js--multi-line-declaration-indentation)) ((nth 1 parse-status) diff --git a/test/lisp/progmodes/js-tests.el b/test/lisp/progmodes/js-tests.el index de322f2..effd58c 100644 --- a/test/lisp/progmodes/js-tests.el +++ b/test/lisp/progmodes/js-tests.el @@ -69,6 +69,77 @@ (should (equal (buffer-substring (point-at-bol) (point-at-eol)) "\tdo_something();")))) +(ert-deftest js-mode-indent-bug-20896-chain () + (with-temp-buffer + (js-mode) + (setq-local js-chain-indent t) + (setq-local indent-tabs-mode nil) + (insert "let x = svg.mumble()\n.zzz") + (js-indent-line) + (should (equal (buffer-substring (point-at-bol) (point-at-eol)) + " .zzz")))) + +(ert-deftest js-mode-indent-bug-20896-chain-comment () + (with-temp-buffer + (js-mode) + (setq-local js-chain-indent t) + (setq-local indent-tabs-mode nil) + (insert "let x = svg.mumble() // line comment\n.zzz") + (js-indent-line) + (should (equal (buffer-substring (point-at-bol) (point-at-eol)) + " .zzz")))) + +(ert-deftest js-mode-indent-bug-20896-chain-multi () + (with-temp-buffer + (js-mode) + (setq-local js-chain-indent t) + (setq-local indent-tabs-mode nil) + ;; Must line up to the first "." at the same level. + (insert "let x = svg.selectAll().something()\n.zzz") + (js-indent-line) + (should (equal (buffer-substring (point-at-bol) (point-at-eol)) + " .zzz")))) + +(ert-deftest js-mode-indent-bug-20896-chain-nested () + (with-temp-buffer + (js-mode) + (setq-local js-chain-indent t) + (setq-local indent-tabs-mode nil) + ;; Must line up to the first "." at the same level. + (insert "let x = svg.selectAll(d3.svg.something()\n.zzz") + (js-indent-line) + (should (equal (buffer-substring (point-at-bol) (point-at-eol)) + " .zzz")))) + +(ert-deftest js-mode-indent-bug-20896-no-chain-1 () + (with-temp-buffer + (js-mode) + ;; Don't set js-chain-indent. + (insert "let x = svg.mumble()\n.zzz") + (js-indent-line) + (should (equal (buffer-substring (point-at-bol) (point-at-eol)) + " .zzz")))) + +(ert-deftest js-mode-indent-bug-20896-no-chain-2 () + (with-temp-buffer + (js-mode) + (setq-local js-chain-indent t) + ;; Nothing to chain to. + (insert "let x = svg()\n.zzz") + (js-indent-line) + (should (equal (buffer-substring (point-at-bol) (point-at-eol)) + " .zzz")))) + +(ert-deftest js-mode-indent-bug-20896-no-chain-2 () + (with-temp-buffer + (js-mode) + (setq-local js-chain-indent t) + ;; Nothing to chain to. + (insert "let x = svg().mumble.x() + 73\n.zzz") + (js-indent-line) + (should (equal (buffer-substring (point-at-bol) (point-at-eol)) + " .zzz")))) + (provide 'js-tests) ;;; js-tests.el ends here