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