Skip to content

Commit 1ad78e3

Browse files
authored
Merge pull request swiftlang#196 from dabelknap/overloads
Fix spacing for binary operator overloads
2 parents 1afa185 + e941662 commit 1ad78e3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ private final class TokenStreamCreator: SyntaxVisitor {
189189
// MARK: - Function and function-like declaration nodes (initializers, deinitializers, subscripts)
190190

191191
override func visit(_ node: FunctionDeclSyntax) {
192+
if case .spacedBinaryOperator = node.identifier.tokenKind {
193+
after(node.identifier.lastToken, tokens: .space)
194+
}
192195
arrangeFunctionLikeDecl(
193196
node,
194197
attributes: node.attributes,

Tests/SwiftFormatPrettyPrintTests/FunctionDeclTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,22 @@ public class FunctionDeclTests: PrettyPrintTestCase {
373373
"""
374374
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 12)
375375
}
376+
377+
public func testOperatorOverloads() {
378+
let input =
379+
"""
380+
func < (lhs: Position, rhs: Position) -> Bool {
381+
// do stuff
382+
}
383+
384+
func + (left: [Int], right: [Int]) -> [Int] {
385+
// add two arrays
386+
}
387+
388+
func ⊕ (left: Tensor, right: Tensor) -> Tensor {
389+
// tensor addition
390+
}
391+
"""
392+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 50)
393+
}
376394
}

0 commit comments

Comments
 (0)