Skip to content

Commit

Permalink
Prompt_toolkit 2.0 changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanslenders committed Jun 2, 2018
1 parent bf12d09 commit d00f8d4
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 149 deletions.
7 changes: 5 additions & 2 deletions ptpython/completer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import unicode_literals

from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.contrib.completers import PathCompleter
from prompt_toolkit.completion import Completer, Completion, PathCompleter
from prompt_toolkit.contrib.regular_languages.compiler import compile as compile_grammar
from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter

Expand Down Expand Up @@ -151,6 +150,10 @@ def get_completions(self, document, complete_event):
# In jedi.parser.__init__.py: 227, in remove_last_newline,
# the assertion "newline.value.endswith('\n')" can fail.
pass
except SystemError:
# In jedi.api.helpers.py: 144, in get_stack_at_position
# raise SystemError("This really shouldn't happen. There's a bug in Jedi.")
pass
else:
for c in completions:
yield Completion(c.name_with_symbols, len(c.complete) - len(c.name_with_symbols),
Expand Down
2 changes: 0 additions & 2 deletions ptpython/eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
in readline. ``prompt-toolkit`` doesn't understand that input hook, but this
will fix it for Tk.)
"""
from prompt_toolkit.eventloop.defaults import create_event_loop as _create_event_loop
from prompt_toolkit.eventloop.defaults import set_event_loop
import sys
import time

Expand Down
50 changes: 21 additions & 29 deletions ptpython/history_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
from prompt_toolkit.document import Document
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import Condition, has_focus
from prompt_toolkit.formatted_text.utils import fragment_list_to_text
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.layout.containers import HSplit, VSplit, Window, FloatContainer, Float, ConditionalContainer, Container, ScrollOffsets, Align
from prompt_toolkit.layout.containers import HSplit, VSplit, Window, FloatContainer, Float, ConditionalContainer, Container, ScrollOffsets, WindowAlign
from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
from prompt_toolkit.layout.dimension import Dimension as D
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.layout.lexers import PygmentsLexer
from prompt_toolkit.layout.margins import Margin, ScrollbarMargin
from prompt_toolkit.layout.processors import Processor, Transformation, HighlightSearchProcessor, HighlightSelectionProcessor, merge_processors
from prompt_toolkit.layout.widgets.toolbars import ArgToolbar, SearchToolbar
from prompt_toolkit.layout.utils import fragment_list_to_text
from prompt_toolkit.layout.widgets import Frame
from prompt_toolkit.layout.processors import Processor, Transformation
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.widgets import Frame
from prompt_toolkit.widgets.toolbars import ArgToolbar, SearchToolbar
from pygments.lexers import RstLexer

from .utils import if_mousedown
Expand Down Expand Up @@ -110,34 +110,29 @@ class HistoryLayout(object):
application.
"""
def __init__(self, history):
default_processors = [
HighlightSearchProcessor(preview_search=True),
HighlightSelectionProcessor()
]
search_toolbar = SearchToolbar()

self.help_buffer_control = BufferControl(
buffer=history.help_buffer,
lexer=PygmentsLexer(RstLexer),
input_processor=merge_processors(default_processors))
lexer=PygmentsLexer(RstLexer))

help_window = _create_popup_window(
title='History Help',
body=Window(
content=self.help_buffer_control,
right_margins=[ScrollbarMargin(display_arrows=True)],
scroll_offsets=ScrollOffsets(top=2, bottom=2),
transparent=False))
scroll_offsets=ScrollOffsets(top=2, bottom=2)))

self.default_buffer_control = BufferControl(
buffer=history.default_buffer,
input_processor=merge_processors(
default_processors + [GrayExistingText(history.history_mapping)]),
input_processors=[GrayExistingText(history.history_mapping)],
lexer=PygmentsLexer(PythonLexer))

self.history_buffer_control = BufferControl(
buffer=history.history_buffer,
lexer=PygmentsLexer(PythonLexer),
input_processor=merge_processors(default_processors))
search_buffer_control=search_toolbar.control,
preview_search=True)

