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

Fix completion issue #1160

Merged
merged 3 commits into from Jan 21, 2021
Merged

Fix completion issue #1160

merged 3 commits into from Jan 21, 2021

Conversation

casatir
Copy link
Contributor

@casatir casatir commented Jan 21, 2021

Completion implemented in #1155 is not fully working with JQuery terminal in console.html. It actually drops every completions that does not start with the prefix it computes (using whitespace splitting). See this comment.

One way to fix this is to prepend the prefix to each completion choice but this PR goes another way. CPython has a builtin completion system to work with readline called rlcompleter which you can use without standalone. This is the completion used in CPython's REPL. This will result in a faster completion experience, without any extra dependency (CPython's core instead of jedi/parso). Moreover, it already computes the completions with the prefix JQuery terminal is looking for.

This PR re-implements the completion in InteractiveConsole while fixing the issue exposed in this comment.

@rth
Copy link
Member

rth commented Jan 21, 2021

So if I understand correctly, IPython is also using rlcompleter with an option use of jedi if it's installed? https://github.com/ipython/ipython/blob/master/IPython/core/completer.py#L68 Then rlcompleter should indeed be sufficient for us as well. jedi package is 1.4 MB compressed so it's clearly not negligible for web usage.

@casatir
Copy link
Contributor Author

casatir commented Jan 21, 2021

One little issue remains. Completion works only on prefixes preceded by a whitespace as JQuery Terminal split words at whitespaces.

>>> str.isa <tab> <tab>
str.isalnum(        str.isalpha(        str.isascii(
>>> a = 5 ; str.isa <tab> <tab>
str.isalnum(        str.isalpha(        str.isascii(
>>> a = 5 ;str.isa <tab> <tab>  # no proposition

This is known upstream and could be fixed with wordAutocomplete: false and a bit of work. Should this be fixed in this PR? WDYT?

@rth
Copy link
Member

rth commented Jan 21, 2021

Should this be fixed in this PR? WDYT?

I think having this PR as is is already good. I'll try to review later today. We could always improve it in a follow up PR.

@casatir
Copy link
Contributor Author

casatir commented Jan 21, 2021

So if I understand correctly, IPython is also using rlcompleter with an option use of jedi if it's installed? https://github.com/ipython/ipython/blob/master/IPython/core/completer.py#L68

Thanks, haven't noticed:

This module started as fork of the rlcompleter module in the Python standard
library. The original enhancements made to rlcompleter have been sent
upstream and were accepted as of Python 2.3,

Then rlcompleter should indeed be sufficient for us as well. jedi package is 1.4 MB compressed so it's clearly not negligible for web usage.

Totally agree.

@casatir
Copy link
Contributor Author

casatir commented Jan 21, 2021

I think having this PR as is is already good. I'll try to review later today. We could always improve it in a follow up PR.

Great!

@hoodmane
Copy link
Member

hoodmane commented Jan 21, 2021

By the way, a major concern with using jedi for autocompletions is that jedi can cause fatal errors #821. It isn't great if requesting a completion causes the Python runtime to segfault. Surely rlcompleter doesn't suffer from this issue so I think it's a much better idea to use it over jedi.

Copy link
Member

@rth rth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment otherwise LGTM.

Could you please add separate changelog entry for the switch from jedi to rlcompleter in console.html?

# import re
# regex = '|'.join(map(re.escape, self.completer_word_break_characters))
# start = len(source) - re.search(regex, source[::-1]).start()
start = max(map(source.rfind, self.completer_word_break_characters)) + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This versions seems faster,

In [10]: %%timeit 
    ...: regex = '|'.join(map(re.escape, completer_word_break_characters)
    ...: ) 
    ...: re.search(regex, source[::-1]) 
    ...:  
    ...:                                                                 
8.74 µs ± 18.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [11]: %timeit max(map(source.rfind, completer_word_break_characters)) 
    ...: + 1                                                             

2.7 µs ± 11.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each

particularly given that this benchmark is biased toward regex since regex compilation will be cached.

So I think we can remove the comment. In any case it's µs so it shouldn't matter much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks for the benchmark!

start = max(map(source.rfind, self.completer_word_break_characters)) + 1
source = source[start:]
if "." in source:
completions = self._completer.attr_matches(source) # type: ignore
Copy link
Member

@rth rth Jan 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strange that mypy can't find these attributes, type annotations are likely incorrect somewhere in CPython. Not much we can do about it on our side.

@rth rth merged commit 1fa4b07 into pyodide:master Jan 21, 2021
@casatir casatir deleted the fix-completion-issue branch January 21, 2021 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants