auto_suggest.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. """
  2. `Fish-style <http://fishshell.com/>`_ like auto-suggestion.
  3. While a user types input in a certain buffer, suggestions are generated
  4. (asynchronously.) Usually, they are displayed after the input. When the cursor
  5. presses the right arrow and the cursor is at the end of the input, the
  6. suggestion will be inserted.
  7. If you want the auto suggestions to be asynchronous (in a background thread),
  8. because they take too much time, and could potentially block the event loop,
  9. then wrap the :class:`.AutoSuggest` instance into a
  10. :class:`.ThreadedAutoSuggest`.
  11. """
  12. from __future__ import annotations
  13. from abc import ABCMeta, abstractmethod
  14. from typing import TYPE_CHECKING, Callable
  15. from prompt_toolkit.eventloop import run_in_executor_with_context
  16. from .document import Document
  17. from .filters import Filter, to_filter
  18. if TYPE_CHECKING:
  19. from .buffer import Buffer
  20. __all__ = [
  21. "Suggestion",
  22. "AutoSuggest",
  23. "ThreadedAutoSuggest",
  24. "DummyAutoSuggest",
  25. "AutoSuggestFromHistory",
  26. "ConditionalAutoSuggest",
  27. "DynamicAutoSuggest",
  28. ]
  29. class Suggestion:
  30. """
  31. Suggestion returned by an auto-suggest algorithm.
  32. :param text: The suggestion text.
  33. """
  34. def __init__(self, text: str) -> None:
  35. self.text = text
  36. def __repr__(self) -> str:
  37. return "Suggestion(%s)" % self.text
  38. class AutoSuggest(metaclass=ABCMeta):
  39. """
  40. Base class for auto suggestion implementations.
  41. """
  42. @abstractmethod
  43. def get_suggestion(self, buffer: Buffer, document: Document) -> Suggestion | None:
  44. """
  45. Return `None` or a :class:`.Suggestion` instance.
  46. We receive both :class:`~prompt_toolkit.buffer.Buffer` and
  47. :class:`~prompt_toolkit.document.Document`. The reason is that auto
  48. suggestions are retrieved asynchronously. (Like completions.) The
  49. buffer text could be changed in the meantime, but ``document`` contains
  50. the buffer document like it was at the start of the auto suggestion
  51. call. So, from here, don't access ``buffer.text``, but use
  52. ``document.text`` instead.
  53. :param buffer: The :class:`~prompt_toolkit.buffer.Buffer` instance.
  54. :param document: The :class:`~prompt_toolkit.document.Document` instance.
  55. """
  56. async def get_suggestion_async(
  57. self, buff: Buffer, document: Document
  58. ) -> Suggestion | None:
  59. """
  60. Return a :class:`.Future` which is set when the suggestions are ready.
  61. This function can be overloaded in order to provide an asynchronous
  62. implementation.
  63. """
  64. return self.get_suggestion(buff, document)
  65. class ThreadedAutoSuggest(AutoSuggest):
  66. """
  67. Wrapper that runs auto suggestions in a thread.
  68. (Use this to prevent the user interface from becoming unresponsive if the
  69. generation of suggestions takes too much time.)
  70. """
  71. def __init__(self, auto_suggest: AutoSuggest) -> None:
  72. self.auto_suggest = auto_suggest
  73. def get_suggestion(self, buff: Buffer, document: Document) -> Suggestion | None:
  74. return self.auto_suggest.get_suggestion(buff, document)
  75. async def get_suggestion_async(
  76. self, buff: Buffer, document: Document
  77. ) -> Suggestion | None:
  78. """
  79. Run the `get_suggestion` function in a thread.
  80. """
  81. def run_get_suggestion_thread() -> Suggestion | None:
  82. return self.get_suggestion(buff, document)
  83. return await run_in_executor_with_context(run_get_suggestion_thread)
  84. class DummyAutoSuggest(AutoSuggest):
  85. """
  86. AutoSuggest class that doesn't return any suggestion.
  87. """
  88. def get_suggestion(self, buffer: Buffer, document: Document) -> Suggestion | None:
  89. return None # No suggestion
  90. class AutoSuggestFromHistory(AutoSuggest):
  91. """
  92. Give suggestions based on the lines in the history.
  93. """
  94. def get_suggestion(self, buffer: Buffer, document: Document) -> Suggestion | None:
  95. history = buffer.history
  96. # Consider only the last line for the suggestion.
  97. text = document.text.rsplit("\n", 1)[-1]
  98. # Only create a suggestion when this is not an empty line.
  99. if text.strip():
  100. # Find first matching line in history.
  101. for string in reversed(list(history.get_strings())):
  102. for line in reversed(string.splitlines()):
  103. if line.startswith(text):
  104. return Suggestion(line[len(text) :])
  105. return None
  106. class ConditionalAutoSuggest(AutoSuggest):
  107. """
  108. Auto suggest that can be turned on and of according to a certain condition.
  109. """
  110. def __init__(self, auto_suggest: AutoSuggest, filter: bool | Filter) -> None:
  111. self.auto_suggest = auto_suggest
  112. self.filter = to_filter(filter)
  113. def get_suggestion(self, buffer: Buffer, document: Document) -> Suggestion | None:
  114. if self.filter():
  115. return self.auto_suggest.get_suggestion(buffer, document)
  116. return None
  117. class DynamicAutoSuggest(AutoSuggest):
  118. """
  119. Validator class that can dynamically returns any Validator.
  120. :param get_validator: Callable that returns a :class:`.Validator` instance.
  121. """
  122. def __init__(self, get_auto_suggest: Callable[[], AutoSuggest | None]) -> None:
  123. self.get_auto_suggest = get_auto_suggest
  124. def get_suggestion(self, buff: Buffer, document: Document) -> Suggestion | None:
  125. auto_suggest = self.get_auto_suggest() or DummyAutoSuggest()
  126. return auto_suggest.get_suggestion(buff, document)
  127. async def get_suggestion_async(
  128. self, buff: Buffer, document: Document
  129. ) -> Suggestion | None:
  130. auto_suggest = self.get_auto_suggest() or DummyAutoSuggest()
  131. return await auto_suggest.get_suggestion_async(buff, document)