Restored r303067 and fixed failing test.

Summary:
This commit restores r303067(reverted by r303094) and fixes the 'formatting.test'
failure.
The failure is due to destructors of `ClangdLSPServer`'s fields(`FixItsMap` and
`FixItsMutex`) being called before destructor of `Server`. It led to the worker
thread calling `consumeDiagnostics` after `FixItsMutex` and `FixItsMap`
destructors were called.
Also, clangd is now run with '-run-synchronously' flag in 'formatting.test'.

Reviewers: bkramer, krasimir

Reviewed By: krasimir

Subscribers: mgorny, cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D33233

llvm-svn: 303151
diff --git a/clang-tools-extra/clangd/ClangdMain.cpp b/clang-tools-extra/clangd/ClangdMain.cpp
index c6e16a7..44f8665 100644
--- a/clang-tools-extra/clangd/ClangdMain.cpp
+++ b/clang-tools-extra/clangd/ClangdMain.cpp
@@ -7,15 +7,19 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "ASTManager.h"
-#include "DocumentStore.h"
 #include "JSONRPCDispatcher.h"
+#include "ClangdLSPServer.h"
+#include "Protocol.h"
 #include "ProtocolHandlers.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Program.h"
+
 #include <iostream>
+#include <memory>
 #include <string>
+
+using namespace clang;
 using namespace clang::clangd;
 
 static llvm::cl::opt<bool>
@@ -34,9 +38,7 @@
 
   // Set up a document store and intialize all the method handlers for JSONRPC
   // dispatching.
-  DocumentStore Store;
-  ASTManager AST(Out, Store, RunSynchronously);
-  Store.addListener(&AST);
+  ClangdLSPServer LSPServer(Out, RunSynchronously);
   JSONRPCDispatcher Dispatcher(llvm::make_unique<Handler>(Out));
   Dispatcher.registerHandler("initialize",
                              llvm::make_unique<InitializeHandler>(Out));
@@ -45,26 +47,26 @@
   Dispatcher.registerHandler("shutdown", std::move(ShutdownPtr));
   Dispatcher.registerHandler(
       "textDocument/didOpen",
-      llvm::make_unique<TextDocumentDidOpenHandler>(Out, Store));
+      llvm::make_unique<TextDocumentDidOpenHandler>(Out, LSPServer));
   Dispatcher.registerHandler(
       "textDocument/didClose",
-      llvm::make_unique<TextDocumentDidCloseHandler>(Out, Store));
+      llvm::make_unique<TextDocumentDidCloseHandler>(Out, LSPServer));
   Dispatcher.registerHandler(
       "textDocument/didChange",
-      llvm::make_unique<TextDocumentDidChangeHandler>(Out, Store));
+      llvm::make_unique<TextDocumentDidChangeHandler>(Out, LSPServer));
   Dispatcher.registerHandler(
       "textDocument/rangeFormatting",
-      llvm::make_unique<TextDocumentRangeFormattingHandler>(Out, Store));
+      llvm::make_unique<TextDocumentRangeFormattingHandler>(Out, LSPServer));
   Dispatcher.registerHandler(
       "textDocument/onTypeFormatting",
-      llvm::make_unique<TextDocumentOnTypeFormattingHandler>(Out, Store));
+      llvm::make_unique<TextDocumentOnTypeFormattingHandler>(Out, LSPServer));
   Dispatcher.registerHandler(
       "textDocument/formatting",
-      llvm::make_unique<TextDocumentFormattingHandler>(Out, Store));
+      llvm::make_unique<TextDocumentFormattingHandler>(Out, LSPServer));
   Dispatcher.registerHandler("textDocument/codeAction",
-                             llvm::make_unique<CodeActionHandler>(Out, AST));
+                             llvm::make_unique<CodeActionHandler>(Out, LSPServer));
   Dispatcher.registerHandler("textDocument/completion",
-                             llvm::make_unique<CompletionHandler>(Out, AST));
+                             llvm::make_unique<CompletionHandler>(Out, LSPServer));
 
   while (std::cin.good()) {
     // A Language Server Protocol message starts with a HTTP header, delimited