Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to get autosuggest from history #293

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion jupyter_console/ptshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import time
from warnings import warn

from typing import Dict as DictType, Any as AnyType
from typing import Dict as DictType, Any as AnyType, Union as UnionType

from zmq import ZMQError
from IPython.core import page
Expand Down Expand Up @@ -44,6 +44,7 @@
# use_ayncio_event_loop obsolete in PKT3
from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop

from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.document import Document
from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
Expand Down Expand Up @@ -188,6 +189,8 @@ class ZMQTerminalInteractiveShell(SingletonConfigurable):
help="Shortcut style to use at the prompt. 'vi' or 'emacs'.",
)

auto_suggest: UnionType[AutoSuggestFromHistory, None] = None

highlighting_style = Unicode('', config=True,
help="The name of a Pygments style to use for syntax highlighting"
)
Expand Down Expand Up @@ -325,6 +328,15 @@ class ZMQTerminalInteractiveShell(SingletonConfigurable):
config=True
)

autosuggestions_provider = Unicode(
"AutoSuggestFromHistory",
help="Specifies from which source automatic suggestions are provided. "
"Can be set to 'AutoSuggestFromHistory'``, "
" or ``None`` to disable automatic suggestions. "
"Default is `'AutoSuggestFromHistory`'.",
allow_none=True,
).tag(config=True)

manager = Instance("jupyter_client.KernelManager", allow_none=True)
client = Instance("jupyter_client.KernelClient", allow_none=True)

Expand All @@ -350,6 +362,7 @@ def __init__(self, **kwargs):
self.init_io()

self.init_kernel_info()
self._set_autosuggestions()
self.init_prompt_toolkit_cli()
self.keep_running = True
self.execution_count = 1
Expand Down Expand Up @@ -444,6 +457,14 @@ def show_banner(self):
print(self.banner.format(version=__version__,
kernel_banner=self.kernel_info.get('banner', '')),end='',flush=True)

def _set_autosuggestions(self):
if self.autosuggestions_provider is None:
self.auto_suggest = None
elif self.autosuggestions_provider == "AutoSuggestFromHistory":
self.auto_suggest = AutoSuggestFromHistory()
else:
raise ValueError("No valid provider.")

def init_prompt_toolkit_cli(self):
if self.simple_prompt or ('JUPYTER_CONSOLE_TEST' in os.environ):
# Simple restricted interface for tests so we can find prompts with
Expand Down Expand Up @@ -555,6 +576,7 @@ def _(event):
use_asyncio_event_loop()

self.pt_cli = PromptSession(
auto_suggest=self.auto_suggest,
message=(lambda: PygmentsTokens(self.get_prompt_tokens())),
multiline=True,
complete_style=self.pt_complete_style,
Expand Down