Skip to content

Commit 579e2bc

Browse files
authored
Merge pull request #1368 from kimdv/kimdv/add-newline-after-endif
Add newline for pund-keywords in `IfConfigDeclSyntax`
2 parents ef9a434 + 2c8f6e9 commit 579e2bc

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ public let DECL_NODES: [Node] = [
523523
children: [
524524
Child(name: "PoundKeyword",
525525
kind: .token(choices: [.token(tokenKind: "PoundIfToken"), .token(tokenKind: "PoundElseifToken"), .token(tokenKind: "PoundElseToken")]),
526-
classification: "BuildConfigId"),
526+
classification: "BuildConfigId",
527+
requiresLeadingNewline: true),
527528
Child(name: "Condition",
528529
kind: .node(kind: "Expr"),
529530
nameForDiagnostics: "condition",
@@ -553,7 +554,8 @@ public let DECL_NODES: [Node] = [
553554
kind: .collection(kind: "IfConfigClauseList", collectionElementName: "Clause")),
554555
Child(name: "PoundEndif",
555556
kind: .token(choices: [.token(tokenKind: "PoundEndifToken")]),
556-
classification: "BuildConfigId")
557+
classification: "BuildConfigId",
558+
requiresLeadingNewline: true)
557559
]),
558560

559561
Node(name: "ImportDecl",

CodeGeneration/Sources/generate-swiftsyntax/templates/basicformat/BasicFormatFile.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ let basicFormatFile = SourceFileSyntax(leadingTrivia: generateCopyrightHeader(fo
119119
/// - Parameter node: the node that is being visited
120120
/// - Returns: returns true if newline should be omitted
121121
open func shouldOmitNewline(_ node: TokenSyntax) -> Bool {
122+
if node.previousToken(viewMode: .sourceAccurate) == nil {
123+
return true
124+
}
122125
var ancestor: Syntax = Syntax(node)
123126
while let parent = ancestor.parent {
124127
ancestor = parent

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ open class BasicFormat: SyntaxRewriter {
7777
/// - Parameter node: the node that is being visited
7878
/// - Returns: returns true if newline should be omitted
7979
open func shouldOmitNewline(_ node: TokenSyntax) -> Bool {
80+
if node.previousToken(viewMode: .sourceAccurate) == nil {
81+
return true
82+
}
8083
var ancestor: Syntax = Syntax(node)
8184
while let parent = ancestor.parent {
8285
ancestor = parent
@@ -129,6 +132,10 @@ open class BasicFormat: SyntaxRewriter {
129132
return true
130133
case \CodeBlockSyntax.rightBrace:
131134
return true
135+
case \IfConfigClauseSyntax.poundKeyword:
136+
return true
137+
case \IfConfigDeclSyntax.poundEndif:
138+
return true
132139
case \MemberDeclBlockSyntax.rightBrace:
133140
return true
134141
case \SwitchExprSyntax.rightBrace:
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import XCTest
14+
import SwiftSyntax
15+
import SwiftSyntaxBuilder
16+
17+
final class IfConfigDeclSyntaxTests: XCTestCase {
18+
func testIfConfigClauseSyntax() {
19+
let buildable = IfConfigDeclSyntax(
20+
clauses: IfConfigClauseListSyntax {
21+
IfConfigClauseSyntax(
22+
poundKeyword: .poundIfKeyword(),
23+
condition: ExprSyntax("DEBUG"),
24+
elements: .statements(
25+
CodeBlockItemListSyntax {
26+
DeclSyntax(
27+
"""
28+
public func debug(_ data: Foo) -> String {
29+
return data.debugDescription
30+
}
31+
"""
32+
)
33+
}
34+
)
35+
)
36+
IfConfigClauseSyntax(
37+
poundKeyword: .poundElseKeyword(leadingTrivia: .newline),
38+
elements: .statements(
39+
CodeBlockItemListSyntax {
40+
DeclSyntax(
41+
"""
42+
public func debug(_ data: Foo) -> String {
43+
return data.description
44+
}
45+
"""
46+
)
47+
}
48+
)
49+
)
50+
},
51+
poundEndif: .poundEndifKeyword(leadingTrivia: .newline)
52+
)
53+
54+
AssertBuildResult(
55+
buildable,
56+
"""
57+
#if DEBUG
58+
public func debug(_ data: Foo) -> String {
59+
return data.debugDescription
60+
}
61+
#else
62+
public func debug(_ data: Foo) -> String {
63+
return data.description
64+
}
65+
#endif
66+
"""
67+
)
68+
}
69+
}

gyb_syntax_support/DeclNodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
'PoundIfToken',
9595
'PoundElseifToken',
9696
'PoundElseToken',
97-
]),
97+
], requires_leading_newline=True),
9898
Child('Condition', kind='Expr', name_for_diagnostics='condition', classification='BuildConfigId',
9999
is_optional=True),
100100
Child('Elements', is_optional=True, kind='Syntax',
@@ -118,7 +118,7 @@
118118
Child('Clauses', kind='IfConfigClauseList',
119119
collection_element_name='Clause'),
120120
Child('PoundEndif', kind='PoundEndifToken',
121-
classification='BuildConfigId'),
121+
classification='BuildConfigId', requires_leading_newline=True),
122122
]),
123123

124124
Node('PoundSourceLocation', name_for_diagnostics="'#sourceLocation' directive",

0 commit comments

Comments
 (0)