Show More
@@ -1,63 +1,69 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Adapt readline completer interface to make ZMQ request.""" |
|
2 | """Adapt readline completer interface to make ZMQ request.""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | try: |
|
7 | try: | |
8 | from queue import Empty # Py 3 |
|
8 | from queue import Empty # Py 3 | |
9 | except ImportError: |
|
9 | except ImportError: | |
10 | from Queue import Empty # Py 2 |
|
10 | from Queue import Empty # Py 2 | |
11 |
|
11 | |||
12 | from IPython.config import Configurable |
|
12 | from IPython.config import Configurable | |
13 | from IPython.core.completer import IPCompleter |
|
13 | from IPython.core.completer import IPCompleter | |
14 | from IPython.utils.traitlets import Float |
|
14 | from IPython.utils.traitlets import Float | |
15 | import IPython.utils.rlineimpl as readline |
|
15 | import IPython.utils.rlineimpl as readline | |
16 |
|
16 | |||
17 | class ZMQCompleter(IPCompleter): |
|
17 | class ZMQCompleter(IPCompleter): | |
18 | """Client-side completion machinery. |
|
18 | """Client-side completion machinery. | |
19 |
|
19 | |||
20 | How it works: self.complete will be called multiple times, with |
|
20 | How it works: self.complete will be called multiple times, with | |
21 | state=0,1,2,... When state=0 it should compute ALL the completion matches, |
|
21 | state=0,1,2,... When state=0 it should compute ALL the completion matches, | |
22 | and then return them for each value of state.""" |
|
22 | and then return them for each value of state.""" | |
23 |
|
23 | |||
24 | timeout = Float(5.0, config=True, help='timeout before completion abort') |
|
24 | timeout = Float(5.0, config=True, help='timeout before completion abort') | |
25 |
|
25 | |||
26 | def __init__(self, shell, client, config=None): |
|
26 | def __init__(self, shell, client, config=None): | |
27 | super(ZMQCompleter,self).__init__(config=config) |
|
27 | super(ZMQCompleter,self).__init__(config=config) | |
28 |
|
28 | |||
29 | self.shell = shell |
|
29 | self.shell = shell | |
30 | self.client = client |
|
30 | self.client = client | |
31 | self.matches = [] |
|
31 | self.matches = [] | |
32 |
|
32 | # don't do any splitting client-side, | ||
|
33 | # rely on the kernel for that | |||
|
34 | self.splitter.delims = '\r\n' | |||
|
35 | if self.readline: | |||
|
36 | self.readline.set_completer_delims('\r\n') | |||
|
37 | ||||
33 | def complete_request(self, text): |
|
38 | def complete_request(self, text): | |
34 | line = readline.get_line_buffer() |
|
39 | line = readline.get_line_buffer() | |
35 | cursor_pos = readline.get_endidx() |
|
40 | cursor_pos = readline.get_endidx() | |
36 |
|
41 | |||
37 | # send completion request to kernel |
|
42 | # send completion request to kernel | |
38 |
# Give the kernel up to |
|
43 | # Give the kernel up to 5s to respond | |
39 | msg_id = self.client.complete( |
|
44 | msg_id = self.client.complete( | |
40 | code=line, |
|
45 | code=line, | |
41 | cursor_pos=cursor_pos, |
|
46 | cursor_pos=cursor_pos, | |
42 | ) |
|
47 | ) | |
43 |
|
48 | |||
44 | msg = self.client.shell_channel.get_msg(timeout=self.timeout) |
|
49 | msg = self.client.shell_channel.get_msg(timeout=self.timeout) | |
45 | if msg['parent_header']['msg_id'] == msg_id: |
|
50 | if msg['parent_header']['msg_id'] == msg_id: | |
46 |
|
|
51 | cursor_start = msg['content']['cursor_start'] | |
|
52 | return [ line[:cursor_start] + m for m in msg['content']['matches'] ] | |||
47 | return [] |
|
53 | return [] | |
48 |
|
54 | |||
49 | def rlcomplete(self, text, state): |
|
55 | def rlcomplete(self, text, state): | |
50 | if state == 0: |
|
56 | if state == 0: | |
51 | try: |
|
57 | try: | |
52 | self.matches = self.complete_request(text) |
|
58 | self.matches = self.complete_request(text) | |
53 | except Empty: |
|
59 | except Empty: | |
54 | #print('WARNING: Kernel timeout on tab completion.') |
|
60 | #print('WARNING: Kernel timeout on tab completion.') | |
55 | pass |
|
61 | pass | |
56 |
|
62 | |||
57 | try: |
|
63 | try: | |
58 | return self.matches[state] |
|
64 | return self.matches[state] | |
59 | except IndexError: |
|
65 | except IndexError: | |
60 | return None |
|
66 | return None | |
61 |
|
67 | |||
62 | def complete(self, text, line, cursor_pos=None): |
|
68 | def complete(self, text, line, cursor_pos=None): | |
63 | return self.rlcomplete(text, 0) |
|
69 | return self.rlcomplete(text, 0) |
General Comments 0
You need to be logged in to leave comments.
Login now