Skip to content

Commit 1fda398

Browse files
authored
Merge pull request #171 from allevato/swift-5.2-branch
Cherrypick recent changes into swift-5.2-branch.
2 parents 5cdf916 + 1b46d56 commit 1fda398

File tree

14 files changed

+180
-44
lines changed

14 files changed

+180
-44
lines changed

Package.resolved

-9
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
"revision": "0688b9cfc4c3dd234e4f55f1f056b2affc849873",
1919
"version": "0.50200.0"
2020
}
21-
},
22-
{
23-
"package": "swift-tools-support-core",
24-
"repositoryURL": "https://github.com/apple/swift-tools-support-core.git",
25-
"state": {
26-
"branch": null,
27-
"revision": "693aba4c4c9dcc4767cc853a0dd38bf90ad8c258",
28-
"version": "0.0.1"
29-
}
3021
}
3122
]
3223
},

Package.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ let package = Package(
2222
],
2323
dependencies: [
2424
.package(url: "https://github.com/apple/swift-syntax", from: "0.50200.0"),
25-
.package(url: "https://github.com/apple/swift-tools-support-core.git", from: "0.0.1"),
2625
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "0.0.4")),
2726
],
2827
targets: [
@@ -62,12 +61,11 @@ let package = Package(
6261
.target(
6362
name: "swift-format",
6463
dependencies: [
64+
"ArgumentParser",
6565
"SwiftFormat",
6666
"SwiftFormatConfiguration",
6767
"SwiftFormatCore",
6868
"SwiftSyntax",
69-
"SwiftToolsSupport-auto",
70-
"ArgumentParser",
7169
]
7270
),
7371
.testTarget(

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,19 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
533533
override func visit(_ node: RepeatWhileStmtSyntax) -> SyntaxVisitorContinueKind {
534534
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
535535

536-
let whilePrecedingBreak = config.lineBreakBeforeControlFlowKeywords
537-
? Token.break(.same) : Token.space
538-
before(node.whileKeyword, tokens: whilePrecedingBreak)
536+
if config.lineBreakBeforeControlFlowKeywords {
537+
before(node.whileKeyword, tokens: .break(.same), .open)
538+
after(node.condition.lastToken, tokens: .close)
539+
} else {
540+
// The length of the condition needs to force the breaks around the braces of the repeat
541+
// stmt's body, so that there's always a break before the right brace when the while &
542+
// condition is too long to be on one line.
543+
before(node.whileKeyword, tokens: .space)
544+
// The `open` token occurs after the ending tokens for the braced `body` node.
545+
before(node.body.rightBrace, tokens: .open)
546+
after(node.condition.lastToken, tokens: .close)
547+
}
539548
after(node.whileKeyword, tokens: .space)
540-
541549
return .visitChildren
542550
}
543551

Sources/SwiftFormatRules/OrderedImports.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ fileprivate class Line {
503503
else {
504504
return ""
505505
}
506-
return importDecl.path.description.trimmingCharacters(in: .whitespaces)
506+
return importDecl.path.description.trimmingCharacters(in: .whitespacesAndNewlines)
507507
}
508508

509509
/// Returns the first `TokenSyntax` in the code block(s) from this Line, or nil when this Line

Sources/swift-format/Subcommands/DumpConfiguration.swift

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import ArgumentParser
1414
import Foundation
1515
import SwiftFormatConfiguration
16-
import TSCBasic
17-
import TSCUtility
1816

1917
extension SwiftFormatCommand {
2018
/// Dumps the tool's default configuration in JSON format to standard output.

Sources/swift-format/Subcommands/Format.swift

+11-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import Foundation
1515
import SwiftFormat
1616
import SwiftFormatConfiguration
1717
import SwiftSyntax
18-
import TSCBasic
1918

2019
extension SwiftFormatCommand {
2120
/// Formats one or more files containing Swift code.
@@ -89,31 +88,29 @@ private func formatMain(
8988
// fixed anyway.
9089
let formatter = SwiftFormatter(configuration: configuration, diagnosticEngine: nil)
9190
formatter.debugOptions = debugOptions
92-
let assumingFileURL = URL(fileURLWithPath: assumingFilename ?? "<stdin>")
91+
92+
let path = assumingFilename ?? "<stdin>"
93+
let assumingFileURL = URL(fileURLWithPath: path)
9394

9495
guard let source = readSource(from: sourceFile) else {
9596
diagnosticEngine.diagnose(
96-
Diagnostic.Message(
97-
.error, "Unable to read source for formatting from \(assumingFileURL.path)."))
97+
Diagnostic.Message(.error, "Unable to read source for formatting from \(path)."))
9898
return
9999
}
100100

101+
var stdoutStream = FileHandle.standardOutput
101102
do {
102103
if inPlace {
103-
let cwd = FileManager.default.currentDirectoryPath
104-
var buffer = BufferedOutputByteStream()
104+
var buffer = ""
105105
try formatter.format(source: source, assumingFileURL: assumingFileURL, to: &buffer)
106-
buffer.flush()
107-
try localFileSystem.writeFileContents(
108-
AbsolutePath(assumingFileURL.path, relativeTo: AbsolutePath(cwd)),
109-
bytes: buffer.bytes
110-
)
106+
107+
let bufferData = buffer.data(using: .utf8)! // Conversion to UTF-8 cannot fail
108+
try bufferData.write(to: assumingFileURL, options: .atomic)
111109
} else {
112110
try formatter.format(source: source, assumingFileURL: assumingFileURL, to: &stdoutStream)
113-
stdoutStream.flush()
111+
stdoutStream.synchronizeFile()
114112
}
115113
} catch SwiftFormatError.fileNotReadable {
116-
let path = assumingFileURL.path
117114
diagnosticEngine.diagnose(
118115
Diagnostic.Message(
119116
.error, "Unable to format \(path): file is not readable or does not exist."))
@@ -125,17 +122,15 @@ private func formatMain(
125122
return
126123
}
127124
stdoutStream.write(source)
128-
stdoutStream.flush()
125+
stdoutStream.synchronizeFile()
129126
return
130127
}
131-
let path = assumingFileURL.path
132128
let location = SourceLocationConverter(file: path, source: source).location(for: position)
133129
diagnosticEngine.diagnose(
134130
Diagnostic.Message(.error, "file contains invalid or unrecognized Swift syntax."),
135131
location: location)
136132
return
137133
} catch {
138-
let path = assumingFileURL.path
139134
diagnosticEngine.diagnose(Diagnostic.Message(.error, "Unable to format \(path): \(error)"))
140135
return
141136
}

Sources/swift-format/Subcommands/LegacyMain.swift

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import SwiftFormat
1616
import SwiftFormatConfiguration
1717
import SwiftFormatCore
1818
import SwiftSyntax
19-
import TSCBasic
2019

2120
extension SwiftFormatCommand {
2221
/// Keep the legacy `-m/--mode` flag working temporarily when no other subcommand is specified.

Sources/swift-format/Subcommands/Lint.swift

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import Foundation
1515
import SwiftFormat
1616
import SwiftFormatConfiguration
1717
import SwiftSyntax
18-
import TSCBasic
1918

2019
extension SwiftFormatCommand {
2120
/// Emits style diagnostics for one or more files containing Swift code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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 Foundation
14+
15+
extension FileHandle: TextOutputStream {
16+
public func write(_ string: String) {
17+
self.write(string.data(using: .utf8)!) // Conversion to UTF-8 cannot fail
18+
}
19+
}

Sources/swift-format/Utilities/Helpers.swift

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import SwiftFormat
1616
import SwiftFormatConfiguration
1717
import SwiftFormatCore
1818
import SwiftSyntax
19-
import TSCBasic
2019

2120
/// Throws an error that causes the current command to exit the process with a failure exit code if
2221
/// any of the preceding operations emitted diagnostics.

Tests/SwiftFormatPrettyPrintTests/DifferentiationAttributeTests.swift

+8-6
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ final class DifferentiationAttributeTests: PrettyPrintTestCase {
4343
assertPrettyPrintEqual(input: input, expected: expected, linelength: 43)
4444
}
4545

46-
#if HAS_DERIVATIVE_REGISTRATION_ATTRIBUTE
47-
func testDerivative() {
46+
func testDerivative() {
47+
#if HAS_DERIVATIVE_REGISTRATION_ATTRIBUTE
4848
let input =
4949
"""
5050
@derivative(of: foo, wrt: x)
@@ -78,9 +78,11 @@ final class DifferentiationAttributeTests: PrettyPrintTestCase {
7878
"""
7979

8080
assertPrettyPrintEqual(input: input, expected: expected, linelength: 28)
81-
}
81+
#endif
82+
}
8283

83-
func testTranspose() {
84+
func testTranspose() {
85+
#if HAS_DERIVATIVE_REGISTRATION_ATTRIBUTE
8486
let input =
8587
"""
8688
@transpose(of: foo, wrt: 0)
@@ -114,6 +116,6 @@ final class DifferentiationAttributeTests: PrettyPrintTestCase {
114116
"""
115117

116118
assertPrettyPrintEqual(input: input, expected: expected, linelength: 27)
117-
}
118-
#endif
119+
#endif
120+
}
119121
}

Tests/SwiftFormatPrettyPrintTests/RepeatStmtTests.swift

+25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ final class RepeatStmtTests: PrettyPrintTestCase {
1010
while x
1111
repeat { foo() }
1212
while longcondition
13+
repeat { f() }
14+
while long.condition
15+
repeat { f() } while long.condition
16+
repeat { f() } while long.condition.that.ison.many.lines
1317
repeat {
1418
let a = 123
1519
var b = "abc"
@@ -29,6 +33,16 @@ final class RepeatStmtTests: PrettyPrintTestCase {
2933
repeat {
3034
foo()
3135
} while longcondition
36+
repeat {
37+
f()
38+
} while long.condition
39+
repeat {
40+
f()
41+
} while long.condition
42+
repeat {
43+
f()
44+
} while long.condition
45+
.that.ison.many.lines
3246
repeat {
3347
let a = 123
3448
var b = "abc"
@@ -50,6 +64,10 @@ final class RepeatStmtTests: PrettyPrintTestCase {
5064
repeat {} while x
5165
repeat { f() } while x
5266
repeat { foo() } while longcondition
67+
repeat { f() }
68+
while long.condition
69+
repeat { f() } while long.condition
70+
repeat { f() } while long.condition.that.ison.many.lines
5371
repeat {
5472
let a = 123
5573
var b = "abc"
@@ -68,6 +86,13 @@ final class RepeatStmtTests: PrettyPrintTestCase {
6886
repeat { f() } while x
6987
repeat { foo() }
7088
while longcondition
89+
repeat { f() }
90+
while long.condition
91+
repeat { f() }
92+
while long.condition
93+
repeat { f() }
94+
while long.condition.that
95+
.ison.many.lines
7196
repeat {
7297
let a = 123
7398
var b = "abc"

Tests/SwiftFormatRulesTests/OrderedImportsTests.swift

+100
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,30 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
396396
)
397397
}
398398

399+
func testImportsContainingNewlines() {
400+
let input =
401+
"""
402+
import
403+
zeta
404+
import Zeta
405+
import
406+
Alpha
407+
import Beta
408+
"""
409+
410+
let expected =
411+
"""
412+
import
413+
Alpha
414+
import Beta
415+
import Zeta
416+
import
417+
zeta
418+
"""
419+
420+
XCTAssertFormatting(OrderedImports.self, input: input, expected: expected)
421+
}
422+
399423
func testRemovesDuplicateImports() {
400424
let input =
401425
"""
@@ -541,4 +565,80 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
541565

542566
XCTAssertFormatting(OrderedImports.self, input: input, expected: expected)
543567
}
568+
569+
func testConditionalImports() {
570+
let input =
571+
"""
572+
import Zebras
573+
import Apples
574+
#if canImport(Darwin)
575+
import Darwin
576+
#elseif canImport(Glibc)
577+
import Glibc
578+
#endif
579+
import Aardvarks
580+
581+
foo()
582+
bar()
583+
baz()
584+
"""
585+
586+
let expected =
587+
"""
588+
import Aardvarks
589+
import Apples
590+
import Zebras
591+
592+
#if canImport(Darwin)
593+
import Darwin
594+
#elseif canImport(Glibc)
595+
import Glibc
596+
#endif
597+
598+
foo()
599+
bar()
600+
baz()
601+
"""
602+
603+
XCTAssertFormatting(OrderedImports.self, input: input, expected: expected)
604+
}
605+
606+
func testIgnoredConditionalImports() {
607+
let input =
608+
"""
609+
import Zebras
610+
import Apples
611+
#if canImport(Darwin)
612+
import Darwin
613+
#elseif canImport(Glibc)
614+
import Glibc
615+
#endif
616+
// swift-format-ignore
617+
import Aardvarks
618+
619+
foo()
620+
bar()
621+
baz()
622+
"""
623+
624+
let expected =
625+
"""
626+
import Apples
627+
import Zebras
628+
629+
#if canImport(Darwin)
630+
import Darwin
631+
#elseif canImport(Glibc)
632+
import Glibc
633+
#endif
634+
// swift-format-ignore
635+
import Aardvarks
636+
637+
foo()
638+
bar()
639+
baz()
640+
"""
641+
642+
XCTAssertFormatting(OrderedImports.self, input: input, expected: expected)
643+
}
544644
}

0 commit comments

Comments
 (0)