MCPcopy Index your code
hub / github.com/python/cpython / newline_and_indent_event

Method newline_and_indent_event

Lib/idlelib/editor.py:1376–1489  ·  view source on GitHub ↗

Insert a newline and indentation after Enter keypress event. Properly position the cursor on the new line based on information from the current line. This takes into account if the current line is a shell prompt, is empty, has selected text, contains a block opener,

(self, event)

Source from the content-addressed store, hash-verified

1374 text.undo_block_stop()
1375
1376 def newline_and_indent_event(self, event):
1377 """Insert a newline and indentation after Enter keypress event.
1378
1379 Properly position the cursor on the new line based on information
1380 from the current line. This takes into account if the current line
1381 is a shell prompt, is empty, has selected text, contains a block
1382 opener, contains a block closer, is a continuation line, or
1383 is inside a string.
1384 """
1385 text = self.text
1386 first, last = self.get_selection_indices()
1387 text.undo_block_start()
1388 try: # Close undo block and expose new line in finally clause.
1389 if first and last:
1390 text.delete(first, last)
1391 text.mark_set("insert", first)
1392 line = text.get("insert linestart", "insert")
1393
1394 # Count leading whitespace for indent size.
1395 i, n = 0, len(line)
1396 while i < n and line[i] in " \t":
1397 i += 1
1398 if i == n:
1399 # The cursor is in or at leading indentation in a continuation
1400 # line; just inject an empty line at the start.
1401 text.insert("insert linestart", '\n',
1402 self.user_input_insert_tags)
1403 return "break"
1404 indent = line[:i]
1405
1406 # Strip whitespace before insert point unless it's in the prompt.
1407 i = 0
1408 while line and line[-1] in " \t":
1409 line = line[:-1]
1410 i += 1
1411 if i:
1412 text.delete("insert - %d chars" % i, "insert")
1413
1414 # Strip whitespace after insert point.
1415 while text.get("insert") in " \t":
1416 text.delete("insert")
1417
1418 # Insert new line.
1419 text.insert("insert", '\n', self.user_input_insert_tags)
1420
1421 # Adjust indentation for continuations and block open/close.
1422 # First need to find the last statement.
1423 lno = index2line(text.index('insert'))
1424 y = pyparse.Parser(self.indentwidth, self.tabwidth)
1425 if not self.prompt_last_line:
1426 for context in self.num_context_lines:
1427 startat = max(lno - context, 1)
1428 startatindex = repr(startat) + ".0"
1429 rawtext = text.get(startatindex, "insert")
1430 y.set_code(rawtext)
1431 bod = y.find_good_parse_start(
1432 self._build_char_in_string_func(startatindex))
1433 if bod is not None or startat == 1:

Callers 3

linefeed_callbackMethod · 0.80
enter_callbackMethod · 0.80
recallMethod · 0.80

Calls 15

get_selection_indicesMethod · 0.95
set_codeMethod · 0.95
find_good_parse_startMethod · 0.95
set_loMethod · 0.95
get_continuation_typeMethod · 0.95
reindent_toMethod · 0.95
get_num_lines_in_stmtMethod · 0.95
is_block_openerMethod · 0.95

Tested by

no test coverage detected