From: Terry Jan Reedy
Date: Wed, 27 Nov 2019 01:12:56 +0000 (-0500)
Subject: [3.7] bpo-38862: IDLE Strip Trailing Whitespace fixes end newlines (GH-17366) (...
X-Git-Tag: v3.7.6rc1~25
X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2fb971940b18bcf5a58b1b242b697d0f1d8ad7ef;p=thirdparty%2FPython%2Fcpython.git
[3.7] bpo-38862: IDLE Strip Trailing Whitespace fixes end newlines (GH-17366) (#17379)
Extra newlines are removed at the end of non-shell files. If the file only has newlines after stripping other trailing whitespace, all are removed, as is done by patchcheck.py.
(cherry picked from commit 6bf644ec82f14cceae68278dc35bafb00875efae)
Co-authored-by: Terry Jan Reedy
---
diff --git a/Doc/library/idle.rst b/Doc/library/idle.rst
index 0bd248c22b18..273b5830e429 100644
--- a/Doc/library/idle.rst
+++ b/Doc/library/idle.rst
@@ -199,7 +199,8 @@ Format Paragraph
Strip trailing whitespace
Remove trailing space and other whitespace characters after the last
non-whitespace character of a line by applying str.rstrip to each line,
- including lines within multiline strings.
+ including lines within multiline strings. Except for Shell windows,
+ remove extra newlines at the end of the file.
.. index::
single: Run script
diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt
index 53348ee0af0e..efac152e4d03 100644
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -3,6 +3,9 @@ Released on 2019-12-16?
======================================
+bpo-38862: 'Strip Trailing Whitespace' on the Format menu removes extra
+newlines at the end of non-shell files.
+
bpo-38636: Fix IDLE Format menu tab toggle and file indent width. These
functions (default shortcuts Alt-T and Alt-U) were mistakenly disabled
in 3.7.5 and 3.8.0.
diff --git a/Lib/idlelib/format.py b/Lib/idlelib/format.py
index 2b0980565734..4b57a182c9a4 100644
--- a/Lib/idlelib/format.py
+++ b/Lib/idlelib/format.py
@@ -408,6 +408,16 @@ class Rstrip: # 'Strip Trailing Whitespace" on "Format" menu.
if cut < raw:
text.delete('%i.%i' % (cur, cut), '%i.end' % cur)
+ if (text.get('end-2c') == '\n' # File ends with at least 1 newline;
+ and not hasattr(self.editwin, 'interp')): # & is not Shell.
+ # Delete extra user endlines.
+ while (text.index('end-1c') > '1.0' # Stop if file empty.
+ and text.get('end-3c') == '\n'):
+ text.delete('end-3c')
+ # Because tk indexes are slice indexes and never raise,
+ # a file with only newlines will be emptied.
+ # patchcheck.py does the same.
+
undo.undo_block_stop()
diff --git a/Lib/idlelib/help.html b/Lib/idlelib/help.html
index 0754f2453baf..09dc4c57bcdc 100644
--- a/Lib/idlelib/help.html
+++ b/Lib/idlelib/help.html
@@ -4,7 +4,7 @@
- IDLE — Python 3.9.0a0 documentation
+ IDLE — Python 3.9.0a1 documentation
@@ -17,14 +17,14 @@
-
+
@@ -62,7 +62,7 @@
next |
@@ -240,7 +240,8 @@ paragraph will be formatted to less than N columns, where N defaults to 72.
Strip trailing whitespace
Remove trailing space and other whitespace characters after the last
non-whitespace character of a line by applying str.rstrip to each line,
-including lines within multiline strings.
+including lines within multiline strings. Except for Shell windows,
+remove extra newlines at the end of the file.