|
28 | 28 | import sys
|
29 | 29 |
|
30 | 30 | __all__ = (
|
31 |
| - 'create_layout', |
| 31 | + 'PtPythonLayout', |
32 | 32 | 'CompletionVisualisation',
|
33 | 33 | )
|
34 | 34 |
|
@@ -480,139 +480,143 @@ def extra_condition():
|
480 | 480 | filter=visible)
|
481 | 481 |
|
482 | 482 |
|
483 |
| -def create_layout(python_input, |
484 |
| - lexer=PythonLexer, |
485 |
| - extra_body=None, extra_toolbars=None, |
486 |
| - extra_buffer_processors=None, input_buffer_height=None): |
487 |
| - D = Dimension |
488 |
| - extra_body = [extra_body] if extra_body else [] |
489 |
| - extra_toolbars = extra_toolbars or [] |
490 |
| - extra_buffer_processors = extra_buffer_processors or [] |
491 |
| - input_buffer_height = input_buffer_height or D(min=6) |
492 |
| - |
493 |
| - search_toolbar = SearchToolbar(python_input.search_buffer) |
494 |
| - |
495 |
| - def create_python_input_window(): |
496 |
| - def menu_position(): |
497 |
| - """ |
498 |
| - When there is no autocompletion menu to be shown, and we have a signature, |
499 |
| - set the pop-up position at `bracket_start`. |
500 |
| - """ |
501 |
| - b = python_input.default_buffer |
502 |
| - |
503 |
| - if b.complete_state is None and python_input.signatures: |
504 |
| - row, col = python_input.signatures[0].bracket_start |
505 |
| - index = b.document.translate_row_col_to_index(row - 1, col) |
506 |
| - return index |
507 |
| - |
508 |
| - return Window( |
509 |
| - BufferControl( |
510 |
| - buffer=python_input.default_buffer, |
511 |
| - search_buffer_control=search_toolbar.control, |
512 |
| - lexer=lexer, |
513 |
| - include_default_input_processors=False, |
514 |
| - input_processors=[ |
515 |
| - ConditionalProcessor( |
516 |
| - processor=HighlightIncrementalSearchProcessor(), |
517 |
| - filter=has_focus(SEARCH_BUFFER) | has_focus(search_toolbar.control), |
518 |
| - ), |
519 |
| - HighlightSelectionProcessor(), |
520 |
| - DisplayMultipleCursors(), |
521 |
| - # Show matching parentheses, but only while editing. |
522 |
| - ConditionalProcessor( |
523 |
| - processor=HighlightMatchingBracketProcessor(chars='[](){}'), |
524 |
| - filter=has_focus(DEFAULT_BUFFER) & ~is_done & |
525 |
| - Condition(lambda: python_input.highlight_matching_parenthesis)), |
526 |
| - ConditionalProcessor( |
527 |
| - processor=AppendAutoSuggestion(), |
528 |
| - filter=~is_done) |
529 |
| - ] + extra_buffer_processors, |
530 |
| - menu_position=menu_position, |
531 |
| - |
532 |
| - # Make sure that we always see the result of an reverse-i-search: |
533 |
| - preview_search=True, |
534 |
| - ), |
535 |
| - left_margins=[PythonPromptMargin(python_input)], |
536 |
| - # Scroll offsets. The 1 at the bottom is important to make sure the |
537 |
| - # cursor is never below the "Press [Meta+Enter]" message which is a float. |
538 |
| - scroll_offsets=ScrollOffsets(bottom=1, left=4, right=4), |
539 |
| - # As long as we're editing, prefer a minimal height of 6. |
540 |
| - height=(lambda: ( |
541 |
| - None if get_app().is_done or python_input.show_exit_confirmation |
542 |
| - else input_buffer_height)), |
543 |
| - wrap_lines=Condition(lambda: python_input.wrap_lines), |
544 |
| - ) |
545 |
| - |
546 |
| - root_container = HSplit([ |
547 |
| - VSplit([ |
548 |
| - HSplit([ |
549 |
| - FloatContainer( |
550 |
| - content=HSplit( |
551 |
| - [create_python_input_window()] + extra_body |
552 |
| - ), |
553 |
| - floats=[ |
554 |
| - Float(xcursor=True, |
555 |
| - ycursor=True, |
556 |
| - content=ConditionalContainer( |
557 |
| - content=CompletionsMenu( |
558 |
| - scroll_offset=( |
559 |
| - lambda: python_input.completion_menu_scroll_offset), |
560 |
| - max_height=12), |
561 |
| - filter=show_completions_menu(python_input))), |
562 |
| - Float(xcursor=True, |
563 |
| - ycursor=True, |
564 |
| - content=ConditionalContainer( |
565 |
| - content=MultiColumnCompletionsMenu(), |
566 |
| - filter=show_multi_column_completions_menu(python_input))), |
567 |
| - Float(xcursor=True, |
568 |
| - ycursor=True, |
569 |
| - content=signature_toolbar(python_input)), |
570 |
| - Float(left=2, |
571 |
| - bottom=1, |
572 |
| - content=exit_confirmation(python_input)), |
573 |
| - Float(bottom=0, right=0, height=1, |
574 |
| - content=meta_enter_message(python_input), |
575 |
| - hide_when_covering_content=True), |
576 |
| - Float(bottom=1, left=1, right=0, content=python_sidebar_help(python_input)), |
577 |
| - ]), |
578 |
| - ArgToolbar(), |
579 |
| - search_toolbar, |
580 |
| - SystemToolbar(), |
581 |
| - ValidationToolbar(), |
582 |
| - ConditionalContainer( |
583 |
| - content=CompletionsToolbar(), |
584 |
| - filter=show_completions_toolbar(python_input)), |
585 |
| - |
586 |
| - # Docstring region. |
587 |
| - ConditionalContainer( |
588 |
| - content=Window( |
589 |
| - height=D.exact(1), |
590 |
| - char='\u2500', |
591 |
| - style='class:separator'), |
592 |
| - filter=HasSignature(python_input) & ShowDocstring(python_input) & ~is_done), |
593 |
| - ConditionalContainer( |
594 |
| - content=Window( |
595 |
| - BufferControl( |
596 |
| - buffer=python_input.docstring_buffer, |
597 |
| - lexer=SimpleLexer(style='class:docstring'), |
598 |
| - #lexer=PythonLexer, |
| 483 | +class PtPythonLayout(object): |
| 484 | + def __init__(self, python_input, lexer=PythonLexer, extra_body=None, |
| 485 | + extra_toolbars=None, extra_buffer_processors=None, |
| 486 | + input_buffer_height=None): |
| 487 | + D = Dimension |
| 488 | + extra_body = [extra_body] if extra_body else [] |
| 489 | + extra_toolbars = extra_toolbars or [] |
| 490 | + extra_buffer_processors = extra_buffer_processors or [] |
| 491 | + input_buffer_height = input_buffer_height or D(min=6) |
| 492 | + |
| 493 | + search_toolbar = SearchToolbar(python_input.search_buffer) |
| 494 | + |
| 495 | + def create_python_input_window(): |
| 496 | + def menu_position(): |
| 497 | + """ |
| 498 | + When there is no autocompletion menu to be shown, and we have a |
| 499 | + signature, set the pop-up position at `bracket_start`. |
| 500 | + """ |
| 501 | + b = python_input.default_buffer |
| 502 | + |
| 503 | + if b.complete_state is None and python_input.signatures: |
| 504 | + row, col = python_input.signatures[0].bracket_start |
| 505 | + index = b.document.translate_row_col_to_index(row - 1, col) |
| 506 | + return index |
| 507 | + |
| 508 | + return Window( |
| 509 | + BufferControl( |
| 510 | + buffer=python_input.default_buffer, |
| 511 | + search_buffer_control=search_toolbar.control, |
| 512 | + lexer=lexer, |
| 513 | + include_default_input_processors=False, |
| 514 | + input_processors=[ |
| 515 | + ConditionalProcessor( |
| 516 | + processor=HighlightIncrementalSearchProcessor(), |
| 517 | + filter=has_focus(SEARCH_BUFFER) | has_focus(search_toolbar.control), |
599 | 518 | ),
|
600 |
| - height=D(max=12)), |
601 |
| - filter=HasSignature(python_input) & ShowDocstring(python_input) & ~is_done), |
602 |
| - ]), |
603 |
| - ConditionalContainer( |
604 |
| - content=HSplit([ |
605 |
| - python_sidebar(python_input), |
606 |
| - Window(style='class:sidebar,separator', height=1), |
607 |
| - python_sidebar_navigation(python_input), |
| 519 | + HighlightSelectionProcessor(), |
| 520 | + DisplayMultipleCursors(), |
| 521 | + # Show matching parentheses, but only while editing. |
| 522 | + ConditionalProcessor( |
| 523 | + processor=HighlightMatchingBracketProcessor(chars='[](){}'), |
| 524 | + filter=has_focus(DEFAULT_BUFFER) & ~is_done & |
| 525 | + Condition(lambda: python_input.highlight_matching_parenthesis)), |
| 526 | + ConditionalProcessor( |
| 527 | + processor=AppendAutoSuggestion(), |
| 528 | + filter=~is_done) |
| 529 | + ] + extra_buffer_processors, |
| 530 | + menu_position=menu_position, |
| 531 | + |
| 532 | + # Make sure that we always see the result of an reverse-i-search: |
| 533 | + preview_search=True, |
| 534 | + ), |
| 535 | + left_margins=[PythonPromptMargin(python_input)], |
| 536 | + # Scroll offsets. The 1 at the bottom is important to make sure |
| 537 | + # the cursor is never below the "Press [Meta+Enter]" message |
| 538 | + # which is a float. |
| 539 | + scroll_offsets=ScrollOffsets(bottom=1, left=4, right=4), |
| 540 | + # As long as we're editing, prefer a minimal height of 6. |
| 541 | + height=(lambda: ( |
| 542 | + None if get_app().is_done or python_input.show_exit_confirmation |
| 543 | + else input_buffer_height)), |
| 544 | + wrap_lines=Condition(lambda: python_input.wrap_lines), |
| 545 | + ) |
| 546 | + |
| 547 | + sidebar = python_sidebar(python_input) |
| 548 | + |
| 549 | + root_container = HSplit([ |
| 550 | + VSplit([ |
| 551 | + HSplit([ |
| 552 | + FloatContainer( |
| 553 | + content=HSplit( |
| 554 | + [create_python_input_window()] + extra_body |
| 555 | + ), |
| 556 | + floats=[ |
| 557 | + Float(xcursor=True, |
| 558 | + ycursor=True, |
| 559 | + content=ConditionalContainer( |
| 560 | + content=CompletionsMenu( |
| 561 | + scroll_offset=( |
| 562 | + lambda: python_input.completion_menu_scroll_offset), |
| 563 | + max_height=12), |
| 564 | + filter=show_completions_menu(python_input))), |
| 565 | + Float(xcursor=True, |
| 566 | + ycursor=True, |
| 567 | + content=ConditionalContainer( |
| 568 | + content=MultiColumnCompletionsMenu(), |
| 569 | + filter=show_multi_column_completions_menu(python_input))), |
| 570 | + Float(xcursor=True, |
| 571 | + ycursor=True, |
| 572 | + content=signature_toolbar(python_input)), |
| 573 | + Float(left=2, |
| 574 | + bottom=1, |
| 575 | + content=exit_confirmation(python_input)), |
| 576 | + Float(bottom=0, right=0, height=1, |
| 577 | + content=meta_enter_message(python_input), |
| 578 | + hide_when_covering_content=True), |
| 579 | + Float(bottom=1, left=1, right=0, content=python_sidebar_help(python_input)), |
| 580 | + ]), |
| 581 | + ArgToolbar(), |
| 582 | + search_toolbar, |
| 583 | + SystemToolbar(), |
| 584 | + ValidationToolbar(), |
| 585 | + ConditionalContainer( |
| 586 | + content=CompletionsToolbar(), |
| 587 | + filter=show_completions_toolbar(python_input)), |
| 588 | + |
| 589 | + # Docstring region. |
| 590 | + ConditionalContainer( |
| 591 | + content=Window( |
| 592 | + height=D.exact(1), |
| 593 | + char='\u2500', |
| 594 | + style='class:separator'), |
| 595 | + filter=HasSignature(python_input) & ShowDocstring(python_input) & ~is_done), |
| 596 | + ConditionalContainer( |
| 597 | + content=Window( |
| 598 | + BufferControl( |
| 599 | + buffer=python_input.docstring_buffer, |
| 600 | + lexer=SimpleLexer(style='class:docstring'), |
| 601 | + #lexer=PythonLexer, |
| 602 | + ), |
| 603 | + height=D(max=12)), |
| 604 | + filter=HasSignature(python_input) & ShowDocstring(python_input) & ~is_done), |
608 | 605 | ]),
|
609 |
| - filter=ShowSidebar(python_input) & ~is_done) |
610 |
| - ]), |
611 |
| - ] + extra_toolbars + [ |
612 |
| - VSplit([ |
613 |
| - status_bar(python_input), |
614 |
| - show_sidebar_button_info(python_input), |
| 606 | + ConditionalContainer( |
| 607 | + content=HSplit([ |
| 608 | + sidebar, |
| 609 | + Window(style='class:sidebar,separator', height=1), |
| 610 | + python_sidebar_navigation(python_input), |
| 611 | + ]), |
| 612 | + filter=ShowSidebar(python_input) & ~is_done) |
| 613 | + ]), |
| 614 | + ] + extra_toolbars + [ |
| 615 | + VSplit([ |
| 616 | + status_bar(python_input), |
| 617 | + show_sidebar_button_info(python_input), |
| 618 | + ]) |
615 | 619 | ])
|
616 |
| - ]) |
617 | 620 |
|
618 |
| - return Layout(root_container) |
| 621 | + self.layout = Layout(root_container) |
| 622 | + self.sidebar = sidebar |
0 commit comments