Skip to content

Commit 03feb30

Browse files
committed
Merge pull request #290 from allevato/filehandle-fixes
Use a wrapper type instead of retro-conforming `FileHandle` to `TextOutputStream`.
1 parent c062580 commit 03feb30

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
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

0 commit comments

Comments
 (0)