forked from swiftlang/swift-syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine expansion of function-typed placeholders
Per <swiftlang/sourcekit-lsp#1789>, these placeholders no longer expand to multi-line trailing closures. Instead they become inline closures, with nested placeholders for the signature and body. This introduces a new formatter, `ClosureLiteralFormat`, so that other uses of `BasicFormat` are not impacted. `ClosureLiteralFormat` does not insert newlines into a closure when there is only a single statement in the body.
- Loading branch information
1 parent
1cd3534
commit 897ac1a
Showing
5 changed files
with
271 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if swift(>=6) | ||
@_spi(RawSyntax) public import SwiftSyntax | ||
#else | ||
@_spi(RawSyntax) import SwiftSyntax | ||
#endif | ||
|
||
/// A specialization of `BasicFormat` for closure literals, which is more | ||
/// conservative about newline insertion. | ||
/// | ||
/// A closure that seems to be a simple predicate or transform — based on the | ||
/// number of enclosed statements — will not be reformatted to multiple lines. | ||
open class ClosureLiteralFormat: BasicFormat { | ||
open override func requiresNewline(between first: TokenSyntax?, and second: TokenSyntax?) -> Bool { | ||
if let first, isEndOfSmallClosureSignature(first) { | ||
return false | ||
} else if let first, isSmallClosureDelimiter(first, kind: \.leftBrace) { | ||
return false | ||
} else if let second, isSmallClosureDelimiter(second, kind: \.rightBrace) { | ||
return false | ||
} else { | ||
return super.requiresNewline(between: first, and: second) | ||
} | ||
} | ||
|
||
/// Returns `true` if `token` is an opening or closing brace (according to | ||
/// `kind`) of a closure, and that closure has no more than one statement in | ||
/// its body. | ||
private func isSmallClosureDelimiter( | ||
_ token: TokenSyntax, | ||
kind: KeyPath<ClosureExprSyntax, TokenSyntax> | ||
) -> Bool { | ||
guard token.keyPathInParent == kind, | ||
let closure = token.parent?.as(ClosureExprSyntax.self) | ||
else { | ||
return false | ||
} | ||
|
||
return closure.statements.count <= 1 | ||
} | ||
|
||
/// Returns `true` if `token` is the last token in the signature of a closure, | ||
/// and that closure has no more than one statement in its body. | ||
private func isEndOfSmallClosureSignature(_ token: TokenSyntax) -> Bool { | ||
guard let signature = token.ancestorOrSelf(mapping: { $0.as(ClosureSignatureSyntax.self) }), | ||
let closure = signature.parent?.as(ClosureExprSyntax.self) | ||
else { | ||
return false | ||
} | ||
|
||
return signature.lastToken(viewMode: viewMode) == token | ||
&& closure.statements.count <= 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
Tests/SwiftBasicFormatTest/ClosureLiteralFormatTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 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 SwiftBasicFormat | ||
import SwiftParser | ||
import SwiftSyntax | ||
@_spi(Testing) import SwiftSyntaxBuilder | ||
import XCTest | ||
import _SwiftSyntaxTestSupport | ||
|
||
fileprivate func assertFormatted<T: SyntaxProtocol>( | ||
tree: T, | ||
expected: String, | ||
using format: ClosureLiteralFormat = ClosureLiteralFormat(indentationWidth: .spaces(4)), | ||
file: StaticString = #filePath, | ||
line: UInt = #line | ||
) { | ||
assertStringsEqualWithDiff(tree.formatted(using: format).description, expected, file: file, line: line) | ||
} | ||
|
||
fileprivate func assertFormatted( | ||
source: String, | ||
expected: String, | ||
using format: ClosureLiteralFormat = ClosureLiteralFormat(indentationWidth: .spaces(4)), | ||
file: StaticString = #filePath, | ||
line: UInt = #line | ||
) { | ||
assertFormatted( | ||
tree: Parser.parse(source: source), | ||
expected: expected, | ||
using: format, | ||
file: file, | ||
line: line | ||
) | ||
} | ||
|
||
final class ClosureLiteralFormatTests: XCTestCase { | ||
func testSingleStatementClosureArg() { | ||
assertFormatted( | ||
source: """ | ||
foo(bar: { baz in baz.quux }) | ||
""", | ||
expected: """ | ||
foo(bar: { baz in baz.quux }) | ||
""" | ||
) | ||
} | ||
|
||
func testSingleStatementClosureArgAlreadyMultiLine() { | ||
assertFormatted( | ||
source: """ | ||
foo( | ||
bar: { baz in | ||
baz.quux | ||
} | ||
) | ||
""", | ||
expected: """ | ||
foo( | ||
bar: { baz in | ||
baz.quux | ||
} | ||
) | ||
""" | ||
) | ||
} | ||
|
||
func testMultiStatmentClosureArg() { | ||
assertFormatted( | ||
source: """ | ||
foo( | ||
bar: { baz in print(baz); return baz.quux } | ||
) | ||
""", | ||
expected: """ | ||
foo( | ||
bar: { baz in | ||
print(baz); | ||
return baz.quux | ||
} | ||
) | ||
""" | ||
) | ||
} | ||
|
||
func testMultiStatementClosureArgAlreadyMultiLine() { | ||
assertFormatted( | ||
source: """ | ||
foo( | ||
bar: { baz in | ||
print(baz) | ||
return baz.quux | ||
} | ||
) | ||
""", | ||
expected: """ | ||
foo( | ||
bar: { baz in | ||
print(baz) | ||
return baz.quux | ||
} | ||
) | ||
""" | ||
) | ||
} | ||
|
||
func testFormatClosureWithInitialIndentation() throws { | ||
assertFormatted( | ||
tree: ClosureExprSyntax( | ||
statements: CodeBlockItemListSyntax([ | ||
CodeBlockItemSyntax(item: CodeBlockItemSyntax.Item(IntegerLiteralExprSyntax(integerLiteral: 2))) | ||
]) | ||
), | ||
expected: """ | ||
{ 2 } | ||
""", | ||
using: ClosureLiteralFormat(initialIndentation: .spaces(4)) | ||
) | ||
} | ||
} |
Oops, something went wrong.