diff options
author | Tal Einat <taleinat+github@gmail.com> | 2018-12-24 14:05:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-24 14:05:51 +0200 |
commit | 44a79cc5b3d1fb0c03c99077aa26def85ec26c67 (patch) | |
tree | 6f1cb81c1acec007ff2ea6c8e823b24241e4a5e1 /Lib/idlelib/squeezer.py | |
parent | e7eed78f04085e6cded0cebcc715364b05e8a71b (diff) | |
download | cpython-git-44a79cc5b3d1fb0c03c99077aa26def85ec26c67.tar.gz |
bpo-35208: Fix IDLE Squeezer line counting (GH-10449)
Diffstat (limited to 'Lib/idlelib/squeezer.py')
-rw-r--r-- | Lib/idlelib/squeezer.py | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/Lib/idlelib/squeezer.py b/Lib/idlelib/squeezer.py index f5aac813a1..8960356799 100644 --- a/Lib/idlelib/squeezer.py +++ b/Lib/idlelib/squeezer.py @@ -46,6 +46,14 @@ def count_lines_with_wrapping(s, linewidth=80, tabwidth=8): # deal with tab or newline if s[pos] == '\n': + # Avoid the `current_column == 0` edge-case, and while we're at it, + # don't bother adding 0. + if current_column > linewidth: + # If the current column was exactly linewidth, divmod would give + # (1,0), even though a new line hadn't yet been started. The same + # is true if length is any exact multiple of linewidth. Therefore, + # subtract 1 before dividing a non-empty line. + linecount += (current_column - 1) // linewidth linecount += 1 current_column = 0 else: @@ -60,17 +68,6 @@ def count_lines_with_wrapping(s, linewidth=80, tabwidth=8): pos += 1 # after the tab or newline - # avoid divmod(-1, linewidth) - if current_column > 0: - # If the length was exactly linewidth, divmod would give (1,0), - # even though a new line hadn't yet been started. The same is true - # if length is any exact multiple of linewidth. Therefore, subtract - # 1 before doing divmod, and later add 1 to the column to - # compensate. - lines, column = divmod(current_column - 1, linewidth) - linecount += lines - current_column = column + 1 - # process remaining chars (no more tabs or newlines) current_column += len(s) - pos # avoid divmod(-1, linewidth) @@ -106,7 +103,8 @@ class ExpandingButton(tk.Button): # before the iomark self.base_text = editwin.per.bottom - button_text = "Squeezed text (%d lines)." % self.numoflines + line_plurality = "lines" if numoflines != 1 else "line" + button_text = f"Squeezed text ({numoflines} {line_plurality})." tk.Button.__init__(self, text, text=button_text, background="#FFFFC0", activebackground="#FFFFE0") |