python-3.xjupyter-notebookcode-completion

Extended Information For An IPython Custom Completer


I'm setting a custom completer using set_custom_completer:

import IPython

def foo(self, t):
    return []

IPython.get_ipython().set_custom_completer(foo)

The problem is with the signature of foo: the argument t is just a string containing the content from the beginning of the line to the cursor. Is there a way of finding the entire cell content and the cursor position?

For example, suppose the state in the cell is:

foo
bar<TAB>baz

Then t will be bar, but I'd like something like

(`foo\barbaz`, 
1, # line 1
4 # cursor position 4 in the line
)

The system information is:

The version of the notebook server is 5.0.0b2 and is running on:
Python 3.6.3rc1+ (default, Sep 29 2017, 16:55:05) 
[GCC 5.x 20170328]

Current Kernel Information:
Python 3.6.3rc1+ (default, Sep 29 2017, 16:55:05) 
Type "copyright", "credits" or "license" for more information.

IPython 5.3.0 -- An enhanced Interactive Python.

I cannot upgrade it, unfortunately.


Solution

  • Well after digging the source code and stack traces, I couldn't find anything that obviously exposes the cell text. But then I have no detailed idea about the ipython source, so I worked out below hack which would get you what you need:

    import IPython
    import inspect
    
    def foo(self, t):
        locals_caller = inspect.currentframe().f_back.f_back.f_back.f_back.f_locals
        code = locals_caller['code']
        cursor_pos = locals_caller['cursor_pos']
        # remove any reference to avoid leakages
        del locals_caller
        return [code]
    
    IPython.get_ipython().set_custom_completer(foo)
    

    I have hardcoded the stack backtracking, but you may want to put a logic around it if you want a stable function which works across versions/updates. This should be good enough to get you moving in the right direction.