Skip to content

Fix spacing around <, >, *, ! and ? #1164

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 8 commits into from
Jan 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
PunctuatorSpec(name: "RightBrace", kind: "r_brace", text: "}"),
PunctuatorSpec(name: "LeftSquareBracket", kind: "l_square", text: "["),
PunctuatorSpec(name: "RightSquareBracket", kind: "r_square", text: "]"),
PunctuatorSpec(name: "LeftAngle", kind: "l_angle", text: "<", requiresLeadingSpace: true, requiresTrailingSpace: true),
PunctuatorSpec(name: "RightAngle", kind: "r_angle", text: ">", requiresLeadingSpace: true, requiresTrailingSpace: true),
PunctuatorSpec(name: "LeftAngle", kind: "l_angle", text: "<"),
PunctuatorSpec(name: "RightAngle", kind: "r_angle", text: ">"),
PunctuatorSpec(name: "Period", kind: "period", text: "."),
PunctuatorSpec(name: "Comma", kind: "comma", text: ",", requiresTrailingSpace: true),
PunctuatorSpec(name: "Ellipsis", kind: "ellipsis", text: "..."),
Expand All @@ -275,8 +275,8 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
PunctuatorSpec(name: "Arrow", kind: "arrow", text: "->", requiresLeadingSpace: true, requiresTrailingSpace: true),
PunctuatorSpec(name: "Backtick", kind: "backtick", text: "`"),
PunctuatorSpec(name: "Backslash", kind: "backslash", text: "\\"),
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!"),
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?"),
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!", requiresTrailingSpace: true),
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?", requiresTrailingSpace: true),
PunctuatorSpec(name: "InfixQuestionMark", kind: "question_infix", text: "?"),
PunctuatorSpec(name: "StringQuote", kind: "string_quote", text: "\"", classification: "StringLiteral"),
PunctuatorSpec(name: "SingleQuote", kind: "single_quote", text: "\'", classification: "StringLiteral"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ let basicFormatFile = SourceFile {
}

FunctionDecl("open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool") {
SwitchStmt("""
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
case (.leftParen, .spacedBinaryOperator):
return false
default:
break
}
""")

SwitchStmt(expression: Expr("token.tokenKind")) {
for token in SYNTAX_TOKENS {
if token.requiresLeadingSpace {
Expand All @@ -146,12 +155,19 @@ let basicFormatFile = SourceFile {

SwitchStmt("""
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
case (.asKeyword, .exclamationMark),
(.asKeyword, .postfixQuestionMark),
(.initKeyword, .leftParen),
(.initKeyword, .postfixQuestionMark),
(.tryKeyword, .exclamationMark),
(.tryKeyword, .postfixQuestionMark):
case (.asKeyword, .exclamationMark), // Ensures there is not space in `as!`
(.asKeyword, .postfixQuestionMark), // Ensures there is not space in `as?`
(.exclamationMark, .leftParen), // Ensures there is not space in `myOptionalClosure!()`
(.exclamationMark, .period), // Ensures there is not space in `myOptionalBar!.foo()`
(.initKeyword, .leftParen), // Ensures there is not space in `init()`
(.initKeyword, .postfixQuestionMark), // Ensures there is not space in `init?`
(.postfixQuestionMark, .leftParen), // Ensures there is not space in `init?()`
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
Comment on lines +165 to +166
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those are also no longer necessary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see they are necessary to remove the trailing space on postfixQuestionMark

(.tryKeyword, .exclamationMark), // Ensures there is not space in `try!`
(.tryKeyword, .postfixQuestionMark): // Ensures there is not space in `try?`
return false
case (.spacedBinaryOperator, .comma):
return false
default:
break
Expand Down
37 changes: 23 additions & 14 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ open class BasicFormat: SyntaxRewriter {
}

open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool {
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
case (.leftParen, .spacedBinaryOperator):
return false
default:
break
}
switch token.tokenKind {
case .inKeyword:
return true
Expand All @@ -139,10 +145,6 @@ open class BasicFormat: SyntaxRewriter {
return true
case .leftBrace:
return true
case .leftAngle:
return true
case .rightAngle:
return true
case .equal:
return true
case .arrow:
Expand All @@ -160,12 +162,19 @@ open class BasicFormat: SyntaxRewriter {
return false
}
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
case (.asKeyword, .exclamationMark),
(.asKeyword, .postfixQuestionMark),
(.initKeyword, .leftParen),
(.initKeyword, .postfixQuestionMark),
(.tryKeyword, .exclamationMark),
(.tryKeyword, .postfixQuestionMark):
case (.asKeyword, .exclamationMark), // Ensures there is not space in `as!`
(.asKeyword, .postfixQuestionMark), // Ensures there is not space in `as?`
(.exclamationMark, .leftParen), // Ensures there is not space in `myOptionalClosure!()`
(.exclamationMark, .period), // Ensures there is not space in `myOptionalBar!.foo()`
(.initKeyword, .leftParen), // Ensures there is not space in `init()`
(.initKeyword, .postfixQuestionMark), // Ensures there is not space in `init?`
(.postfixQuestionMark, .leftParen), // Ensures there is not space in `init?()`
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
(.tryKeyword, .exclamationMark), // Ensures there is not space in `try!`
(.tryKeyword, .postfixQuestionMark): // Ensures there is not space in `try?`
return false
case (.spacedBinaryOperator, .comma):
return false
default:
break
Expand Down Expand Up @@ -261,10 +270,6 @@ open class BasicFormat: SyntaxRewriter {
return true
case .wildcardKeyword:
return true
case .leftAngle:
return true
case .rightAngle:
return true
case .comma:
return true
case .colon:
Expand All @@ -273,6 +278,10 @@ open class BasicFormat: SyntaxRewriter {
return true
case .arrow:
return true
case .exclamationMark:
return true
case .postfixQuestionMark:
return true
case .poundKeyPathKeyword:
return true
case .poundLineKeyword:
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9039,17 +9039,17 @@ public enum SyntaxFactory {
}
@available(*, deprecated, message: "Use TokenSyntax.leftAngleToken instead")
public static func makeLeftAngleToken(
leadingTrivia: Trivia = .space,
trailingTrivia: Trivia = .space
leadingTrivia: Trivia = [],
trailingTrivia: Trivia = []
) -> TokenSyntax {
return makeToken(.leftAngle, presence: .present,
leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia)
}
@available(*, deprecated, message: "Use TokenSyntax.rightAngleToken instead")
public static func makeRightAngleToken(
leadingTrivia: Trivia = .space,
trailingTrivia: Trivia = .space
leadingTrivia: Trivia = [],
trailingTrivia: Trivia = []
) -> TokenSyntax {
return makeToken(.rightAngle, presence: .present,
leadingTrivia: leadingTrivia,
Expand Down Expand Up @@ -9166,7 +9166,7 @@ public enum SyntaxFactory {
@available(*, deprecated, message: "Use TokenSyntax.exclamationMarkToken instead")
public static func makeExclamationMarkToken(
leadingTrivia: Trivia = [],
trailingTrivia: Trivia = []
trailingTrivia: Trivia = .space
) -> TokenSyntax {
return makeToken(.exclamationMark, presence: .present,
leadingTrivia: leadingTrivia,
Expand All @@ -9175,7 +9175,7 @@ public enum SyntaxFactory {
@available(*, deprecated, message: "Use TokenSyntax.postfixQuestionMarkToken instead")
public static func makePostfixQuestionMarkToken(
leadingTrivia: Trivia = [],
trailingTrivia: Trivia = []
trailingTrivia: Trivia = .space
) -> TokenSyntax {
return makeToken(.postfixQuestionMark, presence: .present,
leadingTrivia: leadingTrivia,
Expand Down
100 changes: 100 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/DoStmtTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,104 @@ final class DoStmtTests: XCTestCase {
"""
)
}

func testDoStmtWithExclamationMark() {
let buildable = DoStmt(
body: CodeBlock(statementsBuilder: {
TryExpr(questionOrExclamationMark: .exclamationMark, expression: FunctionCallExpr(callee: ExprSyntax("a.b")))
}),
catchClauses: [
CatchClause(
CatchItemList {
CatchItem(pattern: PatternSyntax("Error1"))
CatchItem(pattern: PatternSyntax("Error2"))
}
) {
FunctionCallExpr(callee: ExprSyntax("print")) {
TupleExprElement(expression: StringLiteralExpr(content: "Known error"))
}
},
CatchClause(
CatchItemList {
CatchItem(
pattern: PatternSyntax("Error3"),
whereClause: WhereClause(guardResult: MemberAccessExpr(base: "error", name: "isError4"))
)
}
) {
ThrowStmt(expression: MemberAccessExpr(base: "Error4", name: "error3"))
},
CatchClause {
FunctionCallExprSyntax(callee: ExprSyntax("print")) {
TupleExprElementSyntax(expression: Expr("error"))
}
},
]
)

AssertBuildResult(
buildable,
"""
do {
try! a.b()
} catch Error1, Error2 {
print("Known error")
} catch Error3 where error.isError4 {
throw Error4.error3
} catch {
print(error)
}
"""
)
}

func testDoStmtWithPostfixQuestionMark() {
let buildable = DoStmt(
body: CodeBlock(statementsBuilder: {
TryExpr(questionOrExclamationMark: .postfixQuestionMark, expression: FunctionCallExpr(callee: ExprSyntax("a.b")))
}),
catchClauses: [
CatchClause(
CatchItemList {
CatchItem(pattern: PatternSyntax("Error1"))
CatchItem(pattern: PatternSyntax("Error2"))
}
) {
FunctionCallExpr(callee: ExprSyntax("print")) {
TupleExprElement(expression: StringLiteralExpr(content: "Known error"))
}
},
CatchClause(
CatchItemList {
CatchItem(
pattern: PatternSyntax("Error3"),
whereClause: WhereClause(guardResult: MemberAccessExpr(base: "error", name: "isError4"))
)
}
) {
ThrowStmt(expression: MemberAccessExpr(base: "Error4", name: "error3"))
},
CatchClause {
FunctionCallExprSyntax(callee: ExprSyntax("print")) {
TupleExprElementSyntax(expression: Expr("error"))
}
},
]
)

AssertBuildResult(
buildable,
"""
do {
try? a.b()
} catch Error1, Error2 {
print("Known error")
} catch Error3 where error.isError4 {
throw Error4.error3
} catch {
print(error)
}
"""
)
}
}
Loading