[clangd] Refactor stream output into a single thread-safe output object.

This abstracts away the passing of raw_ostreams everywhere, thread
safety will be used soon.

llvm-svn: 294747
diff --git a/clang-tools-extra/clangd/ClangDMain.cpp b/clang-tools-extra/clangd/ClangDMain.cpp
index b37be99..6ebdb63 100644
--- a/clang-tools-extra/clangd/ClangDMain.cpp
+++ b/clang-tools-extra/clangd/ClangDMain.cpp
@@ -19,6 +19,7 @@
 int main(int argc, char *argv[]) {
   llvm::raw_ostream &Outs = llvm::outs();
   llvm::raw_ostream &Logs = llvm::errs();
+  JSONOutput Out(Outs, Logs);
 
   // Change stdin to binary to not lose \r\n on windows.
   llvm::sys::ChangeStdinToBinary();
@@ -26,24 +27,24 @@
   // Set up a document store and intialize all the method handlers for JSONRPC
   // dispatching.
   DocumentStore Store;
-  JSONRPCDispatcher Dispatcher(llvm::make_unique<Handler>(Outs, Logs));
+  JSONRPCDispatcher Dispatcher(llvm::make_unique<Handler>(Out));
   Dispatcher.registerHandler("initialize",
-                             llvm::make_unique<InitializeHandler>(Outs, Logs));
+                             llvm::make_unique<InitializeHandler>(Out));
   Dispatcher.registerHandler("shutdown",
-                             llvm::make_unique<ShutdownHandler>(Outs, Logs));
+                             llvm::make_unique<ShutdownHandler>(Out));
   Dispatcher.registerHandler(
       "textDocument/didOpen",
-      llvm::make_unique<TextDocumentDidOpenHandler>(Outs, Logs, Store));
+      llvm::make_unique<TextDocumentDidOpenHandler>(Out, Store));
   // FIXME: Implement textDocument/didClose.
   Dispatcher.registerHandler(
       "textDocument/didChange",
-      llvm::make_unique<TextDocumentDidChangeHandler>(Outs, Logs, Store));
+      llvm::make_unique<TextDocumentDidChangeHandler>(Out, Store));
   Dispatcher.registerHandler(
       "textDocument/rangeFormatting",
-      llvm::make_unique<TextDocumentRangeFormattingHandler>(Outs, Logs, Store));
+      llvm::make_unique<TextDocumentRangeFormattingHandler>(Out, Store));
   Dispatcher.registerHandler(
       "textDocument/formatting",
-      llvm::make_unique<TextDocumentFormattingHandler>(Outs, Logs, Store));
+      llvm::make_unique<TextDocumentFormattingHandler>(Out, Store));
 
   while (std::cin.good()) {
     // A Language Server Protocol message starts with a HTTP header, delimited
@@ -89,6 +90,10 @@
       // Finally, execute the action for this JSON message.
       if (!Dispatcher.call(JSONRef))
         Logs << "JSON dispatch failed!\n";
+
+      // If we're done, exit the loop.
+      if (Out.isDone())
+        break;
     }
   }
 }