Skip to content

Commit 40b8304

Browse files
authored
Merge pull request #1148 from stevapple/tuple-indent
Mark function parameters and tuple elements as indented
2 parents 46cc7c0 + 6d6dc28 commit 40b8304

File tree

10 files changed

+180
-11
lines changed

10 files changed

+180
-11
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public let DECL_NODES: [Node] = [
116116
]),
117117
Child(name: "ParameterList",
118118
kind: "FunctionParameterList",
119-
collectionElementName: "Parameter"),
119+
collectionElementName: "Parameter",
120+
isIndented: true),
120121
Child(name: "RightParen",
121122
kind: "RightParenToken",
122123
tokenChoices: [

CodeGeneration/Sources/SyntaxSupport/gyb_generated/ExprNodes.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ public let EXPR_NODES: [Node] = [
344344
]),
345345
Child(name: "ElementList",
346346
kind: "TupleExprElementList",
347-
collectionElementName: "Element"),
347+
collectionElementName: "Element",
348+
isIndented: true),
348349
Child(name: "RightParen",
349350
kind: "RightParenToken",
350351
tokenChoices: [
@@ -829,7 +830,8 @@ public let EXPR_NODES: [Node] = [
829830
]),
830831
Child(name: "ArgumentList",
831832
kind: "TupleExprElementList",
832-
collectionElementName: "Argument"),
833+
collectionElementName: "Argument",
834+
isIndented: true),
833835
Child(name: "RightParen",
834836
kind: "RightParenToken",
835837
isOptional: true,

CodeGeneration/Sources/SyntaxSupport/gyb_generated/TypeNodes.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ public let TYPE_NODES: [Node] = [
300300
]),
301301
Child(name: "Elements",
302302
kind: "TupleTypeElementList",
303-
collectionElementName: "Element"),
303+
collectionElementName: "Element",
304+
isIndented: true),
304305
Child(name: "RightParen",
305306
kind: "RightParenToken",
306307
tokenChoices: [
@@ -322,7 +323,8 @@ public let TYPE_NODES: [Node] = [
322323
]),
323324
Child(name: "Arguments",
324325
kind: "TupleTypeElementList",
325-
collectionElementName: "Argument"),
326+
collectionElementName: "Argument",
327+
isIndented: true),
326328
Child(name: "RightParen",
327329
kind: "RightParenToken",
328330
tokenChoices: [

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,20 @@ open class BasicFormat: SyntaxRewriter {
7676
return true
7777
case \CodeBlockSyntax.statements:
7878
return true
79+
case \FunctionCallExprSyntax.argumentList:
80+
return true
81+
case \FunctionTypeSyntax.arguments:
82+
return true
7983
case \MemberDeclBlockSyntax.members:
8084
return true
85+
case \ParameterClauseSyntax.parameterList:
86+
return true
8187
case \SwitchCaseSyntax.statements:
8288
return true
89+
case \TupleExprSyntax.elementList:
90+
return true
91+
case \TupleTypeSyntax.elements:
92+
return true
8393
default:
8494
return false
8595
}

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,69 @@ final class FunctionTests: XCTestCase {
4242
}
4343
AssertBuildResult(buildable, "test(value1, p2: value2, value3, p4: value4, value5)")
4444
}
45-
45+
46+
func testFunctionDeclBuilder() {
47+
let builder = FunctionDecl("""
48+
func test(_ p1: Int, p2: Int, _ p3: Int, p4: Int, _ p5: Int) -> Int {
49+
return p1 + p2 + p3 + p4 + p5
50+
}
51+
""")
52+
53+
AssertBuildResult(builder, """
54+
func test(_ p1: Int, p2: Int, _ p3: Int, p4: Int, _ p5: Int) -> Int {
55+
return p1 + p2 + p3 + p4 + p5
56+
}
57+
""")
58+
}
59+
60+
func testMultilineFunctionParameterList() {
61+
let builder = FunctionDecl("""
62+
func test(
63+
_ p1: Int,
64+
p2: Int,
65+
_ p3: Int,
66+
p4: Int,
67+
_ p5: Int
68+
) -> Int {
69+
return p1 + p2 + p3 + p4 + p5
70+
}
71+
""")
72+
73+
AssertBuildResult(builder, """
74+
func test(
75+
_ p1: Int,
76+
p2: Int,
77+
_ p3: Int,
78+
p4: Int,
79+
_ p5: Int
80+
) -> Int {
81+
return p1 + p2 + p3 + p4 + p5
82+
}
83+
""")
84+
}
85+
86+
func testMultilineFunctionCallExpr() {
87+
let builder = FunctionCallExpr("""
88+
test(
89+
p1: value1,
90+
p2: value2,
91+
p3: value3,
92+
p4: value4,
93+
p5: value5
94+
)
95+
""")
96+
97+
AssertBuildResult(builder, """
98+
test(
99+
p1: value1,
100+
p2: value2,
101+
p3: value3,
102+
p4: value4,
103+
p5: value5
104+
)
105+
""")
106+
}
107+
46108
func testParensEmittedForNoArgumentsAndNoTrailingClosure() {
47109
let buildable = FunctionCallExpr(callee: ExprSyntax("test"))
48110
AssertBuildResult(buildable, "test()")

Tests/SwiftSyntaxBuilderTest/InitializerDeclTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,30 @@ final class InitializerDeclTests: XCTestCase {
4646
}
4747
""")
4848
}
49+
50+
func testMultilineParameterList() {
51+
let builder = InitializerDecl("""
52+
init(
53+
_ p1: Int,
54+
p2: Int,
55+
_ p3: Int,
56+
p4: Int,
57+
_ p5: Int
58+
) {
59+
self.init(p1 + p2 + p3 + p4 + p5)
60+
}
61+
""")
62+
63+
AssertBuildResult(builder, """
64+
init(
65+
_ p1: Int,
66+
p2: Int,
67+
_ p3: Int,
68+
p4: Int,
69+
_ p5: Int
70+
) {
71+
self.init(p1 + p2 + p3 + p4 + p5)
72+
}
73+
""")
74+
}
4975
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 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 TupleTests: XCTestCase {
18+
func testLabeledElementList() {
19+
let builder = TupleExprSyntax("(p1: value1, p2: value2, p3: value3)")
20+
AssertBuildResult(builder, "(p1: value1, p2: value2, p3: value3)")
21+
}
22+
23+
func testMultilineTupleExpr() {
24+
let builder = TupleExprSyntax("""
25+
(
26+
p1: value1,
27+
p2: value2,
28+
p3: value3,
29+
p4: value4,
30+
p5: value5
31+
)
32+
""")
33+
34+
AssertBuildResult(builder, """
35+
(
36+
p1: value1,
37+
p2: value2,
38+
p3: value3,
39+
p4: value4,
40+
p5: value5
41+
)
42+
""")
43+
}
44+
45+
func testMultilineTupleType() {
46+
let builder = TupleTypeSyntax("""
47+
(
48+
Int,
49+
p2: Int,
50+
Int,
51+
p4: Int,
52+
Int
53+
)
54+
""")
55+
56+
AssertBuildResult(builder, """
57+
(
58+
Int,
59+
p2: Int,
60+
Int,
61+
p4: Int,
62+
Int
63+
)
64+
""")
65+
}
66+
}

gyb_syntax_support/DeclNodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
children=[
6262
Child('LeftParen', kind='LeftParenToken'),
6363
Child('ParameterList', kind='FunctionParameterList', name_for_diagnostics='parameters',
64-
collection_element_name='Parameter'),
64+
collection_element_name='Parameter', is_indented=True),
6565
Child('RightParen', kind='RightParenToken'),
6666
]),
6767

gyb_syntax_support/ExprNodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
children=[
207207
Child('LeftParen', kind='LeftParenToken'),
208208
Child('ElementList', kind='TupleExprElementList',
209-
collection_element_name='Element'),
209+
collection_element_name='Element', is_indented=True),
210210
Child('RightParen', kind='RightParenToken'),
211211
]),
212212

@@ -480,7 +480,7 @@
480480
Child('LeftParen', kind='LeftParenToken',
481481
is_optional=True),
482482
Child('ArgumentList', kind='TupleExprElementList', name_for_diagnostics='arguments',
483-
collection_element_name='Argument'),
483+
collection_element_name='Argument', is_indented=True),
484484
Child('RightParen', kind='RightParenToken',
485485
is_optional=True),
486486
Child('TrailingClosure', kind='ClosureExpr', name_for_diagnostics='trailing closure',

gyb_syntax_support/TypeNodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
children=[
171171
Child('LeftParen', kind='LeftParenToken'),
172172
Child('Elements', kind='TupleTypeElementList',
173-
collection_element_name='Element'),
173+
collection_element_name='Element', is_indented=True),
174174
Child('RightParen', kind='RightParenToken'),
175175
]),
176176

@@ -182,7 +182,7 @@
182182
children=[
183183
Child('LeftParen', kind='LeftParenToken'),
184184
Child('Arguments', kind='TupleTypeElementList',
185-
collection_element_name='Argument'),
185+
collection_element_name='Argument', is_indented=True),
186186
Child('RightParen', kind='RightParenToken'),
187187
Child('AsyncKeyword', kind='ContextualKeyworkToken',
188188
text_choices=['async'], is_optional=True),

0 commit comments

Comments
 (0)