Skip to content

Mark function parameters and tuple elements as indented #1148

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 7 commits into from
Dec 15, 2022
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 @@ -116,7 +116,8 @@ public let DECL_NODES: [Node] = [
]),
Child(name: "ParameterList",
kind: "FunctionParameterList",
collectionElementName: "Parameter"),
collectionElementName: "Parameter",
isIndented: true),
Child(name: "RightParen",
kind: "RightParenToken",
tokenChoices: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ public let EXPR_NODES: [Node] = [
]),
Child(name: "ElementList",
kind: "TupleExprElementList",
collectionElementName: "Element"),
collectionElementName: "Element",
isIndented: true),
Child(name: "RightParen",
kind: "RightParenToken",
tokenChoices: [
Expand Down Expand Up @@ -813,7 +814,8 @@ public let EXPR_NODES: [Node] = [
]),
Child(name: "ArgumentList",
kind: "TupleExprElementList",
collectionElementName: "Argument"),
collectionElementName: "Argument",
isIndented: true),
Child(name: "RightParen",
kind: "RightParenToken",
isOptional: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ public let TYPE_NODES: [Node] = [
]),
Child(name: "Elements",
kind: "TupleTypeElementList",
collectionElementName: "Element"),
collectionElementName: "Element",
isIndented: true),
Child(name: "RightParen",
kind: "RightParenToken",
tokenChoices: [
Expand All @@ -322,7 +323,8 @@ public let TYPE_NODES: [Node] = [
]),
Child(name: "Arguments",
kind: "TupleTypeElementList",
collectionElementName: "Argument"),
collectionElementName: "Argument",
isIndented: true),
Child(name: "RightParen",
kind: "RightParenToken",
tokenChoices: [
Expand Down
10 changes: 10 additions & 0 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,20 @@ open class BasicFormat: SyntaxRewriter {
return true
case \CodeBlockSyntax.statements:
return true
case \FunctionCallExprSyntax.argumentList:
return true
case \FunctionTypeSyntax.arguments:
return true
case \MemberDeclBlockSyntax.members:
return true
case \ParameterClauseSyntax.parameterList:
return true
case \SwitchCaseSyntax.statements:
return true
case \TupleExprSyntax.elementList:
return true
case \TupleTypeSyntax.elements:
return true
default:
return false
}
Expand Down
64 changes: 63 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,69 @@ final class FunctionTests: XCTestCase {
}
AssertBuildResult(buildable, "test(value1, p2: value2, value3, p4: value4, value5)")
}


func testFunctionDeclBuilder() {
let builder = FunctionDecl("""
func test(_ p1: Int, p2: Int, _ p3: Int, p4: Int, _ p5: Int) -> Int {
return p1 + p2 + p3 + p4 + p5
}
""")

AssertBuildResult(builder, """
func test(_ p1: Int, p2: Int, _ p3: Int, p4: Int, _ p5: Int) -> Int {
return p1 + p2 + p3 + p4 + p5
}
""")
}

func testMultilineFunctionParameterList() {
let builder = FunctionDecl("""
func test(
_ p1: Int,
p2: Int,
_ p3: Int,
p4: Int,
_ p5: Int
) -> Int {
return p1 + p2 + p3 + p4 + p5
}
""")

AssertBuildResult(builder, """
func test(
_ p1: Int,
p2: Int,
_ p3: Int,
p4: Int,
_ p5: Int
) -> Int {
return p1 + p2 + p3 + p4 + p5
}
""")
}

func testMultilineFunctionCallExpr() {
let builder = FunctionCallExpr("""
test(
p1: value1,
p2: value2,
p3: value3,
p4: value4,
p5: value5
)
""")

AssertBuildResult(builder, """
test(
p1: value1,
p2: value2,
p3: value3,
p4: value4,
p5: value5
)
""")
}

func testParensEmittedForNoArgumentsAndNoTrailingClosure() {
let buildable = FunctionCallExpr(callee: ExprSyntax("test"))
AssertBuildResult(buildable, "test()")
Expand Down
26 changes: 26 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/InitializerDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,30 @@ final class InitializerDeclTests: XCTestCase {
}
""")
}

func testMultilineParameterList() {
let builder = InitializerDecl("""
init(
_ p1: Int,
p2: Int,
_ p3: Int,
p4: Int,
_ p5: Int
) {
self.init(p1 + p2 + p3 + p4 + p5)
}
""")

AssertBuildResult(builder, """
init(
_ p1: Int,
p2: Int,
_ p3: Int,
p4: Int,
_ p5: Int
) {
self.init(p1 + p2 + p3 + p4 + p5)
}
""")
}
}
66 changes: 66 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/TupleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import XCTest
import SwiftSyntax
import SwiftSyntaxBuilder

final class TupleTests: XCTestCase {
func testLabeledElementList() {
let builder = TupleExprSyntax("(p1: value1, p2: value2, p3: value3)")
AssertBuildResult(builder, "(p1: value1, p2: value2, p3: value3)")
}

func testMultilineTupleExpr() {
let builder = TupleExprSyntax("""
(
p1: value1,
p2: value2,
p3: value3,
p4: value4,
p5: value5
)
""")

AssertBuildResult(builder, """
(
p1: value1,
p2: value2,
p3: value3,
p4: value4,
p5: value5
)
""")
}

func testMultilineTupleType() {
let builder = TupleTypeSyntax("""
(
Int,
p2: Int,
Int,
p4: Int,
Int
)
""")

AssertBuildResult(builder, """
(
Int,
p2: Int,
Int,
p4: Int,
Int
)
""")
}
}
2 changes: 1 addition & 1 deletion gyb_syntax_support/DeclNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
children=[
Child('LeftParen', kind='LeftParenToken'),
Child('ParameterList', kind='FunctionParameterList', name_for_diagnostics='parameters',
collection_element_name='Parameter'),
collection_element_name='Parameter', is_indented=True),
Child('RightParen', kind='RightParenToken'),
]),

Expand Down
4 changes: 2 additions & 2 deletions gyb_syntax_support/ExprNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
children=[
Child('LeftParen', kind='LeftParenToken'),
Child('ElementList', kind='TupleExprElementList',
collection_element_name='Element'),
collection_element_name='Element', is_indented=True),
Child('RightParen', kind='RightParenToken'),
]),

Expand Down Expand Up @@ -472,7 +472,7 @@
Child('LeftParen', kind='LeftParenToken',
is_optional=True),
Child('ArgumentList', kind='TupleExprElementList', name_for_diagnostics='arguments',
collection_element_name='Argument'),
collection_element_name='Argument', is_indented=True),
Child('RightParen', kind='RightParenToken',
is_optional=True),
Child('TrailingClosure', kind='ClosureExpr', name_for_diagnostics='trailing closure',
Expand Down
4 changes: 2 additions & 2 deletions gyb_syntax_support/TypeNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
children=[
Child('LeftParen', kind='LeftParenToken'),
Child('Elements', kind='TupleTypeElementList',
collection_element_name='Element'),
collection_element_name='Element', is_indented=True),
Child('RightParen', kind='RightParenToken'),
]),

Expand All @@ -182,7 +182,7 @@
children=[
Child('LeftParen', kind='LeftParenToken'),
Child('Arguments', kind='TupleTypeElementList',
collection_element_name='Argument'),
collection_element_name='Argument', is_indented=True),
Child('RightParen', kind='RightParenToken'),
Child('AsyncKeyword', kind='ContextualKeyworkToken',
text_choices=['async'], is_optional=True),
Expand Down