nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 639 forks source link

Braces and allman not applied to multi-line functions #1146

Closed jlnbuiles closed 2 years ago

jlnbuiles commented 2 years ago

I noticed that the allman formatting is not being applied to multi-line functions. e.g.:

For this guy it works beautifully 🌸:

func httpURLResponse(withResponse response: URLResponse) throws -> Bool {
    ...
}

But for this guy it doesn't:

func application(_: UIApplication,
                     didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{// i wasn't taken up next to the return argumen
        ...
}

Is this intended behavior? if so maybe we should mention it in the docs? how could I accomplish consistent opening brace formatting for all methods?

What i'm setting on the config file:

--enable braces
...
--allman false

Attaching the complete config file for reference as well. It could be that I have conflicting configuration. swiftformat 2.txt


Versions for all the stuffs

jlnbuiles commented 2 years ago

also noticed it on a guard statement.

I had this:

guard let cell = tableView.dequeueReusableCell(withIdentifier: CryptoListsTableViewCell.identifier, for: indexPath) as? CryptoListsTableViewCell else {fatalError("Unable to create cell.")}

and it was formatted tot this:

guard let cell = tableView.dequeueReusableCell(withIdentifier: CryptoListsTableViewCell.identifier,
                                                                          for: indexPath) as? CryptoListsTableViewCell else
{
    fatalError("Unable to create cell.")
}
nicklockwood commented 2 years ago

@jlnbuiles this behavior is caused by the wrapMultilineStatementBraces rule. You can disable it as follows:

--disable wrapMultilineStatementBraces
jlnbuiles commented 2 years ago

So i went ahead and did this and it does implement it in some cases, but not in others.

Worked for this one: initial format

        guard let cell = tableView.dequeueReusableCell(withIdentifier: CryptoListsTableViewCell.identifier,
                                                       for: indexPath) as? CryptoListsTableViewCell else 
        {
            fatalError("Unable to create cell.")
        }

resulting format

        guard let cell = tableView.dequeueReusableCell(withIdentifier: CryptoListsTableViewCell.identifier,
                                                       for: indexPath) as? CryptoListsTableViewCell else {
            fatalError("Unable to create cell.")
        }

For these 2 it did not apply applied any formatting and left them as is:

    func application(_: UIApplication,
                     didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
    {
        // Override point for customization after application launch.
        return true
    }

and this one:

    func application(_: UIApplication,
                     configurationForConnecting connectingSceneSession: UISceneSession,
                     options _: UIScene.ConnectionOptions) -> UISceneConfiguration
    {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

¿is there a specific reason why that is?

Additionally, maybe it'd be helpful to add a small section within the rules md that lists explicitly rules NOT applied by default? it seems like most are enabled by default, but this wasn't that clear to me unless I went one by one and read their respective values.

Doing this would probably lead people to simpler configuration .swiftformat files. As they should only deal with rules they want to disable, or modify the default behavior or, right?

nicklockwood commented 2 years ago

@jlnbuiles fixed in 0.49.5

jlnbuiles commented 2 years ago

dopeness! thanks brother!