Debian Codegraph @

jupyter_console/completer.py

raw
1 # -*- coding: utf-8 -*-
2 """Adapt readline completer interface to make ZMQ request."""
3
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
6
7 from traitlets.config import Configurable
8 from traitlets import Float
9
10 from jupyter_console.utils import run_sync
11
12
13 class ZMQCompleter(Configurable):
14 """Client-side completion machinery.
15
16 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 and then return them for each value of state."""
19
20 timeout = Float(5.0, config=True, help='timeout before completion abort')
21
22 def __init__(self, shell, client, config=None):
23 super(ZMQCompleter,self).__init__(config=config)
24
25 self.shell = shell
26 self.client = client
27 self.matches = []
28
29 def complete_request(self, code, cursor_pos):
30 # send completion request to kernel
31 # Give the kernel up to 5s to respond
32 msg_id = self.client.complete(
33 code=code,
34 cursor_pos=cursor_pos,
35 )
36
37 msg = run_sync(self.client.shell_channel.get_msg)(timeout=self.timeout)
38 if msg['parent_header']['msg_id'] == msg_id:
39 return msg['content']
40
41 return {'matches': [], 'cursor_start': 0, 'cursor_end': 0,
42 'metadata': {}, 'status': 'ok'}
43