Wrapper around completer that hides private fields, depending on whether or not public fields are shown. (The reason this is implemented as a `Completer` wrapper is because this way it works also with `FuzzyCompleter`.)
| 613 | |
| 614 | |
| 615 | class HidePrivateCompleter(Completer): |
| 616 | """ |
| 617 | Wrapper around completer that hides private fields, depending on whether or |
| 618 | not public fields are shown. |
| 619 | |
| 620 | (The reason this is implemented as a `Completer` wrapper is because this |
| 621 | way it works also with `FuzzyCompleter`.) |
| 622 | """ |
| 623 | |
| 624 | def __init__( |
| 625 | self, |
| 626 | completer: Completer, |
| 627 | complete_private_attributes: Callable[[], CompletePrivateAttributes], |
| 628 | ) -> None: |
| 629 | self.completer = completer |
| 630 | self.complete_private_attributes = complete_private_attributes |
| 631 | |
| 632 | def get_completions( |
| 633 | self, document: Document, complete_event: CompleteEvent |
| 634 | ) -> Iterable[Completion]: |
| 635 | completions = list( |
| 636 | # Limit at 5k completions for performance. |
| 637 | islice(self.completer.get_completions(document, complete_event), 0, 5000) |
| 638 | ) |
| 639 | complete_private_attributes = self.complete_private_attributes() |
| 640 | hide_private = False |
| 641 | |
| 642 | def is_private(completion: Completion) -> bool: |
| 643 | text = fragment_list_to_text(to_formatted_text(completion.display)) |
| 644 | return text.startswith("_") |
| 645 | |
| 646 | if complete_private_attributes == CompletePrivateAttributes.NEVER: |
| 647 | hide_private = True |
| 648 | |
| 649 | elif complete_private_attributes == CompletePrivateAttributes.IF_NO_PUBLIC: |
| 650 | hide_private = any(not is_private(completion) for completion in completions) |
| 651 | |
| 652 | if hide_private: |
| 653 | completions = [ |
| 654 | completion for completion in completions if not is_private(completion) |
| 655 | ] |
| 656 | |
| 657 | return completions |
| 658 | |
| 659 | |
| 660 | class ReprFailedError(Exception): |