Skip to content

gh-95191: IDLE: Include prompts when saving Shell #95554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/idlelib/NEWS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Released on 2022-10-03
=========================


gh-95191: Include prompts when saving Shell (interactive input/output).

gh-95511: Fix the Shell context menu copy-with-prompts bug of copying
an extra line when one selects whole lines.

Expand Down
13 changes: 10 additions & 3 deletions Lib/idlelib/idle_test/test_iomenu.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"Test , coverage 17%."

from idlelib import iomenu, util
from idlelib import iomenu
import unittest
from test.support import requires
from tkinter import Tk
from idlelib.editor import EditorWindow
from idlelib import util
from idlelib.idle_test.mock_idle import Func


class IOBindingTest(unittest.TestCase):
Expand Down Expand Up @@ -36,9 +38,14 @@ def test_fixnewlines_end(self):
io = self.io
fix = io.fixnewlines
text = io.editwin.text

# Make the editor temporarily look like Shell.
self.editwin.interp = None
eq(fix(), '')
del self.editwin.interp
shelltext = '>>> if 1'
self.editwin.get_prompt_text = Func(result=shelltext)
eq(fix(), shelltext) # Get... call and '\n' not added.
del self.editwin.interp, self.editwin.get_prompt_text

text.insert(1.0, 'a')
eq(fix(), 'a'+io.eol_convention)
eq(text.get('1.0', 'end-1c'), 'a\n')
Expand Down
16 changes: 11 additions & 5 deletions Lib/idlelib/iomenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,17 @@ def writefile(self, filename):
return False

def fixnewlines(self):
"Return text with final \n if needed and os eols."
if (self.text.get("end-2c") != '\n'
and not hasattr(self.editwin, "interp")): # Not shell.
self.text.insert("end-1c", "\n")
text = self.text.get("1.0", "end-1c")
"""Return text with os eols.

Add prompts if shell else final \n if missing.
"""

if hasattr(self.editwin, "interp"): # Saving shell.
text = self.editwin.get_prompt_text('1.0', self.text.index('end-1c'))
else:
if self.text.get("end-2c") != '\n':
self.text.insert("end-1c", "\n") # Changes 'end-1c' value.
text = self.text.get('1.0', "end-1c")
if self.eol_convention != "\n":
text = text.replace("\n", self.eol_convention)
return text
Expand Down
35 changes: 19 additions & 16 deletions Lib/idlelib/pyshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,23 @@ def replace_event(self, event):
def get_standard_extension_names(self):
return idleConf.GetExtensions(shell_only=True)

def get_prompt_text(self, first, last):
"""Return text between first and last with prompts added."""
text = self.text.get(first, last)
lineno_range = range(
int(float(first)),
int(float(last))
)
prompts = [
self.shell_sidebar.line_prompts.get(lineno)
for lineno in lineno_range
]
return "\n".join(
line if prompt is None else f"{prompt} {line}"
for prompt, line in zip(prompts, text.splitlines())
) + "\n"


def copy_with_prompts_callback(self, event=None):
"""Copy selected lines to the clipboard, with prompts.

Expand All @@ -1002,23 +1019,9 @@ def copy_with_prompts_callback(self, event=None):
sellast = text.index('sel.last')
if sellast[-1] != '0':
sellast = text.index("sel.last+1line linestart")

selected_text = self.text.get(selfirst, sellast)
selection_lineno_range = range(
int(float(selfirst)),
int(float(sellast))
)
prompts = [
self.shell_sidebar.line_prompts.get(lineno)
for lineno in selection_lineno_range
]
selected_text_with_prompts = "\n".join(
line if prompt is None else f"{prompt} {line}"
for prompt, line in zip(prompts, selected_text.splitlines())
) + "\n"

text.clipboard_clear()
text.clipboard_append(selected_text_with_prompts)
prompt_text = self.get_prompt_text(selfirst, sellast)
text.clipboard_append(prompt_text)

reading = False
executing = False
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Include prompts when saving Shell (interactive input and output).