##// END OF EJS Templates
Add completion support
Thomas Kluyver -
Show More
@@ -1,86 +1,103 b''
1 1 from IPython.core.interactiveshell import InteractiveShell
2 2
3 from prompt_toolkit.completion import Completer, Completion
3 4 from prompt_toolkit.history import InMemoryHistory
4 5 from prompt_toolkit.shortcuts import create_prompt_application
5 6 from prompt_toolkit.interface import CommandLineInterface
6 7 from prompt_toolkit.key_binding.manager import KeyBindingManager
7 8 from prompt_toolkit.keys import Keys
8 9 from prompt_toolkit.layout.lexers import PygmentsLexer
9 10
10 11 from pygments.lexers import Python3Lexer
11 12 from pygments.token import Token
12 13
13 14
15 class IPythonPTCompleter(Completer):
16 """Adaptor to provide IPython completions to prompt_toolkit"""
17 def __init__(self, ipy_completer):
18 self.ipy_completer = ipy_completer
19
20 def get_completions(self, document, complete_event):
21 used, matches = self.ipy_completer.complete(
22 line_buffer=document.current_line,
23 cursor_pos=document.cursor_position_col
24 )
25 start_pos = -len(used)
26 for m in matches:
27 yield Completion(m, start_position=start_pos)
28
29
14 30 class PTInteractiveShell(InteractiveShell):
15 31 pt_cli = None
16 32
17 33 def get_prompt_tokens(self, cli):
18 34 return [
19 35 (Token.Prompt, 'In ['),
20 36 (Token.Prompt, str(self.execution_count)),
21 37 (Token.Prompt, ']: '),
22 38 ]
23 39
24 40
25 41 def init_prompt_toolkit_cli(self):
26 42 kbmanager = KeyBindingManager.for_prompt()
27 43 @kbmanager.registry.add_binding(Keys.ControlJ) # Ctrl+J == Enter, seemingly
28 44 def _(event):
29 45 b = event.current_buffer
30 46 if not b.document.on_last_line:
31 47 b.newline()
32 48 return
33 49
34 50 status, indent = self.input_splitter.check_complete(b.document.text)
35 51
36 52 if (status != 'incomplete') and b.accept_action.is_returnable:
37 53 b.accept_action.validate_and_handle(event.cli, b)
38 54 else:
39 55 b.insert_text('\n' + (' ' * (indent or 0)))
40 56
41 57 @kbmanager.registry.add_binding(Keys.ControlC)
42 58 def _(event):
43 59 event.current_buffer.reset()
44 60
45 61 # Pre-populate history from IPython's history database
46 62 history = InMemoryHistory()
47 63 last_cell = u""
48 64 for _, _, cell in self.history_manager.get_tail(self.history_load_length,
49 65 include_latest=True):
50 66 # Ignore blank lines and consecutive duplicates
51 67 cell = cell.rstrip()
52 68 if cell and (cell != last_cell):
53 69 history.append(cell)
54 70
55 71 app = create_prompt_application(multiline=True,
56 72 lexer=PygmentsLexer(Python3Lexer),
57 73 get_prompt_tokens=self.get_prompt_tokens,
58 74 key_bindings_registry=kbmanager.registry,
59 75 history=history,
76 completer=IPythonPTCompleter(self.Completer),
60 77 )
61 78
62 79 self.pt_cli = CommandLineInterface(app)
63 80
64 81 def __init__(self, *args, **kwargs):
65 82 super(PTInteractiveShell, self).__init__(*args, **kwargs)
66 83 self.init_prompt_toolkit_cli()
67 84 self.keep_running = True
68 85
69 86 def ask_exit(self):
70 87 self.keep_running = False
71 88
72 89 def interact(self):
73 90 while self.keep_running:
74 91 try:
75 92 document = self.pt_cli.run()
76 93 except EOFError:
77 94 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
78 95 self.ask_exit()
79 96
80 97 else:
81 98 if document:
82 99 self.run_cell(document.text, store_history=True)
83 100
84 101
85 102 if __name__ == '__main__':
86 PTInteractiveShell().interact()
103 PTInteractiveShell.instance().interact()
General Comments 0
You need to be logged in to leave comments. Login now