Repository: spyder-ide/spyder

Issue:
Explorer "New" actions should depend on selected files
### Problem Description

The explorer context menu actions in "New" use the root path of the current directory. Prior to #13032 the directory was depending on the selected file or folder, which IMHO is more convenient. 
As an argument why the new behavior is inconvenient, when just entering a file name and clicking save the file would be created outside the current project.

### What steps reproduce the problem?

1. Open a project
2. Right click on a file in the project
3. New -> New file...

### What is the expected output? What do you see instead?
The save-file-dialog opens at the root folder, in which the project is located. Instead, i would expect the dialog to open at the directory containing the right clicked file.

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 5.0.0.dev0
* Python version: 3.7.7 64-bit
* Qt version: 5.9.6
* PyQt version: 5.9.2
* Operating System name/version: Linux 5.7.10-arch1-1

### Dependencies

```

# Mandatory:
atomicwrites >=1.2.0        :  1.4.0 (OK)
chardet >=2.0.0             :  3.0.4 (OK)
cloudpickle >=0.5.0         :  1.5.0 (OK)
cookiecutter >=1.6.0        :  None (OK)
diff_match_patch >=20181111 :  20200713 (OK)
intervaltree                :  None (OK)
IPython >=4.0               :  7.16.1 (OK)
jedi =0.17.1                :  0.17.1 (OK)
keyring                     :  None (OK)
nbconvert >=4.0             :  5.6.1 (OK)
numpydoc >=0.6.0            :  1.1.0 (OK)
parso =0.7.0                :  0.7.0 (OK)
pexpect >=4.4.0             :  4.8.0 (OK)
pickleshare >=0.4           :  0.7.5 (OK)
psutil >=5.3                :  5.7.0 (OK)
pygments >=2.0              :  2.6.1 (OK)
pylint >=1.0                :  2.4.4 (OK)
pyls >=0.34.0;<1.0.0        :  v4.1.4+579.g4f1100b4f (OK)
qdarkstyle >=2.8            :  2.8.1 (OK)
qtawesome >=0.5.7           :  0.7.2 (OK)
qtconsole >=4.6.0           :  4.7.5 (OK)
qtpy >=1.5.0                :  1.9.0 (OK)
rtree >=0.8.3               :  0.9.4 (OK)
sphinx >=0.6.6              :  3.1.2 (OK)
spyder_kernels >=2.0.0dev0  :  2.0.0.dev0 (OK)
watchdog                    :  None (OK)
xdg >=0.26                  :  0.26 (OK)
zmq >=17                    :  19.0.1 (OK)

# Optional:
cython >=0.21               :  0.29.21 (OK)
matplotlib >=2.0.0          :  3.2.2 (OK)
numpy >=1.7                 :  1.18.5 (OK)
pandas >=0.13.1             :  1.0.1 (OK)
scipy >=0.17.0              :  1.5.0 (OK)
sympy >=0.7.3               :  1.6 (OK)
```



Discussion:
Hi @jleuschn, thanks for opening this. I think you are right. @steff456 can you take a look at this one?

Resolving patch:
diff --git a/spyder/plugins/explorer/widgets/explorer.py b/spyder/plugins/explorer/widgets/explorer.py
--- a/spyder/plugins/explorer/widgets/explorer.py
+++ b/spyder/plugins/explorer/widgets/explorer.py
@@ -433,7 +433,24 @@ def toggle_hidden(self, checked):
 
     def create_file_new_actions(self, fnames):
         """Return actions for submenu 'New...'"""
-        root_path = self.fsmodel.rootPath()
+        if not fnames:
+            root_path = self.fsmodel.rootPath()
+        else:
+            # Verify if the user is trying to add something new inside a folder
+            # so it can be added in that folder and not in the rootpath
+            # See spyder-ide/spyder#13444
+            level_list = []
+            current_index = self.currentIndex()
+            if current_index.isValid():
+                level_list.append(current_index.data())
+
+            while current_index.isValid():
+                current_index = current_index.parent()
+                if current_index.data() is not None:
+                    level_list.insert(0, current_index.data())
+
+            root_path = osp.join(*level_list)
+
         new_file_act = create_action(self, _("File..."),
                                      icon=ima.icon('TextFileIcon'),
                                      triggered=lambda:


Test patch: