Skip to content
Prev Previous commit
Next Next commit
Remove newline addition.
  • Loading branch information
terryjreedy committed Nov 24, 2019
commit d13fc65b9c34ceae9ced6f8735ee9aab5db252b1
22 changes: 9 additions & 13 deletions Lib/idlelib/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,15 @@ def do_rstrip(self, event=None):
if cut < raw:
text.delete('%i.%i' % (cur, cut), '%i.end' % cur)

# 'Empty' file has tk guard newline at index 'end-1c' = '1.0'.
# Only adjust ending newlines for non-empty non-shell window.
if (text.index('end-1c') > '1.0'
and not hasattr(self.editwin, 'interp')):
if text.get('end-2c') != '\n': # No user endline, add one.
text.insert('end-1c', '\n')
else: # Delete extra user endlines.
while (text.index('end-1c') > '1.0' # Stop when 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.
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()

Expand Down
14 changes: 7 additions & 7 deletions Lib/idlelib/idle_test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,15 +651,15 @@ def test_rstrip_lines(self):

def test_rstrip_end(self):
text = self.text
for t in ('', '\n', '\n\n\n'):
with self.subTest(t=t):
text.insert('1.0', t)
for code in ('', '\n', '\n\n\n'):
with self.subTest(code=code):
text.insert('1.0', code)
self.do_rstrip()
self.assertEqual(text.get('1.0','end-1c'), '')
for t in ('a', 'a\n', 'a\n\n', 'a\n\n\n'):
with self.subTest(t=t):
text.delete('1.0', 'end')
text.insert('1.0', t)
for code in ('a\n', 'a\n\n', 'a\n\n\n'):
with self.subTest(code=code):
text.delete('1.0', 'end-1c')
text.insert('1.0', code)
self.do_rstrip()
self.assertEqual(text.get('1.0','end-1c'), 'a\n')

Expand Down