Inserting a new line comment into an existing block of code

Greetings,

Question about the line comment behavior in the PyCharm editor.

Say I have cheeky code as follows:

line 1| released = ['1.0', '1.0.1']
line 2| if version in released:
line 3|    print 'released'

line 4| else:
line 5|    print 'not released'

And now I want to add a new single line comment of 'check version' as a new line between the lines 1 and 2.

If place the cursor before the i in if on line 2 and type # check version and then hit the <Enter> key, what was line 2 rightfully becomes line 3 *but* is automatically assumed to be a comment.  Thus I get this:

line 1| released = ['1.0', '1.0.1']
line 2| # check version

line 3| # if version in released:
line 4|    print 'released'

line 5| else:
line 6|    print 'not released'

Ideally, I want to end up with this:

line 1| released = ['1.0', '1.0.1']
line 2| # check version

line 3| if version in released:
line 4|    print 'released'

line 5| else:
line 6|    print 'not released'

Is there Code function/keyboard-shortcut that can move the existing code lines down 1 line and insert a single line comment with the # comment line auto-adjusted to the same position as the current line of code and the cursor placed 1 space away from the # comment character?  Thus doing this:

line 1| released = ['1.0', '1.0.1']
line 2| #

line 3| if version in released:
line 4|    print 'released'

line 5| else:
line 6|    print 'not released'

Or is there a preference that can disable the assumption that a return in the middle of the line comment should auto-comment everything following the cursor when the <Enter> key is pressed?

As far as I can tell, there appear to be two work-arounds:

Technique A:

  1. Place the cursor
  2. Hit <Enter>
  3. Hit the up-arrow
  4. Type the # and then continue on with the rest of the comment

Technique B:

  1. Place the cursor
  2. Type # and then continue on with the rest of the comment
  3. Hit <Enter>
  4. Backspace twice

Both work-arounds do the job but neither "feels" natural with my typing tendancies or the fact that I tend to enter comments after fact.  ;-)

Cheers,

Rob

1 comment

Didn't see any replies yet, so I authored this enhancement request in the YouTrack system to get the new feature added:

http://youtrack.jetbrains.net/issue/IDEA-62630

If you would like to see that feature get added too, please vote for it!

Or if you have a better work-around then what I noted, please share.

Cheers,

Rob

0

Please sign in to leave a comment.