base.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. """
  2. """
  3. from __future__ import annotations
  4. from abc import ABCMeta, abstractmethod
  5. from typing import AsyncGenerator, Callable, Iterable, Sequence
  6. from prompt_toolkit.document import Document
  7. from prompt_toolkit.eventloop import aclosing, generator_to_async_generator
  8. from prompt_toolkit.filters import FilterOrBool, to_filter
  9. from prompt_toolkit.formatted_text import AnyFormattedText, StyleAndTextTuples
  10. __all__ = [
  11. "Completion",
  12. "Completer",
  13. "ThreadedCompleter",
  14. "DummyCompleter",
  15. "DynamicCompleter",
  16. "CompleteEvent",
  17. "ConditionalCompleter",
  18. "merge_completers",
  19. "get_common_complete_suffix",
  20. ]
  21. class Completion:
  22. """
  23. :param text: The new string that will be inserted into the document.
  24. :param start_position: Position relative to the cursor_position where the
  25. new text will start. The text will be inserted between the
  26. start_position and the original cursor position.
  27. :param display: (optional string or formatted text) If the completion has
  28. to be displayed differently in the completion menu.
  29. :param display_meta: (Optional string or formatted text) Meta information
  30. about the completion, e.g. the path or source where it's coming from.
  31. This can also be a callable that returns a string.
  32. :param style: Style string.
  33. :param selected_style: Style string, used for a selected completion.
  34. This can override the `style` parameter.
  35. """
  36. def __init__(
  37. self,
  38. text: str,
  39. start_position: int = 0,
  40. display: AnyFormattedText | None = None,
  41. display_meta: AnyFormattedText | None = None,
  42. style: str = "",
  43. selected_style: str = "",
  44. ) -> None:
  45. from prompt_toolkit.formatted_text import to_formatted_text
  46. self.text = text
  47. self.start_position = start_position
  48. self._display_meta = display_meta
  49. if display is None:
  50. display = text
  51. self.display = to_formatted_text(display)
  52. self.style = style
  53. self.selected_style = selected_style
  54. assert self.start_position <= 0
  55. def __repr__(self) -> str:
  56. if isinstance(self.display, str) and self.display == self.text:
  57. return "{}(text={!r}, start_position={!r})".format(
  58. self.__class__.__name__,
  59. self.text,
  60. self.start_position,
  61. )
  62. else:
  63. return "{}(text={!r}, start_position={!r}, display={!r})".format(
  64. self.__class__.__name__,
  65. self.text,
  66. self.start_position,
  67. self.display,
  68. )
  69. def __eq__(self, other: object) -> bool:
  70. if not isinstance(other, Completion):
  71. return False
  72. return (
  73. self.text == other.text
  74. and self.start_position == other.start_position
  75. and self.display == other.display
  76. and self._display_meta == other._display_meta
  77. )
  78. def __hash__(self) -> int:
  79. return hash((self.text, self.start_position, self.display, self._display_meta))
  80. @property
  81. def display_text(self) -> str:
  82. "The 'display' field as plain text."
  83. from prompt_toolkit.formatted_text import fragment_list_to_text
  84. return fragment_list_to_text(self.display)
  85. @property
  86. def display_meta(self) -> StyleAndTextTuples:
  87. "Return meta-text. (This is lazy when using a callable)."
  88. from prompt_toolkit.formatted_text import to_formatted_text
  89. return to_formatted_text(self._display_meta or "")
  90. @property
  91. def display_meta_text(self) -> str:
  92. "The 'meta' field as plain text."
  93. from prompt_toolkit.formatted_text import fragment_list_to_text
  94. return fragment_list_to_text(self.display_meta)
  95. def new_completion_from_position(self, position: int) -> Completion:
  96. """
  97. (Only for internal use!)
  98. Get a new completion by splitting this one. Used by `Application` when
  99. it needs to have a list of new completions after inserting the common
  100. prefix.
  101. """
  102. assert position - self.start_position >= 0
  103. return Completion(
  104. text=self.text[position - self.start_position :],
  105. display=self.display,
  106. display_meta=self._display_meta,
  107. )
  108. class CompleteEvent:
  109. """
  110. Event that called the completer.
  111. :param text_inserted: When True, it means that completions are requested
  112. because of a text insert. (`Buffer.complete_while_typing`.)
  113. :param completion_requested: When True, it means that the user explicitly
  114. pressed the `Tab` key in order to view the completions.
  115. These two flags can be used for instance to implement a completer that
  116. shows some completions when ``Tab`` has been pressed, but not
  117. automatically when the user presses a space. (Because of
  118. `complete_while_typing`.)
  119. """
  120. def __init__(
  121. self, text_inserted: bool = False, completion_requested: bool = False
  122. ) -> None:
  123. assert not (text_inserted and completion_requested)
  124. #: Automatic completion while typing.
  125. self.text_inserted = text_inserted
  126. #: Used explicitly requested completion by pressing 'tab'.
  127. self.completion_requested = completion_requested
  128. def __repr__(self) -> str:
  129. return "{}(text_inserted={!r}, completion_requested={!r})".format(
  130. self.__class__.__name__,
  131. self.text_inserted,
  132. self.completion_requested,
  133. )
  134. class Completer(metaclass=ABCMeta):
  135. """
  136. Base class for completer implementations.
  137. """
  138. @abstractmethod
  139. def get_completions(
  140. self, document: Document, complete_event: CompleteEvent
  141. ) -> Iterable[Completion]:
  142. """
  143. This should be a generator that yields :class:`.Completion` instances.
  144. If the generation of completions is something expensive (that takes a
  145. lot of time), consider wrapping this `Completer` class in a
  146. `ThreadedCompleter`. In that case, the completer algorithm runs in a
  147. background thread and completions will be displayed as soon as they
  148. arrive.
  149. :param document: :class:`~prompt_toolkit.document.Document` instance.
  150. :param complete_event: :class:`.CompleteEvent` instance.
  151. """
  152. while False:
  153. yield
  154. async def get_completions_async(
  155. self, document: Document, complete_event: CompleteEvent
  156. ) -> AsyncGenerator[Completion, None]:
  157. """
  158. Asynchronous generator for completions. (Probably, you won't have to
  159. override this.)
  160. Asynchronous generator of :class:`.Completion` objects.
  161. """
  162. for item in self.get_completions(document, complete_event):
  163. yield item
  164. class ThreadedCompleter(Completer):
  165. """
  166. Wrapper that runs the `get_completions` generator in a thread.
  167. (Use this to prevent the user interface from becoming unresponsive if the
  168. generation of completions takes too much time.)
  169. The completions will be displayed as soon as they are produced. The user
  170. can already select a completion, even if not all completions are displayed.
  171. """
  172. def __init__(self, completer: Completer) -> None:
  173. self.completer = completer
  174. def get_completions(
  175. self, document: Document, complete_event: CompleteEvent
  176. ) -> Iterable[Completion]:
  177. return self.completer.get_completions(document, complete_event)
  178. async def get_completions_async(
  179. self, document: Document, complete_event: CompleteEvent
  180. ) -> AsyncGenerator[Completion, None]:
  181. """
  182. Asynchronous generator of completions.
  183. """
  184. # NOTE: Right now, we are consuming the `get_completions` generator in
  185. # a synchronous background thread, then passing the results one
  186. # at a time over a queue, and consuming this queue in the main
  187. # thread (that's what `generator_to_async_generator` does). That
  188. # means that if the completer is *very* slow, we'll be showing
  189. # completions in the UI once they are computed.
  190. # It's very tempting to replace this implementation with the
  191. # commented code below for several reasons:
  192. # - `generator_to_async_generator` is not perfect and hard to get
  193. # right. It's a lot of complexity for little gain. The
  194. # implementation needs a huge buffer for it to be efficient
  195. # when there are many completions (like 50k+).
  196. # - Normally, a completer is supposed to be fast, users can have
  197. # "complete while typing" enabled, and want to see the
  198. # completions within a second. Handling one completion at a
  199. # time, and rendering once we get it here doesn't make any
  200. # sense if this is quick anyway.
  201. # - Completers like `FuzzyCompleter` prepare all completions
  202. # anyway so that they can be sorted by accuracy before they are
  203. # yielded. At the point that we start yielding completions
  204. # here, we already have all completions.
  205. # - The `Buffer` class has complex logic to invalidate the UI
  206. # while it is consuming the completions. We don't want to
  207. # invalidate the UI for every completion (if there are many),
  208. # but we want to do it often enough so that completions are
  209. # being displayed while they are produced.
  210. # We keep the current behavior mainly for backward-compatibility.
  211. # Similarly, it would be better for this function to not return
  212. # an async generator, but simply be a coroutine that returns a
  213. # list of `Completion` objects, containing all completions at
  214. # once.
  215. # Note that this argument doesn't mean we shouldn't use
  216. # `ThreadedCompleter`. It still makes sense to produce
  217. # completions in a background thread, because we don't want to
  218. # freeze the UI while the user is typing. But sending the
  219. # completions one at a time to the UI maybe isn't worth it.
  220. # def get_all_in_thread() -> List[Completion]:
  221. # return list(self.get_completions(document, complete_event))
  222. # completions = await get_running_loop().run_in_executor(None, get_all_in_thread)
  223. # for completion in completions:
  224. # yield completion
  225. async with aclosing(
  226. generator_to_async_generator(
  227. lambda: self.completer.get_completions(document, complete_event)
  228. )
  229. ) as async_generator:
  230. async for completion in async_generator:
  231. yield completion
  232. def __repr__(self) -> str:
  233. return f"ThreadedCompleter({self.completer!r})"
  234. class DummyCompleter(Completer):
  235. """
  236. A completer that doesn't return any completion.
  237. """
  238. def get_completions(
  239. self, document: Document, complete_event: CompleteEvent
  240. ) -> Iterable[Completion]:
  241. return []
  242. def __repr__(self) -> str:
  243. return "DummyCompleter()"
  244. class DynamicCompleter(Completer):
  245. """
  246. Completer class that can dynamically returns any Completer.
  247. :param get_completer: Callable that returns a :class:`.Completer` instance.
  248. """
  249. def __init__(self, get_completer: Callable[[], Completer | None]) -> None:
  250. self.get_completer = get_completer
  251. def get_completions(
  252. self, document: Document, complete_event: CompleteEvent
  253. ) -> Iterable[Completion]:
  254. completer = self.get_completer() or DummyCompleter()
  255. return completer.get_completions(document, complete_event)
  256. async def get_completions_async(
  257. self, document: Document, complete_event: CompleteEvent
  258. ) -> AsyncGenerator[Completion, None]:
  259. completer = self.get_completer() or DummyCompleter()
  260. async for completion in completer.get_completions_async(
  261. document, complete_event
  262. ):
  263. yield completion
  264. def __repr__(self) -> str:
  265. return f"DynamicCompleter({self.get_completer!r} -> {self.get_completer()!r})"
  266. class ConditionalCompleter(Completer):
  267. """
  268. Wrapper around any other completer that will enable/disable the completions
  269. depending on whether the received condition is satisfied.
  270. :param completer: :class:`.Completer` instance.
  271. :param filter: :class:`.Filter` instance.
  272. """
  273. def __init__(self, completer: Completer, filter: FilterOrBool) -> None:
  274. self.completer = completer
  275. self.filter = to_filter(filter)
  276. def __repr__(self) -> str:
  277. return f"ConditionalCompleter({self.completer!r}, filter={self.filter!r})"
  278. def get_completions(
  279. self, document: Document, complete_event: CompleteEvent
  280. ) -> Iterable[Completion]:
  281. # Get all completions in a blocking way.
  282. if self.filter():
  283. yield from self.completer.get_completions(document, complete_event)
  284. async def get_completions_async(
  285. self, document: Document, complete_event: CompleteEvent
  286. ) -> AsyncGenerator[Completion, None]:
  287. # Get all completions in a non-blocking way.
  288. if self.filter():
  289. async with aclosing(
  290. self.completer.get_completions_async(document, complete_event)
  291. ) as async_generator:
  292. async for item in async_generator:
  293. yield item
  294. class _MergedCompleter(Completer):
  295. """
  296. Combine several completers into one.
  297. """
  298. def __init__(self, completers: Sequence[Completer]) -> None:
  299. self.completers = completers
  300. def get_completions(
  301. self, document: Document, complete_event: CompleteEvent
  302. ) -> Iterable[Completion]:
  303. # Get all completions from the other completers in a blocking way.
  304. for completer in self.completers:
  305. yield from completer.get_completions(document, complete_event)
  306. async def get_completions_async(
  307. self, document: Document, complete_event: CompleteEvent
  308. ) -> AsyncGenerator[Completion, None]:
  309. # Get all completions from the other completers in a non-blocking way.
  310. for completer in self.completers:
  311. async with aclosing(
  312. completer.get_completions_async(document, complete_event)
  313. ) as async_generator:
  314. async for item in async_generator:
  315. yield item
  316. def merge_completers(
  317. completers: Sequence[Completer], deduplicate: bool = False
  318. ) -> Completer:
  319. """
  320. Combine several completers into one.
  321. :param deduplicate: If `True`, wrap the result in a `DeduplicateCompleter`
  322. so that completions that would result in the same text will be
  323. deduplicated.
  324. """
  325. if deduplicate:
  326. from .deduplicate import DeduplicateCompleter
  327. return DeduplicateCompleter(_MergedCompleter(completers))
  328. return _MergedCompleter(completers)
  329. def get_common_complete_suffix(
  330. document: Document, completions: Sequence[Completion]
  331. ) -> str:
  332. """
  333. Return the common prefix for all completions.
  334. """
  335. # Take only completions that don't change the text before the cursor.
  336. def doesnt_change_before_cursor(completion: Completion) -> bool:
  337. end = completion.text[: -completion.start_position]
  338. return document.text_before_cursor.endswith(end)
  339. completions2 = [c for c in completions if doesnt_change_before_cursor(c)]
  340. # When there is at least one completion that changes the text before the
  341. # cursor, don't return any common part.
  342. if len(completions2) != len(completions):
  343. return ""
  344. # Return the common prefix.
  345. def get_suffix(completion: Completion) -> str:
  346. return completion.text[-completion.start_position :]
  347. return _commonprefix([get_suffix(c) for c in completions2])
  348. def _commonprefix(strings: Iterable[str]) -> str:
  349. # Similar to os.path.commonprefix
  350. if not strings:
  351. return ""
  352. else:
  353. s1 = min(strings)
  354. s2 = max(strings)
  355. for i, c in enumerate(s1):
  356. if c != s2[i]:
  357. return s1[:i]
  358. return s1