Skip to content

Commit

Permalink
Merge pull request swiftlang#291 from allevato/cherrypick-filehandle-…
Browse files Browse the repository at this point in the history
…fixes

[5.6] Use a wrapper type instead of retro-conforming FileHandle to `TextOutputStream`.
  • Loading branch information
allevato authored Mar 22, 2022
2 parents c062580 + 466914d commit e6b8c60
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/swift-format/Frontend/FormatFrontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FormatFrontend: Frontend {
return
}

var stdoutStream = FileHandle.standardOutput
var stdoutStream = FileHandleTextOutputStream(FileHandle.standardOutput)
do {
if inPlace {
var buffer = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@

import Foundation

extension FileHandle: TextOutputStream {
public func write(_ string: String) {
self.write(string.data(using: .utf8)!) // Conversion to UTF-8 cannot fail
/// Wraps a `FileHandle` so that it can be used by APIs that take a `TextOutputStream`-conforming
/// type as an input.
struct FileHandleTextOutputStream: TextOutputStream {
/// The underlying file handle to which the text will be written.
private var fileHandle: FileHandle

/// Creates a new output stream that writes to the given file handle.
init(_ fileHandle: FileHandle) {
self.fileHandle = fileHandle
}

func write(_ string: String) {
fileHandle.write(string.data(using: .utf8)!) // Conversion to UTF-8 cannot fail
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final class StderrDiagnosticPrinter {
/// Prints a diagnostic to standard error.
func printDiagnostic(_ diagnostic: TSCBasic.Diagnostic) {
printQueue.sync {
let stderr = FileHandle.standardError
let stderr = FileHandleTextOutputStream(FileHandle.standardError)

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

Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-format/VersionOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct VersionOptions: ParsableArguments {
func validate() throws {
if version {
// TODO: Automate updates to this somehow.
print("0.50600.0")
print("0.50600.1")
throw ExitCode.success
}
}
Expand Down

0 comments on commit e6b8c60

Please sign in to comment.