Skip to content

Commit 4eab100

Browse files
gh-95511: IDLE - fix Shell context menu copy-with-prompts bug (GH-95512)
If one selects whole lines, as the sidebar makes easy, do not add an extra line. Only move the end of a selection to the beginning of the next line when not already at the beginning of a line. (Also improve the surrounding code.) (cherry picked from commit fc31a13) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent 03fed0a commit 4eab100

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

Lib/idlelib/NEWS.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Released 2023-04-03?
44
=========================
55

66

7+
gh-95511: Fix the Shell context menu copy-with-prompts bug of copying
8+
an extra line when one selects whole lines.
9+
710
gh-95471: Tweak Edit menu. Move 'Select All' above 'Cut' as it is used
811
with 'Cut' and 'Copy' but not 'Paste'. Add a separator between 'Replace'
912
and 'Go to Line' to help IDLE issue triagers.

Lib/idlelib/idle_test/test_sidebar.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def test_copy_with_prompts(self):
733733
first_line = get_end_linenumber(text)
734734
self.do_input(dedent('''\
735735
if True:
736-
print(1)
736+
print(1)
737737
738738
'''))
739739
yield
@@ -744,9 +744,10 @@ def test_copy_with_prompts(self):
744744

745745
selected_lines_text = text.get('sel.first linestart', 'sel.last')
746746
selected_lines = selected_lines_text.split('\n')
747-
# Expect a block of input, a single output line, and a new prompt
747+
selected_lines.pop() # Final '' is a split artifact, not a line.
748+
# Expect a block of input and a single output line.
748749
expected_prompts = \
749-
['>>>'] + ['...'] * (len(selected_lines) - 3) + [None, '>>>']
750+
['>>>'] + ['...'] * (len(selected_lines) - 2) + [None]
750751
selected_text_with_prompts = '\n'.join(
751752
line if prompt is None else prompt + ' ' + line
752753
for prompt, line in zip(expected_prompts,

Lib/idlelib/pyshell.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,19 +1005,17 @@ def copy_with_prompts_callback(self, event=None):
10051005
and/or last lines is selected.
10061006
"""
10071007
text = self.text
1008-
1009-
selection_indexes = (
1010-
self.text.index("sel.first linestart"),
1011-
self.text.index("sel.last +1line linestart"),
1012-
)
1013-
if selection_indexes[0] is None:
1014-
# There is no selection, so do nothing.
1015-
return
1016-
1017-
selected_text = self.text.get(*selection_indexes)
1008+
selfirst = text.index('sel.first linestart')
1009+
if selfirst is None: # Should not be possible.
1010+
return # No selection, do nothing.
1011+
sellast = text.index('sel.last')
1012+
if sellast[-1] != '0':
1013+
sellast = text.index("sel.last+1line linestart")
1014+
1015+
selected_text = self.text.get(selfirst, sellast)
10181016
selection_lineno_range = range(
1019-
int(float(selection_indexes[0])),
1020-
int(float(selection_indexes[1]))
1017+
int(float(selfirst)),
1018+
int(float(sellast))
10211019
)
10221020
prompts = [
10231021
self.shell_sidebar.line_prompts.get(lineno)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the Shell context menu copy-with-prompts bug of copying an extra line
2+
when one selects whole lines.

0 commit comments

Comments
 (0)