swiftlang / swift-format

Formatting technology for Swift source code
Apache License 2.0
2.55k stars 231 forks source link

if expression is moved to the next line when assigned to the property #835

Open mcichecki opened 1 month ago

mcichecki commented 1 month ago

I wonder if that's my config file or it's a default behaviour that can't be changed but it seems quite weird to me. When using if conditional blocks as expressions (introduced in Swift 5.9) and assigning them to the properties I'd like to keep them in the same line as the property. When running swift-format the whole expression is moved to the next line.

original:

let queryParams = if queryParams.isEmpty {
    ""
} else {
    "?" + queryParams.encodedQueryParams
}

after formatting:

let queryParams =
    if queryParams.isEmpty {
        ""
    } else {
        "?" + queryParams.encodedQueryParams
    }
Config: ```json { "fileScopedDeclarationPrivacy" : { "accessLevel" : "private" }, "indentConditionalCompilationBlocks" : false, "indentSwitchCaseLabels" : false, "indentation" : { "spaces" : 4 }, "lineBreakAroundMultilineExpressionChainComponents" : false, "lineBreakBeforeControlFlowKeywords" : false, "lineBreakBeforeEachArgument" : false, "lineBreakBeforeEachGenericRequirement" : true, "lineBreakBetweenDeclarationAttributes": true, "lineLength" : 90, "maximumBlankLines" : 1, "multiElementCollectionTrailingCommas" : false, "prioritizeKeepingFunctionOutputTogether" : true, "respectsExistingLineBreaks" : true, "rules" : { "AllPublicDeclarationsHaveDocumentation" : false, "AlwaysUseLiteralForEmptyCollectionInit" : true, "AlwaysUseLowerCamelCase" : true, "AmbiguousTrailingClosureOverload" : true, "BeginDocumentationCommentWithOneLineSummary" : false, "DoNotUseSemicolons" : true, "DontRepeatTypeInStaticProperties" : true, "FileScopedDeclarationPrivacy" : true, "FullyIndirectEnum" : true, "GroupNumericLiterals" : true, "IdentifiersMustBeASCII" : true, "NeverForceUnwrap" : false, "NeverUseForceTry" : false, "NeverUseImplicitlyUnwrappedOptionals" : false, "NoAccessLevelOnExtensionDeclaration" : true, "NoAssignmentInExpressions" : true, "NoBlockComments" : true, "NoCasesWithOnlyFallthrough" : true, "NoEmptyTrailingClosureParentheses" : true, "NoLabelsInCasePatterns" : true, "NoLeadingUnderscores" : true, "NoParensAroundConditions" : true, "NoPlaygroundLiterals" : false, "NoVoidReturnOnFunctionSignature" : true, "OmitExplicitReturns" : true, "OneCasePerLine" : true, "OneVariableDeclarationPerLine" : true, "OnlyOneTrailingClosureArgument" : true, "OrderedImports" : false, "ReplaceForEachWithForLoop" : true, "ReturnVoidInsteadOfEmptyTuple" : true, "TypeNamesShouldBeCapitalized" : true, "UseEarlyExits" : false, "UseExplicitNilCheckInConditions" : true, "UseLetInEveryBoundCaseVariable" : true, "UseShorthandTypeNames" : true, "UseSingleLinePropertyGetter" : true, "UseSynthesizedInitializer" : true, "UseTripleSlashForDocumentationComments" : true, "UseWhereClausesInForLoops" : false, "ValidateDocumentationComments" : false }, "spacesAroundRangeFormationOperators" : false, "tabWidth" : 4, "version" : 1 } ```

How can I prevent from adding line break in this case?

ahoppen commented 1 month ago

Synced to Apple’s issue tracker as rdar://137341062

allevato commented 1 month ago

This is by design; in most cases where a complex expression follows an assignment, we move the expression to the next line and indent it. There are a limited number of cases where we don't do this, like function calls. However, my gut feeling is to not add more cases like that and we're better off using something like the rectangle rule to guide our line breaking and indentation philosophy here (which is what we do for cases not covered by those exceptions).

mcichecki commented 1 month ago

@allevato so there is no way to prevent that? I wouldn't say that the example that I provided is "a complex expression".

allevato commented 1 month ago

There is no way to prevent that behavior today and it's not planned.