Skip to content

Resolve SwiftFormat issues and reformat #562

@stackotter

Description

@stackotter

I've found a few SwiftFormat quirks that I'm not a fan of. We should probably fork SwiftFormat, fix these, PR them upstream, and use our fork until the upstream fixes get merged.

I'll expand this list if I discover any more.

Bugs

Comments get outdented in compiler conditional blocks if the block doesn't have code

#if canImport(AppKitBackend)
    image.isEditable = true
#elseif canImport(UIKitBackend)
    image.layer.borderWidth = 1
    image.layer.borderColor = .init(red: 0, green: 1, blue: 0, alpha: 1)
#elseif canImport(WinUIBackend)
// Couldn't find anything visually interesting
// to do to the WinUI.Image, but the point is
// that you could do something if you wanted to.
#elseif canImport(GtkBackend)
    image.css.set(property: .border(color: .init(0, 1, 0), width: 2))
#elseif canImport(Gtk3Backend)
    image.css.set(property: .border(color: .init(0, 1, 0), width: 2))
#endif

AdvancedCustomizationApp.swift

Hanging indents get removed when wrapping guard condition expressions

This issue has been reported already; nicklockwood/SwiftFormat#1131

guard
    let pkgConfigPath = ProcessInfo.processInfo
    .environment["PKG_CONFIG_PATH"]
else {
    continue
}

Adapted from Package.swift

Fully wrapped function arguments don't get a hanging indent

To my eyes, it's confusing when the value is placed at the same indentation level as the parameter label.

callMyFunction(
    parameterWithVeryVeryLongLabelAndALongValue:
    theLongValueForTheParameterWithTheLongLabel
)

Switch inside ifdef doesn't indent case bodies

This may be specific to --indent-case true configurations?

let defaultColor: UIColor?
#if targetEnvironment(macCatalyst)
    defaultColor = nil
#else
    switch environment.colorScheme {
        case .light:
        defaultColor = UIColor(...)
        case .dark:
        #if os(tvOS)
            defaultColor = UIColor(...)
        #else
            defaultColor = UIColor(...)
        #endif
    }
#endif

Adapted from UIKitBackend+Sheet.swift

indentCase option doesn't handle case pattern wrapping properly

nicklockwood/SwiftFormat#2442

switch blah {
    case .arc(
    let center,
    let radius,
):
        path.appendArc(...)
}

Clashes with my preferences

These issues aren't necessarily SwiftFormat bugs, but they do clash with my personal preferences, and should at least be configurable.

If statement condition wrapping

What SwiftFormat does

if let value = getValue(),
   let otherValue = getOtherValue(value),
   let finalValue = getFinalValue(otherValue)
{
    // ...
} else if let value = getValue(),
          let otherValue = getOtherValue(value),
          let finalValue = getFinalValue(otherValue)
{
    // ...
}

What I prefer

I really don't like indentation based off visual alignment with keywords or opening parenthesis location, etc. Such schemes get progressively worse when used with longer keywords or identifiers, whereas 'simple' indentation rules based on multiples of a single indent scale much better, and in my opinion look better. My preferred way also doesn't require your editor to have special language-specific indentation rules.

if let value = getValue(),
    let otherValue = getOtherValue(value),
    let finalValue = getFinalValue(otherValue)
{
    // ...
} else if let value = getValue(),
    let otherValue = getOtherValue(value),
    let finalValue = getFinalValue(otherValue)
{
    // ...
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions