Skip to content

Commit d00f8d4

Browse files
Prompt_toolkit 2.0 changes.
1 parent bf12d09 commit d00f8d4

10 files changed

+177
-149
lines changed

ptpython/completer.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import unicode_literals
22

3-
from prompt_toolkit.completion import Completer, Completion
4-
from prompt_toolkit.contrib.completers import PathCompleter
3+
from prompt_toolkit.completion import Completer, Completion, PathCompleter
54
from prompt_toolkit.contrib.regular_languages.compiler import compile as compile_grammar
65
from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter
76

@@ -151,6 +150,10 @@ def get_completions(self, document, complete_event):
151150
# In jedi.parser.__init__.py: 227, in remove_last_newline,
152151
# the assertion "newline.value.endswith('\n')" can fail.
153152
pass
153+
except SystemError:
154+
# In jedi.api.helpers.py: 144, in get_stack_at_position
155+
# raise SystemError("This really shouldn't happen. There's a bug in Jedi.")
156+
pass
154157
else:
155158
for c in completions:
156159
yield Completion(c.name_with_symbols, len(c.complete) - len(c.name_with_symbols),

ptpython/eventloop.py

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
in readline. ``prompt-toolkit`` doesn't understand that input hook, but this
88
will fix it for Tk.)
99
"""
10-
from prompt_toolkit.eventloop.defaults import create_event_loop as _create_event_loop
11-
from prompt_toolkit.eventloop.defaults import set_event_loop
1210
import sys
1311
import time
1412

ptpython/history_browser.py

+21-29
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
from prompt_toolkit.document import Document
1313
from prompt_toolkit.enums import DEFAULT_BUFFER
1414
from prompt_toolkit.filters import Condition, has_focus
15+
from prompt_toolkit.formatted_text.utils import fragment_list_to_text
1516
from prompt_toolkit.key_binding import KeyBindings
16-
from prompt_toolkit.layout.containers import HSplit, VSplit, Window, FloatContainer, Float, ConditionalContainer, Container, ScrollOffsets, Align
17+
from prompt_toolkit.layout.containers import HSplit, VSplit, Window, FloatContainer, Float, ConditionalContainer, Container, ScrollOffsets, WindowAlign
1718
from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
1819
from prompt_toolkit.layout.dimension import Dimension as D
1920
from prompt_toolkit.layout.layout import Layout
20-
from prompt_toolkit.layout.lexers import PygmentsLexer
2121
from prompt_toolkit.layout.margins import Margin, ScrollbarMargin
22-
from prompt_toolkit.layout.processors import Processor, Transformation, HighlightSearchProcessor, HighlightSelectionProcessor, merge_processors
23-
from prompt_toolkit.layout.widgets.toolbars import ArgToolbar, SearchToolbar
24-
from prompt_toolkit.layout.utils import fragment_list_to_text
25-
from prompt_toolkit.layout.widgets import Frame
22+
from prompt_toolkit.layout.processors import Processor, Transformation
23+
from prompt_toolkit.lexers import PygmentsLexer
24+
from prompt_toolkit.widgets import Frame
25+
from prompt_toolkit.widgets.toolbars import ArgToolbar, SearchToolbar
2626
from pygments.lexers import RstLexer
2727

2828
from .utils import if_mousedown
@@ -110,34 +110,29 @@ class HistoryLayout(object):
110110
application.
111111
"""
112112
def __init__(self, history):
113-
default_processors = [
114-
HighlightSearchProcessor(preview_search=True),
115-
HighlightSelectionProcessor()
116-
]
113+
search_toolbar = SearchToolbar()
117114

118115
self.help_buffer_control = BufferControl(
119116
buffer=history.help_buffer,
120-
lexer=PygmentsLexer(RstLexer),
121-
input_processor=merge_processors(default_processors))
117+
lexer=PygmentsLexer(RstLexer))
122118

123119
help_window = _create_popup_window(
124120
title='History Help',
125121
body=Window(
126122
content=self.help_buffer_control,
127123
right_margins=[ScrollbarMargin(display_arrows=True)],
128-
scroll_offsets=ScrollOffsets(top=2, bottom=2),
129-
transparent=False))
124+
scroll_offsets=ScrollOffsets(top=2, bottom=2)))
130125

131126
self.default_buffer_control = BufferControl(
132127
buffer=history.default_buffer,
133-
input_processor=merge_processors(
134-
default_processors + [GrayExistingText(history.history_mapping)]),
128+
input_processors=[GrayExistingText(history.history_mapping)],
135129
lexer=PygmentsLexer(PythonLexer))
136130

