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

Method do

Lib/_pyrepl/readline.py:277–320  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

275
276class maybe_accept(commands.Command):
277 def do(self) -> None:
278 r: ReadlineAlikeReader
279 r = self.reader # type: ignore[assignment]
280 r.dirty = True # this is needed to hide the completion menu, if visible
281
282 # if there are already several lines and the cursor
283 # is not on the last one, always insert a new \n.
284 text = r.get_unicode()
285
286 if "\n" in r.buffer[r.pos :] or (
287 r.more_lines is not None and r.more_lines(text)
288 ):
289 def _newline_before_pos():
290 before_idx = r.pos - 1
291 while before_idx > 0 and text[before_idx].isspace():
292 before_idx -= 1
293 return text[before_idx : r.pos].count("\n") > 0
294
295 # if there's already a new line before the cursor then
296 # even if the cursor is followed by whitespace, we assume
297 # the user is trying to terminate the block
298 if _newline_before_pos() and text[r.pos:].isspace():
299 self.finish = True
300 return
301
302 # auto-indent the next line like the previous line
303 prevlinestart, indent = _get_previous_line_indent(r.buffer, r.pos)
304 r.insert("\n")
305 if not self.reader.paste_mode:
306 if indent:
307 for i in range(prevlinestart, prevlinestart + indent):
308 r.insert(r.buffer[i])
309 r.update_last_used_indentation()
310 if _should_auto_indent(r.buffer, r.pos):
311 if r.last_used_indentation is not None:
312 indentation = r.last_used_indentation
313 else:
314 # default
315 indentation = " " * 4
316 r.insert(indentation)
317 elif not self.reader.paste_mode:
318 self.finish = True
319 else:
320 r.insert("\n")
321
322
323class backspace_dedent(commands.Command):

Callers

nothing calls this directly

Calls 6

_should_auto_indentFunction · 0.85
get_unicodeMethod · 0.80
isspaceMethod · 0.80
insertMethod · 0.45

Tested by

no test coverage detected