@@ -735,6 +735,18 @@ def update_completion(self, tab=False):
735735 self .current_match = None
736736 self .list_win_visible = BpythonRepl .complete (self , tab )
737737
738+ def predicted_indent (self , line ):
739+ logger .debug ('line is %r' , line )
740+ indent = len (re .match (r'[ ]*' , line ).group ())
741+ if line .endswith (':' ):
742+ indent = max (0 , indent + self .config .tab_length )
743+ elif line and line .count (' ' ) == len (line ):
744+ indent = max (0 , indent - self .config .tab_length )
745+ elif line and ':' not in line and line .strip ().startswith (('return' , 'pass' , 'raise' , 'yield' )):
746+ indent = max (0 , indent - self .config .tab_length )
747+ logger .debug ('indent we found was %s' , indent )
748+ return indent
749+
738750 def push (self , line , insert_into_history = True ):
739751 """Push a line of code onto the buffer, start running the buffer
740752
@@ -743,14 +755,7 @@ def push(self, line, insert_into_history=True):
743755 if self .paste_mode :
744756 self .saved_indent = 0
745757 else :
746- indent = len (re .match (r'[ ]*' , line ).group ())
747- if line .endswith (':' ):
748- indent = max (0 , indent + self .config .tab_length )
749- elif line and line .count (' ' ) == len (line ):
750- indent = max (0 , indent - self .config .tab_length )
751- elif line and ':' not in line and line .strip ().startswith (('return' , 'pass' , 'raise' , 'yield' )):
752- indent = max (0 , indent - self .config .tab_length )
753- self .saved_indent = indent
758+ self .saved_indent = self .predicted_indent (line )
754759
755760 #current line not added to display buffer if quitting #TODO I don't understand this comment
756761 if self .config .syntax :
@@ -1197,8 +1202,15 @@ def reprint_line(self, lineno, tokens):
11971202 def take_back_buffer_line (self ):
11981203 self .display_buffer .pop ()
11991204 self .buffer .pop ()
1200- self .cursor_offset = 0
1201- self .current_line = ''
1205+
1206+ if not self .buffer :
1207+ self .current_line = ''
1208+ self .cursor_offset = 0
1209+ else :
1210+ line = self .buffer [- 1 ]
1211+ indent = self .predicted_indent (line )
1212+ self .current_line = indent * ' '
1213+ self .cursor_offset = len (self .current_line )
12021214
12031215 def reevaluate (self , insert_into_history = False ):
12041216 """bpython.Repl.undo calls this"""
0 commit comments