Skip to content
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

Custom dict completion with _ipython_key_completions_ should allow access to prefix string #12420

Open
da1910 opened this issue Jul 2, 2020 · 3 comments · May be fixed by #13921
Open

Custom dict completion with _ipython_key_completions_ should allow access to prefix string #12420

da1910 opened this issue Jul 2, 2020 · 3 comments · May be fixed by #13921

Comments

@da1910
Copy link

da1910 commented Jul 2, 2020

I would love to be able to use dictionary completion to implement a quick-search style feature. It looks as though currently I can implement the ipython_key_completions method, but it does not receive any information about what has been typed after the opening square braces.

Could we have a way to receive the prefix string in the ipython_key_completions method?
Something like: my_object['str <tab>
causes _ipython_key_completions_(prefix: str) to be triggered, where prefix == 'str'

I looked into implementing a custom completer, but couldn't work out how to associate the line context with MyClass without some AST magic. Is there a simple recipe to implement more general completion for dict keys?

@da1910
Copy link
Author

da1910 commented Jul 2, 2020

@meeseeksdev tag tab-completion

@TomNicholas
Copy link
Contributor

I have a use case where I would love to be able to do this - a tree-like xarray data structure which can be indexed with file-like paths, e.g. 'path/to/node', or even 'path/to/../to/node'.

For path segments like . and .. to work I would need to be able to dynamically alter the list of possible path completions based on the partial path already typed out. So this suggestion

Something like: my_object['str <tab>
causes _ipython_key_completions_(prefix: str) to be triggered, where prefix == 'str'

would work for me.

@TomNicholas
Copy link
Contributor

TomNicholas commented Jan 25, 2023

Looking inside the ipython completer, it seems there is already a key_prefix that gets read from the prompt.

This is available internally before _get_keys(obj) is performed, which is where _ipython_key_completions_() is called.

Could we simply pass key_prefix as an optional argument down through _get_keys and into _ipython_key_completions_()? Then if the user's class gets a chance to say what the list of possible keys should be given the currently-typed-out prefix?

@TomNicholas TomNicholas linked a pull request Feb 2, 2023 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants