unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Hong Xu <hong@topbug.net>
To: npostavs@users.sourceforge.net
Cc: matthew@woodcraft.me.uk, 22661@debbugs.gnu.org
Subject: bug#22661: 25.0.91; python.el electric-indent misbehaviour with 'else:' and nested 'if:'
Date: Sun, 22 Jan 2017 16:50:22 -0800	[thread overview]
Message-ID: <3defc94b-3d38-95ec-1d18-e272a412f5fc@topbug.net> (raw)
In-Reply-To: <87fukavgks.fsf@users.sourceforge.net>


[-- Attachment #1.1.1: Type: text/plain, Size: 623 bytes --]

On 01/22/2017 03:08 PM, npostavs@users.sourceforge.net wrote:
> 
> I think it's better to maximize the range of save-match-data, i.e., have
> it cover as much code as practical between the place that sets the match
> data and the place that uses the it, rather than minimizing the range so
> that it only covers functions which modify match data (because the
> default assumption in Emacs is that functions may modify match data
> unless they say otherwise).  In this case, I would put it around the
> `and` in the line above.
> 

Thanks. I've attached a newer version with tests with all the issues fixed.



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.1.2: 0001-python-mode-Fix-detection-for-opening-blocks.patch --]
[-- Type: text/x-patch; name="0001-python-mode-Fix-detection-for-opening-blocks.patch", Size: 3938 bytes --]

From 1f2108514c14339ff2db5fea2d1b35c3538b2efd Mon Sep 17 00:00:00 2001
From: Hong Xu <hong@topbug.net>
Date: Mon, 12 Dec 2016 17:55:25 -0800
Subject: [PATCH] python-mode: Fix detection for opening blocks.

	* python.el (python-info-dedenter-opening-block-positions): There
	can't be any back-indented lines between an opening block and the
	current line.

	* python-tests.el (python-indent-electric-colon-4): Add an indent
	test case where there is one-more indented previous opening block.
---
 lisp/progmodes/python.el            | 20 ++++++++++++++++++--
 test/lisp/progmodes/python-tests.el | 21 +++++++++++++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d8262dd0a750..90b5e4e0dc67 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -4693,7 +4693,8 @@ python-info-dedenter-opening-block-positions
     (let ((dedenter-pos (python-info-dedenter-statement-p)))
       (when dedenter-pos
         (goto-char dedenter-pos)
-        (let* ((pairs '(("elif" "elif" "if")
+        (let* ((cur-line (line-beginning-position))
+               (pairs '(("elif" "elif" "if")
                         ("else" "if" "elif" "except" "for" "while")
                         ("except" "except" "try")
                         ("finally" "else" "except" "try")))
@@ -4709,7 +4710,22 @@ python-info-dedenter-opening-block-positions
               (let ((indentation (current-indentation)))
                 (when (and (not (memq indentation collected-indentations))
                            (or (not collected-indentations)
-                               (< indentation (apply #'min collected-indentations))))
+                               (< indentation (apply #'min collected-indentations)))
+                           ;; There must be no line with indentation
+                           ;; smaller than `indentation' (except for
+                           ;; blank lines) between the found opening
+                           ;; block and the current line, otherwise it
+                           ;; is not an opening block.
+                           (save-excursion
+                             (forward-line)
+                             (let ((no-back-indent t))
+                               (save-match-data
+                                 (while (and (< (point) cur-line)
+                                             (setq no-back-indent
+                                                   (or (> (current-indentation) indentation)
+                                                       (python-info-current-line-empty-p))))
+                                   (forward-line)))
+                               no-back-indent)))
                   (setq collected-indentations
                         (cons indentation collected-indentations))
                   (when (member (match-string-no-properties 0)
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index 2df1bbf50d81..158c52f080c4 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -1156,6 +1156,27 @@ python-tests-visible-string
    (python-tests-look-at "that)")
    (should (= (current-indentation) 6))))
 
+(ert-deftest python-indent-electric-colon-4 ()
+  "Test indentation case where there is one more-indented previous open block."
+  (python-tests-with-temp-buffer
+   "
+def f():
+    if True:
+        a = 5
+
+        if True:
+            a = 10
+
+        b = 3
+
+else
+"
+   (python-tests-look-at "else")
+   (goto-char (line-end-position))
+   (python-tests-self-insert ":")
+   (python-tests-look-at "else" -1)
+   (should (= (current-indentation) 4))))
+
 (ert-deftest python-indent-region-1 ()
   "Test indentation case from Bug#18843."
   (let ((contents "
-- 
2.1.4


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

  reply	other threads:[~2017-01-23  0:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-14 17:09 bug#22661: 25.0.91; python.el electric-indent misbehaviour with 'else:' and nested 'if:' Matthew Woodcraft
2016-12-13  2:01 ` Hong Xu
2017-01-21 17:34   ` Matthew Woodcraft
2017-01-22  2:08     ` Hong Xu
2017-01-22 22:34       ` Matthew Woodcraft
2017-01-22 23:08       ` npostavs
2017-01-23  0:50         ` Hong Xu [this message]
2017-01-24  1:24           ` npostavs
2017-01-27  1:20             ` npostavs

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=3defc94b-3d38-95ec-1d18-e272a412f5fc@topbug.net \
    --to=hong@topbug.net \
    --cc=22661@debbugs.gnu.org \
    --cc=matthew@woodcraft.me.uk \
    --cc=npostavs@users.sourceforge.net \
    /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).