137131
self.history_buffer_control = BufferControl(
138132
buffer=history.history_buffer,
139133
lexer=PygmentsLexer(PythonLexer),
140-
input_processor=merge_processors(default_processors))
134+
search_buffer_control=search_toolbar.control,
135+
preview_search=True)
141136

142137
history_window = Window(
143138
content=self.history_buffer_control,
@@ -149,7 +144,7 @@ def __init__(self, history):
149144
# Top title bar.
150145
Window(
151146
content=FormattedTextControl(_get_top_toolbar_fragments),
152-
align=Align.CENTER,
147+
align=WindowAlign.CENTER,
153148
style='class:status-toolbar'),
154149
FloatContainer(
155150
content=VSplit([
@@ -170,16 +165,12 @@ def __init__(self, history):
170165
# Help text as a float.
171166
Float(width=60, top=3, bottom=2,
172167
content=ConditionalContainer(
173-
# XXXX XXX
174-
# (We use InFocusStack, because it's possible to search
175-
# through the help text as well, and at that point the search
176-
# buffer has the focus.)
177-
content=help_window, filter=has_focus(history.help_buffer))), # XXX
168+
content=help_window, filter=has_focus(history.help_buffer))),
178169
]
179170
),
180171
# Bottom toolbars.
181172
ArgToolbar(),
182-
# SearchToolbar(), # XXX
173+
search_toolbar,
183174
Window(
184175
content=FormattedTextControl(
185176
partial(_get_bottom_toolbar_fragments, history=history)),
@@ -338,15 +329,16 @@ def __init__(self, history, python_history, original_document):
338329
self.selected_lines = set()
339330

340331
# Process history.
332+
history_strings = python_history.get_strings()
341333
history_lines = []
342334

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

346338
for line in entry.splitlines():
347339
history_lines.append(line)
348340

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

352344
self.history_lines = history_lines
@@ -501,12 +493,12 @@ def _(event):
501493
@handle('c-g', filter=main_buffer_focussed)
502494
def _(event):
503495
" Cancel and go back. "
504-
event.app.set_return_value(None)
496+
event.app.exit(result=None)
505497

506498
@handle('enter', filter=main_buffer_focussed)
507499
def _(event):
508500
" Accept input. "
509-
event.app.set_return_value(history.default_buffer.text)
501+
event.app.exit(result=history.default_buffer.text)
510502

511503
enable_system_bindings = Condition(lambda: python_input.enable_system_bindings)
512504

@@ -540,7 +532,7 @@ def __init__(self, python_input, original_document):
540532
document=document,
541533
on_cursor_position_changed=self._history_buffer_pos_changed,
542534
accept_handler=(
543-
lambda buff: get_app().set_return_value(self.default_buffer.text)),
535+
lambda buff: get_app().exit(result=self.default_buffer.text)),
544536
read_only=True)
545537

546538
self.default_buffer = Buffer(

ptpython/ipython.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class IPythonPrompt(PromptStyle):
4343
def __init__(self, prompt_manager):
4444
self.prompt_manager = prompt_manager
4545

46-
def in_tokens(self):
46+
def in_prompt(self):
4747
text = self.prompt_manager.render('in', color=False, just=False)
4848
return [('class:in', text)]
4949

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

@@ -65,13 +65,13 @@ class IPython5Prompt(PromptStyle):
6565
def __init__(self, prompts):
6666
self.prompts = prompts
6767

68-
def in_tokens(self):
68+
def in_prompt(self):
6969
return self.prompts.in_prompt_tokens()
7070

71-
def in2_tokens(self, width):
71+
def in2_prompt(self, width):
7272
return self.prompts.continuation_prompt_tokens()
7373

74-
def out_tokens(self):
74+
def out_prompt(self):
7575
return []
7676

7777

ptpython/key_bindings.py

+29-29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from prompt_toolkit.document import Document
44
from prompt_toolkit.enums import DEFAULT_BUFFER
5-
from prompt_toolkit.filters import HasSelection, HasFocus, Condition, ViInsertMode, EmacsInsertMode, EmacsMode
5+
from prompt_toolkit.filters import has_selection, has_focus, Condition, vi_insert_mode, emacs_insert_mode, emacs_mode
66
from prompt_toolkit.key_binding import KeyBindings
77
from prompt_toolkit.keys import Keys
88
from prompt_toolkit.application import get_app
@@ -39,7 +39,6 @@ def load_python_bindings(python_input):
3939

4040
sidebar_visible = Condition(lambda: python_input.show_sidebar)
4141
handle = bindings.add
42-
has_selection = HasSelection()
4342

4443
@handle('c-l')
4544
def _(event):
@@ -88,9 +87,9 @@ def is_multiline():
8887
return document_is_multiline_python(python_input.default_buffer.document)
8988

9089
@handle('enter', filter= ~sidebar_visible & ~has_selection &
91-
(ViInsertMode() | EmacsInsertMode()) &
92-
HasFocus(DEFAULT_BUFFER) & ~is_multiline)
93-
@handle(Keys.Escape, Keys.Enter, filter= ~sidebar_visible & EmacsMode())
90+
(vi_insert_mode | emacs_insert_mode) &
91+
has_focus(DEFAULT_BUFFER) & ~is_multiline)
92+
@handle(Keys.Escape, Keys.Enter, filter= ~sidebar_visible & emacs_mode)
9493
def _(event):
9594
"""
9695
Accept input (for single line input).
@@ -107,8 +106,8 @@ def _(event):
107106
b.validate_and_handle()
108107

109108
@handle('enter', filter= ~sidebar_visible & ~has_selection &
110-
(ViInsertMode() | EmacsInsertMode()) &
111-
HasFocus(DEFAULT_BUFFER) & is_multiline)
109+
(vi_insert_mode | emacs_insert_mode) &
110+
has_focus(DEFAULT_BUFFER) & is_multiline)
112111
def _(event):
113112
"""
114113
Behaviour of the Enter key.
@@ -142,22 +141,23 @@ def at_the_end(b):
142141
else:
143142
auto_newline(b)
144143

