Skip to content

Commit e6b8c60

Browse files
authored
Merge pull request #291 from allevato/cherrypick-filehandle-fixes
[5.6] Use a wrapper type instead of retro-conforming FileHandle to `TextOutputStream`.
2 parents c062580 + 466914d commit e6b8c60

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

Sources/swift-format/Frontend/FormatFrontend.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class FormatFrontend: Frontend {
3939
return
4040
}
4141

42-
var stdoutStream = FileHandle.standardOutput
42+
var stdoutStream = FileHandleTextOutputStream(FileHandle.standardOutput)
4343
do {
4444
if inPlace {
4545
var buffer = ""

Sources/swift-format/Utilities/FileHandle+TextOutputStream.swift renamed to Sources/swift-format/Utilities/FileHandleTextOutputStream.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@
1212

1313
import Foundation
1414

15-
extension FileHandle: TextOutputStream {
16-
public func write(_ string: String) {
17-
self.write(string.data(using: .utf8)!) // Conversion to UTF-8 cannot fail
15+
/// Wraps a `FileHandle` so that it can be used by APIs that take a `TextOutputStream`-conforming
16+
/// type as an input.
17+
struct FileHandleTextOutputStream: TextOutputStream {
18+
/// The underlying file handle to which the text will be written.
19+
private var fileHandle: FileHandle
20+
21+
/// Creates a new output stream that writes to the given file handle.
22+
init(_ fileHandle: FileHandle) {
23+
self.fileHandle = fileHandle
24+
}
25+
26+
func write(_ string: String) {
27+
fileHandle.write(string.data(using: .utf8)!) // Conversion to UTF-8 cannot fail
1828
}
1929
}

Sources/swift-format/Utilities/StderrDiagnosticPrinter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ final class StderrDiagnosticPrinter {
6464
/// Prints a diagnostic to standard error.
6565
func printDiagnostic(_ diagnostic: TSCBasic.Diagnostic) {
6666
printQueue.sync {
67-
let stderr = FileHandle.standardError
67+
let stderr = FileHandleTextOutputStream(FileHandle.standardError)
6868

6969
stderr.write("\(ansiSGR(.boldWhite))\(diagnostic.location): ")
7070

Sources/swift-format/VersionOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct VersionOptions: ParsableArguments {
2020
func validate() throws {
2121
if version {
2222
// TODO: Automate updates to this somehow.
23-
print("0.50600.0")
23+
print("0.50600.1")
2424
throw ExitCode.success
2525
}
2626
}

0 commit comments

Comments
 (0)