When the user presses return, insert a newline or execute the code.
(event)
| 408 | |
| 409 | def newline_or_execute_outer(shell): |
| 410 | def newline_or_execute(event): |
| 411 | """When the user presses return, insert a newline or execute the code.""" |
| 412 | b = event.current_buffer |
| 413 | d = b.document |
| 414 | |
| 415 | if b.complete_state: |
| 416 | cc = b.complete_state.current_completion |
| 417 | if cc: |
| 418 | b.apply_completion(cc) |
| 419 | else: |
| 420 | b.cancel_completion() |
| 421 | return |
| 422 | |
| 423 | # If there's only one line, treat it as if the cursor is at the end. |
| 424 | # See https://github.com/ipython/ipython/issues/10425 |
| 425 | if d.line_count == 1: |
| 426 | check_text = d.text |
| 427 | else: |
| 428 | check_text = d.text[: d.cursor_position] |
| 429 | status, indent = shell.check_complete(check_text) |
| 430 | |
| 431 | # if all we have after the cursor is whitespace: reformat current text |
| 432 | # before cursor |
| 433 | after_cursor = d.text[d.cursor_position :] |
| 434 | reformatted = False |
| 435 | if not after_cursor.strip(): |
| 436 | reformat_text_before_cursor(b, d, shell) |
| 437 | reformatted = True |
| 438 | if not ( |
| 439 | d.on_last_line |
| 440 | or d.cursor_position_row >= d.line_count - d.empty_line_count_at_the_end() |
| 441 | ): |
| 442 | if shell.autoindent: |
| 443 | b.insert_text("\n" + indent) |
| 444 | else: |
| 445 | b.insert_text("\n") |
| 446 | return |
| 447 | |
| 448 | if (status != "incomplete") and b.accept_handler: |
| 449 | if not reformatted: |
| 450 | reformat_text_before_cursor(b, d, shell) |
| 451 | b.validate_and_handle() |
| 452 | else: |
| 453 | if shell.autoindent: |
| 454 | b.insert_text("\n" + indent) |
| 455 | else: |
| 456 | b.insert_text("\n") |
| 457 | |
| 458 | return newline_or_execute |
| 459 |
nothing calls this directly
no test coverage detected
searching dependent graphs…