Skip to content

Remove requiresLeadingNewline from basic format #1690

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
May 30, 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
5 changes: 1 addition & 4 deletions CodeGeneration/Sources/SyntaxSupport/Child.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public class Child {
public let description: String?
public let forceClassification: Bool
public let isIndented: Bool
public let requiresLeadingNewline: Bool
public let isOptional: Bool
public let classification: SyntaxClassification?

Expand Down Expand Up @@ -160,8 +159,7 @@ public class Child {
isOptional: Bool = false,
classification: String? = nil,
forceClassification: Bool = false,
isIndented: Bool = false,
requiresLeadingNewline: Bool = false
isIndented: Bool = false
) {
if let firstCharInName = name.first {
precondition(firstCharInName.isUppercase == true, "The first letter of a child’s name should be uppercase")
Expand All @@ -173,7 +171,6 @@ public class Child {
self.classification = classificationByName(classification)
self.forceClassification = forceClassification
self.isIndented = isIndented
self.requiresLeadingNewline = requiresLeadingNewline
self.isOptional = isOptional
}
}
3 changes: 1 addition & 2 deletions CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public let COMMON_NODES: [Node] = [
),
Child(
name: "RightBrace",
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
requiresLeadingNewline: true
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
),
]
),
Expand Down
12 changes: 4 additions & 8 deletions CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public let DECL_NODES: [Node] = [
),
Child(
name: "RightBrace",
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
requiresLeadingNewline: true
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
),
]
),
Expand Down Expand Up @@ -992,8 +991,7 @@ public let DECL_NODES: [Node] = [
Child(
name: "PoundKeyword",
kind: .token(choices: [.token(tokenKind: "PoundIfToken"), .token(tokenKind: "PoundElseifToken"), .token(tokenKind: "PoundElseToken")]),
classification: "BuildConfigId",
requiresLeadingNewline: true
classification: "BuildConfigId"
),
Child(
name: "Condition",
Expand Down Expand Up @@ -1045,8 +1043,7 @@ public let DECL_NODES: [Node] = [
Child(
name: "PoundEndif",
kind: .token(choices: [.token(tokenKind: "PoundEndifToken")]),
classification: "BuildConfigId",
requiresLeadingNewline: true
classification: "BuildConfigId"
),
]
),
Expand Down Expand Up @@ -1355,8 +1352,7 @@ public let DECL_NODES: [Node] = [
),
Child(
name: "RightBrace",
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
requiresLeadingNewline: true
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
),
]
),
Expand Down
6 changes: 2 additions & 4 deletions CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,7 @@ public let EXPR_NODES: [Node] = [
),
Child(
name: "RightBrace",
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
requiresLeadingNewline: true
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
),
]
),
Expand Down Expand Up @@ -1643,8 +1642,7 @@ public let EXPR_NODES: [Node] = [
),
Child(
name: "RightBrace",
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
requiresLeadingNewline: true
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
),
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,6 @@ let basicFormatExtensionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader)
)
}

try! ExtensionDeclSyntax("public extension TokenSyntax") {
DeclSyntax(
"""
var requiresLeadingNewline: Bool {
if let keyPath = keyPathInParent, keyPath.requiresLeadingNewline {
return true
}
return false
}
"""
)
}

try! ExtensionDeclSyntax("fileprivate extension AnyKeyPath") {
try VariableDeclSyntax("var requiresIndent: Bool") {
try SwitchExprSyntax("switch self") {
Expand All @@ -94,21 +81,6 @@ let basicFormatExtensionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader)
}
}

try VariableDeclSyntax("var requiresLeadingNewline: Bool") {
try SwitchExprSyntax("switch self") {
for node in SYNTAX_NODES.compactMap(\.layoutNode) {
for child in node.children where child.requiresLeadingNewline {
SwitchCaseSyntax("case \\\(raw: node.type.syntaxBaseName).\(raw: child.varName):") {
StmtSyntax("return true")
}
}
}
SwitchCaseSyntax("default:") {
StmtSyntax("return false")
}
}
}

