1from __future__ import annotations
2
3from collections.abc import Callable, Iterable, Mapping, Sequence
4from re import Pattern
5
6from prompt_toolkit.completion import CompleteEvent, Completer, Completion
7from prompt_toolkit.document import Document
8from prompt_toolkit.formatted_text import AnyFormattedText
9
10__all__ = [
11 "WordCompleter",
12]
13
14
15class WordCompleter(Completer):
16 """
17 Simple autocompletion on a list of words.
18
19 :param words: List of words or callable that returns a list of words.
20 :param ignore_case: If True, case-insensitive completion.
21 :param meta_dict: Optional dict mapping words to their meta-text. (This
22 should map strings to strings or formatted text.)
23 :param WORD: When True, use WORD characters.
24 :param sentence: When True, don't complete by comparing the word before the
25 cursor, but by comparing all the text before the cursor. In this case,
26 the list of words is just a list of strings, where each string can
27 contain spaces. (Can not be used together with the WORD option.)
28 :param match_middle: When True, match not only the start, but also in the
29 middle of the word.
30 :param pattern: Optional compiled regex for finding the word before
31 the cursor to complete. When given, use this regex pattern instead of
32 default one (see document._FIND_WORD_RE)
33 """
34
35 def __init__(
36 self,
37 words: Sequence[str] | Callable[[], Sequence[str]],
38 ignore_case: bool = False,
39 display_dict: Mapping[str, AnyFormattedText] | None = None,
40 meta_dict: Mapping[str, AnyFormattedText] | None = None,
41 WORD: bool = False,
42 sentence: bool = False,
43 match_middle: bool = False,
44 pattern: Pattern[str] | None = None,
45 ) -> None:
46 assert not (WORD and sentence)
47
48 self.words = words
49 self.ignore_case = ignore_case
50 self.display_dict = display_dict or {}
51 self.meta_dict = meta_dict or {}
52 self.WORD = WORD
53 self.sentence = sentence
54 self.match_middle = match_middle
55 self.pattern = pattern
56
57 def get_completions(
58 self, document: Document, complete_event: CompleteEvent
59 ) -> Iterable[Completion]:
60 # Get list of words.
61 words = self.words
62 if callable(words):
63 words = words()
64
65 # Get word/text before cursor.
66 if self.sentence:
67 word_before_cursor = document.text_before_cursor
68 else:
69 word_before_cursor = document.get_word_before_cursor(
70 WORD=self.WORD, pattern=self.pattern
71 )
72
73 if self.ignore_case:
74 word_before_cursor = word_before_cursor.lower()
75
76 def word_matches(word: str) -> bool:
77 """True when the word before the cursor matches."""
78 if self.ignore_case:
79 word = word.lower()
80
81 if self.match_middle:
82 return word_before_cursor in word
83 else:
84 return word.startswith(word_before_cursor)
85
86 for a in words:
87 if word_matches(a):
88 display = self.display_dict.get(a, a)
89 display_meta = self.meta_dict.get(a, "")
90 yield Completion(
91 text=a,
92 start_position=-len(word_before_cursor),
93 display=display,
94 display_meta=display_meta,
95 )