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#15582: notes on js-mode indentation bug Date: Mon, 09 Jan 2017 22:03:59 -0700 Message-ID: <87fukr1p6o.fsf@tromey.com> References: <20131010140648.2d3d1c4a@jcubic> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1484024719 17459 195.159.176.226 (10 Jan 2017 05:05:19 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 10 Jan 2017 05:05:19 +0000 (UTC) Cc: Daniel Colascione To: 15582@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Jan 10 06:05:13 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 1cQocS-0003Nt-CX for geb-bug-gnu-emacs@m.gmane.org; Tue, 10 Jan 2017 06:05:12 +0100 Original-Received: from localhost ([::1]:44782 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cQocT-0000bu-LV for geb-bug-gnu-emacs@m.gmane.org; Tue, 10 Jan 2017 00:05:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56896) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cQocL-0000ag-RR for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2017 00:05:06 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cQocI-0004zZ-M8 for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2017 00:05:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:32932) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cQocI-0004zJ-EW for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2017 00:05:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cQocI-0003pJ-0q for bug-gnu-emacs@gnu.org; Tue, 10 Jan 2017 00:05:02 -0500 X-Loop: help-debbugs@gnu.org In-Reply-To: <20131010140648.2d3d1c4a@jcubic> Resent-From: Tom Tromey Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 10 Jan 2017 05:05:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 15582 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 15582-submit@debbugs.gnu.org id=B15582.148402465714646 (code B ref 15582); Tue, 10 Jan 2017 05:05:01 +0000 Original-Received: (at 15582) by debbugs.gnu.org; 10 Jan 2017 05:04:17 +0000 Original-Received: from localhost ([127.0.0.1]:48331 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQobZ-0003oA-7j for submit@debbugs.gnu.org; Tue, 10 Jan 2017 00:04:17 -0500 Original-Received: from gproxy9-pub.mail.unifiedlayer.com ([69.89.20.122]:52473 helo=gproxy9.mail.unifiedlayer.com) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQobX-0003nx-3V for 15582@debbugs.gnu.org; Tue, 10 Jan 2017 00:04:15 -0500 Original-Received: from cmgw4 (unknown [10.0.90.85]) by gproxy9.mail.unifiedlayer.com (Postfix) with ESMTP id 371771E0AFA for <15582@debbugs.gnu.org>; Mon, 9 Jan 2017 22:04:05 -0700 (MST) Original-Received: from box522.bluehost.com ([74.220.219.122]) by cmgw4 with id WV411u01H2f2jeq01V440w; Mon, 09 Jan 2017 22:04:05 -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=Zx47kWw7-Z4Ii6z4YucA: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=W04XmmF6XtnVAduead8mfEMEkVXqneAfQz4ByCadvyw=; b=W5Hod61TsYKnffabgKWCZqZzo dDcxnAhQwHO+TmQALiZjgFE2g+W2wTuw9cwbojHEnpJwvUIc40zmbRovw/ZVAoRgqt1Ch/oNF78/5 Rj4OUUgGIayB5gkBKElJ/CTigt; Original-Received: from 174-16-146-181.hlrn.qwest.net ([174.16.146.181]:55362 helo=bapiya) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.86_1) (envelope-from ) id 1cQobJ-0003Ze-Hu; Mon, 09 Jan 2017 22:04:01 -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: 1cQobJ-0003Ze-Hu X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 174-16-146-181.hlrn.qwest.net (bapiya) [174.16.146.181]:55362 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:127949 Archived-At: I looked into this bug. With the comment, js--continued-expression-p returns t, causing js--proper-indentation to take this branch of a cond: (continued-expr-p (+ (current-column) (* 2 js-indent-level) js-expr-indent-offset)) Without the comment, this doesn't happen, so I think the problem here is that js--continued-expression-p returns t. Digging into this a bit more, the issue is that js--re-search-backward moves point to the wrong newline, because the newline at the end of the "//" comment is rejected. This patch works by introducing a new js--find-newline-backward that tries to do the right thing for line comments. I've included a new test case (as a patch to a file first appearing in another pending patch), but considering that there aren't any other indentation tests for js-mode, I think some review is necessary. It would be helpful to know when the continued-expr-p branch really is supposed to trigger. Or, if there is an informal corpus of indentation tests, it would be nice to put them all into js-tests.el. Tom diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 1484b79..0551f2a 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -1771,6 +1771,24 @@ js--looking-at-operator-p ;; return NaN anyway. Shouldn't be a problem. (memq (char-before) '(?, ?} ?{)))))))) +(defun js--find-newline-backward () + "Move backward to the nearest newline that is not in a block comment." + (let ((continue t) + (result t)) + (while continue + (setq continue nil) + (if (re-search-backward "\n" nil t) + (let ((parse (syntax-ppss))) + ;; We match the end of a // comment but not a newline in a + ;; block comment. + (when (nth 4 parse) + (goto-char (nth 8 parse)) + ;; If we saw a block comment, keep trying. + (unless (nth 7 parse) + (setq continue t)))) + (setq result nil))) + result)) + (defun js--continued-expression-p () "Return non-nil if the current line continues an expression." (save-excursion @@ -1780,7 +1798,7 @@ js--continued-expression-p (progn (forward-comment (- (point))) (not (memq (char-before) '(?, ?\[ ?\())))) - (and (js--re-search-backward "\n" nil t) + (and (js--find-newline-backward) (progn (skip-chars-backward " \t") (or (bobp) (backward-char)) diff --git a/test/lisp/progmodes/js-tests.el b/test/lisp/progmodes/js-tests.el index 9bf7258..de322f2 100644 --- a/test/lisp/progmodes/js-tests.el +++ b/test/lisp/progmodes/js-tests.el @@ -59,6 +59,16 @@ * Load the inspector's shared head.js for use by tests that need to * open the something or other")))) +(ert-deftest js-mode-indent-bug-15582 () + (with-temp-buffer + (js-mode) + (setq-local js-indent-level 8) + (insert "if (x > 72 &&\n y < 85) { // found\n\t") + (save-excursion (insert "do_something();\n")) + (js-indent-line) + (should (equal (buffer-substring (point-at-bol) (point-at-eol)) + "\tdo_something();")))) + (provide 'js-tests) ;;; js-tests.el ends here