-
Notifications
You must be signed in to change notification settings - Fork 344
Add rules for how to wrap attributes on property declarations #254
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
29119ee
Update to SwiftFormat 0.53-beta-7
calda 356ee2f
Update airbnb.swiftformat
calda 9a23e67
Add rules for wrapping attributes on property declarations
calda 6d5c762
Remove stay spaces
calda 8f87c61
Remove special case for @Environment
calda 65679af
Simplify and generalize rule for stored properties
calda ad59cbe
Add example of a macro
calda 0c873e3
Update README.md
calda cfb7639
Update README.md
calda fe2982e
Update README.md
calda d8d3b48
Update to 0.53-beta-8
calda 086acca
Update README.md
calda 0e567b0
Update README.md
calda 9a9efc9
Update new rocket example
calda bf75a21
Update README.md
calda 187bd7e
Fix example
calda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
|
||
</details> | ||
|
||
* <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) | ||
* <a id='attributes-on-prev-line'></a>(<a href='#attributes-on-prev-line'>link</a>) **Place attributes for functions, types, and computed properties on the line above the declaration**. [](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#wrapAttributes) | ||
|
||
<details> | ||
|
||
```swift | ||
// WRONG | ||
@objc class Spaceship { | ||
|
||
@ViewBuilder var controlPanel: some View { | ||
// ... | ||
} | ||
|
||
@discardableResult func fly() -> Bool { | ||
// ... | ||
} | ||
|
||
} | ||
|
||
// RIGHT | ||
@objc | ||
class Spaceship { | ||
|
||
@ViewBuilder | ||
var controlPanel: some View { | ||
// ... | ||
} | ||
|
||
@discardableResult | ||
func fly() -> Bool { | ||
// ... | ||
} | ||
|
||
} | ||
``` | ||
|
||
</details> | ||
|
||
* <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) | ||
|
||
<details> | ||
|
||
```swift | ||
// WRONG. These simple property wrappers should be written on the same line as the declaration. | ||
struct SpaceshipDashboardView { | ||
|
||
@State | ||
private var warpDriveEnabled: Bool | ||
|
||
@ObservedObject | ||
private var lifeSupportService: LifeSupportService | ||
|
||
@Environment(\.controlPanelStyle) | ||
private var controlPanelStyle | ||
|
||
} | ||
|
||
// RIGHT | ||
struct SpaceshipDashboardView { | ||
|
||
@State private var warpDriveEnabled: Bool | ||
|
||
@ObservedObject private var lifeSupportService: LifeSupportService | ||
|
||
@Environment(\.controlPanelStyle) private var controlPanelStyle | ||
|
||
} | ||
``` | ||
|
||
```swift | ||
// WRONG. These complex attached macros should be written on the previous line. | ||
struct SolarSystemView { | ||
|
||
@Query(sort: \.distance) var allPlanets: [Planet] | ||
|
||
@Query(sort: \.age, order: .reverse) var moonsByAge: [Moon] | ||
|
||
} | ||
|
||
// RIGHT | ||
struct SolarSystemView { | ||
|
||
@Query(sort: \.distance) | ||
var allPlanets: [Planet] | ||
|
||
@Query(sort: \.age, order: .reverse) | ||
var oldestMoons: [Moon] | ||
|
||
} | ||
```swift | ||
|
||
``` | ||
// WRONG. These long, complex attributes should be written on the previous line. | ||
struct RocketFactory { | ||
|
||
@available(*, unavailable, message: "No longer in production") var saturn5Builder: Saturn5Builder | ||
|
||
@available(*, deprecated, message: "To be retired by 2030") var atlas5Builder: Atlas5Builder | ||
|
||
@available(*, iOS 18.0, tvOS 18.0, macOS 15.0, watchOS 11.0) var newGlennBuilder: NewGlennBuilder | ||
|
||
} | ||
|
||
// RIGHT | ||
struct RocketFactory { | ||
calda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@available(*, unavailable, message: "No longer in production") | ||
var saturn5Builder: Saturn5Builder | ||
|
||
@available(*, deprecated, message: "To be retired by 2030") | ||
var atlas5Builder: Atlas5Builder | ||
|
||
@available(*, iOS 18.0, tvOS 18.0, macOS 15.0, watchOS 11.0) | ||
var newGlennBuilder: NewGlennBuilder | ||
|
||
} | ||
``` | ||
|
||
#### Why? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In some other rules we put the "why?" before the examples but I am fine moving it after the examples here. |
||
|
||
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: | ||
|
||
```swift | ||
struct SpaceshipDashboardView { | ||
@State private var warpDriveEnabled: Bool | ||
@State private var lifeSupportEnabled: Bool | ||
@State private var artificialGravityEnabled: Bool | ||
@State private var tractorBeamEnabled: Bool | ||
|
||
@Environment(\.controlPanelStyle) private var controlPanelStyle | ||
@Environment(\.toggleButtonStyle) private var toggleButtonStyle | ||
} | ||
``` | ||
|
||
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: | ||
|
||
```swift | ||
struct SpaceshipDashboardView { | ||
@State | ||
private var warpDriveEnabled: Bool | ||
@State | ||
private var lifeSupportEnabled: Bool | ||
@State | ||
private var artificialGravityEnabled: Bool | ||
@State | ||
private var tractorBeamEnabled: Bool | ||
|
||
@Environment(\.controlPanelStyle) | ||
private var controlPanelStyle | ||
@Environment(\.toggleButtonStyle) | ||
private var toggleButtonStyle | ||
} | ||
``` | ||
|
||
If you add blank lines, the list of properties becomes much longer and you lose the ability to group related properties together: | ||
|
||
```swift | ||
struct SpaceshipDashboardView { | ||
@State | ||
private var warpDriveEnabled: Bool | ||
|
||
@State | ||
private var lifeSupportEnabled: Bool | ||
|
||
@State | ||
private var artificialGravityEnabled: Bool | ||
|
||
@State | ||
private var tractorBeamEnabled: Bool | ||
|
||
@Environment(\.controlPanelStyle) | ||
private var controlPanelStyle | ||
|
||
@Environment(\.toggleButtonStyle) | ||
private var toggleButtonStyle | ||
} | ||
``` | ||
|
||
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: | ||
|
||
```swift | ||
// Despite being less than 100 characters long, these lines are very complex and feel unnecessarily long: | ||
@available(*, unavailable, message: "No longer in production") var saturn5Builder: Saturn5Builder | ||
@available(*, deprecated, message: "To be retired by 2030") var atlas5Builder: Atlas5Builder | ||
@available(*, iOS 18.0, tvOS 18.0, macOS 15.0, watchOS 11.0) var newGlennBuilder: NewGlennBuilder | ||
``` | ||
|
||
</details> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
--closingparen same-line # wrapArguments | ||
--wraptypealiases before-first # wrapArguments | ||
--funcattributes prev-line # wrapAttributes | ||
--computedvarattrs prev-line # wrapAttributes | ||
--storedvarattrs same-line # wrapAttributes | ||
--complexattrs prev-line # wrapAttributes | ||
Comment on lines
+22
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
--typeattributes prev-line # wrapAttributes | ||
--wrapternary before-operators # wrap | ||
--structthreshold 20 # organizeDeclarations | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.