##// END OF EJS Templates
Merge pull request #4915 from minrk/console-completer...
Brian E. Granger -
r14891:c49cfd62 merge
parent child Browse files
Show More
@@ -1,57 +1,58
1 """Adapt readline completer interface to make ZMQ request.
1 """Adapt readline completer interface to make ZMQ request.
2 """
2 """
3 # -*- coding: utf-8 -*-
3 # -*- coding: utf-8 -*-
4 import readline
4 import readline
5 try:
5 try:
6 from queue import Empty # Py 3
6 from queue import Empty # Py 3
7 except ImportError:
7 except ImportError:
8 from Queue import Empty # Py 2
8 from Queue import Empty # Py 2
9
9
10 from IPython.config import Configurable
10 from IPython.config import Configurable
11 from IPython.core.completer import IPCompleter
11 from IPython.utils.traitlets import Float
12 from IPython.utils.traitlets import Float
12
13
13 class ZMQCompleter(Configurable):
14 class ZMQCompleter(IPCompleter):
14 """Client-side completion machinery.
15 """Client-side completion machinery.
15
16
16 How it works: self.complete will be called multiple times, with
17 How it works: self.complete will be called multiple times, with
17 state=0,1,2,... When state=0 it should compute ALL the completion matches,
18 state=0,1,2,... When state=0 it should compute ALL the completion matches,
18 and then return them for each value of state."""
19 and then return them for each value of state."""
19
20
20 timeout = Float(5.0, config=True, help='timeout before completion abort')
21 timeout = Float(5.0, config=True, help='timeout before completion abort')
21
22
22 def __init__(self, shell, client, config=None):
23 def __init__(self, shell, client, config=None):
23 super(ZMQCompleter,self).__init__(config=config)
24 super(ZMQCompleter,self).__init__(config=config)
24
25
25 self.shell = shell
26 self.shell = shell
26 self.client = client
27 self.client = client
27 self.matches = []
28 self.matches = []
28
29
29 def complete_request(self,text):
30 def complete_request(self,text):
30 line = readline.get_line_buffer()
31 line = readline.get_line_buffer()
31 cursor_pos = readline.get_endidx()
32 cursor_pos = readline.get_endidx()
32
33
33 # send completion request to kernel
34 # send completion request to kernel
34 # Give the kernel up to 0.5s to respond
35 # Give the kernel up to 0.5s to respond
35 msg_id = self.client.shell_channel.complete(text=text, line=line,
36 msg_id = self.client.shell_channel.complete(text=text, line=line,
36 cursor_pos=cursor_pos)
37 cursor_pos=cursor_pos)
37
38
38 msg = self.client.shell_channel.get_msg(timeout=self.timeout)
39 msg = self.client.shell_channel.get_msg(timeout=self.timeout)
39 if msg['parent_header']['msg_id'] == msg_id:
40 if msg['parent_header']['msg_id'] == msg_id:
40 return msg["content"]["matches"]
41 return msg["content"]["matches"]
41 return []
42 return []
42
43
43 def rlcomplete(self, text, state):
44 def rlcomplete(self, text, state):
44 if state == 0:
45 if state == 0:
45 try:
46 try:
46 self.matches = self.complete_request(text)
47 self.matches = self.complete_request(text)
47 except Empty:
48 except Empty:
48 #print('WARNING: Kernel timeout on tab completion.')
49 #print('WARNING: Kernel timeout on tab completion.')
49 pass
50 pass
50
51
51 try:
52 try:
52 return self.matches[state]
53 return self.matches[state]
53 except IndexError:
54 except IndexError:
54 return None
55 return None
55
56
56 def complete(self, text, line, cursor_pos=None):
57 def complete(self, text, line, cursor_pos=None):
57 return self.rlcomplete(text, 0)
58 return self.rlcomplete(text, 0)
General Comments 0
You need to be logged in to leave comments. Login now