Swiftformat: Exception for braces on same line

Created on 3 Jun 2020  路  11Comments  路  Source: nicklockwood/SwiftFormat

Google's style guide states an exception for the K&R-style braces when the opening brace follows a line-wrapped expression. Here's the recommendation:

When an open curly brace ({) follows a line-wrapped declaration or expression, it is on the same line as the final continuation line unless that line is indented at +2 from the original line. In that case, the brace is placed on its own line, to avoid the continuation lines from blending visually with the body of the subsequent block.

image

In the following code sample, I'd hope that running the braces rule wouldn't move the opening brace of the init method:

class TestClass {
    init(
        foo: Foo
    ) where
        Foo: Fooable,
        Foo.Bar: Barable,
        Foo.Something == Something,
        Foo.SomethingElse == SomethingElse
    {
        self.foo = foo
    }
}

Presently, running the following command does shift the opening brace to the right after == SomethingElse, making it less readable.

$ Pods/SwiftFormat/CommandLineTool/swiftformat test.swift --rules braces

enhancement fixed in develop

Most helpful comment

@meherkasam fixed in 0.45.1.

All 11 comments

Link to the Google style guide: https://google.github.io/swift/#line-wrapping

@meherkasam this is almost implemented in 0.45.0. It works for functions and other control structures, but I accidentally forgot to handle init and subscript methods. Those will be fixed in the next release.

Thank you for the update. I'm excited for it!

@meherkasam fixed in 0.45.1.

@nicklockwood I tried the braces rule with 0.45.1 and the following was the output:

class TestClass {
    init(
        foo: Foo
    ) where
        Foo: Fooable,
        Foo.Bar: Barable,
        Foo.Something == Something,
        Foo.SomethingElse == SomethingElse {
        self.foo = foo
    }
}

Is there something else I need to do apart from running the braces rule as follows?

$ Pods/SwiftFormat/CommandLineTool/swiftformat test.swift --rules braces

@meherkasam you need to enable the wrapMultilineStatementBraces rule as well.

Thank you, that worked!

@nicklockwood This rule has worked really well for most cases in a sizeable codebase. I did notice one case where it didn't work as expected:

class MyClass: NSObject,
    FooProtocol,
    BarProtocol
{
    func someFunction() {}
}

I ran the following command:
$ Pods/SwiftFormat/CommandLineTool/swiftformat test.swift --rules braces,wrapMultilineStatementBraces

I expected it to remain the same but got the following instead:

class MyClass: NSObject,
    FooProtocol,
    BarProtocol {
    func someFunction() {}
}

@meherkasam thanks for reporting, I'll take a look.

@meherkasam fixed in 0.45.2.

It worked great! Thank you once again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

meherkasam picture meherkasam  路  3Comments

DavinAhn picture DavinAhn  路  4Comments

MaxDesiatov picture MaxDesiatov  路  3Comments

mdiep picture mdiep  路  3Comments

MaxDesiatov picture MaxDesiatov  路  4Comments