Skip to content

Commit

Permalink
Merge pull request ipython#222 from Carreau/new-completer
Browse files Browse the repository at this point in the history
Use the new IPython completer API
  • Loading branch information
ellisonbg authored May 30, 2017
2 parents 9aafe40 + 398d87f commit 8acaee8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
49 changes: 48 additions & 1 deletion ipykernel/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import getpass
import sys
import traceback

from IPython.core import release
from ipython_genutils.py3compat import builtin_mod, PY3, unicode_type, safe_unicode
Expand All @@ -14,6 +13,15 @@
from .zmqshell import ZMQInteractiveShell


try:
from IPython.core.completer import rectify_completions, provisionalcompleter
_use_experimental_60_completion = True
except ImportError:
_use_experimental_60_completion = False

_EXPERIMENTAL_KEY_NAME = '_jupyter_types_experimental'


class IPythonKernel(KernelBase):
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
allow_none=True)
Expand Down Expand Up @@ -246,6 +254,9 @@ def do_execute(self, code, silent, store_history=True,
return reply_content

def do_complete(self, code, cursor_pos):
if _use_experimental_60_completion:
return self._experimental_do_complete(code, cursor_pos)

# FIXME: IPython completers currently assume single line,
# but completion messages give multi-line context
# For now, extract line from cell, based on cursor_pos:
Expand All @@ -261,6 +272,42 @@ def do_complete(self, code, cursor_pos):
'metadata' : {},
'status' : 'ok'}

def _experimental_do_complete(self, code, cursor_pos):
"""
Experimental completions from IPython, using Jedi.
"""
if cursor_pos is None:
cursor_pos = len(code)
with provisionalcompleter():
raw_completions = self.shell.Completer.completions(code, cursor_pos)
completions = list(rectify_completions(code, raw_completions))

comps = []
for comp in completions:
comps.append(dict(
start=comp.start,
end=comp.end,
text=comp.text,
type=comp.type,
))

if completions:
s = completions[0].start
e = completions[0].end
matches = [c.text for c in completions]
else:
s = cursor_pos
e = cursor_pos
matches = []

return {'matches': matches,
'cursor_end': e,
'cursor_start': s,
'metadata': {_EXPERIMENTAL_KEY_NAME: comps},
'status': 'ok'}



def do_inspect(self, code, cursor_pos, detail_level=0):
name = token_at_cursor(code, cursor_pos)
info = self.shell.object_inspect(name)
Expand Down
6 changes: 1 addition & 5 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def _default_ident(self):

def __init__(self, **kwargs):
super(Kernel, self).__init__(**kwargs)

# Build dict of handlers for message types
self.shell_handlers = {}
for msg_type in self.msg_types:
Expand Down Expand Up @@ -212,8 +211,6 @@ def dispatch_shell(self, stream, msg):
self.set_parent(idents, msg)
self._publish_status(u'busy')

header = msg['header']
msg_id = header['msg_id']
msg_type = msg['header']['msg_type']

# Print some info about this message and leave a '--->' marker, so it's
Expand Down Expand Up @@ -430,12 +427,11 @@ def complete_request(self, stream, ident, parent):
content = parent['content']
code = content['code']
cursor_pos = content['cursor_pos']

matches = self.do_complete(code, cursor_pos)
matches = json_clean(matches)
completion_msg = self.session.send(stream, 'complete_reply',
matches, parent, ident)
self.log.debug("%s", completion_msg)

def do_complete(self, code, cursor_pos):
"""Override in subclasses to find completions.
Expand Down
9 changes: 2 additions & 7 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
universal=0

[nosetests]
warningfilters= default |.* |DeprecationWarning |ipykernel.*
default |.* |DeprecationWarning |IPython.*
ignore |.*assert.* |DeprecationWarning |.*
ignore |.*observe.* |DeprecationWarning |IPython.*
ignore |.*default.* |DeprecationWarning |IPython.*
ignore |.*default.* |DeprecationWarning |jupyter_client.*
ignore |.*Metada.* |DeprecationWarning |IPython.*
warningfilters= default |.* |DeprecationWarning |ipykernel.*
error |.*invalid.* |DeprecationWarning |matplotlib.*


0 comments on commit 8acaee8

Please sign in to comment.