Skip to content

Commit

Permalink
Merge pull request #16014 from ccordoba12/fix-messagecheckbox
Browse files Browse the repository at this point in the history
PR: Fix resetting variables after clicking on the reset button (IPython console)
  • Loading branch information
ccordoba12 committed Jul 19, 2021
2 parents 29f4b5b + 82d121f commit 5f0df74
Showing 1 changed file with 58 additions and 20 deletions.
78 changes: 58 additions & 20 deletions spyder/plugins/ipythonconsole/widgets/shell.py
Expand Up @@ -455,42 +455,69 @@ def _reset_namespace(self):

def reset_namespace(self, warning=False, message=False):
"""Reset the namespace by removing all names defined by the user."""
reset_str = _("Remove all variables")
warn_str = _("All user-defined variables will be removed. "
"Are you sure you want to proceed?")

# Don't show the warning when running our tests.
if running_under_pytest():
warning = False

# This is necessary to make resetting variables work in external
# kernels.
# See spyder-ide/spyder#9505.
try:
kernel_env = self.kernel_manager._kernel_spec.env
except AttributeError:
kernel_env = {}

if warning:
reset_str = _("Remove all variables")
warn_str = _("All user-defined variables will be removed. "
"Are you sure you want to proceed?")
box = MessageCheckBox(icon=QMessageBox.Warning, parent=self)
box.setWindowTitle(reset_str)
box.set_checkbox_text(_("Don't show again."))
box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
box.setDefaultButton(QMessageBox.Yes)

box.set_checkbox_text(_("Don't show again."))
box.set_checked(False)
box.set_check_visible(True)
box.setText(warn_str)

answer = box.show()
box.buttonClicked.connect(
lambda button: self.handle_reset_message_answer(
box, button, message)
)
box.show()
else:
self._perform_reset(message)

def handle_reset_message_answer(self, message_box, button, message):
"""
Handle the answer of the reset namespace message box.
Parameters
----------
message_box
Instance of the message box shown to the user.
button: QPushButton
Instance of the button clicked by the user on the dialog.
message: bool
Whether to show a message in the console telling users the
namespace was reset.
"""
if message_box.buttonRole(button) == QMessageBox.YesRole:
self._update_reset_options(message_box)
self._perform_reset(message)
else:
self._update_reset_options(message_box)

# Update checkbox based on user interaction
self.set_conf(
'show_reset_namespace_warning', not box.is_checked())
self.ipyclient.reset_warning = not box.is_checked()
def _perform_reset(self, message):
"""
Perform the reset namespace operation.
if answer != QMessageBox.Yes:
return
Parameters
----------
message: bool
Whether to show a message in the console telling users the
namespace was reset.
"""
# This is necessary to make resetting variables work in external
# kernels.
# See spyder-ide/spyder#9505.
try:
kernel_env = self.kernel_manager._kernel_spec.env
except AttributeError:
kernel_env = {}

try:
if self.is_waiting_pdb_input():
Expand Down Expand Up @@ -528,6 +555,17 @@ def reset_namespace(self, warning=False, message=False):
except AttributeError:
pass

def _update_reset_options(self, message_box):
"""
Update options and variables based on the interaction in the
reset warning message box shown to the user.
"""
self.set_conf(
'show_reset_namespace_warning',
not message_box.is_checked()
)
self.ipyclient.reset_warning = not message_box.is_checked()

def create_shortcuts(self):
"""Create shortcuts for ipyconsole."""
inspect = self.config_shortcut(
Expand Down

0 comments on commit 5f0df74

Please sign in to comment.