Skip to content

Format property wrappers correctly. #192

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 1 commit into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,22 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
return .visitChildren
}

override func visit(_ node: CustomAttributeSyntax) -> SyntaxVisitorContinueKind {
// "Custom attributes" are better known to users as "property wrappers".
before(node.firstToken, tokens: .open)
if let argumentList = node.argumentList,
let leftParen = node.leftParen, let rightParen = node.rightParen
{
arrangeFunctionCallArgumentList(
argumentList,
leftDelimiter: leftParen,
rightDelimiter: rightParen,
forcesBreakBeforeRightDelimiter: false)
}
after(node.lastToken, tokens: .close)
return .visitChildren
}

override func visit(_ node: AvailabilitySpecListSyntax) -> SyntaxVisitorContinueKind {
insertTokens(.break(.same, size: 1), betweenElementsOf: node)
return .visitChildren
Expand Down
45 changes: 45 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/AttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,49 @@ final class AttributeTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
}

func testPropertyWrappers() {
// Property wrappers are `CustomAttributeSyntax` nodes (not `AttributeSyntax`) and their
// arguments are `TupleExprElementListSyntax` (like regular function call argument lists), so
// make sure that those are formatted properly.
let input =
"""
struct X {
@Wrapper var value: String

@Wrapper ( ) var value: String

@Wrapper (arg1:"value")var value: String

@Wrapper (arg1:"value")
var value: String

@Wrapper (arg1:"value",arg2:otherValue)
var value: String
}
"""

let expected =
"""
struct X {
@Wrapper var value: String

@Wrapper() var value: String

@Wrapper(arg1: "value")
var value: String

@Wrapper(arg1: "value")
var value: String

@Wrapper(
arg1: "value",
arg2: otherValue)
var value: String
}

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 32)
}
}
1 change: 1 addition & 0 deletions Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ extension AttributeTests {
("testObjCAttributesDiscretionaryLineBreaking", testObjCAttributesDiscretionaryLineBreaking),
("testObjCAttributesPerLineBreaking", testObjCAttributesPerLineBreaking),
("testObjCBinPackedAttributes", testObjCBinPackedAttributes),
("testPropertyWrappers", testPropertyWrappers),
]
}

Expand Down