Repository: spyder-ide/spyder

Issue:
Quotes and colons autocompletion
_From [nunoport...@gmail.com](https://code.google.com/u/108617919618299841919/) 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_



Discussion:
_From [ccordoba12](https://code.google.com/u/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  

_From [nunoport...@gmail.com](https://code.google.com/u/108617919618299841919/) 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...

_From [ccordoba12](https://code.google.com/u/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  

_From [nunoport...@gmail.com](https://code.google.com/u/108617919618299841919/) 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! :)

_From [techtonik@gmail.com](https://code.google.com/u/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?

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2011-12-26T10:12:35Z_

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

_From [ccordoba12](https://code.google.com/u/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 ?

_From [pierre.raybaut](https://code.google.com/u/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.

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2011-12-31T07:15:13Z_

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

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2011-12-31T07:15:49Z_

This issue was updated by revision 8cb13b3c5f93 .

**Status:** Started  

_From [ccordoba12](https://code.google.com/u/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  

_From [ccordoba12](https://code.google.com/u/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

_From [nunoport...@gmail.com](https://code.google.com/u/108617919618299841919/) 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

_From [pmav99@gmail.com](https://code.google.com/u/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

_From [pmav99@gmail.com](https://code.google.com/u/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)
    """*"""

_From [ccordoba12](https://code.google.com/u/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  

_From [pmav99@gmail.com](https://code.google.com/u/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 :)

_From [pmav99@gmail.com](https://code.google.com/u/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

_From [ccordoba12](https://code.google.com/u/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 <at>
gmail <dot> com suggestions

_From [ccordoba12](https://code.google.com/u/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.

_From [pmav99@gmail.com](https://code.google.com/u/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.

_From [pmav99@gmail.com](https://code.google.com/u/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.

_From [pmav99@gmail.com](https://code.google.com/u/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.

_From [pmav99@gmail.com](https://code.google.com/u/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
*

_From [ccordoba12](https://code.google.com/u/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.

_From [pmav99@gmail.com](https://code.google.com/u/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.

_From [ccordoba12](https://code.google.com/u/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.

_From [pmav99@gmail.com](https://code.google.com/u/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.

_From [pmav99@gmail.com](https://code.google.com/u/pmav99@gmail.com/) on 2012-01-10T11:13:30Z_

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

_From [pmav99@gmail.com](https://code.google.com/u/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"]
```

_From [airdrik@gmail.com](https://code.google.com/u/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

_From [steve.f....@gmail.com](https://code.google.com/u/117650558843796505335/) 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.

_From [steve.f....@gmail.com](https://code.google.com/u/117650558843796505335/) 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.

_From [ccordoba12](https://code.google.com/u/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.

_From [airdrik@gmail.com](https://code.google.com/u/airdrik@gmail.com/) on 2012-01-17T19:25:34Z_

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

_From [steve.f....@gmail.com](https://code.google.com/u/117650558843796505335/) 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?

_From [steve.f....@gmail.com](https://code.google.com/u/117650558843796505335/) 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?

_From [steve.f....@gmail.com](https://code.google.com/u/117650558843796505335/) 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 ):"

_From [ccordoba12](https://code.google.com/u/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?

_From [pmav99@gmail.com](https://code.google.com/u/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 ):

_From [steve.f....@gmail.com](https://code.google.com/u/117650558843796505335/) on 2012-01-27T11:09:07Z_

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

_From [steve.f....@gmail.com](https://code.google.com/u/117650558843796505335/) 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:

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2012-02-10T07:22:57Z_

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

_From [steve.f....@gmail.com](https://code.google.com/u/117650558843796505335/) on 2012-02-10T10:58:20Z_

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

_From [ccordoba12](https://code.google.com/u/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

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2012-02-20T06:23:07Z_

This issue was updated by revision 1c12254812cf .

Try to address comment #34

_From [ccordoba12](https://code.google.com/u/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`.

_From [ccordoba12](https://code.google.com/u/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!

_From [ccordoba12](https://code.google.com/u/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  

_From [pmav99@gmail.com](https://code.google.com/u/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/

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2012-02-21T07:34:12Z_

That's a great idea Panos! I just made a wiki page with your initial suggestions: https://code.google.com/p/spyderlib/wiki/AutomaticColonAndQuotesInsertion I think anyone can edit it, so please add to it any other desired behavior I have to take into account while coding this feature. I will also be adding what we have right now.

_From [pmav99@gmail.com](https://code.google.com/u/pmav99@gmail.com/) on 2012-02-21T07:42:20Z_

I am glad that you liked it :)

If I were you, I would wait until the specs are more complete before starting to code.

_From [pmav99@gmail.com](https://code.google.com/u/pmav99@gmail.com/) on 2012-02-21T11:28:35Z_

Carlos, the wiki page is not editable by common users.

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2012-02-21T12:06:45Z_

I see. I think Pierre is the only one who has rights to let you edit our wiki.

Pierre, could you grant Panos permission to do it?

_From [techtonik@gmail.com](https://code.google.com/u/techtonik@gmail.com/) on 2012-02-28T11:52:05Z_

I also thought that 'specification first' approach will greatly help with debugging of this feature - https://groups.google.com/d/msg/spyderlib/ICWotnlS7bM/dqGaSzr62UYJ It is a pleasure to see the efforts in this direction.

It is possible to clone and edit wiki offline. https://code.google.com/p/spyderlib/source/browse?repo=wiki There is also an Edit link at the page source that allows common users to edit files online and send a patch through the tracker https://code.google.com/p/spyderlib/source/browse/AutomaticColonAndQuotesInsertion.wiki?repo=wiki but it lacks wiki preview, which is not fun.

_From [pierre.raybaut](https://code.google.com/u/pierre.raybaut/) on 2012-02-28T13:05:34Z_

That is done, Carlos. Panos may now edit wiki pages, among other things.

Sorry for the delay, I tend to react slowly these days... (Anatoly woke me up!).

_From [pmav99@gmail.com](https://code.google.com/u/pmav99@gmail.com/) on 2012-02-28T15:24:23Z_

I added some more test cases and rewrote some comments. 

I also took the liberty to add content for more features (auto indentation) apart from the insertions of colons and quotes. I haven't added any content yet, but there are more features that would benefit from this type of specification, e.g. "smart indentation". 

If it is ok, I was thinking that a more generic title would be more appropriate. Maybe something like "Specification for the text editor widget". If you agree, perhaps the url should change too.

_From [techtonik@gmail.com](https://code.google.com/u/techtonik@gmail.com/) on 2012-02-28T23:28:00Z_

That's great. Can you rename the page to something like SpecAutocompletion? It would better reflect the status and purpose.

**Summary:** Autocompletion customization  

_From [techtonik@gmail.com](https://code.google.com/u/techtonik@gmail.com/) on 2012-02-28T23:37:05Z_

On the second thought more generic title is not that nice. Scope should be limited to a munchable chunks. Even SpecAutocompletion is a very broad term, which includes integration with rope. SpecColonsQuotesAndWhitespace is more appropriate? Or just SPEP001 with a description "Colons, quotes and whitespace autocompletion".

**Summary:** Quotes and colons autocompletion  

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2012-03-01T07:08:45Z_

Thanks for your efforts. The page is looking great and it would help me a lot to improve and finish this feature.

@-Pierre: No problem. Thanks for adding Panos. He's being of great help.

@-Anatoly: I think SpecColonsAndQuotesAutocompletion is the right name. Why did you add 'Whitespace'?

_From [techtonik@gmail.com](https://code.google.com/u/techtonik@gmail.com/) on 2012-03-02T00:52:53Z_

Whitespace is to cover indentation part, because indentation may deserve its own separate spec. SpecColonsAndQuotesAutocompletion name is a bit excessive. I can't figure out what else can spec for colons and quotes define in editor if not auto completion, so SpecColonsAndQuotes is fine with me.

_From [pierre.raybaut](https://code.google.com/u/pierre.raybaut/) on 2012-03-18T13:45:59Z_

**Labels:** -Milestone-Release2.2 Milestone-v2.2  

_From [pierre.raybaut](https://code.google.com/u/pierre.raybaut/) on 2012-03-18T13:56:32Z_

**Labels:** -Milestone-v2.2 MS-v2.2  

_From [pierre.raybaut](https://code.google.com/u/pierre.raybaut/) on 2012-03-18T14:15:24Z_

**Labels:** -Type-Enhancement Type-Enh  

_From [pmav99@gmail.com](https://code.google.com/u/pmav99@gmail.com/) on 2012-03-18T15:00:28Z_

I added some more tests for auto-indentation. 

In regard of the wikipage title, perhaps the issues regarding the whitepage should be moved to a seperate page.

_From [steve.f....@gmail.com](https://code.google.com/u/117650558843796505335/) on 2013-03-01T12:39:55Z_

What's the status on this?  I have a case where colon autocompletion is still broken.

Example:
for x, y in [
 [1,2],     #^ Pressing enter here will insert a colon when it should not.
 [3,4],
]

Running 2.2.0 beta 3

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2013-03-05T13:46:45Z_

Steve, this is the last thing to do before 2.2 is released. I'll fix it in beta4.

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2013-03-30T22:45:27Z_

Ok guys, after neglecting this issue for a year, I just committed several fixes to improve the situation significantly for both colon and quotes auto-insertion. 

I also added a new section to our specification for this feature about quotes insertion (which was missing). https://code.google.com/p/spyderlib/wiki/AutomaticColonAndQuotesInsertion Please test again to see if thing are working as expected for you.

_From [airdrik@gmail.com](https://code.google.com/u/airdrik@gmail.com/) on 2013-04-02T08:30:37Z_

So I just checked out a fresh copy (Spyder 2.2.0beta4 (b6204549563f+:2690+)) and tried out some of the auto-quote stuff, but the only thing I got was that if the cursor is between two quotes and you hit backspace it will delete both quotes.  I didn't get any auto-inserting of quotes or moving past a quote if you type the quote.  
Or in other words according to the Quotes Insertion section on that wiki page, A3 works, A4 doesn't work, A5 works (does nothing special), A6 doesn't work, A7 works, A8 works, A9 works (you should also include some more basic test cases, including adding a quote to a fresh line, or to a new assignment e.g. a = "hello", typing a quote when in front of the end-quote - neither of which worked when I tried them).

I also tested the Colon insertion and auto-indentation and came to the same conclusions as are listed on the wiki page.

_From [airdrik@gmail.com](https://code.google.com/u/airdrik@gmail.com/) on 2013-04-02T08:34:15Z_

Also, quoting selected text isn't working ( issue #877 )

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2013-04-02T11:57:31Z_

airdrik, I think you haven't activated the feature. In 2.2 auto-quotes are off by default and you have to explicitly turn them on.

For that you have to go to

Tools > Preferences > Editor > Advanced Settings > Source code

and then select the option

"Automatic insertion of closing quotes."

By the way, issue #877 depends on this feature too.

I'll add your recommendations for basic tests, thanks for your suggestions.

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2013-04-05T14:33:18Z_

Jed, could you help me test if everything is working according to our spec?

**Cc:** jed.lud...@gmail.com  

_From [jed.lud...@gmail.com](https://code.google.com/u/114333776555678317161/) on 2013-04-05T20:48:05Z_

Test results at revision b6204549563f on Windows 7.

Colon insertion: Test cases A3, A5, B3, B5, C3, and E4 are not working.

Quote insertion: I can't seem to get any of the test cases to work. I did enable the setting in Preferences, but it doesn't seem to matter.

Auto indent: Test cases A3, A5, B3, and B5 are not working.

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2013-04-06T06:34:46Z_

Thanks Jed. About your results:

Colon insertion: Your reported failures were expected because all correspond to multi-line statements, which I didn't fixed yet.

Quote insertion: I don't know why is failing for you. I tested it on WinXP and Linux, and it's working fine for me. I'll investigate further.

Auto indent: I haven't touched that part yet because it's not part of this issue.

_From [jed.lud...@gmail.com](https://code.google.com/u/114333776555678317161/) on 2013-04-06T12:12:46Z_

Ok, retesting quote insertion now that I understand the spec a little better :).

Advanced test A4 requires that I enter four " symbols before the auto insert happens.

There are still a couple of test cases that I'm not sure I completely understand, but as best I can tell they seem to be working.

_From [airdrik@gmail.com](https://code.google.com/u/airdrik@gmail.com/) on 2013-04-08T15:50:51Z_

Going back and retesting the quotes insertion with the option enabled, I get the same results as Jed - everything works except that A4 requires typing a 4th quote to activate, but it should activate on the 3rd quote.

Another (more advanced, feel free to veto) suggestion: if you hit enter inside of a fully-closed single or double-quote string to update the quotes to triple-quotes
example:
a = "some string I want to *make into a multi-line string"
->
a = """some string I want to 
make into a multi-line string"""

Which also brings up a couple more advanced indentation cases:  indentation within and after triple-quote strings.
Inside triple-quote strings indentation should be manual, or at least ignore any previous indentation.
After triple-quote strings indentation should resume from where it was before the string.

_From [jed.lud...@gmail.com](https://code.google.com/u/114333776555678317161/) on 2013-04-29T15:08:36Z_

**Labels:** MS-v2.2.1  

_From [jed.lud...@gmail.com](https://code.google.com/u/114333776555678317161/) on 2013-04-29T15:10:06Z_

**Labels:** -MS-v2.2  

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2013-06-10T18:47:16Z_

**Labels:** -MS-v2.2.1 MS-v2.3  

_From [jed.lud...@gmail.com](https://code.google.com/u/114333776555678317161/) on 2013-09-09T07:23:21Z_

**Cc:** -jed.lud...@gmail.com  

_From [wsu...@gmail.com](https://code.google.com/u/115225145614373588440/) on 2013-11-07T16:12:26Z_

Try this using 2.2.3:

`class MyNewException(Exception): pass`

After hitting enter, you will see this:

`class MyNewException(Exception): pass:`

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2013-12-11T12:18:17Z_

**Labels:** -MS-v2.3 MS-v2.3.1  

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2014-08-17T18:36:20Z_

**Labels:** -MS-v2.3.1 MS-v2.4  


Resolving patch:
diff --git a/spyderlib/widgets/sourcecode/codeeditor.py b/spyderlib/widgets/sourcecode/codeeditor.py
--- a/spyderlib/widgets/sourcecode/codeeditor.py
+++ b/spyderlib/widgets/sourcecode/codeeditor.py
@@ -2300,6 +2300,22 @@ def __unmatched_braces_in_line(self, text):
                 match = self.find_brace_match(line_pos+pos, char, forward=True)
                 if (match is None) or (match > line_pos+len(text)):
                     return True
+            if char in [')', ']', '}']:
+                match = self.find_brace_match(line_pos+pos, char, forward=False)
+                if (match is None) or (match < line_pos):
+                    return True
+        return False
+
+    def __has_colon_not_in_brackets(self, text):
+        """
+        Return whether a string has a colon which is not between brackets.
+        This function returns True if the given string has a colon which is
+        not between a pair of (round, square or curly) brackets. It assumes
+        that the brackets in the string are balanced.
+        """
+        for pos, char in enumerate(text):
+            if char == ':' and not self.__unmatched_braces_in_line(text[:pos]):
+                return True
         return False
 
     def autoinsert_colons(self):
@@ -2315,6 +2331,8 @@ def autoinsert_colons(self):
             return False
         elif self.__unmatched_braces_in_line(line_text):
             return False
+        elif self.__has_colon_not_in_brackets(line_text):
+            return False
         else:
             return True
 


Test patch:
diff --git a/spyderlib/widgets/sourcecode/tests/test_autocolon.py b/spyderlib/widgets/sourcecode/tests/test_autocolon.py
new file mode 100644
--- /dev/null
+++ b/spyderlib/widgets/sourcecode/tests/test_autocolon.py
@@ -0,0 +1,102 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright © 2009- The Spyder Development Team
+# Licensed under the terms of the MIT License
+#
+
+"""
+Tests for the automatic insertion of colons in the editor
+"""
+
+# Third party imports
+from qtpy.QtGui import QTextCursor
+import pytest
+
+# Local imports
+from spyderlib.utils.qthelpers import qapplication
+from spyderlib.widgets.sourcecode.codeeditor import CodeEditor
+
+
+# --- Fixtures
+# -----------------------------------------------------------------------------
+def construct_editor(text):
+    app = qapplication()
+    editor = CodeEditor(parent=None)
+    editor.setup_editor(language='Python')
+    editor.set_text(text)
+    cursor = editor.textCursor()
+    cursor.movePosition(QTextCursor.End)
+    editor.setTextCursor(cursor)
+    return editor
+
+
+# --- Tests
+# -----------------------------------------------------------------------------
+def test_no_auto_colon_after_simple_statement():
+    editor = construct_editor("x = 1")
+    assert editor.autoinsert_colons() == False
+
+def test_auto_colon_after_if_statement():
+    editor = construct_editor("if x == 1")
+    assert editor.autoinsert_colons() == True
+    
+def test_no_auto_colon_if_not_at_end_of_line():
+    editor = construct_editor("if x == 1")
+    cursor = editor.textCursor()
+    cursor.movePosition(QTextCursor.Left)
+    editor.setTextCursor(cursor)
+    assert editor.autoinsert_colons() == False
+
+def test_no_auto_colon_if_unterminated_string():    
+    editor = construct_editor("if x == '1") 
+    assert editor.autoinsert_colons() == False
+
+def test_no_auto_colon_in_comment():
+    editor = construct_editor("if x == 1 # comment")
+    assert editor.autoinsert_colons() == False
+
+def test_no_auto_colon_if_already_ends_in_colon():
+    editor = construct_editor("if x == 1:") 
+    assert editor.autoinsert_colons() == False
+
+def test_no_auto_colon_if_ends_in_backslash():
+    editor = construct_editor("if x == 1 \\") 
+    assert editor.autoinsert_colons() == False
+
+def test_no_auto_colon_in_one_line_if_statement():
+    editor = construct_editor("if x < 0: x = 0") 
+    assert editor.autoinsert_colons() == False
+
+def test_auto_colon_even_if_colon_inside_brackets():
+    editor = construct_editor("if text[:-1].endswith('bla')")
+    assert editor.autoinsert_colons() == True
+
+def test_no_auto_colon_in_listcomp_over_two_lines():
+    editor = construct_editor("ns = [ n for ns in range(10) \n if n < 5 ]")
+    assert editor.autoinsert_colons() == False
+
+    
+# --- Failing tests
+# -----------------------------------------------------------------------------
+@pytest.mark.xfail
+def test_auto_colon_even_if_colon_inside_quotes():
+    editor = construct_editor("if text == ':'")
+    assert editor.autoinsert_colons() == True
+
+@pytest.mark.xfail
+def test_no_auto_colon_in_listcomp_over_three_lines():
+    editor = construct_editor("ns = [ n \n for ns in range(10) \n if n < 5 ]")
+    cursor = editor.textCursor()
+    cursor.movePosition(QTextCursor.Up)
+    cursor.movePosition(QTextCursor.EndOfLine)
+    editor.setTextCursor(cursor)
+    assert editor.autoinsert_colons() == False
+
+@pytest.mark.xfail
+def test_auto_colon_in_two_if_statements_on_one_line():
+    editor = construct_editor("if x < 0: x = 0; if x == 0") 
+    assert editor.autoinsert_colons() == True
+    
+
+if __name__ == "__main__":
+    pytest.main()