ipython icon indicating copy to clipboard operation
ipython copied to clipboard

autocomplete-while-you-type in IPython shell

Open jab opened this issue 4 years ago • 12 comments

Would it be possible to expose an option to enable an autocomplete-while-you-type experience in IPython shell? In other words, try to show the user the menu of available autocompletions when they hit any key, rather than making them hit Tab to see it, similar to how autocompletion works in IDEs like VS Code, PyCharm, etc.

I searched the tracker and the docs for this and couldn't find it. Sorry if I missed something.

Thanks for your consideration and for the great work on IPython!

jab avatar Feb 13 '21 15:02 jab

It requires changing how the completer from prompt_toolkit is used. Absolutely doable, although IIRC it requires changing the default keybindings: left/right arrow keys could no longer be used to move cursor, as they are bound to the autocomplete menu by default.

MrMino avatar Feb 13 '21 16:02 MrMino

Reference: search for complete_while_typing in Prompt Toolkit reference.

MrMino avatar Feb 13 '21 16:02 MrMino

Thanks for the quick reply, and great to know this is doable! I had a look at the Prompt Toolkit docs you linked to, and so far I've gotten as far as:

In [1]: import IPython

In [2]: ipython = IPython.get_ipython()

In [3]: ipython.pt_app.app.current_buffer.complete_while_typing.func()
Out[3]: False

but still haven't figured out how to set complete_while_typing to True.

jab avatar Feb 13 '21 17:02 jab

@jab I think it has to be done by using a Filter, you need to pass it to complete_while_typing at the moment the instance of Buffer is created.

MrMino avatar Feb 15 '21 13:02 MrMino

Thanks. Sounds like this is something that requires changes to IPython itself then?

jab avatar Feb 15 '21 13:02 jab

@jab if you want to do it the supported way, then yes. It's probably possible to hack it in just by changing parameters in some of the instances of prompt toolkit classes, but I haven't looked that deep into it.

I might try to PR it though. But I'd first wait for @Carreau to comment on whether it's something he'd like to see: it would have to be some kind of a config option. It's very user-specific behavior and I don't think that everyone would be onboard to have it as the new default.

MrMino avatar Feb 15 '21 16:02 MrMino

Any updates?

seralouk avatar Oct 20 '22 12:10 seralouk

Out of curiosity, was this added in the past? We got a report of this over on the Terminal repo: https://github.com/microsoft/terminal/issues/14510 and it's certainly not us providing that completion text

zadjii-msft avatar Dec 09 '22 22:12 zadjii-msft

You are probably refering to #12570 which implemented autosuggestion from history (in terminal only) and not auto-trigger of full completer as this issue is asking for (although that can be achieved with jupyterlab-lsp).

Changelog entry for autosuggestions: https://ipython.readthedocs.io/en/stable/whatsnew/version8.html#autosuggestions

krassowski avatar Dec 09 '22 22:12 krassowski

Thanks for the clarification!

zadjii-msft avatar Dec 09 '22 22:12 zadjii-msft

According to @MrMino

@jab I think it has to be done by using a Filter, you need to pass it to complete_while_typing at the moment the instance of Buffer is created.

A compromise solution is to modify the IPython/terminal/interactiveshell.py. Add complete_while_typing=True to the PromptSession constructor in the init_prompt_toolkit_cli method of the TerminalInteractiveShell class like:

self.pt_app = PromptSession(
    complete_while_typing=True,
    ...
)

You also need to disable history search (change the enable_history_search to False in the config file) in order for complete_while_typing to work properly.

RIvance avatar Apr 04 '23 14:04 RIvance

Thanks @RIvance, tried that but still didn't get the autosuggest-while-typing behavior that you get out-of-the-box with e.g. bpython. (For example, type "foo". and immediately see a menu of all possible methods you can call on "foo" (and the menu appears as soon as you type ., no need to hit Tab first). As you continue typing, the menu gets filtered down to match what you've typed.) Do you get that in IPython when you try the above?

Would also be awesome if IPython had autosuggest for function parameters that automatically appeared as soon as you type ( (bpython also has this out-of-the-box), but not sure if that deserves its own separate issue.



Screenshot 2024-02-19 at 1 06 33 PM



Screenshot 2024-02-19 at 1 06 54 PM



Screenshot 2024-02-19 at 1 07 13 PM

jab avatar Feb 19 '24 18:02 jab