From b95d65fc7ba07a03feed5a92171dbf0d913c2fc7 Mon Sep 17 00:00:00 2001 From: Kim de Vos Date: Wed, 24 May 2023 13:32:55 +0200 Subject: [PATCH] Remove `requiresLeadingNewline` from basic format --- .../Sources/SyntaxSupport/Child.swift | 5 +--- .../Sources/SyntaxSupport/CommonNodes.swift | 3 +- .../Sources/SyntaxSupport/DeclNodes.swift | 12 +++----- .../Sources/SyntaxSupport/ExprNodes.swift | 6 ++-- .../BasicFormatExtensionsFile.swift | 28 ----------------- Sources/SwiftBasicFormat/BasicFormat.swift | 11 +++---- .../generated/BasicFormat+Extensions.swift | 30 ------------------- .../IfConfigDeclSyntaxTests.swift | 23 ++++++++++++-- .../StringLiteralExprSyntaxTests.swift | 8 ++--- 9 files changed, 39 insertions(+), 87 deletions(-) diff --git a/CodeGeneration/Sources/SyntaxSupport/Child.swift b/CodeGeneration/Sources/SyntaxSupport/Child.swift index 6b2b755e14f..242fbe70eb7 100644 --- a/CodeGeneration/Sources/SyntaxSupport/Child.swift +++ b/CodeGeneration/Sources/SyntaxSupport/Child.swift @@ -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? @@ -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") @@ -173,7 +171,6 @@ public class Child { self.classification = classificationByName(classification) self.forceClassification = forceClassification self.isIndented = isIndented - self.requiresLeadingNewline = requiresLeadingNewline self.isOptional = isOptional } } diff --git a/CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift b/CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift index 4e28d3418cd..1ed703e53e0 100644 --- a/CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift +++ b/CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift @@ -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")]) ), ] ), diff --git a/CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift b/CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift index eb18f9e954f..174137e53a8 100644 --- a/CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift +++ b/CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift @@ -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")]) ), ] ), @@ -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", @@ -1045,8 +1043,7 @@ public let DECL_NODES: [Node] = [ Child( name: "PoundEndif", kind: .token(choices: [.token(tokenKind: "PoundEndifToken")]), - classification: "BuildConfigId", - requiresLeadingNewline: true + classification: "BuildConfigId" ), ] ), @@ -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")]) ), ] ), diff --git a/CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift b/CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift index 2b713d3bc6a..c83ae1a8907 100644 --- a/CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift +++ b/CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift @@ -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")]) ), ] ), @@ -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")]) ), ] ), diff --git a/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftbasicformat/BasicFormatExtensionsFile.swift b/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftbasicformat/BasicFormatExtensionsFile.swift index 2a10e9a2a34..ec5237d4fec 100644 --- a/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftbasicformat/BasicFormatExtensionsFile.swift +++ b/CodeGeneration/Sources/generate-swiftsyntax/templates/swiftbasicformat/BasicFormatExtensionsFile.swift @@ -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") { @@ -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) { diff --git a/Sources/SwiftBasicFormat/BasicFormat.swift b/Sources/SwiftBasicFormat/BasicFormat.swift index dbdffe9b028..1a808e4030a 100644 --- a/Sources/SwiftBasicFormat/BasicFormat.swift +++ b/Sources/SwiftBasicFormat/BasicFormat.swift @@ -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 @@ -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 diff --git a/Sources/SwiftBasicFormat/generated/BasicFormat+Extensions.swift b/Sources/SwiftBasicFormat/generated/BasicFormat+Extensions.swift index 2e07341afc0..71de04baa52 100644 --- a/Sources/SwiftBasicFormat/generated/BasicFormat+Extensions.swift +++ b/Sources/SwiftBasicFormat/generated/BasicFormat+Extensions.swift @@ -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 { @@ -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: diff --git a/Tests/SwiftSyntaxBuilderTest/IfConfigDeclSyntaxTests.swift b/Tests/SwiftSyntaxBuilderTest/IfConfigDeclSyntaxTests.swift index 20bd7f27945..00752caee94 100644 --- a/Tests/SwiftSyntaxBuilderTest/IfConfigDeclSyntaxTests.swift +++ b/Tests/SwiftSyntaxBuilderTest/IfConfigDeclSyntaxTests.swift @@ -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( @@ -48,7 +63,7 @@ final class IfConfigDeclSyntaxTests: XCTestCase { ) ) }, - poundEndif: .poundEndifKeyword(leadingTrivia: .newline) + poundEndif: .poundEndifKeyword() ) assertBuildResult( @@ -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 diff --git a/Tests/SwiftSyntaxBuilderTest/StringLiteralExprSyntaxTests.swift b/Tests/SwiftSyntaxBuilderTest/StringLiteralExprSyntaxTests.swift index b760e2dd309..7cc43d887c1 100644 --- a/Tests/SwiftSyntaxBuilderTest/StringLiteralExprSyntaxTests.swift +++ b/Tests/SwiftSyntaxBuilderTest/StringLiteralExprSyntaxTests.swift @@ -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() ), #""" """ @@ -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) @@ -236,7 +236,7 @@ final class StringLiteralExprSyntaxTests: XCTestCase { } ) }, - closeQuote: .multilineStringQuoteToken(leadingTrivia: .newline) + closeQuote: .multilineStringQuoteToken() ) assertBuildResult(