From 6bf644ec82f14cceae68278dc35bafb00875efae Mon Sep 17 00:00:00 2001
From: Terry Jan Reedy
Date: Sun, 24 Nov 2019 16:29:29 -0500
Subject: bpo-38862: IDLE Strip Trailing Whitespace fixes end newlines
(GH-17366)
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.
---
Doc/library/idle.rst | 3 +-
Lib/idlelib/NEWS.txt | 3 ++
Lib/idlelib/format.py | 10 ++++
Lib/idlelib/help.html | 25 ++++-----
Lib/idlelib/idle_test/mock_idle.py | 5 +-
Lib/idlelib/idle_test/test_format.py | 62 +++++++++++++---------
.../IDLE/2019-11-23-21-50-57.bpo-38862.KQ9A0m.rst | 2 +
7 files changed, 69 insertions(+), 41 deletions(-)
create mode 100644 Misc/NEWS.d/next/IDLE/2019-11-23-21-50-57.bpo-38862.KQ9A0m.rst
diff --git a/Doc/library/idle.rst b/Doc/library/idle.rst
index 0bd248c..273b583 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 c6aa00d..5eb7739 100644
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -3,6 +3,9 @@ Released on 2020-10-05?
======================================
+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 2b09805..4b57a18 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 0754f24..09dc4c5 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.