spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.21k stars 1.59k forks source link

Quotes and colons autocompletion #872

Closed spyder-bot closed 8 years ago

spyder-bot commented 9 years ago

From nunoport...@gmail.com on 2011-12-07T06:58:36Z

Hi,

How can i edit Spyder autocomplete feauture? I can't seem to find in preferences a way to do this...

thanks

Original issue: http://code.google.com/p/spyderlib/issues/detail?id=872

spyder-bot commented 9 years ago

From ccordoba12 on 2011-12-07T09:03:09Z

What do you mean exactly by "edit"? Please give us an example

You can change how autocompletion works in:

  1. Tools > Preferences > Editor > Code Introspection/Analysis (for the Editor)
  2. Tools > Preferences > Console > Introspection (for the Console)

Summary: Autocomplete edition
Labels: Cat-CodeCompletion

spyder-bot commented 9 years ago

From nunoport...@gmail.com on 2011-12-07T09:28:22Z

In preferences you just activate or deactivate autocompletion.

This are some examples of what i would like to add

Auto complete single quotation marks.

Putting a colon after parenthesis when defining a function.

Putting a colon in front of each if...else statements (don't know if this is doable or pratical).

Autoindent (at least on my spyder is not working don't know if this is already implemented...)

I'm used to that so i think that would be a plus...

spyder-bot commented 9 years ago

From ccordoba12 on 2011-12-07T09:40:03Z

Ok, I understand you now. The problem is these things have to be added to the source code. I don't think there is a way for users to add them.

What do you mean by 'Auto complete single quotation marks'? Something like: if you write ' you want to get ''?

Autoindentation can be turned on/off in Tools > Preferences > Editor > Advanced Settings. To make it work you have to write colons at the end of def's, if's, etc, though.

Summary: Code completion enhancements
Labels: -Type-Defect Type-Enhancement

spyder-bot commented 9 years ago

From nunoport...@gmail.com on 2011-12-07T09:57:11Z

In pydev for eclipse, when you put a single quotation mark it automatically adds the other one and the cursor stays between them for typing whatever you want...

if colons were automatically placed after if or def's, indentation should work as it normally does... i guess it would save some time but i can't argue with you because you probably know more (a lot more) of python than i do! :)

spyder-bot commented 9 years ago

From techtonik@gmail.com on 2011-12-08T03:45:40Z

To me it is completely normal to add autocompletion enhancements to the source code. The difficulty is that the process is not documented. =) Carlos, can you provide an entrypoint for people who want to improve this part of Spyder to look at?

spyder-bot commented 9 years ago

From ccordoba12 on 2011-12-26T10:12:35Z

Summary: Several edition enhancements
Labels: -Cat-CodeCompletion Cat-Editor

spyder-bot commented 9 years ago

From ccordoba12 on 2011-12-29T16:40:59Z

Pierre, I added the possibility to automatically insert colons after an Enter keypress. I also added a config option so the user can decide if he/she wants to turn on/off this functionality. Should I update the UserConfig version because of this change, like you did f.ex in revision 7ef1137a0c95 ?

spyder-bot commented 9 years ago

From pierre.raybaut on 2011-12-30T05:07:16Z

We need to change the configuration file version number only when changing the default value of an existing option (like I did in the revision you mentioned). If this is a new option (or section) in the configuration file, this is not necessary. I suppose that it's a new option here, so you shouldn't have to change the version number.

spyder-bot commented 9 years ago

From ccordoba12 on 2011-12-31T07:15:13Z

Yep, I'm adding a new option. Thanks for the clarification.

spyder-bot commented 9 years ago

From ccordoba12 on 2011-12-31T07:15:49Z

This issue was updated by revision 8cb13b3c5f93 .

Status: Started

spyder-bot commented 9 years ago

From ccordoba12 on 2011-12-31T07:15:49Z

This issue was updated by revision faa8ad54d476 .

-. The user can decide at will if he/she wants to turn on/off this functionality -. The option to add colons is turned on by default.

Status: Fixed

spyder-bot commented 9 years ago

From ccordoba12 on 2011-12-31T07:21:46Z

Nuno, I hope these additions cover all your suggestions. Thanks for taking the time to post them here, they're a great addition to Spyder's usability.

Cheers, Carlos

spyder-bot commented 9 years ago

From nunoport...@gmail.com on 2011-12-31T10:40:49Z

Yes, i believe that these additions make life easier for programmers...

I'm glad i could contribute for the improvement of Spyder! it's a great tool! keep up the good work! :)

Regards, Nuno

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-01T05:49:14Z

