Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mcichecki opened this issue Oct 5, 2024 · 4 comments
Open

Comments

@mcichecki
Copy link

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:
{
  "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?

@mcichecki mcichecki changed the title swift-format breaks line when assigning if as expression to the variable if expression is moved to the next line when assigned to the property Oct 5, 2024
@ahoppen
Copy link
Member

ahoppen commented Oct 5, 2024

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

@allevato
Copy link
Member

allevato commented Oct 7, 2024

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
Copy link
Author

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

@allevato
Copy link
Member

allevato commented Oct 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants