Repository: spyder-ide/spyder
Pull request: PR: Create new file with %edit magic (IPython console)
<!--- Make sure to read the Contributing Guidelines:                   --->
<!--- https://github.com/spyder-ide/spyder/blob/master/CONTRIBUTING.md --->
<!--- and follow PEP 8, PEP 257 and Spyder's code style:               --->
<!--- https://github.com/spyder-ide/spyder/wiki/Dev:-Coding-Style      --->

## Description of Changes
When using edit magic on a non-existing file, this creates a new file instead of doing nothing.

* [ ] Wrote at least one-line docstrings (for any new functions)
* [ ] Added unit test(s) covering the changes (if testable)
<!--- Remember that an image/animation is worth a thousand words! --->
* [ ] Included a screenshot or animation (if affecting the UI, see [Licecap](https://www.cockos.com/licecap/))


<!--- Explain what you've done and why --->




### Issue(s) Resolved

<!--- List the issue(s) below, in the form "Fixes #1234"; one per line --->

Fixes #3394


### Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the [Developer Certificate of Origin](https://developercertificate.org)
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

<!--- TYPE YOUR USER/NAME AFTER THE FOLLOWING: --->
I certify the above statement is true and correct:

<!--- Thanks for your help making Spyder better for everyone! --->


Problem statement:
%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:

Hints from issue 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.

Commit message: Merge remote-tracking branch 'upstream/5.x' into edit_new_file

Patch to review:
diff --git a/spyder/plugins/ipythonconsole/widgets/main_widget.py b/spyder/plugins/ipythonconsole/widgets/main_widget.py
index 369eb9fbcb9..36d30f6b430 100644
--- a/spyder/plugins/ipythonconsole/widgets/main_widget.py
+++ b/spyder/plugins/ipythonconsole/widgets/main_widget.py
@@ -1314,6 +1314,10 @@ def tab_name_editor(self):
     @Slot(object, object)
     def edit_file(self, filename, line):
         """Handle %edit magic petitions."""
+        if not osp.isfile(filename):
+            # Create new file
+            with open(filename, "w") as f:
+                f.write("")
         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.


Code review comments:
diff_hunk: @@ -1314,6 +1314,10 @@ def tab_name_editor(self):
     @Slot(object, object)
     def edit_file(self, filename, line):
         """Handle %edit magic petitions."""
+        if not osp.isfile(filename):
+            # Create new file
+            with open(filename, "w") as f:
+                f.write("")
path: spyder/plugins/ipythonconsole/widgets/main_widget.py
text: @user1:
I think we should add our template for untitled files here, instead of creating the file with empty content. What do you think?