Fixing line breaks

Hi, what's the best tool today to fix

  • line break issues, like else on a new line like so

      }
      else {
    
  • single empty lines between the function heads and bodies

  • Array<String>[String] (regex is ok, but maybe there's a tool?)

I tried setting respectsExistingLineBreaks to false but this breaks a lot of things.

The first one already would be quite nice to get fixed.

1 Like

cc @allevato who knows more about swift-format

2 Likes

I moved this post to "Development > Swift Format" since it's referring to one of the configuration settings specific to it.

If you want to remove a line break that the user has placed there, set the configuration setting lineBreakBeforeControlFlowKeywords to false (which is the default). That will force a non-breaking space between the } and the else. If you want to force a line break between the } and else, you can set it to true.

That setting will also apply to other similar constructs, like do {} catch, repeat {} while, etc.

I don't think we do anything about these currently. It wouldn't be a bad idea to have a setting to reject "exterior" blank lines and only allow "interior" ones. Folks might want different settings for type declarations vs. functions though.

swift-format's UseShorthandTypeNames rule should apply this transformation for you everywhere that it's allowed. (So, for example, we can't transform Array<String>.Index to [String].Index.)

I just fixed this yesterday (swift-format#65) and cherry-picked it into the swift-5.1-branch a few minutes ago along with some other changes, so please try it again with an updated checkout of the sources and see if it resolves your issues!

It's unclear whether you're looking for a tool within swift or a general tool. For the latter I use SwiftLint where I can.

Settings customization is supported and there's an autocorrect command that will format your code with your standards.

Terms of Service

Privacy Policy

Cookie Policy