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

[qtconsole] take %,%% prefix into account for completion #1944

Merged
merged 2 commits into from Jun 17, 2012

Conversation

Carreau
Copy link
Member

@Carreau Carreau commented Jun 13, 2012

fixes #1767

@fperez
Copy link
Member

fperez commented Jun 13, 2012

I'm busy today, but if anyone can review and test this, it would be great. This is an important fix to get in for 0.13, so it would be better to have it before the first beta (which I'm delaying to give @ellisonbg some time to review some NB stuff)

@takluyver
Copy link
Member

I've run it and verified that it fixes the bug.

@fperez
Copy link
Member

fperez commented Jun 13, 2012

Great, go for it then. It's an important one; I have to run now.

prefix = u'%'
if prefix :
prefix_filter = lambda x: u"".join(x.split(u'%', 2))
items = map(prefix_filter, items)
Copy link
Member

Choose a reason for hiding this comment

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

I feel like there ought to be a neater way to do this. How about [s.lstrip('%') for s in items]. Admittedly that would remove any number of leading %, but if we ever do have something with 3, that's probably the behaviour we want anyway.

Copy link
Member

Choose a reason for hiding this comment

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

we do lstrip(ESC_MAGIC) elsewhere in inspection, so this should be fine.

@takluyver
Copy link
Member

Doing it in the kernel would require an addition to the messaging protocol. If we think that's the right way to do it, I think we should apply this quick fix for 0.13, give or take some small tweaks, and revisit it after the release. I'm happy that it works with the code we have today, and I don't think we want to start refactoring completion code just now.

@Carreau
Copy link
Member Author

Carreau commented Jun 13, 2012

I thing that i can extract the prefixes, reverse them, and find the common sufixes of the prefix,that should be doable.

@takluyver
Copy link
Member

Not sure I follow that ;-)

@Carreau
Copy link
Member Author

Carreau commented Jun 13, 2012

Forced push to have only one commit, should take into account any prefix made of #!$...

Not sure I follow that ;-)

Sure you'll understand while reading the code (just because os.path.commonsuffix does not exist)

@takluyver
Copy link
Member

I did understand the code...eventually ;-) I'm pretty sure there should be a more readable solution. It might involve a regex.

@minrk
Copy link
Member

minrk commented Jun 13, 2012

I'm pretty sure there should be a more readable solution. It might involve a regex.

I think I'm going to keep this quote for a long time.

@fperez
Copy link
Member

fperez commented Jun 13, 2012

On Wed, Jun 13, 2012 at 2:01 PM, Min RK
reply@reply.github.com
wrote:

I think I'm going to keep this quote for a long time.

+1

@takluyver
Copy link
Member

Yes, it did feel a bit odd typing that. I stand by it, though - regexes have their place.

Thinking about this a bit more, I'm not sure that common_prefix is the easiest place to fix it - because the easy solution requires knowing the text that's already there. @Carreau , have a look at the cleanup code that's already in _handle_complete_reply - I think that a fix there might be simpler:

https://github.com/ipython/ipython/blob/master/IPython/frontend/qt/console/ipython_widget.py#L138

@minrk
Copy link
Member

minrk commented Jun 13, 2012

I stand by it, though - regexes have their place.

Absolutely true, and I am a lot more comfortable with them now than I used to be. Still an amusing statement.

@Carreau
Copy link
Member Author

Carreau commented Jun 14, 2012

@takluyver,

IPython console :

typed >>>  ti<tab>
compl >>>  ti   # I would expect time....
%%timeit  %time     %timeit   

IPython qtonsole (myfix):

typed >>>  ti<tab>
compl >>>  %time # got a leading %
%%timeit  %time     %timeit
In[1] >>>  import time
typed >>>  ti<tab>
compl >>>  time 
%%timeit  %time     %timeit   time

At least for me... so IMHO, the current solution is not a the level of what users would expect. It does not transform the prompt to % anymore, but py trying to get a common prefix with what the user typed you can't fast-forward the completion as it does with completions without leading % items.

And I'll let you try with regex :-)

@takluyver
Copy link
Member

We don't have to achieve everything the user could expect - tab completion is a complex thing, and there will be time to improve it later. What's important is that it shouldn't remove what you've typed without a good reason.

I'll have a play with it later and see what I can come up with.

@Carreau
Copy link
Member Author

Carreau commented Jun 14, 2012

Sorry, my memory is faulty, I though that at 0.12 milestone, after importing time typing ti<tab> would complete to time and propose %timeit ,%time, and time as available completions, but apparently it was not the case. I just tried to mimic this behaviour as best as I could, because I saw a regression in the current behavior.

So please consider this PR as both a fix and a proposal for enhancement that could eventually be brought to other frontend.

I just want to point out that it is the behaviour of the current notebook completer, but for the sake of consistency between frontend, I would accept to remove tis 'feature' also from the notebook if you think it is necessary.

@takluyver
Copy link
Member

I don't think consistency between frontends is a big concern right now
for tab completion. So long as each one works and isn't annoying, we
can come back to the system after 0.13.

@minrk
Copy link
Member

minrk commented Jun 15, 2012

I haven't quite followed this review, so is this one ready for merge now?

@Carreau
Copy link
Member Author

Carreau commented Jun 15, 2012

@takluyver wrote :

I'll have a play with it later and see what I can come up with.

I think Thomas want go try with regular expressions.

@takluyver
Copy link
Member

Sorry, I forgot about it. I'll have a look now and see if I can come up with something cleaner.

@takluyver
Copy link
Member

(Refer to #1972)

@takluyver
Copy link
Member

Focussing on this one again:

I think if we take the regex from #1972, we can separate prefixes and items more easily:

ESCAPE_RE = re.compile("^[" + ESC_SHELL + ESC_HELP + ESC_MAGIC + ESC_QUOTE + ESC_QUOTE2 + ESC_PAREN + "]+")

first_prefix = ESCAPE_RE.match(min(items)).group(0)
last_prefix = ESCAPE_RE.match(max(items)).group(0)
# ...
items = [ESCAPE_RE.sub("", s) for s in items]

That saves us the think-through-this-carefully lambda to get prefixes. I think the common-suffix line is the best way of doing what it does.

@takluyver
Copy link
Member

Also, let's make the docstring a bit clearer. Something like:

"Return the longest common prefix of a list of strings, but with special treatment of escape characters that might precede commands in IPython, such as %magic functions. Used in tab completion.

For a more general function, see os.path.commonprefix"

@Carreau
Copy link
Member Author

Carreau commented Jun 17, 2012

items = [ESCAPE_RE.sub("", s) for s in items]

lstrip is much faster on a few test, i'll keep it

@Carreau
Copy link
Member Author

Carreau commented Jun 17, 2012

Ok, done with regular expression.
I'm still desapointed that the whole function is not just a big regex...

@takluyver
Copy link
Member

OK, this is looking OK to me, and I've just done a quick manual test. I'm happy for you to merge this when you're ready.

@Carreau
Copy link
Member Author

Carreau commented Jun 17, 2012

Merging then.

Carreau added a commit that referenced this pull request Jun 17, 2012
[qtconsole] take %,%% prefix into account for completion
@Carreau Carreau merged commit 2484ba1 into ipython:master Jun 17, 2012
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
[qtconsole] take %,%% prefix into account for completion
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.

Tab completion problems with cell magics
4 participants