how to disable python line wrap

Hello,

I love the reformat code feature, but I'm unsure how to customize line wrapping for Python. It works as desired for HTML (for example), where there is a "Wrap attributes:" dropdown in Settings > Code Style > HTML > Other. But for Python, I cannot see how to do the same thing. I have:

Settings > Code Style > General > "Wrap when typing reaches right margin" unchecked
Settings > Code Style > Python > Wrapping and Braces > Keep when reformatting > Line breaks checked
Settings > Code Style > Python > Wrapping and Braces > "Ensure right margin is not exceeded" unchecked
Settings > Code Style > Python > Wrapping and Braces > "Force new line after colon" unchecked for both subitems
Settings > Inspections > Python > PEP 8 coding style violation unchecked

How can I disable line wrapping/breaking on code reformat? (I've found a bunch of forum questions asking the opposite question – how to enable – but not this one.)

Thanks,
Kim
8 comments
For all languages, the formatter wraps lines at the right margin at least in some situations. If you don't want the wrapping to happen, you need to increase the right margin so that it is not exceeded by your code.
0
Thank you Dmitry for your reply. In the Help, under Reference > Settings Dialog > Project Settings > Code Style > Code Style. Python, there is a section entitled "Wrapping options" referring to a "Wrapping style" dropdown, which would do exactly what I want. The same option exists for JavaScript and works great there – but for Python I do not see this dropdown associated with any of the items in the lefthand pane. I am running 2.7.1.
0
I found in 2.7.1 that I have to do exactly this to prevent wrapping when I paste blocks of code now.

I like having the margin set to 80 as a mental guideline rather than having pycharm wrap for me.  In prior versions, Pycharm did not wrap the line on reformatting.

It looks like reformat wrapping is being enforced regardless of settings.
0
Confirmed happening in fresh install of 2.7.3

I'm taking a leap here and going to assume the following ticket applies to this issue as well.

http://youtrack.jetbrains.com/issue/PY-4664
0
Hopefully Kim will see this...

I stumbled on an interesting fact.  Having 'Smart indent pasted lines' disabled when pasting code which exceeds the margin would not result in a forced reformatting of that code. In other words, turning this option off fixes the problem of pasted code reformatting beyond the right margin.

This setting already has an issue with it where pasting causes extra whitespace (tabs) to appear after the pasted code.  I'm beginning to wonder if these two bugs aren't somehow related.

If yours is enabled, perhaps you would be willing to test turning it off to confirm my finding?
1
Marcel – very much appreciate your attempts to solve this. I spent some more time fiddling with settings, attempting to understand what is controlling the behavior I'm seeing, but unfortunately I'm still not understanding. I tried turning off 'smart indent pasted lines' and can confirm that a pasted long line will not break with it off, whereas it will with it on – but of course then I lose the smart indent as well!

But more importantly (from my point of view), this setting does not appear to affect the behavior of a Reformat – on or off, my lines still break when I do Ctrl+Alt+L, whereas the settings for javascript and html prevent this. I still think this is inconsistent, but maybe there's some other setting combination still interfering.
0
I thought I'd share my findings since last time.

I've updated to 3.1 (#PY-133.804) and found the issue I mentioned with Smart Indent is fixed, however there still appears to be an issue with pasting longer lines beyond the margin. I had secretly hoped that fixing one would fix the other but that does not appear to be the case.  The thing I found which I think is worth mentioning is only certain items appear to want to wrap.


This long line will not wrap no matter where I paste it.

################################################################################
abcde = 1
#original line
x = abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde

# pasted
x = abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde

# pasted in function - several indents
def foo():
    x = abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde
    if x:
        x = abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde
        if x:
            x = abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde + abcde

################################################################################
prettyPrint();

However, the following line with a few str() calls in it and suddenly things start wrapping.

################################################################################
# original line
z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde)

#pasted
z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(
    abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde)

#pasted in function - multiple indents
def foo():
    z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(
        abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde)
    if z:
        z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(
            abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde)
        if z:
            z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(
                abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(
                abcde)
################################################################################
prettyPrint();

The weird thing is where the wrap occurs.  Notice the wrap inside the function.  It did not wrap at 80 characters (which is where my margin is set to).  It wrapped at 126 characters.  I haven't figured out the pattern yet, but it's particularly odd.  


If I change the margin to 90 chars the wrap position changes as well.

##########################################################################################
#original
z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde)

# pasted
z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde)

# pasted in function - multiple indents
def foo():
    z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(
        abcde) + abcde + str(abcde) + abcde + str(abcde)
    if z:
        z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(
            abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde)
        if z:
            z = abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(
                abcde) + abcde + str(abcde) + abcde + str(abcde) + abcde + str(abcde)
##########################################################################################
prettyPrint();


Some settings to note that may be factors which I have not determined yet.
  • I'm using tabs for indents
  • My tabs and indents are set to 4 (chars)
  • My normal margin setting is 80 chars)
  • Smart Indent is enabled
  • Reformat on paste is set to None
  • Smart indent pasted lines is enabled.

Turning off "smart indent" turns off the wrapping behavior altogether (as was found in 2.7.3), but as Kim stated above this is not ideal since pasting sections of code without having to reformat the indentation is really convenient.

I will post the link to this post in http://youtrack.jetbrains.com/issue/PY-4664.
0
Oh!  I see it now...

If any part of the the call for str() – which occurs between the parens – lands on the margin it will cause a wrap.  

I then played with a few code style settings and found when I disable 'Keep when formatting'> 'Line breaks'.  
The wrapping doesn't occur anymore. EDIT: spoke too soon. It still occurs but it just moves where the wrap happens.  I'm so confused by where the wrap wants to happen.

code_style_settings.png





code_style_settings.png
0

Please sign in to leave a comment.