Merge pull request #1033 from dart-lang/simpler-cascades

Simpler cascades
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 97d8689..68467db 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,7 @@
 # 2.0.2-dev
 
 * Don't unnecessarily split argument lists with `/* */` comments (#837).
+* Return correct exit code from `FormatCommand` when formatting stdin (#1035).
 
 # 2.0.1
 
diff --git a/bin/format.dart b/bin/format.dart
index e757c00..2e30848 100644
--- a/bin/format.dart
+++ b/bin/format.dart
@@ -12,7 +12,7 @@
 import 'package:dart_style/src/io.dart';
 import 'package:dart_style/src/style_fix.dart';
 
-void main(List<String> args) {
+void main(List<String> args) async {
   var parser = ArgParser(allowTrailingOptions: true);
 
   defineOptions(parser,
@@ -135,7 +135,7 @@
       setExitIfChanged: setExitIfChanged);
 
   if (argResults.rest.isEmpty) {
-    formatStdin(options, selection, argResults['stdin-name'] as String);
+    await formatStdin(options, selection, argResults['stdin-name'] as String);
   } else {
     formatPaths(options, argResults.rest);
   }
diff --git a/lib/src/cli/format_command.dart b/lib/src/cli/format_command.dart
index 6f6627e..1badec6 100644
--- a/lib/src/cli/format_command.dart
+++ b/lib/src/cli/format_command.dart
@@ -152,7 +152,7 @@
         setExitIfChanged: setExitIfChanged);
 
     if (argResults.rest.isEmpty) {
-      formatStdin(options, selection, stdinName);
+      await formatStdin(options, selection, stdinName);
     } else {
       formatPaths(options, argResults.rest);
       options.summary.show();
diff --git a/lib/src/io.dart b/lib/src/io.dart
index c0064b4..bcdcbc9 100644
--- a/lib/src/io.dart
+++ b/lib/src/io.dart
@@ -1,9 +1,7 @@
 // Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
-
-library dart_style.src.io;
-
+import 'dart:async';
 import 'dart:convert';
 import 'dart:io';
 
@@ -15,7 +13,8 @@
 import 'source_code.dart';
 
 /// Reads and formats input from stdin until closed.
-void formatStdin(FormatterOptions options, List<int>? selection, String name) {
+Future<void> formatStdin(
+    FormatterOptions options, List<int>? selection, String name) async {
   var selectionStart = 0;
   var selectionLength = 0;
 
@@ -24,6 +23,7 @@
     selectionLength = selection[1];
   }
 
+  var completer = Completer<void>();
   var input = StringBuffer();
   stdin.transform(Utf8Decoder()).listen(input.write, onDone: () {
     var formatter = DartFormatter(
@@ -50,7 +50,11 @@
 $stack''');
       exitCode = 70; // sysexits.h: EX_SOFTWARE
     }
+
+    completer.complete();
   });
+
+  return completer.future;
 }
 
 /// Formats all of the files and directories given by [paths].