Skip to content

Commit fc31a13

Browse files
authored
gh-95511: IDLE - fix Shell context menu copy-with-prompts bug (#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.)
1 parent d29e279 commit fc31a13

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 on 2022-10-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
@@ -996,19 +996,17 @@ def copy_with_prompts_callback(self, event=None):
996996
and/or last lines is selected.
997997
"""
998998
text = self.text
999-
1000-
selection_indexes = (
1001-
self.text.index("sel.first linestart"),
1002-
self.text.index("sel.last +1line linestart"),
1003-
)
1004-
if selection_indexes[0] is None:
1005-
# There is no selection, so do nothing.
1006-
return
1007-
1008-
selected_text = self.text.get(*selection_indexes)
999+
selfirst = text.index('sel.first linestart')
1000+
if selfirst is None: # Should not be possible.
1001+
return # No selection, do nothing.
1002+
sellast = text.index('sel.last')
1003+
if sellast[-1] != '0':
1004+
sellast = text.index("sel.last+1line linestart")
1005+
1006+
selected_text = self.text.get(selfirst, sellast)
10091007
selection_lineno_range = range(
1010-
int(float(selection_indexes[0])),
1011-
int(float(selection_indexes[1]))
1008+
int(float(selfirst)),
1009+
int(float(sellast))
10121010
)
10131011
prompts = [
10141012
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)