Skip to content

Add a group around member types used in return clauses. #149

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
Feb 24, 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
7 changes: 7 additions & 0 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {

override func visit(_ node: ReturnClauseSyntax) -> SyntaxVisitorContinueKind {
after(node.arrow, tokens: .space)

// Member type identifier is used when the return type is a member of another type. Add a group
// here so that the base, dot, and member type are kept together when they fit.
if node.returnType.is(MemberTypeIdentifierSyntax.self) {
before(node.returnType.firstToken, tokens: .open)
after(node.returnType.lastToken, tokens: .close)
}
return .visitChildren
}

Expand Down
21 changes: 20 additions & 1 deletion Tests/SwiftFormatPrettyPrintTests/FunctionDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ final class FunctionDeclTests: PrettyPrintTestCase {
}

func testFunctionDeclReturns() {
// TODO: The tuple return case needs a lot of work.
let input =
"""
func myFun(var1: Int, var2: Double) -> Double {
Expand All @@ -104,6 +103,12 @@ final class FunctionDeclTests: PrettyPrintTestCase {
func tupleFunc() throws -> (one: Int, two: Double, three: Bool, four: String) {
return (one: 1, two: 2.0, three: true, four: "four")
}
func memberTypeThrowingFunc() throws -> SomeBaseType<GenericArg1, GenericArg2, GenericArg3>.SomeInnerType {
}
func memberTypeReallyLongNameFunc() -> Type.InnerMember {
}
func tupleMembersFunc() -> (Type.Inner, Type2.Inner2) {
}
"""

let expected =
Expand All @@ -125,6 +130,20 @@ final class FunctionDeclTests: PrettyPrintTestCase {
one: 1, two: 2.0, three: true, four: "four"
)
}
func memberTypeThrowingFunc() throws
-> SomeBaseType<
GenericArg1, GenericArg2, GenericArg3
>.SomeInnerType
{
}
func memberTypeReallyLongNameFunc()
-> Type.InnerMember
{
}
func tupleMembersFunc() -> (
Type.Inner, Type2.Inner2
) {
}

"""

Expand Down