Repository: spyder-ide/spyder

Issue:
%edit magic can't create a new file
## Description of your problem

From an ipython prompt in Spyder, editing a non-existent file gives an error.  It would be great if this just created a new, blank file.  If there's a way to do this, or different command, I couldn't find it.

**What steps will reproduce the problem?**
1. Open ipython console in spyder
2. type "edit foo.py" in a directory with no file foo.py

**What is the expected output? What do you see instead?**

This isn't an issue but a feature request maybe.  I'd like an editor window with a blank file to open.  Instead I get an error.

**Please provide any additional information below**
## Versions and main components

Using Anaconda distribution
- Spyder Version: 2.3.9
- Python Version: 3.5.2
- Operating system: Windows 7
## Dependencies

Please go to the menu entry `Help > Optional Dependencies` (or
`Help > Dependencies`), press the button `Copy to clipboard`
and paste the contents below:



Discussion:
Thanks for the suggestion. We'll see what we can do about it :-)

OK, thanks!  It's not a big deal, just an annoying bump in workflow.


Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py
--- a/spyder/plugins/ipythonconsole/plugin.py
+++ b/spyder/plugins/ipythonconsole/plugin.py
@@ -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.
@@ -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)
@@ -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)
@@ -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)
diff --git a/spyder/plugins/ipythonconsole/widgets/main_widget.py b/spyder/plugins/ipythonconsole/widgets/main_widget.py
--- a/spyder/plugins/ipythonconsole/widgets/main_widget.py
+++ b/spyder/plugins/ipythonconsole/widgets/main_widget.py
@@ -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.
@@ -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):
+            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.


Test patch: