Repository: spyder-ide/spyder

Issue:
Indenting at the first position in file fails/crashes
This happens under Spyder 3.0.0-dev (from winpython) as well as Spyder 2.3.5.2 (from Arch Repos).
Under windows it simply crashes the application, under Linux I get a nice traceback:

```
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 2466, in keyPressEvent
    self.intelligent_tab()
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 758, in intelligent_tab
    self.indent_or_replace()
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 1930, in indent_or_replace
    self.indent()
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 1916, in indent
    if not self.fix_indent(forward=True):
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 1822, in fix_indent
    correct_indent = self.get_block_indentation(prevline)
UnboundLocalError: local variable 'prevline' referenced before assignment
```

The problem is clear: there is no previous line

Indenting at the first position in file fails/crashes
This happens under Spyder 3.0.0-dev (from winpython) as well as Spyder 2.3.5.2 (from Arch Repos).
Under windows it simply crashes the application, under Linux I get a nice traceback:

```
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 2466, in keyPressEvent
    self.intelligent_tab()
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 758, in intelligent_tab
    self.indent_or_replace()
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 1930, in indent_or_replace
    self.indent()
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 1916, in indent
    if not self.fix_indent(forward=True):
  File "/usr/lib/python3.4/site-packages/spyderlib/widgets/sourcecode/codeeditor.py", line 1822, in fix_indent
    correct_indent = self.get_block_indentation(prevline)
UnboundLocalError: local variable 'prevline' referenced before assignment
```

The problem is clear: there is no previous line



Discussion:
I am trying to fix this, but I am unsure whether to do this in intelligent_tab (checking for cursor position) or in fix_indent (making sure that no codepath ever hits this problem again). It should probably be the latter.

I am trying to fix this, but I am unsure whether to do this in intelligent_tab (checking for cursor position) or in fix_indent (making sure that no codepath ever hits this problem again). It should probably be the latter.


Resolving patch:
diff --git a/spyderlib/widgets/sourcecode/codeeditor.py b/spyderlib/widgets/sourcecode/codeeditor.py
--- a/spyderlib/widgets/sourcecode/codeeditor.py
+++ b/spyderlib/widgets/sourcecode/codeeditor.py
@@ -1883,6 +1883,7 @@ def fix_indent(self, forward=True, comment_or_string=False):
         # find the line that contains our scope
         diff = 0
         add_indent = False
+        prevline = None
         for prevline in range(block_nb-1, -1, -1):
             cursor.movePosition(QTextCursor.PreviousBlock)
             prevtext = to_text_string(cursor.block().text()).rstrip()
@@ -1903,6 +1904,10 @@ def fix_indent(self, forward=True, comment_or_string=False):
                     break
 
         indent = self.get_block_indentation(block_nb)
+        
+        if not prevline:
+            return False
+
         correct_indent = self.get_block_indentation(prevline)
 
         if add_indent:


Test patch: