diff options
author | Ivan Blinkov <ivan@blinkov.ru> | 2022-02-10 16:47:10 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:10 +0300 |
commit | 1aeb9a455974457866f78722ad98114bafc84e8a (patch) | |
tree | e4340eaf1668684d83a0a58c36947c5def5350ad /contrib/python/prompt-toolkit/py2/prompt_toolkit/auto_suggest.py | |
parent | bd5ef432f5cfb1e18851381329d94665a4c22470 (diff) | |
download | ydb-1aeb9a455974457866f78722ad98114bafc84e8a.tar.gz |
Restoring authorship annotation for Ivan Blinkov <ivan@blinkov.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/python/prompt-toolkit/py2/prompt_toolkit/auto_suggest.py')
-rw-r--r-- | contrib/python/prompt-toolkit/py2/prompt_toolkit/auto_suggest.py | 176 |
1 files changed, 88 insertions, 88 deletions
diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/auto_suggest.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/auto_suggest.py index 1d5130521f..5e5eb80ef1 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/auto_suggest.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/auto_suggest.py @@ -1,88 +1,88 @@ -""" -`Fish-style <http://fishshell.com/>`_ like auto-suggestion. - -While a user types input in a certain buffer, suggestions are generated -(asynchronously.) Usually, they are displayed after the input. When the cursor -presses the right arrow and the cursor is at the end of the input, the -suggestion will be inserted. -""" -from __future__ import unicode_literals -from abc import ABCMeta, abstractmethod -from six import with_metaclass - -from .filters import to_cli_filter - -__all__ = ( - 'Suggestion', - 'AutoSuggest', - 'AutoSuggestFromHistory', - 'ConditionalAutoSuggest', -) - - -class Suggestion(object): - """ - Suggestion returned by an auto-suggest algorithm. - - :param text: The suggestion text. - """ - def __init__(self, text): - self.text = text - - def __repr__(self): - return 'Suggestion(%s)' % self.text - - -class AutoSuggest(with_metaclass(ABCMeta, object)): - """ - Base class for auto suggestion implementations. - """ - @abstractmethod - def get_suggestion(self, cli, buffer, document): - """ - Return `None` or a :class:`.Suggestion` instance. - - We receive both ``buffer`` and ``document``. The reason is that auto - suggestions are retrieved asynchronously. (Like completions.) The - buffer text could be changed in the meantime, but ``document`` contains - the buffer document like it was at the start of the auto suggestion - call. So, from here, don't access ``buffer.text``, but use - ``document.text`` instead. - - :param buffer: The :class:`~prompt_toolkit.buffer.Buffer` instance. - :param document: The :class:`~prompt_toolkit.document.Document` instance. - """ - - -class AutoSuggestFromHistory(AutoSuggest): - """ - Give suggestions based on the lines in the history. - """ - def get_suggestion(self, cli, buffer, document): - history = buffer.history - - # Consider only the last line for the suggestion. - text = document.text.rsplit('\n', 1)[-1] - - # Only create a suggestion when this is not an empty line. - if text.strip(): - # Find first matching line in history. - for string in reversed(list(history)): - for line in reversed(string.splitlines()): - if line.startswith(text): - return Suggestion(line[len(text):]) - - -class ConditionalAutoSuggest(AutoSuggest): - """ - Auto suggest that can be turned on and of according to a certain condition. - """ - def __init__(self, auto_suggest, filter): - assert isinstance(auto_suggest, AutoSuggest) - - self.auto_suggest = auto_suggest - self.filter = to_cli_filter(filter) - - def get_suggestion(self, cli, buffer, document): - if self.filter(cli): - return self.auto_suggest.get_suggestion(cli, buffer, document) +""" +`Fish-style <http://fishshell.com/>`_ like auto-suggestion. + +While a user types input in a certain buffer, suggestions are generated +(asynchronously.) Usually, they are displayed after the input. When the cursor +presses the right arrow and the cursor is at the end of the input, the +suggestion will be inserted. +""" +from __future__ import unicode_literals +from abc import ABCMeta, abstractmethod +from six import with_metaclass + +from .filters import to_cli_filter + +__all__ = ( + 'Suggestion', + 'AutoSuggest', + 'AutoSuggestFromHistory', + 'ConditionalAutoSuggest', +) + + +class Suggestion(object): + """ + Suggestion returned by an auto-suggest algorithm. + + :param text: The suggestion text. + """ + def __init__(self, text): + self.text = text + + def __repr__(self): + return 'Suggestion(%s)' % self.text + + +class AutoSuggest(with_metaclass(ABCMeta, object)): + """ + Base class for auto suggestion implementations. + """ + @abstractmethod + def get_suggestion(self, cli, buffer, document): + """ + Return `None` or a :class:`.Suggestion` instance. + + We receive both ``buffer`` and ``document``. The reason is that auto + suggestions are retrieved asynchronously. (Like completions.) The + buffer text could be changed in the meantime, but ``document`` contains + the buffer document like it was at the start of the auto suggestion + call. So, from here, don't access ``buffer.text``, but use + ``document.text`` instead. + + :param buffer: The :class:`~prompt_toolkit.buffer.Buffer` instance. + :param document: The :class:`~prompt_toolkit.document.Document` instance. + """ + + +class AutoSuggestFromHistory(AutoSuggest): + """ + Give suggestions based on the lines in the history. + """ + def get_suggestion(self, cli, buffer, document): + history = buffer.history + + # Consider only the last line for the suggestion. + text = document.text.rsplit('\n', 1)[-1] + + # Only create a suggestion when this is not an empty line. + if text.strip(): + # Find first matching line in history. + for string in reversed(list(history)): + for line in reversed(string.splitlines()): + if line.startswith(text): + return Suggestion(line[len(text):]) + + +class ConditionalAutoSuggest(AutoSuggest): + """ + Auto suggest that can be turned on and of according to a certain condition. + """ + def __init__(self, auto_suggest, filter): + assert isinstance(auto_suggest, AutoSuggest) + + self.auto_suggest = auto_suggest + self.filter = to_cli_filter(filter) + + def get_suggestion(self, cli, buffer, document): + if self.filter(cli): + return self.auto_suggest.get_suggestion(cli, buffer, document) |