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
Copy file name to clipboardExpand all lines: README.md
+165-1Lines changed: 165 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -655,26 +655,190 @@ _You can enable the following settings in Xcode by running [this script](resourc
655
655
656
656
</details>
657
657
658
-
*<a id='attributes-on-prev-line'></a>(<a href='#attributes-on-prev-line'>link</a>) **Place function/type attributes on the line above the declaration**. [](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#wrapAttributes)
658
+
*<a id='attributes-on-prev-line'></a>(<a href='#attributes-on-prev-line'>link</a>) **Place attributesfor functions, types, and computed properties on the line above the declaration**. [](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#wrapAttributes)
659
659
660
660
<details>
661
661
662
662
```swift
663
663
// WRONG
664
664
@objcclassSpaceship {
665
665
666
+
@ViewBuildervar controlPanel: some View {
667
+
// ...
668
+
}
669
+
666
670
@discardableResultfuncfly() ->Bool {
671
+
// ...
667
672
}
673
+
668
674
}
669
675
670
676
// RIGHT
671
677
@objc
672
678
classSpaceship {
673
679
680
+
@ViewBuilder
681
+
var controlPanel: some View {
682
+
// ...
683
+
}
684
+
674
685
@discardableResult
675
686
funcfly() ->Bool {
687
+
// ...
676
688
}
689
+
690
+
}
691
+
```
692
+
693
+
</details>
694
+
695
+
*<a id='simple-stored-property-attributes-on-same-line'></a>(<a href='#simple-stored-property-attributes-on-same-line'>link</a>) **Place simple attributes for stored properties on the same line as the rest of the declaration**. Complex attributes with named arguments, or more than one unnamed argument, should be placed on the previous line. [](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#wrapAttributes)
696
+
697
+
<details>
698
+
699
+
```swift
700
+
// WRONG. These simple property wrappers should be written on the same line as the declaration.
Unlike other types of declarations, which have braces and span multiple lines, stored property declarations are often only a single line of code. Stored properties are often written sequentially without any blank lines between them. This makes the code compact without hurting readability, and allows for related properties to be grouped together in blocks:
If stored property attributes were written on the previous line (like other types of attributes), then the properties start to visually bleed together unless you add blank lines between them:
792
+
793
+
```swift
794
+
structSpaceshipDashboardView {
795
+
@State
796
+
privatevar warpDriveEnabled: Bool
797
+
@State
798
+
privatevar lifeSupportEnabled: Bool
799
+
@State
800
+
privatevar artificialGravityEnabled: Bool
801
+
@State
802
+
privatevar tractorBeamEnabled: Bool
803
+
804
+
@Environment(\.controlPanelStyle)
805
+
privatevar controlPanelStyle
806
+
@Environment(\.toggleButtonStyle)
807
+
privatevar toggleButtonStyle
808
+
}
809
+
```
810
+
811
+
If you add blank lines, the list of properties becomes much longer and you lose the ability to group related properties together:
812
+
813
+
```swift
814
+
structSpaceshipDashboardView {
815
+
@State
816
+
privatevar warpDriveEnabled: Bool
817
+
818
+
@State
819
+
privatevar lifeSupportEnabled: Bool
820
+
821
+
@State
822
+
privatevar artificialGravityEnabled: Bool
823
+
824
+
@State
825
+
privatevar tractorBeamEnabled: Bool
826
+
827
+
@Environment(\.controlPanelStyle)
828
+
privatevar controlPanelStyle
829
+
830
+
@Environment(\.toggleButtonStyle)
831
+
privatevar toggleButtonStyle
832
+
}
833
+
```
834
+
835
+
This doesn't apply to complex attributes with named arguments, or multiple unnamed arguments. These arguments are visually complex and typically encode a lot of information, so feel cramped and difficult to read when written on a single line:
836
+
837
+
```swift
838
+
// Despite being less than 100 characters long, these lines are very complex and feel unnecessarily long:
839
+
@available(*, unavailable, message: "No longer in production") var saturn5Builder: Saturn5Builder
840
+
@available(*, deprecated, message: "To be retired by 2030") var atlas5Builder: Atlas5Builder
841
+
@available(*, iOS18.0, tvOS18.0, macOS15.0, watchOS11.0) var newGlennBuilder: NewGlennBuilder
0 commit comments