try VariableDeclSyntax("var requiresLeadingSpace: Bool?") {
try SwitchExprSyntax("switch self") {
for node in SYNTAX_NODES.compactMap(\.layoutNode) {
Expand Down
11 changes: 6 additions & 5 deletions Sources/SwiftBasicFormat/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ open class BasicFormat: SyntaxRewriter {
{
return false
} else if let second {
if second.requiresLeadingNewline {
return true
}

var ancestor: Syntax = Syntax(second)
while let parent = ancestor.parent {
ancestor = parent
Expand All @@ -178,11 +174,16 @@ open class BasicFormat: SyntaxRewriter {
}

switch (first?.tokenKind, second?.tokenKind) {

case (.multilineStringQuote, .backslash), // string interpolation segment inside a multi-line string literal
(.multilineStringQuote, .multilineStringQuote), // empty multi-line string literal
(.multilineStringQuote, .stringSegment), // segment starting a multi-line string literal
(.stringSegment, .multilineStringQuote), // ending a multi-line string literal that has a string interpolation segment at its end
(.rightParen, .multilineStringQuote): // ending a multi-line string literal that has a string interpolation segment at its end
(.rightParen, .multilineStringQuote), // ending a multi-line string literal that has a string interpolation segment at its end
(_, .poundElseKeyword),
(_, .poundElseifKeyword),
(_, .poundEndifKeyword),
(_, .rightBrace):
return true
default:
return false
Expand Down
30 changes: 0 additions & 30 deletions Sources/SwiftBasicFormat/generated/BasicFormat+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ public extension SyntaxProtocol {
}
}

public extension TokenSyntax {
var requiresLeadingNewline: Bool {
if let keyPath = keyPathInParent, keyPath.requiresLeadingNewline {
return true
}
return false
}
}

fileprivate extension AnyKeyPath {
var requiresIndent: Bool {
switch self {
Expand Down Expand Up @@ -70,27 +61,6 @@ fileprivate extension AnyKeyPath {
}
}

var requiresLeadingNewline: Bool {
switch self {
case \AccessorBlockSyntax.rightBrace:
return true
case \ClosureExprSyntax.rightBrace:
return true
case \CodeBlockSyntax.rightBrace:
return true
case \IfConfigClauseSyntax.poundKeyword:
return true
case \IfConfigDeclSyntax.poundEndif:
return true
case \MemberDeclBlockSyntax.rightBrace:
return true
case \SwitchExprSyntax.rightBrace:
return true
default:
return false
}
}

var requiresLeadingSpace: Bool? {
switch self {
case \AvailabilityArgumentSyntax.entry:
Expand Down
23 changes: 21 additions & 2 deletions Tests/SwiftSyntaxBuilderTest/IfConfigDeclSyntaxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,22 @@ final class IfConfigDeclSyntaxTests: XCTestCase {
)
)
IfConfigClauseSyntax(
poundKeyword: .poundElseKeyword(leadingTrivia: .newline),
poundKeyword: .poundElseifKeyword(),
condition: ExprSyntax("TEST"),
elements: .statements(
CodeBlockItemListSyntax {
DeclSyntax(
"""
public func debug(_ data: Foo) -> String {
return data.description
}
"""
)
}
)
)
IfConfigClauseSyntax(
poundKeyword: .poundElseKeyword(),
elements: .statements(
CodeBlockItemListSyntax {
DeclSyntax(
Expand All @@ -48,7 +63,7 @@ final class IfConfigDeclSyntaxTests: XCTestCase {
)
)
},
poundEndif: .poundEndifKeyword(leadingTrivia: .newline)
poundEndif: .poundEndifKeyword()
)

assertBuildResult(
Expand All @@ -58,6 +73,10 @@ final class IfConfigDeclSyntaxTests: XCTestCase {
public func debug(_ data: Foo) -> String {
return data.debugDescription
}
#elseif TEST
public func debug(_ data: Foo) -> String {
return data.description
}
#else
public func debug(_ data: Foo) -> String {
return data.description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ final class StringLiteralExprSyntaxTests: XCTestCase {
// Tab should not be escaped in single-line string literals
assertBuildResult(
StringLiteralExprSyntax(
openQuote: .multilineStringQuoteToken(trailingTrivia: .newline),
openQuote: .multilineStringQuoteToken(),
content: "a\tb",
closeQuote: .multilineStringQuoteToken(leadingTrivia: .newline)
closeQuote: .multilineStringQuoteToken()
),
#"""
"""
Expand Down Expand Up @@ -225,7 +225,7 @@ final class StringLiteralExprSyntaxTests: XCTestCase {

func testMultiLineStringWithResultBuilder() {
let buildable = StringLiteralExprSyntax(
openQuote: .multilineStringQuoteToken(trailingTrivia: .newline),
openQuote: .multilineStringQuoteToken(),
segments: StringLiteralSegmentsSyntax {
StringSegmentSyntax(content: .stringSegment(#"Error validating child at index \(index) of \(nodeKind):"#), trailingTrivia: .newline)
StringSegmentSyntax(content: .stringSegment(#"Node did not satisfy any node choice requirement."#), trailingTrivia: .newline)
Expand All @@ -236,7 +236,7 @@ final class StringLiteralExprSyntaxTests: XCTestCase {
}
)
},
closeQuote: .multilineStringQuoteToken(leadingTrivia: .newline)
closeQuote: .multilineStringQuoteToken()
)

assertBuildResult(
Expand Down