MCPcopy Create free account
hub / github.com/ipython/ipython / newline_or_execute

Function newline_or_execute

IPython/terminal/shortcuts.py:109–154  ·  view source on GitHub ↗

When the user presses return, insert a newline or execute the code.

(event)

Source from the content-addressed store, hash-verified

107def newline_or_execute_outer(shell):
108
109 def newline_or_execute(event):
110 """When the user presses return, insert a newline or execute the code."""
111 b = event.current_buffer
112 d = b.document
113
114 if b.complete_state:
115 cc = b.complete_state.current_completion
116 if cc:
117 b.apply_completion(cc)
118 else:
119 b.cancel_completion()
120 return
121
122 # If there's only one line, treat it as if the cursor is at the end.
123 # See https://github.com/ipython/ipython/issues/10425
124 if d.line_count == 1:
125 check_text = d.text
126 else:
127 check_text = d.text[:d.cursor_position]
128 status, indent = shell.check_complete(check_text)
129
130 # if all we have after the cursor is whitespace: reformat current text
131 # before cursor
132 after_cursor = d.text[d.cursor_position:]
133 reformatted = False
134 if not after_cursor.strip():
135 reformat_text_before_cursor(b, d, shell)
136 reformatted = True
137 if not (d.on_last_line or
138 d.cursor_position_row >= d.line_count - d.empty_line_count_at_the_end()
139 ):
140 if shell.autoindent:
141 b.insert_text('\n' + indent)
142 else:
143 b.insert_text('\n')
144 return
145
146 if (status != 'incomplete') and b.accept_handler:
147 if not reformatted:
148 reformat_text_before_cursor(b, d, shell)
149 b.validate_and_handle()
150 else:
151 if shell.autoindent:
152 b.insert_text('\n' + indent)
153 else:
154 b.insert_text('\n')
155 return newline_or_execute
156
157

Callers

nothing calls this directly

Calls 2

check_completeMethod · 0.45

Tested by

no test coverage detected