MCPcopy Index your code
hub / github.com/RustPython/RustPython / do

Method do

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected