Show More
@@ -1,74 +1,85 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.py3compat import str_to_unicode, cast_bytes, unicode_to_str | |||
14 | from IPython.utils.traitlets import Float |
|
15 | from IPython.utils.traitlets import Float | |
15 | import IPython.utils.rlineimpl as readline |
|
16 | import IPython.utils.rlineimpl as readline | |
16 |
|
17 | |||
17 | class ZMQCompleter(IPCompleter): |
|
18 | class ZMQCompleter(IPCompleter): | |
18 | """Client-side completion machinery. |
|
19 | """Client-side completion machinery. | |
19 |
|
20 | |||
20 | How it works: self.complete will be called multiple times, with |
|
21 | 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, |
|
22 | state=0,1,2,... When state=0 it should compute ALL the completion matches, | |
22 | and then return them for each value of state.""" |
|
23 | and then return them for each value of state.""" | |
23 |
|
24 | |||
24 | timeout = Float(5.0, config=True, help='timeout before completion abort') |
|
25 | timeout = Float(5.0, config=True, help='timeout before completion abort') | |
25 |
|
26 | |||
26 | def __init__(self, shell, client, config=None): |
|
27 | def __init__(self, shell, client, config=None): | |
27 | super(ZMQCompleter,self).__init__(config=config) |
|
28 | super(ZMQCompleter,self).__init__(config=config) | |
28 |
|
29 | |||
29 | self.shell = shell |
|
30 | self.shell = shell | |
30 | self.client = client |
|
31 | self.client = client | |
31 | self.matches = [] |
|
32 | self.matches = [] | |
32 | # don't do any splitting client-side, |
|
33 | # don't do any splitting client-side, | |
33 | # rely on the kernel for that |
|
34 | # rely on the kernel for that | |
34 | self.splitter.delims = '\r\n' |
|
35 | self.splitter.delims = '\r\n' | |
35 | if self.readline: |
|
36 | if self.readline: | |
36 | self.readline.set_completer_delims('\r\n') |
|
37 | self.readline.set_completer_delims('\r\n') | |
37 |
|
38 | |||
38 | def complete_request(self, text): |
|
39 | def complete_request(self, text): | |
39 | line = readline.get_line_buffer() |
|
40 | line = str_to_unicode(readline.get_line_buffer()) | |
40 | cursor_pos = readline.get_endidx() |
|
41 | byte_cursor_pos = readline.get_endidx() | |
|
42 | ||||
|
43 | # get_endidx is a byte offset | |||
|
44 | # account for multi-byte characters to get correct cursor_pos | |||
|
45 | cursor_pos = byte_cursor_pos | |||
|
46 | i = 0 | |||
|
47 | while i < cursor_pos: | |||
|
48 | bytelen = len(cast_bytes(line[i])) | |||
|
49 | cursor_pos -= (bytelen-1) | |||
|
50 | i += 1 | |||
41 |
|
51 | |||
42 | # send completion request to kernel |
|
52 | # send completion request to kernel | |
43 | # Give the kernel up to 5s to respond |
|
53 | # Give the kernel up to 5s to respond | |
44 | msg_id = self.client.complete( |
|
54 | msg_id = self.client.complete( | |
45 | code=line, |
|
55 | code=line, | |
46 | cursor_pos=cursor_pos, |
|
56 | cursor_pos=cursor_pos, | |
47 | ) |
|
57 | ) | |
48 |
|
58 | |||
49 | msg = self.client.shell_channel.get_msg(timeout=self.timeout) |
|
59 | msg = self.client.shell_channel.get_msg(timeout=self.timeout) | |
50 | if msg['parent_header']['msg_id'] == msg_id: |
|
60 | if msg['parent_header']['msg_id'] == msg_id: | |
51 | content = msg['content'] |
|
61 | content = msg['content'] | |
52 | cursor_start = content['cursor_start'] |
|
62 | cursor_start = content['cursor_start'] | |
53 | matches = [ line[:cursor_start] + m for m in content['matches'] ] |
|
63 | matches = [ line[:cursor_start] + m for m in content['matches'] ] | |
54 | if content["cursor_end"] < cursor_pos: |
|
64 | if content["cursor_end"] < cursor_pos: | |
55 | extra = line[content["cursor_end"]: cursor_pos] |
|
65 | extra = line[content["cursor_end"]: cursor_pos] | |
56 | matches = [m + extra for m in matches] |
|
66 | matches = [m + extra for m in matches] | |
|
67 | matches = [ unicode_to_str(m) for m in matches ] | |||
57 | return matches |
|
68 | return matches | |
58 | return [] |
|
69 | return [] | |
59 |
|
70 | |||
60 | def rlcomplete(self, text, state): |
|
71 | def rlcomplete(self, text, state): | |
61 | if state == 0: |
|
72 | if state == 0: | |
62 | try: |
|
73 | try: | |
63 | self.matches = self.complete_request(text) |
|
74 | self.matches = self.complete_request(text) | |
64 | except Empty: |
|
75 | except Empty: | |
65 | #print('WARNING: Kernel timeout on tab completion.') |
|
76 | #print('WARNING: Kernel timeout on tab completion.') | |
66 | pass |
|
77 | pass | |
67 |
|
78 | |||
68 | try: |
|
79 | try: | |
69 | return self.matches[state] |
|
80 | return self.matches[state] | |
70 | except IndexError: |
|
81 | except IndexError: | |
71 | return None |
|
82 | return None | |
72 |
|
83 | |||
73 | def complete(self, text, line, cursor_pos=None): |
|
84 | def complete(self, text, line, cursor_pos=None): | |
74 | return self.rlcomplete(text, 0) |
|
85 | return self.rlcomplete(text, 0) |
General Comments 0
You need to be logged in to leave comments.
Login now