Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/key_binding/bindings/auto_suggest.py: 29%

31 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1""" 

2Key bindings for auto suggestion (for fish-style auto suggestion). 

3""" 

4from __future__ import annotations 

5 

6import re 

7 

8from prompt_toolkit.application.current import get_app 

9from prompt_toolkit.filters import Condition, emacs_mode 

10from prompt_toolkit.key_binding.key_bindings import KeyBindings 

11from prompt_toolkit.key_binding.key_processor import KeyPressEvent 

12 

13__all__ = [ 

14 "load_auto_suggest_bindings", 

15] 

16 

17E = KeyPressEvent 

18 

19 

20def load_auto_suggest_bindings() -> KeyBindings: 

21 """ 

22 Key bindings for accepting auto suggestion text. 

23 

24 (This has to come after the Vi bindings, because they also have an 

25 implementation for the "right arrow", but we really want the suggestion 

26 binding when a suggestion is available.) 

27 """ 

28 key_bindings = KeyBindings() 

29 handle = key_bindings.add 

30 

31 @Condition 

32 def suggestion_available() -> bool: 

33 app = get_app() 

34 return ( 

35 app.current_buffer.suggestion is not None 

36 and len(app.current_buffer.suggestion.text) > 0 

37 and app.current_buffer.document.is_cursor_at_the_end 

38 ) 

39 

40 @handle("c-f", filter=suggestion_available) 

41 @handle("c-e", filter=suggestion_available) 

42 @handle("right", filter=suggestion_available) 

43 def _accept(event: E) -> None: 

44 """ 

45 Accept suggestion. 

46 """ 

47 b = event.current_buffer 

48 suggestion = b.suggestion 

49 

50 if suggestion: 

51 b.insert_text(suggestion.text) 

52 

53 @handle("escape", "f", filter=suggestion_available & emacs_mode) 

54 def _fill(event: E) -> None: 

55 """ 

56 Fill partial suggestion. 

57 """ 

58 b = event.current_buffer 

59 suggestion = b.suggestion 

60 

61 if suggestion: 

62 t = re.split(r"([^\s/]+(?:\s+|/))", suggestion.text) 

63 b.insert_text(next(x for x in t if x)) 

64 

65 return key_bindings