Skip to content

Commit 44a79cc

Browse files
authored
bpo-35208: Fix IDLE Squeezer line counting (GH-10449)
1 parent e7eed78 commit 44a79cc

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

Lib/idlelib/NEWS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Released on 2019-10-20?
33
======================================
44

55

6+
bpo-35208: Squeezer now counts wrapped lines before newlines.
7+
68
bpo-35555: Gray out Code Context menu entry when it's not applicable.
79

810
bpo-22703: Improve the Code Context and Zoom Height menu labels.

Lib/idlelib/idle_test/test_squeezer.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import namedtuple
2+
from textwrap import dedent
23
from tkinter import Text, Tk
34
import unittest
45
from unittest.mock import Mock, NonCallableMagicMock, patch, sentinel, ANY
@@ -77,6 +78,28 @@ def test_tab_width(self):
7778
self.check(expected=3, text='\t' * 6, linewidth=11, tabwidth=4)
7879
self.check(expected=2, text='\t' * 6, linewidth=13, tabwidth=4)
7980

81+
def test_empty_lines(self):
82+
self.check(expected=1, text='\n', linewidth=80, tabwidth=8)
83+
self.check(expected=2, text='\n\n', linewidth=80, tabwidth=8)
84+
self.check(expected=10, text='\n' * 10, linewidth=80, tabwidth=8)
85+
86+
def test_long_line(self):
87+
self.check(expected=3, text='a' * 200, linewidth=80, tabwidth=8)
88+
self.check(expected=3, text='a' * 200 + '\n', linewidth=80, tabwidth=8)
89+
90+
def test_several_lines_different_lengths(self):
91+
text = dedent("""\
92+
13 characters
93+
43 is the number of characters on this line
94+
95+
7 chars
96+
13 characters""")
97+
self.check(expected=5, text=text, linewidth=80, tabwidth=8)
98+
self.check(expected=5, text=text + '\n', linewidth=80, tabwidth=8)
99+
self.check(expected=6, text=text, linewidth=40, tabwidth=8)
100+
self.check(expected=7, text=text, linewidth=20, tabwidth=8)
101+
self.check(expected=11, text=text, linewidth=10, tabwidth=8)
102+
80103

81104
class SqueezerTest(unittest.TestCase):
82105
"""Tests for the Squeezer class."""

Lib/idlelib/squeezer.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ def count_lines_with_wrapping(s, linewidth=80, tabwidth=8):
4646

4747
# deal with tab or newline
4848
if s[pos] == '\n':
49+
# Avoid the `current_column == 0` edge-case, and while we're at it,
50+
# don't bother adding 0.
51+
if current_column > linewidth:
52+
# If the current column was exactly linewidth, divmod would give
53+
# (1,0), even though a new line hadn't yet been started. The same
54+
# is true if length is any exact multiple of linewidth. Therefore,
55+
# subtract 1 before dividing a non-empty line.
56+
linecount += (current_column - 1) // linewidth
4957
linecount += 1
5058
current_column = 0
5159
else:
@@ -60,17 +68,6 @@ def count_lines_with_wrapping(s, linewidth=80, tabwidth=8):
6068

6169
pos += 1 # after the tab or newline
6270

63-
# avoid divmod(-1, linewidth)
64-
if current_column > 0:
65-
# If the length was exactly linewidth, divmod would give (1,0),
66-
# even though a new line hadn't yet been started. The same is true
67-
# if length is any exact multiple of linewidth. Therefore, subtract
68-
# 1 before doing divmod, and later add 1 to the column to
69-
# compensate.
70-
lines, column = divmod(current_column - 1, linewidth)
71-
linecount += lines
72-
current_column = column + 1
73-
7471
# process remaining chars (no more tabs or newlines)
7572
current_column += len(s) - pos
7673
# avoid divmod(-1, linewidth)
@@ -106,7 +103,8 @@ def __init__(self, s, tags, numoflines, squeezer):
106103
# before the iomark
107104
self.base_text = editwin.per.bottom
108105

109-
button_text = "Squeezed text (%d lines)." % self.numoflines
106+
line_plurality = "lines" if numoflines != 1 else "line"
107+
button_text = f"Squeezed text ({numoflines} {line_plurality})."
110108
tk.Button.__init__(self, text, text=button_text,
111109
background="#FFFFC0", activebackground="#FFFFE0")
112110

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Squeezer now properly counts wrapped lines before newlines.

0 commit comments

Comments
 (0)