Skip to content
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

PR: Create new file with %edit magic (IPython console) #17181

Merged
merged 4 commits into from
Feb 17, 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
13 changes: 13 additions & 0 deletions spyder/plugins/ipythonconsole/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ class IPythonConsole(SpyderDockablePlugin):
requested file.
"""

sig_edit_new = Signal(str)
"""
This signal will request to create a new file in a code editor.

Parameters
----------
path: str
Path to file.
"""

sig_pdb_state_changed = Signal(bool, dict)
"""
This signal is emitted when the debugging state changes.
Expand Down Expand Up @@ -219,6 +229,7 @@ def on_initialize(self):
widget.sig_edit_goto_requested.connect(self.sig_edit_goto_requested)
widget.sig_edit_goto_requested[str, int, str, bool].connect(
self.sig_edit_goto_requested[str, int, str, bool])
widget.sig_edit_new.connect(self.sig_edit_new)
widget.sig_pdb_state_changed.connect(self.sig_pdb_state_changed)
widget.sig_shellwidget_created.connect(self.sig_shellwidget_created)
widget.sig_shellwidget_deleted.connect(self.sig_shellwidget_deleted)
Expand Down Expand Up @@ -299,6 +310,7 @@ def on_editor_available(self):
self.sig_edit_goto_requested.connect(editor.load)
self.sig_edit_goto_requested[str, int, str, bool].connect(
self._load_file_in_editor)
self.sig_edit_new.connect(editor.new)
editor.breakpoints_saved.connect(self.set_spyder_breakpoints)
editor.run_in_current_ipyclient.connect(self.run_script)
editor.run_cell_in_ipyclient.connect(self.run_cell)
Expand Down Expand Up @@ -341,6 +353,7 @@ def on_editor_teardown(self):
self.sig_edit_goto_requested.disconnect(editor.load)
self.sig_edit_goto_requested[str, int, str, bool].disconnect(
self._load_file_in_editor)
self.sig_edit_new.disconnect(editor.new)
editor.breakpoints_saved.disconnect(self.set_spyder_breakpoints)
editor.run_in_current_ipyclient.disconnect(self.run_script)
editor.run_cell_in_ipyclient.disconnect(self.run_cell)
Expand Down
12 changes: 12 additions & 0 deletions spyder/plugins/ipythonconsole/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ class IPythonConsoleWidget(PluginMainWidget):
requested file.
"""

sig_edit_new = Signal(str)
"""
This signal will request to create a new file in a code editor.

Parameters
----------
path: str
Path to file.
"""

sig_pdb_state_changed = Signal(bool, dict)
"""
This signal is emitted when the debugging state changes.
Expand Down Expand Up @@ -1314,6 +1324,8 @@ def tab_name_editor(self):
@Slot(object, object)
def edit_file(self, filename, line):
"""Handle %edit magic petitions."""
if not osp.isfile(filename):
impact27 marked this conversation as resolved.
Show resolved Hide resolved
self.sig_edit_new.emit(filename)
if encoding.is_text_file(filename):
# The default line number sent by ipykernel is always the last
# one, but we prefer to use the first.
Expand Down