Repository: spyder-ide/spyder

Issue:
Error when trying to go to cursor position in Outline
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

1. new python file.
2. click "go to line" button of Outline

![gotoline](https://user-images.githubusercontent.com/22970578/50595450-b1829180-0ee3-11e9-8488-ae6f40944e83.gif)


### Traceback
```python-traceback
  File "/home/ok97465/python/spyder/spyder/plugins/outlineexplorer/widgets.py", line 257, in go_to_cursor_position
    item = item_at_line(root_item, line)
  File "/home/ok97465/python/spyder/spyder/plugins/outlineexplorer/widgets.py", line 142, in item_at_line
    return item
UnboundLocalError: local variable 'item' referenced before assignment
```

## Versions

* Spyder version: 4.0.0.dev0 942f6a931
* Python version: 3.6.7
* Qt version: 5.9.4
* PyQt5 version: 5.9.2
* Operating System: Linux 4.20.0-1-MANJARO

### Dependencies

```
pygments >=2.0    :  2.2.0 (OK)
qdarkstyle >=2.6.4:  2.6.4 (OK)
sphinx >=0.6.6    :  1.7.4 (OK)
psutil >=0.3      :  5.4.5 (OK)
pyls >=0.19.0     :  0.19.0 (OK)
nbconvert >=4.0   :  5.3.1 (OK)
pandas >=0.13.1   :  0.23.0 (OK)
numpy >=1.7       :  1.14.3 (OK)
sympy >=0.7.3     :  1.1.1 (OK)
cython >=0.21     :  0.28.2 (OK)
qtconsole >=4.2.0 :  4.3.1 (OK)
IPython >=4.0     :  6.4.0 (OK)
matplotlib >=2.0.0:  3.0.2 (OK)
pylint >=0.25     :  1.8.4 (OK)
```



Discussion:

Resolving patch:
diff --git a/spyder/plugins/outlineexplorer/widgets.py b/spyder/plugins/outlineexplorer/widgets.py
--- a/spyder/plugins/outlineexplorer/widgets.py
+++ b/spyder/plugins/outlineexplorer/widgets.py
@@ -134,6 +134,7 @@ def item_at_line(root_item, line):
     the specified 'line' of the editor.
     """
     previous_item = root_item
+    item = root_item
     for item in get_item_children(root_item):
         if item.line > line:
             return previous_item


Test patch:
diff --git a/spyder/plugins/outlineexplorer/tests/test_widgets.py b/spyder/plugins/outlineexplorer/tests/test_widgets.py
--- a/spyder/plugins/outlineexplorer/tests/test_widgets.py
+++ b/spyder/plugins/outlineexplorer/tests/test_widgets.py
@@ -131,8 +131,8 @@ def function6(x):
 
 # ---- Qt Test Fixtures
 @pytest.fixture
-def outlineexplorer_bot(qtbot):
-    def _create_bot(code, filename):
+def create_outlineexplorer(qtbot):
+    def _create_outlineexplorer(code, filename):
         code_editor = CodeEditor(None)
         code_editor.set_language('py', filename)
         code_editor.set_text(code)
@@ -145,19 +145,18 @@ def _create_bot(code, filename):
         outlineexplorer.setFixedSize(400, 350)
 
         qtbot.addWidget(outlineexplorer)
-        return outlineexplorer, qtbot
-    return _create_bot
+        return outlineexplorer
+    return _create_outlineexplorer
 
 
 # ---- Test OutlineExplorerWidget
-def test_outline_explorer(outlineexplorer_bot):
+def test_outline_explorer(create_outlineexplorer):
     """
     Test to assert the outline explorer is initializing correctly and
     is showing the expected number of items, the expected type of items, and
     the expected text for each item.
     """
-    outlineexplorer, qtbot = outlineexplorer_bot(
-        TEXT, 'test_outline_explorer.py')
+    outlineexplorer = create_outlineexplorer(TEXT, 'test_outline_explorer.py')
     assert outlineexplorer
 
     outlineexplorer.treewidget.expandAll()
@@ -187,14 +186,14 @@ def test_outline_explorer(outlineexplorer_bot):
             assert item.is_method() == expected_result[2]
 
 
-def test_go_to_cursor_position(outlineexplorer_bot):
+def test_go_to_cursor_position(create_outlineexplorer, qtbot):
     """
     Test that clicking on the 'Go to cursor position' button located in the
     toolbar of the outline explorer is working as expected.
 
     Regression test for issue #7729.
     """
-    outlineexplorer, qtbot = outlineexplorer_bot(TEXT, 'test.py')
+    outlineexplorer = create_outlineexplorer(TEXT, 'test.py')
 
     # Move the mouse cursor in the editor to line 31 :
     editor = outlineexplorer.treewidget.current_editor
@@ -208,7 +207,25 @@ def test_go_to_cursor_position(outlineexplorer_bot):
     assert outlineexplorer.treewidget.currentItem().text(0) == 'method1'
 
 
-def test_go_to_last_item(outlineexplorer_bot):
+def test_go_to_cursor_position_with_new_file(create_outlineexplorer, qtbot):
+    """
+    Test that clicking on the 'Go to cursor position' button located in the
+    toolbar of the outline explorer is working as expected for newly created
+    files.
+
+    Regression test for issue  #8510.
+    """
+    text = "# -*- coding: utf-8 -*-\nSome newly created\nPython file."
+    outlineexplorer = create_outlineexplorer(text, 'new_file.py')
+
+    # Click on the 'Go to cursor position' button of the outline explorer's
+    # toolbar :
+    assert outlineexplorer.treewidget.currentItem() is None
+    qtbot.mouseClick(outlineexplorer.fromcursor_btn, Qt.LeftButton)
+    assert outlineexplorer.treewidget.currentItem().text(0) == 'new_file.py'
+
+
+def test_go_to_last_item(create_outlineexplorer, qtbot):
     """
     Test that clicking on the 'Go to cursor position' button located in the
     toolbar of the outline explorer is working as expected when the cursor
@@ -216,7 +233,7 @@ def test_go_to_last_item(outlineexplorer_bot):
 
     Regression test for issue #7744.
     """
-    outlineexplorer, qtbot = outlineexplorer_bot(TEXT, 'test.py')
+    outlineexplorer = create_outlineexplorer(TEXT, 'test.py')
 
     # Move the mouse cursor in the editor to the last line :
     editor = outlineexplorer.treewidget.current_editor
@@ -231,14 +248,14 @@ def test_go_to_last_item(outlineexplorer_bot):
     assert outlineexplorer.treewidget.currentItem().text(0) == 'method2'
 
 
-def test_code_cell_grouping(outlineexplorer_bot):
+def test_code_cell_grouping(create_outlineexplorer):
     """
     Test to assert the outline explorer is initializing correctly and
     is showing the expected number of items, the expected type of items, and
     the expected text for each item. In addition this tests ancestry, code
     cells comments, code cell grouping and disabling this feature.
     """
-    outlineexplorer, qtbot = outlineexplorer_bot(dedent(CODE), 'test_file.py')
+    outlineexplorer = create_outlineexplorer(dedent(CODE), 'test_file.py')
     assert outlineexplorer
 
     expected_results = [