yapf icon indicating copy to clipboard operation
yapf copied to clipboard

A knob to turn on string splitting

Open spott opened this issue 7 years ago • 8 comments

It would be nice to have a knob for turning this:

mystr = "A really long string that is going to go over the character limit, but as I'm writing this I forget to create a line break"

into this:

mystr = "A really long string that is going to go over the" \
              " character limit, but as I'm writing this I forget " \
              "to create a line break"

Especially when the string is surrounded by brackets. These two are semantically equivalent (with the backslash), and the second is much nicer.

spott avatar May 10 '17 17:05 spott

This would be nice, though I believe parentheses are preferred over backslashes:

mystr = ("A really long string that is going to go over the"
         " character limit, but as I'm writing this I forget "
         "to create a line break")

dsem avatar Jun 16 '17 14:06 dsem

Either one works. Maybe even have it be an option?

I'm not sure how to go about tackling this, is there somewhere in the code that I can start looking at to insert something like this?

spott avatar Jun 17 '17 22:06 spott

It's a design decision that yapf won't add or remove characters from the token stream[*]. I made this decision in order to ensure that no semantic changes would be introduced.

If someone would like to add this though, it could be done as a lib2to3 "fixer". Those fixers make modifying the code rather easy. Much easier than can be done in the formatter. If someone did create a fixer, it could be added in a "contrib" sub-directory that could be called via a command line option.

[*] There is one exception to this rule, which I grudgingly added. It adds fake parentheses to help in formatting dictionary structures in "chromium" style.

bwendling avatar Jul 13 '17 06:07 bwendling

I really really would like this feature. I've had code like a)

"really long string and such ..."

b)

("really long string " +
"and such ...")

c)

("really long string "
"and such ...")

And in all cases I'd like the formatter to join all strings both with and without a plus operator, and do optimal splitting.

joaoe avatar Jun 14 '18 22:06 joaoe

I think this is a major hole in this formatter. One of the primary reasons I started using this was so that it could handle this kind of tedious manual labor of string formatting. It should really be an option - you can accompany it with whatever warnings you feel necessary.

Apollys avatar Dec 04 '19 23:12 Apollys

black doesn't do this kind of string reformatting either. black does add parens to import statements (yapf doesn't based on its design philosophy), although I don't like the format that it uses.

I agree that it would be nice to have a tool that reformats strings and also adds parentheses to import statements, but perhaps that would be best handled by something based on 2to3?

kamahen avatar Dec 05 '19 00:12 kamahen

yes, please add this feature as an option, it could be very useful

IPv777 avatar Feb 24 '20 11:02 IPv777

Is there any python formatter available that supports this?

[EDIT]: Just found out that black supports this since 2023: https://github.com/psf/black/issues/1802

dreamflasher avatar Apr 17 '24 14:04 dreamflasher