Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/completion/deduplicate.py: 44%
18 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1from __future__ import annotations
3from typing import Iterable
5from prompt_toolkit.document import Document
7from .base import CompleteEvent, Completer, Completion
9__all__ = ["DeduplicateCompleter"]
12class DeduplicateCompleter(Completer):
13 """
14 Wrapper around a completer that removes duplicates. Only the first unique
15 completions are kept.
17 Completions are considered to be a duplicate if they result in the same
18 document text when they would be applied.
19 """
21 def __init__(self, completer: Completer) -> None:
22 self.completer = completer
24 def get_completions(
25 self, document: Document, complete_event: CompleteEvent
26 ) -> Iterable[Completion]:
27 # Keep track of the document strings we'd get after applying any completion.
28 found_so_far: set[str] = set()
30 for completion in self.completer.get_completions(document, complete_event):
31 text_if_applied = (
32 document.text[: document.cursor_position + completion.start_position]
33 + completion.text
34 + document.text[document.cursor_position :]
35 )
37 if text_if_applied == document.text:
38 # Don't include completions that don't have any effect at all.
39 continue
41 if text_if_applied in found_so_far:
42 continue
44 found_so_far.add(text_if_applied)
45 yield completion