history_window = Window(
content=self.history_buffer_control,
Expand All @@ -149,7 +144,7 @@ def __init__(self, history):
# Top title bar.
Window(
content=FormattedTextControl(_get_top_toolbar_fragments),
align=Align.CENTER,
align=WindowAlign.CENTER,
style='class:status-toolbar'),
FloatContainer(
content=VSplit([
Expand All @@ -170,16 +165,12 @@ def __init__(self, history):
# Help text as a float.
Float(width=60, top=3, bottom=2,
content=ConditionalContainer(
# XXXX XXX
# (We use InFocusStack, because it's possible to search
# through the help text as well, and at that point the search
# buffer has the focus.)
content=help_window, filter=has_focus(history.help_buffer))), # XXX
content=help_window, filter=has_focus(history.help_buffer))),
]
),
# Bottom toolbars.
ArgToolbar(),
# SearchToolbar(), # XXX
search_toolbar,
Window(
content=FormattedTextControl(
partial(_get_bottom_toolbar_fragments, history=history)),
Expand Down Expand Up @@ -338,15 +329,16 @@ def __init__(self, history, python_history, original_document):
self.selected_lines = set()

# Process history.
history_strings = python_history.get_strings()
history_lines = []

for entry_nr, entry in list(enumerate(python_history))[-HISTORY_COUNT:]:
for entry_nr, entry in list(enumerate(history_strings))[-HISTORY_COUNT:]:
self.lines_starting_new_entries.add(len(history_lines))

for line in entry.splitlines():
history_lines.append(line)

if len(python_history) > HISTORY_COUNT:
if len(history_strings) > HISTORY_COUNT:
history_lines[0] = '# *** History has been truncated to %s lines ***' % HISTORY_COUNT

self.history_lines = history_lines
Expand Down Expand Up @@ -501,12 +493,12 @@ def _(event):
@handle('c-g', filter=main_buffer_focussed)
def _(event):
" Cancel and go back. "
event.app.set_return_value(None)
event.app.exit(result=None)

@handle('enter', filter=main_buffer_focussed)
def _(event):
" Accept input. "
event.app.set_return_value(history.default_buffer.text)
event.app.exit(result=history.default_buffer.text)

enable_system_bindings = Condition(lambda: python_input.enable_system_bindings)

Expand Down Expand Up @@ -540,7 +532,7 @@ def __init__(self, python_input, original_document):
document=document,
on_cursor_position_changed=self._history_buffer_pos_changed,
accept_handler=(
lambda buff: get_app().set_return_value(self.default_buffer.text)),
lambda buff: get_app().exit(result=self.default_buffer.text)),
read_only=True)

self.default_buffer = Buffer(
Expand Down
10 changes: 5 additions & 5 deletions ptpython/ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class IPythonPrompt(PromptStyle):
def __init__(self, prompt_manager):
self.prompt_manager = prompt_manager

def in_tokens(self):
def in_prompt(self):
text = self.prompt_manager.render('in', color=False, just=False)
return [('class:in', text)]

def in2_tokens(self, width):
def in2_prompt(self, width):
text = self.prompt_manager.render('in2', color=False, just=False)
return [('class:in', text.rjust(width))]

Expand All @@ -65,13 +65,13 @@ class IPython5Prompt(PromptStyle):
def __init__(self, prompts):
self.prompts = prompts

def in_tokens(self):
def in_prompt(self):
return self.prompts.in_prompt_tokens()

def in2_tokens(self, width):
def in2_prompt(self, width):
return self.prompts.continuation_prompt_tokens()

def out_tokens(self):
def out_prompt(self):
return []


Expand Down
58 changes: 29 additions & 29 deletions ptpython/key_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from prompt_toolkit.document import Document
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import HasSelection, HasFocus, Condition, ViInsertMode, EmacsInsertMode, EmacsMode
from prompt_toolkit.filters import has_selection, has_focus, Condition, vi_insert_mode, emacs_insert_mode, emacs_mode
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.keys import Keys
from prompt_toolkit.application import get_app
Expand Down Expand Up @@ -39,7 +39,6 @@ def load_python_bindings(python_input):

sidebar_visible = Condition(lambda: python_input.show_sidebar)
handle = bindings.add
has_selection = HasSelection()

@handle('c-l')
def _(event):
Expand Down Expand Up @@ -88,9 +87,9 @@ def is_multiline():
return document_is_multiline_python(python_input.default_buffer.document)

@handle('enter', filter= ~sidebar_visible & ~has_selection &
(ViInsertMode() | EmacsInsertMode()) &
HasFocus(DEFAULT_BUFFER) & ~is_multiline)
@handle(Keys.Escape, Keys.Enter, filter= ~sidebar_visible & EmacsMode())
(vi_insert_mode | emacs_insert_mode) &
has_focus(DEFAULT_BUFFER) & ~is_multiline)
@handle(Keys.Escape, Keys.Enter, filter= ~sidebar_visible & emacs_mode)
def _(event):
"""
Accept input (for single line input).
Expand All @@ -107,8 +106,8 @@ def _(event):
b.validate_and_handle()

@handle('enter', filter= ~sidebar_visible & ~has_selection &
(ViInsertMode() | EmacsInsertMode()) &
HasFocus(DEFAULT_BUFFER) & is_multiline)
(vi_insert_mode | emacs_insert_mode) &
has_focus(DEFAULT_BUFFER) & is_multiline)
def _(event):
"""
Behaviour of the Enter key.
Expand Down Expand Up @@ -142,22 +141,23 @@ def at_the_end(b):
else:
auto_newline(b)

