Skip to content

[5.6] Use a wrapper type instead of retro-conforming FileHandle to TextOutputStream. #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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