Repository: spyder-ide/spyder

Issue:
AttributeError when using outline and split code 
## Description of your problem

**What steps will reproduce the problem?**

1.  Open Spyder
2.  Split the Editor through the cog menu [X2 times]
3.  Close the last panel created
4.  Click on the outline editor

**Traceback :**
Traceback (most recent call last):
  File "C:\Users\jsgosselin\spyder\spyder\plugins\editor.py", line 552, in <lambda>
    editorwindow=self))
  File "C:\Users\jsgosselin\spyder\spyder\plugins\editor.py", line 1923, in load
    focus=focus)
  File "C:\Users\jsgosselin\spyder\spyder\plugins\editor.py", line 1506, in set_current_filename
    return editorstack.set_current_filename(filename, focus)
AttributeError: 'NoneType' object has no attribute 'set_current_filename'
Traceback (most recent call last):
  File "C:\Users\jsgosselin\spyder\spyder\widgets\editortools.py", line 492, in clicked
    
  File "C:\Users\jsgosselin\spyder\spyder\widgets\editortools.py", line 477, in activated
    parent = self.current_editor.parent()
AttributeError: 'NoneType' object has no attribute 'parent'

## Versions and main components

* Spyder Version: branch 3.x and master
* Python Version: 3.6.1
* Qt Version: 5.9.1
* PyQt Version: 5.9
* Operating system: Windows 7 64 bits

## Dependencies

IPython >=4.0    :  6.1.0 (OK)
cython >=0.21    :  0.25.2 (OK)
jedi >=0.9.0     :  0.10.2 (OK)
nbconvert >=4.0  :  5.2.1 (OK)
numpy >=1.7      :  1.13.0 (OK)
pandas >=0.13.1  :  0.20.3 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pyflakes >=0.6.0 :  1.5.0 (OK)
pygments >=2.0   :  2.2.0 (OK)
pylint >=0.25    :  1.7.1 (OK)
qtconsole >=4.2.0:  4.4.0.dev (OK)
rope >=0.9.4     :  0.9.4-1 (OK)
sphinx >=0.6.6   :  1.6.2 (OK)
sympy >=0.7.3    :  None (NOK)



Discussion:

Resolving patch:
diff --git a/spyder/plugins/editor.py b/spyder/plugins/editor.py
--- a/spyder/plugins/editor.py
+++ b/spyder/plugins/editor.py
@@ -1456,13 +1456,15 @@ def get_filename_index(self, filename):
     def get_current_editorstack(self, editorwindow=None):
         if self.editorstacks is not None:
             if len(self.editorstacks) == 1:
-                return self.editorstacks[0]
+                editorstack = self.editorstacks[0]
             else:
                 editorstack = self.__get_focus_editorstack()
                 if editorstack is None or editorwindow is not None:
-                    return self.get_last_focus_editorstack(editorwindow)
-                return editorstack
-        
+                    editorstack = self.get_last_focus_editorstack(editorwindow)
+                    if editorstack is None:
+                        editorstack = self.editorstacks[0]
+            return editorstack
+
     def get_current_editor(self):
         editorstack = self.get_current_editorstack()
         if editorstack is not None:
diff --git a/spyder/widgets/editor.py b/spyder/widgets/editor.py
--- a/spyder/widgets/editor.py
+++ b/spyder/widgets/editor.py
@@ -767,6 +767,12 @@ def add_corner_widgets_to_tabbar(self, widgets):
     def closeEvent(self, event):
         self.threadmanager.close_all_threads()
         self.analysis_timer.timeout.disconnect(self.analyze_script)
+
+        # Remove editor references from the outline explorer settings
+        if self.outlineexplorer is not None:
+            for finfo in self.data:
+                self.outlineexplorer.remove_editor(finfo.editor)
+
         QWidget.closeEvent(self, event)
         if is_pyqt46:
             self.destroyed.emit()
@@ -2015,10 +2021,6 @@ def create_new_editor(self, fname, enc, txt, set_current, new=False,
         editor.zoom_out.connect(lambda: self.zoom_out.emit())
         editor.zoom_reset.connect(lambda: self.zoom_reset.emit())
         editor.sig_eol_chars_changed.connect(lambda eol_chars: self.refresh_eol_chars(eol_chars))
-        if self.outlineexplorer is not None:
-            # Removing editor reference from outline explorer settings:
-            editor.destroyed.connect(lambda obj=editor:
-                                     self.outlineexplorer.remove_editor(obj))
 
         self.find_widget.set_editor(editor)
 
diff --git a/spyder/widgets/editortools.py b/spyder/widgets/editortools.py
--- a/spyder/widgets/editortools.py
+++ b/spyder/widgets/editortools.py
@@ -476,7 +476,6 @@ def activated(self, item):
         parent = self.current_editor.parent()
         for editor_id, i_item in list(self.editor_items.items()):
             if i_item is root_item:
-                #XXX: not working anymore!!!
                 for editor, _id in list(self.editor_ids.items()):
                     if _id == editor_id and editor.parent() is parent:
                         self.current_editor = editor


Test patch: