@@ -18,6 +18,7 @@ def __getattribute__(self, key):
1818 vim = NoOp ()
1919 print ("uh oh, not running inside vim" )
2020
21+ import ast
2122import os
2223import sys
2324import time
@@ -345,7 +346,6 @@ def get_doc_msg(msg_id):
345346 elif 'user_expressions' in content :
346347 doc = content ['user_expressions' ]['_doc' ]
347348 if doc :
348- import ast
349349 content = ast .literal_eval (doc ['data' ]['text/plain' ])
350350
351351 if not content ['found' ]:
@@ -446,20 +446,36 @@ def ipy_complete(base, current_line, pos):
446446 msg_id = complete (code = current_line , cursor_pos = pos )
447447 try :
448448 m = get_child_msg (msg_id , timeout = vim_vars .get ('ipython_completion_timeout' , 2 ))
449- matches = m ['content' ]['matches' ]
450- metadata = m ['content' ].get ('metadata' , None )
451- if not metadata :
452- metadata = ['' for _ in matches ]
453- # we need to be careful with unicode, because we can have unicode
454- # completions for filenames (for the %run magic, for example). So the next
455- # line will fail on those:
456- #completions= [str(u) for u in matches]
457- # because str() won't work for non-ascii characters
458- # and we also have problems with unicode in vim, hence the following:
459- return matches , metadata
449+ try :
450+ return get_completion_metadata ()
451+ except KeyError : # completion_metadata function not available
452+ matches = m ['content' ]['matches' ]
453+ metadata = [{} for _ in matches ]
454+ return matches , metadata
455+ except Empty :
456+ echo ("no reply from IPython kernel" )
457+ raise IOError
458+
459+ def get_completion_metadata ():
460+ """Generate and fetch completion metadata."""
461+ request = '_completions = completion_metadata(get_ipython())'
462+ try :
463+ msg_id = send (request , silent = True , user_variables = ['_completions' ])
464+ except TypeError : # change in IPython 3.0+
465+ msg_id = send (request , silent = True , user_expressions = {'_completions' :'_completions' })
466+ try :
467+ m = get_child_msg (msg_id , timeout = vim_vars .get ('ipython_completion_timeout' , 2 ))
460468 except Empty :
461469 echo ("no reply from IPython kernel" )
462470 raise IOError
471+ content = m ['content' ]
472+ if 'user_variables' in content :
473+ metadata = content ['user_variables' ]['_completions' ]
474+ else :
475+ metadata = content ['user_expressions' ]['_completions' ]
476+ metadata = ast .literal_eval (metadata ['data' ]['text/plain' ])
477+ matches = [c ['match' ] for c in metadata ]
478+ return matches , metadata
463479
464480def vim_ipython_is_open ():
465481 """
0 commit comments