@handle('c-d', filter=~sidebar_visible & Condition(lambda:
# Only when the `confirm_exit` flag is set.
python_input.confirm_exit and
# And the current buffer is empty.
get_app().current_buffer == python_input.default_buffer and
not get_app().current_buffer.text))
@handle('c-d', filter=~sidebar_visible &
has_focus(python_input.default_buffer) &
Condition(lambda:
# Only when the `confirm_exit` flag is set.
python_input.confirm_exit and
# And the current buffer is empty.
not get_app().current_buffer.text))
def _(event):
"""
Override Control-D exit, to ask for confirmation.
"""
python_input.show_exit_confirmation = True

@handle('c-c')
@handle('c-c', filter=has_focus(python_input.default_buffer))
def _(event):
" Abort when Control-C has been pressed. "
event.app.abort()
event.app.exit(exception=KeyboardInterrupt, style='class:aborting')

return bindings

Expand All @@ -171,42 +171,42 @@ def load_sidebar_bindings(python_input):
handle = bindings.add
sidebar_visible = Condition(lambda: python_input.show_sidebar)

@handle(Keys.Up, filter=sidebar_visible)
@handle(Keys.ControlP, filter=sidebar_visible)
@handle('up', filter=sidebar_visible)
@handle('c-p', filter=sidebar_visible)
@handle('k', filter=sidebar_visible)
def _(event):
" Go to previous option. "
python_input.selected_option_index = (
(python_input.selected_option_index - 1) % python_input.option_count)

@handle(Keys.Down, filter=sidebar_visible)
@handle(Keys.ControlN, filter=sidebar_visible)
@handle('down', filter=sidebar_visible)
@handle('c-n', filter=sidebar_visible)
@handle('j', filter=sidebar_visible)
def _(event):
" Go to next option. "
python_input.selected_option_index = (
(python_input.selected_option_index + 1) % python_input.option_count)

@handle(Keys.Right, filter=sidebar_visible)
@handle('right', filter=sidebar_visible)
@handle('l', filter=sidebar_visible)
@handle(' ', filter=sidebar_visible)
def _(event):
" Select next value for current option. "
option = python_input.selected_option
option.activate_next()

@handle(Keys.Left, filter=sidebar_visible)
@handle('left', filter=sidebar_visible)
@handle('h', filter=sidebar_visible)
def _(event):
" Select previous value for current option. "
option = python_input.selected_option
option.activate_previous()

@handle(Keys.ControlC, filter=sidebar_visible)
@handle(Keys.ControlG, filter=sidebar_visible)
@handle(Keys.ControlD, filter=sidebar_visible)
@handle(Keys.Enter, filter=sidebar_visible)
@handle(Keys.Escape, filter=sidebar_visible)
@handle('c-c', filter=sidebar_visible)
@handle('c-d', filter=sidebar_visible)
@handle('c-d', filter=sidebar_visible)
@handle('enter', filter=sidebar_visible)
@handle('escape', filter=sidebar_visible)
def _(event):
" Hide sidebar. "
python_input.show_sidebar = False
Expand All @@ -225,13 +225,13 @@ def load_confirm_exit_bindings(python_input):

@handle('y', filter=confirmation_visible)
@handle('Y', filter=confirmation_visible)
@handle(Keys.Enter, filter=confirmation_visible)
@handle(Keys.ControlD, filter=confirmation_visible)
@handle('enter', filter=confirmation_visible)
@handle('c-d', filter=confirmation_visible)
def _(event):
"""
Really quit.
"""
event.app.exit()
event.app.exit(exception=EOFError, style='class:exiting')

@handle(Keys.Any, filter=confirmation_visible)
def _(event):
Expand Down
Loading

3 comments on commit d00f8d4

@stonebig
Copy link
Contributor

@stonebig stonebig commented on d00f8d4 Jun 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed to be compatible with Prompt-Toolkit 2.0.3 ? If so, a release may be nice to have on pypi.

@jonathanslenders
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right. I was hesitating a bit with pushing that release already, because I thought it would be a good idea to do it around the same time as IPython, but maybe I should push it already anyway.

@stonebig
Copy link
Contributor

@stonebig stonebig commented on d00f8d4 Jun 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oups ! don't push anything if not compatible with latest ipython. please. that compatibility was my next question

Please sign in to comment.