Skip to content

Commit

Permalink
Merge pull request #23505 from ccordoba12/issue-23076
Browse files Browse the repository at this point in the history
PR: Fix Re-run cell when the last executed cell is not in the current file (Run)
  • Loading branch information
ccordoba12 authored Jan 19, 2025
2 parents ec4def2 + 2dec328 commit 40d7591
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
27 changes: 26 additions & 1 deletion spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,32 @@ def test_run_code(main_window, qtbot, tmpdir):
timeout=EVAL_TIMEOUT)
assert shell.get_value('li') == [1, 2, 3]

# try running cell without file name
# Clean namespace
with qtbot.waitSignal(shell.executed):
shell.execute('%reset -f')

# Wait until there are no objects in the variable explorer
qtbot.waitUntil(lambda: nsb.editor.source_model.rowCount() == 0,
timeout=EVAL_TIMEOUT)

# Open a new file
editor.load(osp.join(LOCATION, 'script_pylint.py'))
qtbot.wait(500)

# Re-run last cell (from the previous file).
# This is a regression for spyder-ide/spyder#23076
with qtbot.waitSignal(shell.executed):
re_run_action.trigger()

# We should get the same result as before
qtbot.waitUntil(lambda: nsb.editor.source_model.rowCount() == 1,
timeout=EVAL_TIMEOUT)
assert shell.get_value('li') == [1, 2, 3]

# Close new file
editor.close_file()

# Try running cell without file name
shell.clear()

# Clean namespace
Expand Down
11 changes: 8 additions & 3 deletions spyder/plugins/editor/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -3114,13 +3114,12 @@ def get_run_configuration_per_context(
self, context, extra_action_name, context_modificator, re_run=False
) -> Optional[RunConfiguration]:
editorstack = self.get_current_editorstack()
fname = self.get_current_filename()
__, filename_ext = osp.splitext(fname)
fname_ext = filename_ext[1:]
run_input = {}
context_name = None

if context == RunContext.Selection:
fname = self.get_current_filename()

if context_modificator == SelectionContextModificator.ToLine:
to_current_line = editorstack.get_to_current_line()
if to_current_line is not None:
Expand Down Expand Up @@ -3149,8 +3148,11 @@ def get_run_configuration_per_context(
elif context == RunContext.Cell:
if re_run:
info = editorstack.get_last_cell()
fname = editorstack.last_cell_call[0]
else:
info = editorstack.get_current_cell()
fname = self.get_current_filename()

text, offsets, line_cols, cell_name, enc = info
context_name = 'Cell'
copy_cell = self.get_conf('run_cell_copy', section='run')
Expand All @@ -3162,6 +3164,9 @@ def get_run_configuration_per_context(
if extra_action_name == ExtraAction.Advance:
editorstack.advance_cell()

__, filename_ext = osp.splitext(fname)
fname_ext = filename_ext[1:]

metadata: RunConfigurationMetadata = {
'name': fname,
'source': self._plugin.NAME,
Expand Down
23 changes: 18 additions & 5 deletions spyder/plugins/run/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def setup(self):

self.last_executed_file: Optional[str] = None
self.last_executed_per_context: Set[Tuple[str, str]] = set()
self._last_executed_configuration: Optional[str] = None

def update_actions(self):
pass
Expand All @@ -143,12 +144,25 @@ def anonymous_execution_run():
if self.currently_selected_configuration is None:
return

input_provider = self.run_metadata_provider[
self.currently_selected_configuration]
# We save the last executed configuration in case we need it to
# re-run the last cell with the same parameters.
# This is necessary for spyder-ide/spyder#23076
if not re_run:
uuid = self.currently_selected_configuration
self._last_executed_configuration = (
self.currently_selected_configuration
)
else:
uuid = self._last_executed_configuration

# If nothing has been executed so far, we can't re-run it.
if uuid is None:
return

input_provider = self.run_metadata_provider[uuid]

if context in self.super_contexts:
run_conf = input_provider.get_run_configuration(
self.currently_selected_configuration)
run_conf = input_provider.get_run_configuration(uuid)
else:
run_conf = input_provider.get_run_configuration_per_context(
context, extra_action_name, context_modificator,
Expand All @@ -157,7 +171,6 @@ def anonymous_execution_run():
if run_conf is None:
return

uuid = self.currently_selected_configuration
super_metadata = self.metadata_model[uuid]
extension = super_metadata['input_extension']

Expand Down

0 comments on commit 40d7591

Please sign in to comment.