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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With