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