New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional prefix arg for dynamic key completions #13921
base: main
Are you sure you want to change the base?
Conversation
Q: Should I have done this using a custom completer instead? I couldn't find any documentation on how to write or register custom completers... |
@meeseeksdev tag tab-completion |
If As for adding a custom completers, you can do this by appending to |
Hi @krassowski - thanks for the quick reply!
I'm potentially open to working on generalising this, but this is my first PR to ipython's main codebase, and I would need a bit of guidance. I've read through Are you suggesting we define a new matcher method on
I also don't really understand if this is a potential solution to my original problem or just a dead end. |
@krassowski You've worked more than me on the completion recently, so I'll follow your decision here. |
if "prefix" in inspect.signature(key_completions_getter).parameters: | ||
if prefix is not None: | ||
# strip leading apostrophe from key before passing to ._ipython_key_completions | ||
return key_completions_getter(prefix=prefix[1:]) | ||
else: | ||
return key_completions_getter(prefix=None) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I was suggesting was that instead of passing a prefix
string we would pass an entire CompletionContext
, something like:
class ItemCompletionContext(CompletionContext):
"""Completion context for item sub-matchers (providing information about `__getitem__`)"""
key_prefix: str
if "prefix" in inspect.signature(key_completions_getter).parameters: | |
if prefix is not None: | |
# strip leading apostrophe from key before passing to ._ipython_key_completions | |
return key_completions_getter(prefix=prefix[1:]) | |
else: | |
return key_completions_getter(prefix=None) | |
if ( | |
hasattr(key_completions_getter, 'matcher_api_version') | |
and key_completions_getter.matcher_api_version == 2 | |
): | |
item_context = ItemCompletionContext( | |
key_prefix=key_prefix, | |
full_text=context.full_text, | |
cursor_line=context.cursor_line, | |
cursor_position=context.cursor_position, | |
token=context.token, | |
) | |
return key_completions_getter(item_context)['completions'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As for stripping leading apostrophe: what if the key is a number? Or hex-number? Or raw string? I think we do not want to modify the prefix.
Closes #12420 by adding an optional
prefix
argument to_ipython_key_completions_
.whats-new.rst
New functions/methods are listed in API