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

1from __future__ import annotations 

2 

3from typing import Iterable 

4 

5from prompt_toolkit.document import Document 

6 

7from .base import CompleteEvent, Completer, Completion 

8 

9__all__ = ["DeduplicateCompleter"] 

10 

11 

12class DeduplicateCompleter(Completer): 

13 """ 

14 Wrapper around a completer that removes duplicates. Only the first unique 

15 completions are kept. 

16 

17 Completions are considered to be a duplicate if they result in the same 

18 document text when they would be applied. 

19 """ 

20 

21 def __init__(self, completer: Completer) -> None: 

22 self.completer = completer 

23 

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() 

29 

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 ) 

36 

37 if text_if_applied == document.text: 

38 # Don't include completions that don't have any effect at all. 

39 continue 

40 

41 if text_if_applied in found_so_far: 

42 continue 

43 

44 found_so_far.add(text_if_applied) 

45 yield completion