After the previous commits pressing "enter" at the end of the following lines adds (wrongly) a semicolon

if a == 1 and \

for i in ("a", "b", "c",

I think a check should be a added for "\" and for parentheses.

Also there is a problem with the automatic insertion of the second quote. Par example. After inserting a double quote the cursor is at the position noted with "": "" After writing some text we are like this: "text" Now inserting a double quote inserts 2 more double quotes instead of moving the cursor one position to the right. "text""" I think that this should behave similarly to parentheses.

Regards Panos

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-01T06:11:35Z

Clarifying my previous comment. Now, when you insert a quote you have the following "" After inserting the second quote you should have the following "" instead of """" of course there should be some special care for triple quotes, so inserting a third quote in a row should be recognized and you should have something like this "" (inserting a new quote here) """*"""

spyder-bot commented 9 years ago

From ccordoba12 on 2012-01-01T13:53:07Z

Thanks Panos for your feedback. I'll improve our checking mechanism to cover the cases you describe for 'if' and 'for'.

About double quotes, do you think it would be better to disable completely their automatic insertion? I think it's not that easy to check for open quotes to close them, as it is for parentheses but I'll have to think about it.

Status: Started

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-01T14:23:40Z

Hi Carlos,

In regard of quotes, I think that it is a nice feauture to have, but in no way necessary. I tried to think of an algorithm implementing the correct behavior, but you are right, it is not very easy to find a generic and consistent algorithm. Of course, it is possible but probably it is not worth the effort, since the usefulness is limited (in contrast with parentheses).

I don't know if it easy, or even possible, to add tests for spyder's editor, but I think that this problem would be much easier solved through test driven programming. I think that writing tests for this problem is easier than describing it :)

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-02T06:04:10Z

perhaps the following issue could be merged with this one https://code.google.com/p/spyderlib/issues/detail?id=887

spyder-bot commented 9 years ago

From ccordoba12 on 2012-01-05T15:41:34Z

This issue was updated by revision 18efa83f9033 .

Improve usability of automatic insertion of colons and quotes after pmav99 gmail com suggestions

spyder-bot commented 9 years ago

From ccordoba12 on 2012-01-05T15:45:50Z

I added several changes that I think cover all your suggestions (including triple quotes). Please try them and let me now if they work for you.

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-07T12:54:23Z

After the last changes, each time I press enter I get the following traceback at the internal console

Traceback (most recent call last): File "/home/feanor/Programming/Mercurial/spyderlib/spyderlib/widgets/sourcecode/codeeditor.py", line 2025, in keyPressEvent self.do_insert_colons(): File "/home/feanor/Programming/Mercurial/spyderlib/spyderlib/widgets/sourcecode/codeeditor.py", line 1998, in do_insert_colons line_pos = self.toPlainText().index(leading_text) AttributeError: 'QString' object has no attribute 'index'

changing line 1998 to the following one to

    line_pos = str(self.toPlainText()).index(leading_text)

results in the following traceback

Traceback (most recent call last): File "/home/feanor/Programming/Mercurial/spyderlib/spyderlib/widgets/sourcecode/codeeditor.py", line 2025, in keyPressEvent self.do_insert_colons(): File "/home/feanor/Programming/Mercurial/spyderlib/spyderlib/widgets/sourcecode/codeeditor.py", line 1998, in do_insert_colons line_pos = str(self.toPlainText()).index(leading_text) UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 38: ordinal not in range(128)

using unicode() instead of str() solves the problem but I don't know if there are other side-effects.

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-07T13:03:25Z

In regard the issue with the quotes, it works ok, but there are problems when the quotes are nested. Try to do the following:

(1) the cursor is at the asterisk. It is just before you insert the single quote (2) what you get after you insert the single quote.

(1) "Some text. A single quote follows " (2) "Some text. A single quote follows ''"

As you can see a second quote has been inserted. If it is easy, add a check for already open quotes (of any type). If it is bothersome leave it as it is.

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-07T14:40:28Z

In continuation of comment 21

The need for unicode() comes up when I edit files with unicode characters (using from future import unicode_literals). I don't know if this has any side-effects.

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-07T14:42:58Z

In regard of quotes, erasing the first quote, leaves the second one in place. The second one should be erased too.

Current behavior:

"" # pressing backspace "

wanted behavior:

"" # pressing backspace

spyder-bot commented 9 years ago

From ccordoba12 on 2012-01-08T17:06:43Z

Thanks Panos for your invaluable help on testing these changes and making me improve them.

You're totally right about the use of unicode (I didn't noticed because I was using PySide). I'm going to correct it as soon as possible. Also, I'll try to implement the behavior you described in comment #27.

However, I don't follow you in comment #22. Probably your example expression should be this one?

'Some text. A single quote follows *'

Notice that here the first and last quotes are single ones, not double (as you wrote them). I'll see how easy it is to check for unmatched quotes but can't promise anything.

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-08T23:40:23Z

Ok, perhaps my example was not that clear but you seem to have understood the problem anyway! :) Nonetheless, I'll try to elaborate a little bit.

The problem presents itself when trying to insert single quotes within double quotes (or single quotes within triple quotes, or any other combination). The wanted behavior is simple:

The automatic insertion of closing quotes should be disabled when there is already an unclosed quotation (of any type).

Example:

"text * more text" "text '*' more text" |

The closing single quote (the one above the |) shouldn't be inserted.

If an easy fix cannot be found, then you could: 1) Leave it as it is now. It is not that important after all. Nested quotes are not that common. 2) Just disable them for triple quotes. Checking for an even number of triple quotes in the text preceding the placement of the cursor should do it.

Note. I don't know if 2) is doable with the current mechanism, since it involves knowing what is going on, on the previous lines.

ps. A proper fix, would probably involve some regex magic. Maybe something like this would do it: 1) Finding the position of the starting quote 2) Finding the length of the quoted text ( http://stackoverflow.com/questions/4917932/regex-to-ignore-text-between-quotes ) 3) Combine them in order to find the range of cursor positions that are within the quotes. 3) Disabling the automatic insertion of ending quotes when you are within this range.

Special care would be needed for triple quotes, since they are used for multiline strings.

spyder-bot commented 9 years ago

From ccordoba12 on 2012-01-09T06:30:31Z

Thanks for the very detailed explanation and also for the hints. Could you give me a real example to see why is not desirable to have automatic closing quotes inside other quotes? I'm sorry for my ignorance but I haven't seen this situation before. If you give me some examples, I can decide if it's worth the effort to implement the feature or not.

By the way, I fixed comments #27 and #21 tonight.

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-09T07:39:55Z

Thank you very much for your efforts. I would love to be able to help you more (eg with patches), but understanding codeeditor.py requires more time than I currently have :(

I feel obliged to state that the quote mechanism is more than satisfying. Even if you leave it as it is, it is more than ok. Anyway. I believe that the most usual usage of nested quotes is to create a string like the following one:

"The Wall's forces ..."

I've often used python to programmaticaly create latex source files. So writing strings like the aforementioned one are not that uncommon in my case. If you ask me, check issue #887 next. It is much more useful/annoying than nested quotes.

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-10T11:13:30Z

Hi Carlos, the insertion of closing quotes should be disabled on comments too...

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-11T11:55:49Z

One more edge case...

If you move the cursor just before a starting quote and try to insert a new one (like in inserting a string in list of strings) no new quote is inserted. The cursor moves one position to the right instead...

Initial condition:

["string1", *"string2"]

Inserting a quote results in:

["string1", "*string2"]

Instead of:

["string1", "*""string2"]
spyder-bot commented 9 years ago

From airdrik@gmail.com on 2012-01-16T13:49:23Z

I just updated to 2.1.7 and see the slew of improvements - thank you guys for working this stuff out.

I would like to point out that the triple-quote completion isn't working for single-tick quotes - that is while typing " three times produces """""", typing ' three times produces '''' (with accompanying syntax highlighting problems).

I would also like to point out issue #877 which is somewhat related - if you have text selected and insert a (single/double) quote that it adds quotes around the selected text instead of merely replacing

spyder-bot commented 9 years ago

From steve.f....@gmail.com on 2012-01-17T13:07:03Z

I just started really using 2.1.7 and I am not a fan of the way quote completion is working on a line that already has text in it. It's a hassle to add quotes to anything that's already typed. It seems good for when writing brand new lines of code.

spyder-bot commented 9 years ago

From steve.f....@gmail.com on 2012-01-17T13:25:23Z

This is definitely a problem. Adding a closing quote yields two quotes when it's after a string so I have to manually delete one. Example 'x => 'x'' Instead of 'x' as expected. I hate to say it, but this feature seems poorly tested. It's great for the initial writing of code, but if you're editing code it's a nightmare to use this.

spyder-bot commented 9 years ago

From ccordoba12 on 2012-01-17T19:14:13Z

@-airdrik: Why do you want triple insertion of single quotes to work? Double quotes are important for docstrings and that's why I introduced this feature. It wouldn't be hard to do it but I just want to know why it's important for you.

You're right about what you say in issue #887 , it would be a good addition. I'll work to implement it.

@-steve:I'm working to solve these side effects, i.e. to not automatically add quotes inside other quotes and to match quotes properly.

I didn't imagine there were some many corner cases with this feature, but it would have been very hard for me to know them all before hand and without the help of Panos, airdrik and now you.

If you have more issues please post them here and I'll try to solve them for 2.1.8.

spyder-bot commented 9 years ago

From airdrik@gmail.com on 2012-01-17T19:25:34Z

Because you can also use single ticks for triple-quote strings, including docstrings.

spyder-bot commented 9 years ago

From steve.f....@gmail.com on 2012-01-18T07:10:00Z

Yeah, this is a lot of work. Maybe we should reference netbeans or some other editors that have already implemented this kind of behavior for some tips?

spyder-bot commented 9 years ago

From steve.f....@gmail.com on 2012-01-20T11:29:28Z

I noticed that typing ' for a contraction inside a comment yields an unnecessary trailing '. Maybe we should only do this if we're typing actual code and not comment?

spyder-bot commented 9 years ago

From steve.f....@gmail.com on 2012-01-24T09:58:25Z

: insertion is not working correctly when adding a newline in the middle of a function definition.

Example: "def function( a, b, c, d ):"

Press enter after the "b," and a colon will be inserted such that it looks like this: "def function( a, b,: c, d ):"

spyder-bot commented 9 years ago

From ccordoba12 on 2012-01-27T08:45:45Z

Steve, I can't reproduce this wrong behavior. It's working fine for me, like this:

  1. "def f( a, b* "
  2. "def f( a, b, " Where \ points at the place I hit Enter.

Could someone else confirm it?

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-01-27T09:56:52Z

I confirm it too Carlos. The reason you don't see it, is probably that you have inserted quotes... Try it like this:

----- Before def f(a, b, c, d): ----- After def function( a, b, : c, d ):

spyder-bot commented 9 years ago

From steve.f....@gmail.com on 2012-01-27T11:09:07Z

Ahh, yes. I wasn't implying the quotes should actually be in the source code. Sorry about that.

spyder-bot commented 9 years ago

From steve.f....@gmail.com on 2012-02-09T13:20:58Z

If you press enter after the following line a : is erroneously inserted. force_choke = True

Resulting line: force_choke = True:

spyder-bot commented 9 years ago

From ccordoba12 on 2012-02-10T07:22:57Z

Steve, I've fixed that one in trunk. See issue #913

spyder-bot commented 9 years ago

From steve.f....@gmail.com on 2012-02-10T10:58:20Z

D'oh.. wasn't running devel on my secondary PC. Oh well. :)

spyder-bot commented 9 years ago

From ccordoba12 on 2012-02-20T06:23:06Z

This issue was updated by revision 0710fab37f92 .

-. Add an option to control quotes insertion independently from brace insertion

spyder-bot commented 9 years ago

From ccordoba12 on 2012-02-20T06:23:07Z

This issue was updated by revision 1c12254812cf .

Try to address comment #34

spyder-bot commented 9 years ago

From ccordoba12 on 2012-02-20T06:23:08Z

This issue was updated by revision f56795aa5125 .

-. Use the newly introduced 'char' variable to simplify several parts of the method. -. Try to address comment #35.

spyder-bot commented 9 years ago

From ccordoba12 on 2012-02-20T06:26:47Z

I just added several changes trying to fix/improve the situation given your suggestions. Panos, airdrik, Steve, please help me test the changes ASAP to see if things are working correctly. We need to release a new version very quickly!

spyder-bot commented 9 years ago

From ccordoba12 on 2012-02-20T08:33:19Z

Ok guys, change of plans. This feature was eliminated from 2.1.7 and pushed to the upcoming 2.2 release. If you were working from the default repo then you shouldn't have any problems helping me to test the fixes I did.

If you were working with the official release (through Python(x,y) for example), please use mercurial to download the new code at the default repo. Let me know If you have doubts about how to do that.

Labels: Milestone-Release2.2

spyder-bot commented 9 years ago

From pmav99@gmail.com on 2012-02-21T06:26:12Z

Hi Carlos

I was thinking that maybe a different approach should be followed in this problem. I think that instead of coding, we should first write a specification with the desired behavior.

Perhaps this is not going to be easy, but at least it is going to make easier the actual implementation of the code. At the very least it is going to make easier the testing of the code in order to eliminate regressions.

At the following link I wrote an example of how this spec could be implemented. It's not complete of course and I didn't spend much time thinking the structure. My intention is to provide something like a proof of concept. http://paste.pocoo.org/show/554472/