nicklockwood / SwiftFormat

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

Question about Wrap #1609

Closed mnazirov closed 8 months ago

mnazirov commented 8 months ago

Hi! I noticed wrap lines I didn't expect. Can you please tell me how to avoid it?

I have these rules.

--maxwidth 160
--nowrapoperators +,.,=,*,-
--wraparguments before-first
--wrapparameters before-first
--wrapcollections before-first
--wrapternary before-operators

--disable wrapMultilineStatementBraces 
--nospaceoperators "...,..<"
--disable initCoderUnavailable
--funcattributes prev-line
--typeattributes prev-line
--varattributes prev-line
--enable initCoderUnavailable

 

1) There has been wrap of one parameter. Although it's not a long line. It was expected that there would be no wrap to the new line.

Before the library was run

description.update(model:
    CommentBlockView.Model(
        bodyText: model.description,
        bodyLinkAttachment: .init(linkText: model.link),
        urlClicked: model.action
    )
)

After the library was run

description.update(
    model:
        CommentBlockView.Model(
            bodyText: model.description,
            bodyLinkAttachment: .init(linkText: model.link),
            urlClicked: model.action
        )
)

  2) Wrap of only one protocol

Before the library was run

protocol CreateViewProtocol: ViewEditingProtocol, ValidatableProtocol, LoaderPresentable, PaymentsVisitable, NewUserRegistration {
    ...
}

After the library was run

protocol CreateViewProtocol: ViewEditingProtocol, ValidatableProtocol, LoaderPresentable, PaymentsVisitable, 
NewUserRegistration {
    ...
}

  3) Non-obvious wrap line of only one condition

Before the library was run

let operation = (operation.isExpired && (operation.isExpired || operation.isExpired || operation.isExpired) && operation.isExpired)

After the library was run.

let operation = (
    operation.isExpired && (operation.isExpired || operation.isExpired || operation.isExpired) &&
        operation.isExpired
)

  4) It's not clear why this piece is being moved. Before the library was run

func createAccountDescriptionOfferWorker(accountData: AccountsDescriptionProtocols.AccountDescriptionData?) -> AccountsDescriptionProtocols.AccountDescriptionOfferAcceptProtocol {
    ...
}

After the library was run.

func createAccountDescriptionOfferWorker(accountData: AccountsDescriptionProtocols.AccountDescriptionData?) 
    -> AccountsDescriptionProtocols.AccountDescriptionOfferAcceptProtocol {
    ...
}
nicklockwood commented 8 months ago

@mnazirov

1) is caused by the wrapArguments rule in combination with the --wrapparameters before-first option. Use --disable wrapArguments to prevent it, or set --wrapparameters preserve

2) is because there is no dedicated rule for wrapping protocols at the moment, so the line is just wrapped at the first element that exceeds the --maxwidth 160 characters that you've set

3, 4) same as 2. In all three cases the wrap rule is being invoked, and wrapping at the last point it can to stay within the line width of 160 characters.

mnazirov commented 8 months ago

@nicklockwood Thank you so much for point-by-point. Can you please tell me why there is formatting going on here? How can I remove it? Somehow disabling the rule affects it - wrapMultilineStatementBraces

Before the library was run

public protocol NotificationsFactoryProtocol:
    AnalyticsProtocol,
    DemoHelperProtocol,
    CurrentProviderProtocol
{
    ...
}

After the library was run.

public protocol NotificationsFactoryProtocol:
    AnalyticsProtocol,
    DemoHelperProtocol,
    CurrentProviderProtocol {
    ...
}
nicklockwood commented 8 months ago

@mnazirov this is probably because of the --disable wrapMultilineStatementBraces in your config. That counteracts the effect of the braces rule, so you either need to disable both or neither.

mnazirov commented 8 months ago

@nicklockwood What other rules should I disable?)

nicklockwood commented 8 months ago

@mnazirov I can't really answer that for you. Maybe --disable all and then just enable the ones you want?