Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- ClangdLSPServer.cpp - LSP server ------------------------*- C++-*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ClangdLSPServer.h" |
| 11 | #include "JSONRPCDispatcher.h" |
| 12 | |
| 13 | using namespace clang::clangd; |
| 14 | using namespace clang; |
| 15 | |
| 16 | class ClangdLSPServer::LSPDiagnosticsConsumer : public DiagnosticsConsumer { |
| 17 | public: |
| 18 | LSPDiagnosticsConsumer(ClangdLSPServer &Server) : Server(Server) {} |
| 19 | |
| 20 | virtual void onDiagnosticsReady(PathRef File, |
| 21 | std::vector<DiagWithFixIts> Diagnostics) { |
| 22 | Server.consumeDiagnostics(File, Diagnostics); |
| 23 | } |
| 24 | |
| 25 | private: |
| 26 | ClangdLSPServer &Server; |
| 27 | }; |
| 28 | |
| 29 | ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, bool RunSynchronously) |
| 30 | : Out(Out), |
| 31 | Server(llvm::make_unique<DirectoryBasedGlobalCompilationDatabase>(), |
| 32 | llvm::make_unique<LSPDiagnosticsConsumer>(*this), |
| 33 | RunSynchronously) {} |
| 34 | |
| 35 | void ClangdLSPServer::openDocument(StringRef File, StringRef Contents) { |
| 36 | Server.addDocument(File, Contents); |
| 37 | } |
| 38 | |
| 39 | void ClangdLSPServer::closeDocument(StringRef File) { |
| 40 | Server.removeDocument(File); |
| 41 | } |
| 42 | |
| 43 | std::vector<CompletionItem> ClangdLSPServer::codeComplete(PathRef File, |
| 44 | Position Pos) { |
| 45 | return Server.codeComplete(File, Pos); |
| 46 | } |
| 47 | |
| 48 | std::vector<clang::tooling::Replacement> |
| 49 | ClangdLSPServer::getFixIts(StringRef File, const clangd::Diagnostic &D) { |
| 50 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 51 | auto DiagToFixItsIter = FixItsMap.find(File); |
| 52 | if (DiagToFixItsIter == FixItsMap.end()) |
| 53 | return {}; |
| 54 | |
| 55 | const auto &DiagToFixItsMap = DiagToFixItsIter->second; |
| 56 | auto FixItsIter = DiagToFixItsMap.find(D); |
| 57 | if (FixItsIter == DiagToFixItsMap.end()) |
| 58 | return {}; |
| 59 | |
| 60 | return FixItsIter->second; |
| 61 | } |
| 62 | |
| 63 | std::string ClangdLSPServer::getDocument(PathRef File) { |
| 64 | return Server.getDocument(File); |
| 65 | } |
| 66 | |
| 67 | void ClangdLSPServer::consumeDiagnostics( |
| 68 | PathRef File, std::vector<DiagWithFixIts> Diagnostics) { |
| 69 | std::string DiagnosticsJSON; |
| 70 | |
| 71 | DiagnosticToReplacementMap LocalFixIts; // Temporary storage |
| 72 | for (auto &DiagWithFixes : Diagnostics) { |
| 73 | auto Diag = DiagWithFixes.Diag; |
| 74 | DiagnosticsJSON += |
| 75 | R"({"range":)" + Range::unparse(Diag.range) + |
| 76 | R"(,"severity":)" + std::to_string(Diag.severity) + |
| 77 | R"(,"message":")" + llvm::yaml::escape(Diag.message) + |
| 78 | R"("},)"; |
| 79 | |
| 80 | // We convert to Replacements to become independent of the SourceManager. |
| 81 | auto &FixItsForDiagnostic = LocalFixIts[Diag]; |
| 82 | std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(), |
| 83 | std::back_inserter(FixItsForDiagnostic)); |
| 84 | } |
| 85 | |
| 86 | // Cache FixIts |
| 87 | { |
| 88 | // FIXME(ibiryukov): should be deleted when documents are removed |
| 89 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 90 | FixItsMap[File] = LocalFixIts; |
| 91 | } |
| 92 | |
| 93 | // Publish diagnostics. |
| 94 | if (!DiagnosticsJSON.empty()) |
| 95 | DiagnosticsJSON.pop_back(); // Drop trailing comma. |
| 96 | Out.writeMessage( |
| 97 | R"({"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":")" + |
| 98 | URI::fromFile(File).uri + R"(","diagnostics":[)" + DiagnosticsJSON + |
| 99 | R"(]}})"); |
| 100 | } |