base.py 16 KB

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