Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 617
Ami Tavory Avatar asked Oct 24 '25 10:10

Ami Tavory


2 Answers

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.

like image 75
Tarun Lalwani Avatar answered Oct 27 '25 02:10

Tarun Lalwani


The following works with ipython version 8.12.0.

from IPython.core.completer import context_matcher, CompletionContext, 
_convert_matcher_v1_result_to_v2

@context_matcher()
def custom_matcher(context: CompletionContext):
  
  # context has attributes full_text, cursor_position, cursor_line,
  # token, text_until_cursor, line_with_cursor
  # perform your logic
  results = ["item 1", "item 2"]
  return _convert_matcher_v1_result_to_v2(
                matches, type="my_matcher", suppress_if_matches=True
            )

ipython.Completer.custom_matchers.append(custom_matcher)
like image 26
Saswata Chakravarty Avatar answered Oct 27 '25 00:10

Saswata Chakravarty