Swiftformat: else placement for guard

Created on 23 Jan 2020  路  4Comments  路  Source: nicklockwood/SwiftFormat

When the guard condition is multiple lines, I like having the else in a new line, like this:

guard let keyboardAnimationCurve = (dictionary[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber).flatMap({ UIView.AnimationCurve(rawValue: $0.intValue) }),
    let keyboardAnimationDuration = dictionary[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
    let keyboardFrameBegin = dictionary[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue,
    let keyboardFrameEnd = dictionary[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue
else {
    throw NotificationDecodingError(type: type(of: self), source: dictionary)
}

I think it would be cool to have an option to automatically place else in a new line when the condition is multiple lines, so that the body is separated from the condition instead of continuously having the same indentation.

enhancement

Most helpful comment

Maybe even like this? Where the first conditional statement is on a newline.

guard 
    let keyboardAnimationCurve = (dictionary[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber).flatMap({ UIView.AnimationCurve(rawValue: $0.intValue) }),
    let keyboardAnimationDuration = dictionary[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
    let keyboardFrameBegin = dictionary[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue,
    let keyboardFrameEnd = dictionary[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue
else {
    throw NotificationDecodingError(type: type(of: self), source: dictionary)
}

All 4 comments

Maybe even like this? Where the first conditional statement is on a newline.

guard 
    let keyboardAnimationCurve = (dictionary[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber).flatMap({ UIView.AnimationCurve(rawValue: $0.intValue) }),
    let keyboardAnimationDuration = dictionary[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
    let keyboardFrameBegin = dictionary[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue,
    let keyboardFrameEnd = dictionary[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue
else {
    throw NotificationDecodingError(type: type(of: self), source: dictionary)
}

@nicklockwood I like @gregcotten's suggestion. I can't promise anything but maybe I can find some time to add a rule for this - if it is something you would find useful as well. Let us know what you think!

@daniel-mohemian I'd be happy to add this. Needs some discussion around the best way to configure it though.

There's already the (slightly confusingly-named) elseOnSameLine rule which moves else, catch or while statements to the same line as the previous closing }, so I'm wondering if this should be an extension of that rule, or a new one, etc.

@Cyberbeni this is implemented in 0.45.0.

@gregcotten your suggested extension is not yet implemented, so I'll leave this open for now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaxDesiatov picture MaxDesiatov  路  3Comments

meherkasam picture meherkasam  路  3Comments

zizi4n5 picture zizi4n5  路  3Comments

igorkulman picture igorkulman  路  3Comments

fellipecaetano picture fellipecaetano  路  4Comments