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!

1 Like

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.

Also, maybe someone knows how to disable inserting a line-break before $0?

I'd like to keep this API (GitHub - CaptureContext/swift-declarative-configuration: Declarative configuration for your objects)

UIView { $0
  .backgroundColor(.red)
  .layer.scope { $0
    .masksToBounds(true)
    .cornerRadius(12)
  }
}

but swift-format formats the code above to

UIView { 
  $0
    .backgroundColor(.red)
    .layer.scope {
      $0
        .masksToBounds(true)
        .cornerRadius(12)
  }
}
1 Like

@stephencelis @mbrandonw, afaik you use swift-format a lot, so maybe you know how can I achieve desired behavior? :slightly_smiling_face:

(Forum note: It would be better to start a new thread for a new question than to reply to an 18-month-old thread which notifies previous folks who posted on the thread.)

It's not possible to remove that line break today; letting the closure argument/receiver hang on the same line at the beginning of a member access expression like that looks like a very library-specific convention.

You can of course clone the repo and try to add that customization yourself for your own code; I think it's somewhat unlikely that we'd add support for that style to the main code base today though, even if it was behind a configuration option. Every layout choice the formatter makes has the potential to impact other choices in the same vicinity, and more configuration options increase the complexity, maintenance difficulty, and testing surface of the project so we're trying to keep things fairly focused for the time being.

(It would be nice to allow the layout to be expressed in a more pluggable way than the current design and I have some ideas around that, but it would be a fairly large overhaul of the layout engine.)

If we really cared about that we could set the Discourse option that closes threads after a few months of inactivity. But whenever it comes up nobody actually wants it enabled.

Instead of making that simple change to the forum (which I’ve seen be very successful on other Discourse sites) people here would seem to prefer to complain every time someone is unfamiliar with forum etiquette rather than having the forum enforce it automatically. :man_shrugging:t2:

2 Likes

--allman false will solve this