145-
@handle('c-d', filter=~sidebar_visible & Condition(lambda:
146-
# Only when the `confirm_exit` flag is set.
147-
python_input.confirm_exit and
148-
# And the current buffer is empty.
149-
get_app().current_buffer == python_input.default_buffer and
150-
not get_app().current_buffer.text))
144+
@handle('c-d', filter=~sidebar_visible &
145+
has_focus(python_input.default_buffer) &
146+
Condition(lambda:
147+
# Only when the `confirm_exit` flag is set.
148+
python_input.confirm_exit and
149+
# And the current buffer is empty.
150+
not get_app().current_buffer.text))
151151
def _(event):
152152
"""
153153
Override Control-D exit, to ask for confirmation.
154154
"""
155155
python_input.show_exit_confirmation = True
156156

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

162162
return bindings
163163

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

174-
@handle(Keys.Up, filter=sidebar_visible)
175-
@handle(Keys.ControlP, filter=sidebar_visible)
174+
@handle('up', filter=sidebar_visible)
175+
@handle('c-p', filter=sidebar_visible)
176176
@handle('k', filter=sidebar_visible)
177177
def _(event):
178178
" Go to previous option. "
179179
python_input.selected_option_index = (
180180
(python_input.selected_option_index - 1) % python_input.option_count)
181181

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

190-
@handle(Keys.Right, filter=sidebar_visible)
190+
@handle('right', filter=sidebar_visible)
191191
@handle('l', filter=sidebar_visible)
192192
@handle(' ', filter=sidebar_visible)
193193
def _(event):
194194
" Select next value for current option. "
195195
option = python_input.selected_option
196196
option.activate_next()
197197

198-
@handle(Keys.Left, filter=sidebar_visible)
198+
@handle('left', filter=sidebar_visible)
199199
@handle('h', filter=sidebar_visible)
200200
def _(event):
201201
" Select previous value for current option. "
202202
option = python_input.selected_option
203203
option.activate_previous()
204204

205-
@handle(Keys.ControlC, filter=sidebar_visible)
206-
@handle(Keys.ControlG, filter=sidebar_visible)
207-
@handle(Keys.ControlD, filter=sidebar_visible)
208-
@handle(Keys.Enter, filter=sidebar_visible)
209-
@handle(Keys.Escape, filter=sidebar_visible)
205+
@handle('c-c', filter=sidebar_visible)
206+
@handle('c-d', filter=sidebar_visible)
207+
@handle('c-d', filter=sidebar_visible)
208+
@handle('enter', filter=sidebar_visible)
209+
@handle('escape', filter=sidebar_visible)
210210
def _(event):
211211
" Hide sidebar. "
212212
python_input.show_sidebar = False
@@ -225,13 +225,13 @@ def load_confirm_exit_bindings(python_input):
225225

226226
@handle('y', filter=confirmation_visible)
227227
@handle('Y', filter=confirmation_visible)
228-
@handle(Keys.Enter, filter=confirmation_visible)
229-
@handle(Keys.ControlD, filter=confirmation_visible)
228+
@handle('enter', filter=confirmation_visible)
229+
@handle('c-d', filter=confirmation_visible)
230230
def _(event):
231231
"""
232232
Really quit.
233233
"""
234-
event.app.exit()
234+
event.app.exit(exception=EOFError, style='class:exiting')
235235

236236
@handle(Keys.Any, filter=confirmation_visible)
237237
def _(event):

0 commit comments

Comments
 (0)