Show More
@@ -1,9 +1,9 b'' | |||||
1 | from IPython.core.interactiveshell import InteractiveShell |
|
1 | from IPython.core.interactiveshell import InteractiveShell | |
2 |
|
2 | |||
3 |
from prompt_toolkit. |
|
3 | from prompt_toolkit.shortcuts import create_prompt_application | |
4 |
from prompt_toolkit. |
|
4 | from prompt_toolkit.interface import CommandLineInterface | |
5 |
from prompt_toolkit. |
|
5 | from prompt_toolkit.key_binding.manager import KeyBindingManager | |
6 | from prompt_toolkit.interface import AcceptAction, Application, CommandLineInterface |
|
6 | from prompt_toolkit.keys import Keys | |
7 | from prompt_toolkit.layout.lexers import PygmentsLexer |
|
7 | from prompt_toolkit.layout.lexers import PygmentsLexer | |
8 |
|
8 | |||
9 | from pygments.lexers import Python3Lexer |
|
9 | from pygments.lexers import Python3Lexer | |
@@ -11,17 +11,6 b' from pygments.token import Token' | |||||
11 |
|
11 | |||
12 |
|
12 | |||
13 | class PTInteractiveShell(InteractiveShell): |
|
13 | class PTInteractiveShell(InteractiveShell): | |
14 | def _multiline(self, cli): |
|
|||
15 | doc = cli.current_buffer.document |
|
|||
16 | if not doc.on_last_line: |
|
|||
17 | cli.run_in_terminal(lambda: print('Not on last line')) |
|
|||
18 | return False |
|
|||
19 | status, indent = self.input_splitter.check_complete(doc.text) |
|
|||
20 | return status == 'incomplete' |
|
|||
21 |
|
||||
22 | def _multiline2(self): |
|
|||
23 | return self._multiline(self.pt_cli) |
|
|||
24 |
|
||||
25 | pt_cli = None |
|
14 | pt_cli = None | |
26 |
|
15 | |||
27 | def get_prompt_tokens(self, cli): |
|
16 | def get_prompt_tokens(self, cli): | |
@@ -33,16 +22,27 b' class PTInteractiveShell(InteractiveShell):' | |||||
33 |
|
22 | |||
34 |
|
23 | |||
35 | def init_prompt_toolkit_cli(self): |
|
24 | def init_prompt_toolkit_cli(self): | |
36 | layout = create_prompt_layout( |
|
25 | kbmanager = KeyBindingManager.for_prompt() | |
37 | get_prompt_tokens=self.get_prompt_tokens, |
|
26 | @kbmanager.registry.add_binding(Keys.ControlJ) # Ctrl+J == Enter, seemingly | |
38 | lexer=PygmentsLexer(Python3Lexer), |
|
27 | def _(event): | |
39 | multiline=Condition(self._multiline), |
|
28 | b = event.current_buffer | |
40 | ) |
|
29 | if not b.document.on_last_line: | |
41 | buffer = Buffer( |
|
30 | b.newline() | |
42 | is_multiline=Condition(self._multiline2), |
|
31 | return | |
43 | accept_action=AcceptAction.RETURN_DOCUMENT, |
|
32 | ||
|
33 | status, indent = self.input_splitter.check_complete(b.document.text) | |||
|
34 | ||||
|
35 | if (status != 'incomplete') and b.accept_action.is_returnable: | |||
|
36 | b.accept_action.validate_and_handle(event.cli, b) | |||
|
37 | else: | |||
|
38 | b.insert_text('\n' + (' ' * indent)) | |||
|
39 | ||||
|
40 | app = create_prompt_application(multiline=True, | |||
|
41 | lexer=PygmentsLexer(Python3Lexer), | |||
|
42 | get_prompt_tokens=self.get_prompt_tokens, | |||
|
43 | key_bindings_registry=kbmanager.registry, | |||
44 | ) |
|
44 | ) | |
45 | app = Application(layout=layout, buffer=buffer) |
|
45 | ||
46 | self.pt_cli = CommandLineInterface(app) |
|
46 | self.pt_cli = CommandLineInterface(app) | |
47 |
|
47 | |||
48 | def __init__(self, *args, **kwargs): |
|
48 | def __init__(self, *args, **kwargs): |
General Comments 0
You need to be logged in to leave comments.
Login now