You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Precondition: Have multiElementCollectionTrailingCommas set to true in the swift format configuration.
Trailing commas in multi line arrays are great as they allow the git history for a line to be easily maintained when a new element is added later. For example:
letx=[1,2,3]
gets formatted to
letx=[1,2,3,]
This way, when someone comes along and adds "4," to this list later they will not have needed to add a "," after 3, leaving the git blame for that line alone. Unfortunately the rule for multiElementCollectionTrailingCommas does not keep or insert commas in single element lists. So in the above example if someone wrote
letx=[1,]
it will be formatted to:
letx=[1]
This means that when adding a second element to this list you must add a comma after the first element. It would be great if there was a way to configure swift-format to keep these trailing commas for single element multi line lists. Whether this is tied to multiElementCollectionTrailingCommas or some other configuration is certainly up to debate, I don't know the general guidance for whether or not configurations like this should change behavior.
There's this excerpt from PrettyPrint.swift describing this as a limitation.
// We need to specifically disable trailing commas on elements of single item collections.// The syntax library can't distinguish a collection's initializer (where the elements are// types) from a literal (where the elements are the contents of a collection instance).// We never want to add a trailing comma in an initializer so we disable trailing commas on// single element collections.letshouldHaveTrailingComma=
startLineNumber != openCloseBreakCompensatingLineNumber && !isSingleElement
&& configuration.multiElementCollectionTrailingCommas
if shouldHaveTrailingComma && !hasTrailingComma {diagnose(.addTrailingComma, category:.trailingComma)}else if !shouldHaveTrailingComma && hasTrailingComma {diagnose(.removeTrailingComma, category:.trailingComma)}
The text was updated successfully, but these errors were encountered:
Precondition: Have
multiElementCollectionTrailingCommas
set totrue
in the swift format configuration.Trailing commas in multi line arrays are great as they allow the git history for a line to be easily maintained when a new element is added later. For example:
gets formatted to
This way, when someone comes along and adds "4," to this list later they will not have needed to add a "," after 3, leaving the
git blame
for that line alone. Unfortunately the rule formultiElementCollectionTrailingCommas
does not keep or insert commas in single element lists. So in the above example if someone wroteit will be formatted to:
This means that when adding a second element to this list you must add a comma after the first element. It would be great if there was a way to configure
swift-format
to keep these trailing commas for single element multi line lists. Whether this is tied tomultiElementCollectionTrailingCommas
or some other configuration is certainly up to debate, I don't know the general guidance for whether or not configurations like this should change behavior.There's this excerpt from
PrettyPrint.swift
describing this as a limitation.The text was updated successfully, but these errors were encountered: