@@ -1342,38 +1342,51 @@ def smart_indent_event(self, event):
13421342 text .undo_block_stop ()
13431343
13441344 def newline_and_indent_event (self , event ):
1345+ """Insert a newline and indentation after Enter keypress event.
1346+
1347+ Properly position the cursor on the new line based on information
1348+ from the current line. This takes into account if the current line
1349+ is a shell prompt, is empty, has selected text, contains a block
1350+ opener, contains a block closer, is a continuation line, or
1351+ is inside a string.
1352+ """
13451353 text = self .text
13461354 first , last = self .get_selection_indices ()
13471355 text .undo_block_start ()
1348- try :
1356+ try : # Close undo block and expose new line in finally clause.
13491357 if first and last :
13501358 text .delete (first , last )
13511359 text .mark_set ("insert" , first )
13521360 line = text .get ("insert linestart" , "insert" )
1361+
1362+ # Count leading whitespace for indent size.
13531363 i , n = 0 , len (line )
13541364 while i < n and line [i ] in " \t " :
1355- i = i + 1
1365+ i += 1
13561366 if i == n :
1357- # the cursor is in or at leading indentation in a continuation
1358- # line; just inject an empty line at the start
1367+ # The cursor is in or at leading indentation in a continuation
1368+ # line; just inject an empty line at the start.
13591369 text .insert ("insert linestart" , '\n ' )
13601370 return "break"
13611371 indent = line [:i ]
1362- # strip whitespace before insert point unless it's in the prompt
1372+
1373+ # Strip whitespace before insert point unless it's in the prompt.
13631374 i = 0
13641375 while line and line [- 1 ] in " \t " and line != self .prompt_last_line :
13651376 line = line [:- 1 ]
1366- i = i + 1
1377+ i += 1
13671378 if i :
13681379 text .delete ("insert - %d chars" % i , "insert" )
1369- # strip whitespace after insert point
1380+
1381+ # Strip whitespace after insert point.
13701382 while text .get ("insert" ) in " \t " :
13711383 text .delete ("insert" )
1372- # start new line
1384+
1385+ # Insert new line.
13731386 text .insert ("insert" , '\n ' )
13741387
1375- # adjust indentation for continuations and block
1376- # open/close first need to find the last stmt
1388+ # Adjust indentation for continuations and block open/close.
1389+ # First need to find the last statement.
13771390 lno = index2line (text .index ('insert' ))
13781391 y = pyparse .Parser (self .indentwidth , self .tabwidth )
13791392 if not self .prompt_last_line :
@@ -1383,7 +1396,7 @@ def newline_and_indent_event(self, event):
13831396 rawtext = text .get (startatindex , "insert" )
13841397 y .set_code (rawtext )
13851398 bod = y .find_good_parse_start (
1386- self ._build_char_in_string_func (startatindex ))
1399+ self ._build_char_in_string_func (startatindex ))
13871400 if bod is not None or startat == 1 :
13881401 break
13891402 y .set_lo (bod or 0 )
@@ -1399,26 +1412,26 @@ def newline_and_indent_event(self, event):
13991412
14001413 c = y .get_continuation_type ()
14011414 if c != pyparse .C_NONE :
1402- # The current stmt hasn't ended yet.
1415+ # The current statement hasn't ended yet.
14031416 if c == pyparse .C_STRING_FIRST_LINE :
1404- # after the first line of a string; do not indent at all
1417+ # After the first line of a string do not indent at all.
14051418 pass
14061419 elif c == pyparse .C_STRING_NEXT_LINES :
1407- # inside a string which started before this line;
1408- # just mimic the current indent
1420+ # Inside a string which started before this line;
1421+ # just mimic the current indent.
14091422 text .insert ("insert" , indent )
14101423 elif c == pyparse .C_BRACKET :
1411- # line up with the first (if any) element of the
1424+ # Line up with the first (if any) element of the
14121425 # last open bracket structure; else indent one
14131426 # level beyond the indent of the line with the
1414- # last open bracket
1427+ # last open bracket.
14151428 self .reindent_to (y .compute_bracket_indent ())
14161429 elif c == pyparse .C_BACKSLASH :
1417- # if more than one line in this stmt already, just
1430+ # If more than one line in this statement already, just
14181431 # mimic the current indent; else if initial line
14191432 # has a start on an assignment stmt, indent to
14201433 # beyond leftmost =; else to beyond first chunk of
1421- # non-whitespace on initial line
1434+ # non-whitespace on initial line.
14221435 if y .get_num_lines_in_stmt () > 1 :
14231436 text .insert ("insert" , indent )
14241437 else :
@@ -1427,9 +1440,9 @@ def newline_and_indent_event(self, event):
14271440 assert 0 , "bogus continuation type %r" % (c ,)
14281441 return "break"
14291442
1430- # This line starts a brand new stmt ; indent relative to
1443+ # This line starts a brand new statement ; indent relative to
14311444 # indentation of initial line of closest preceding
1432- # interesting stmt .
1445+ # interesting statement .
14331446 indent = y .get_base_indent_string ()
14341447 text .insert ("insert" , indent )
14351448 if y .is_block_opener ():
0 commit comments