swiftlang / swift-format

Formatting technology for Swift source code
Apache License 2.0
2.47k stars 227 forks source link

Split string literals into multiple lines #724

Closed ahoppen closed 1 month ago

ahoppen commented 5 months ago

Formatting a long string literal single line even though it exceeds the line limit. We should break it up into multiple lines, probably into a multi-line string literal

"some very long string literal that just goes on and on and one without having any real content but it reaches past the line limit"

Should become

"""
some very long string literal that just goes on and on and one without having \
any real content but it reaches past the line limit
"""
ahoppen commented 5 months ago

Synced to Apple’s issue tracker as rdar://126967237

allevato commented 5 months ago

I've been fairly opinionated that for comment wrapping, I'd like to use swift-markdown to re-render comments instead of just implementing something that breaks on spaces. But for string literals, maybe just breaking on spaces is enough.

Do all of Apple's developer tools that scan source files handle multi-line strings well? IIRC, at some point in the past the localized string extractor didn't like them, which was part of our reasoning for not touching them at the time.

We should also nail down the exact desired breaking behavior. Should we only split lines around whitespaces? For example, something like this is still valid, but makes it harder to work with the URL because you can no longer just click on it in most editors:

"""
http://mycoolwebsite.com/directory/\
somepage.html
"""