Swift-format 0.50600.0 adds newline after if/function opening brace sometimes?!

Just started playing with swift-format - built a new version 0.50600.0 which passes the full test suite on my M1 MacBook Pro (with a default 5.6 toolchain) and in just two places, it will consistently move the functions opening brace to a new line (one place) and in an if statement (the other place) - it doesn't do it anywhere else in the code base and I must say I'm confused - is it a bug (can't file one as bugs.swift is gone and issues is not opened for swift-format yet) or a 'feature'. Using default out-of-the-box settings for formatting.

diff --git a/Sources/Generators/SchemaGenerators/Flatbuffers/FlatbuffersModelGenerator.swift b/Sources/Generators/SchemaGenerators/Flatbuffers/FlatbuffersModelGenerator.swift
index 29f1bb5..497a060 100644
--- a/Sources/Generators/SchemaGenerators/Flatbuffers/FlatbuffersModelGenerator.swift
+++ b/Sources/Generators/SchemaGenerators/Flatbuffers/FlatbuffersModelGenerator.swift
@@ -31,7 +31,8 @@ extension FlatbuffersGenerator {
 
       // don't use optional null assignments for structs
       if submirror.displayStyle == .struct
-        || lowerCaseTypeDescriptionString == typeDescriptionString {
+        || lowerCaseTypeDescriptionString == typeDescriptionString
+      {
         optionalString = ""  // structs/tables are implicitly optional in flatbuffers
       }
 
diff --git a/Sources/Generators/SchemaGenerators/Flatbuffers/FlatbuffersSwiftGenerator.swift b/Sources/Generators/SchemaGenerators/Flatbuffers/FlatbuffersSwiftGenerator.swift
index 9d2bbcf..0ec125e 100644
--- a/Sources/Generators/SchemaGenerators/Flatbuffers/FlatbuffersSwiftGenerator.swift
+++ b/Sources/Generators/SchemaGenerators/Flatbuffers/FlatbuffersSwiftGenerator.swift
@@ -64,7 +64,8 @@ extension FlatbuffersGenerator {
     return result
   }
 
-  fileprivate func generateSwiftWrapper<T>(entityType: DataModelEntityType, modelType: T) -> String {
+  fileprivate func generateSwiftWrapper<T>(entityType: DataModelEntityType, modelType: T) -> String
+  {
     var result = ""
 
     let mirror = Mirror(reflecting: modelType)

Any ideas?

I tried SwiftFormat instead as a reference, it would but the brace correctly on the func, but same issue with the if...

The if condition is refusing to share its line with the brace because the condition has multiple lines. Not sure if it is intended or not, but that is the pattern for why it happens sometimes and not others.

In the case of the function, it is simply that the line is too long and that is where it happened to chose to break it. There is a configuration option prioritizeKeepingFunctionOutputTogether which ought to make it do the following instead (or something like it). Unfortunately, a bug currently makes it inoperable (although I have not tried with 5.6 yet). The bug report is sitting at apple/swift#55495 at the moment, but might be transferred to the swift-format repository at any moment.

fileprivate func generateSwiftWrapper<T>(
  entityType: DataModelEntityType,
  modelType: T
) -> String {

Note that if you arrange it that way manually it will leave it as‐is regardless of the configuration (since its only concern there is shortening the long line).

1 Like

Many thanks, the condition was moved by swift-format and then the open brace accordingly, SwiftFormat will not move it and then one can keep the brace correctly on the line so there's one functional difference.

SwiftFormat allowed the func to be kept by default (assuming different max line length by default is the reason).

I guess it is late, but it would have been awesome with one standard formatting that was enforced by the language tool chain (we'll do that locally with swift-format or SwiftFormat + SwiftLint and relevant hooks/CI checks) - would be nice with just one style throughout all code bases. It'd be nice if it was a first-party solution included with the toolchain (the current state with multiple tools is unfortunate).